from flask import Flask, render_template, request, jsonify import psycopg2 app = Flask(__name__) def connect_to_db(): return psycopg2.connect( dbname="glprui_jloddr", user="glprui_jloddr", password="612ef773", host="db.qgiscloud.com", port="5432", sslmode="prefer" ) @app.route('/') def index(): return render_template('index.html') @app.route('/record_data', methods=['POST']) def record_data(): data = request.json conn = connect_to_db() cursor = conn.cursor() try: cursor.execute( """ INSERT INTO public.gettinglost_tracking (Age, Gender, Transport, TimeOfDay, DayOfWeek, Description) VALUES (%s, %s, %s, %s, %s, %s) RETURNING ID; """, (data['age'], data['gender'], data['transport'], data['timeOfDay'], data['dayOfWeek'], data['description']) ) record_id = cursor.fetchone()[0] point_data = data['points'] for pointType, point in point_data.items(): if point: cursor.execute( """ INSERT INTO public.gettinglost_geom (ID, PointType, geom) VALUES (%s, %s, ST_SetSRID(ST_Point(%s, %s), 4326)); """, (record_id, pointType, point['lng'], point['lat']) ) conn.commit() return jsonify({"message": "Data recorded successfully!"}) except Exception as e: conn.rollback() return jsonify({"error": str(e)}) finally: cursor.close() conn.close() if __name__ == '__main__': app.run(debug=True)