File size: 1,670 Bytes
a7ea71f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from flask import Flask, render_template, request, jsonify
import psycopg2
from psycopg2 import sql

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_point():
    data = request.json

    try:
        conn = connect_to_db()
        cursor = conn.cursor()

        # Insert into gettinglost_tracking
        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'])
        )

        # Get the ID of the just inserted record
        record_id = cursor.fetchone()[0]

        # Insert into gettinglost_geom using the retrieved ID
        cursor.execute(
            """
            INSERT INTO public.gettinglost_geom (ID, PointType, geom) 
            VALUES (%s, %s, ST_SetSRID(ST_Point(%s, %s), 4326));
            """,
            (record_id, data['pointType'], data['lon'], data['lat'])
        )

        conn.commit()

        return jsonify(message="Data recorded successfully!")
    except Exception as e:
        return jsonify(error=str(e))
    finally:
        cursor.close()
        conn.close()

if __name__ == '__main__':
    app.run(debug=True)