diff --git a/docker/ploughshares/__pycache__/migrate_approval.cpython-39.pyc b/docker/ploughshares/__pycache__/migrate_approval.cpython-39.pyc index d06f8d4..587e858 100644 Binary files a/docker/ploughshares/__pycache__/migrate_approval.cpython-39.pyc and b/docker/ploughshares/__pycache__/migrate_approval.cpython-39.pyc differ diff --git a/docker/ploughshares/app.py b/docker/ploughshares/app.py index 03cac62..342ef8c 100644 --- a/docker/ploughshares/app.py +++ b/docker/ploughshares/app.py @@ -970,6 +970,90 @@ def api_delete_transaction(id): finally: conn.close() +@app.route('/sources') +def view_sources(): + conn = get_db_connection() + if conn is None: + flash("Database connection error", "error") + return render_template('view_sources.html', sources=[], version=VERSION) + + try: + with conn.cursor() as cur: + cur.execute('SELECT * FROM sources ORDER BY src_id DESC') + sources = cur.fetchall() + except Exception as e: + logger.error(f"Database error: {e}") + flash(f"Database error: {e}", "error") + sources = [] + finally: + conn.close() + + return render_template('view_sources.html', sources=sources, version=VERSION) + +@app.route('/api/source', methods=['POST']) +def api_create_source(): + """API endpoint to create a source""" + data = request.form.to_dict() + + # Validate required fields + required_fields = ['title', 'link', 'type'] + for field in required_fields: + if field not in data or not data[field]: + return jsonify({"error": f"Missing required field: {field}"}), 400 + + conn = get_db_connection() + if conn is None: + return jsonify({"error": "Database connection error"}), 500 + + try: + with conn.cursor() as cur: + cur.execute( + """ + INSERT INTO sources ( + title, link, type + ) VALUES ( + %(title)s, %(link)s, %(type)s + ) RETURNING src_id + """, + { + 'title': data['title'], + 'link': data['link'], + 'type': data['type'] + } + ) + result = cur.fetchone() + if result and 'src_id' in result: + conn.commit() + except Exception as e: + logger.error(f"Error creating source via API: {e}") + finally: + conn.close() + return redirect(url_for("view_sources")) + +@app.route('/api/source/', methods=['DELETE']) +def api_delete_source(id): + """API endpoint to delete a source""" + conn = get_db_connection() + if conn is None: + return jsonify({"error": "Database connection error"}), 500 + + try: + with conn.cursor() as cur: + # Check if transaction exists + cur.execute('SELECT src_id FROM sources WHERE src_id = %s', (id,)) + if cur.fetchone() is None: + return jsonify({"error": "Source not found"}), 404 + + # Delete the transaction + cur.execute('DELETE FROM sources WHERE src_id = %s', (id,)) + conn.commit() + return jsonify({"message": "Source deleted successfully"}), 200 + except Exception as e: + logger.error(f"Error deleting transaction via API: {e}") + return jsonify({"error": f"Error deleting source: {str(e)}"}), 500 + finally: + conn.close() + if __name__ == '__main__': logger.info(f"Starting Ploughshares v{VERSION}") bootstrap_database() diff --git a/docker/ploughshares/schema.sql b/docker/ploughshares/schema.sql index e494e0f..6a77357 100644 --- a/docker/ploughshares/schema.sql +++ b/docker/ploughshares/schema.sql @@ -1,6 +1,7 @@ -- Drop tables if they exist DROP TABLE IF EXISTS transaction_documents CASCADE; DROP TABLE IF EXISTS transactions CASCADE; +DROP TABLE IF EXISTS sources CASCADE; -- Create transactions table CREATE TABLE IF NOT EXISTS transactions ( @@ -41,6 +42,13 @@ CREATE TABLE transaction_documents ( upload_date TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP ); +CREATE TABLE sources ( + src_id SERIAL PRIMARY KEY, + title VARCHAR(255) NOT NULL, + link VARCHAR(255) NOT NULL, + type VARCHAR(255) NOT NULL +); + -- Create indexes for better performance CREATE INDEX IF NOT EXISTS idx_transactions_type ON transactions(transaction_type); CREATE INDEX IF NOT EXISTS idx_transactions_division ON transactions(company_division); diff --git a/docker/ploughshares/templates/base.html b/docker/ploughshares/templates/base.html index 0991dfb..7638228 100644 --- a/docker/ploughshares/templates/base.html +++ b/docker/ploughshares/templates/base.html @@ -59,11 +59,16 @@ - + + diff --git a/docker/ploughshares/templates/view_sources.html b/docker/ploughshares/templates/view_sources.html new file mode 100644 index 0000000..f29f1ba --- /dev/null +++ b/docker/ploughshares/templates/view_sources.html @@ -0,0 +1,73 @@ +{% extends "base.html" %} + +{% block title %}Sources - Project Ploughshares{% endblock %} + +{% block content %} +
+
+
+

Sources

+
+
+
+

Add Source

+ + + + +
+
+ + + + + + + + + + + + {% for source in sources %} + + + + + + + + {% endfor %} + +
Source No.TitleLinkType
{{ source['src_id'] }}{{ source['title'] }}{{ source['link'] }}{{ source['type'] }}
+
+
+
+
+{% endblock %} + +{% block scripts %} + +{% endblock %} \ No newline at end of file