File size: 1,677 Bytes
d740e1a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
62
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)