ParthCodes commited on
Commit
98df545
·
verified ·
1 Parent(s): 53117b4

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +136 -11
main.py CHANGED
@@ -2,21 +2,148 @@ from flask import Flask, jsonify, request
2
  from flask_cors import CORS
3
  from pymongo.mongo_client import MongoClient
4
  from pymongo.server_api import ServerApi
5
- from bson.objectid import ObjectId
6
  import google.generativeai as genai
7
  import urllib.parse
8
- from models import UserSchema
9
  from flask_bcrypt import Bcrypt
 
10
  from flask_jwt_extended import JWTManager, create_access_token
11
- from middleware.authUser import auth_user
12
  from datetime import timedelta
13
- from controllers.demo import get_initial_data
14
- from controllers.mindmap import saveMindmap, getMindmap, deleteMindmap, getMindmapByid
15
  import json
16
- from dotenv import load_dotenv
17
  import os
18
 
19
- load_dotenv()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  app = Flask(__name__)
22
 
@@ -158,8 +285,6 @@ def tree():
158
  # return temp
159
 
160
 
161
-
162
-
163
  @app.route('/tree/demo', methods=["POST"])
164
  def treeDemo():
165
  if request.method == 'POST':
@@ -484,7 +609,7 @@ def signup():
484
  "password": hashed_password
485
  })
486
 
487
- print(result);
488
 
489
  expires = timedelta(days=7)
490
  access_token = create_access_token(identity={"email": email, "id": str(result.inserted_id)}, expires_delta=expires)
@@ -561,7 +686,7 @@ def mindmapDelete():
561
  @app.route('/mindmap/demo', methods=['POST'])
562
  def mindmapDemo():
563
  data = request.json
564
- print(data);
565
  return get_initial_data(), 200
566
 
567
 
 
2
  from flask_cors import CORS
3
  from pymongo.mongo_client import MongoClient
4
  from pymongo.server_api import ServerApi
5
+ from bson import json_util, ObjectId
6
  import google.generativeai as genai
7
  import urllib.parse
 
8
  from flask_bcrypt import Bcrypt
9
+ import jwt
10
  from flask_jwt_extended import JWTManager, create_access_token
11
+ # from middleware.authUser import auth_user
12
  from datetime import timedelta
13
+ # from controllers.demo import get_initial_data
14
+ # from controllers.mindmap import saveMindmap, getMindmap, deleteMindmap, getMindmapByid
15
  import json
16
+ # from dotenv import load_dotenv
17
  import os
18
 
19
+ # load_dotenv()
20
+
21
+ def auth_user(next_function):
22
+ def middleware_function(*args, **kwargs):
23
+ try:
24
+ token = request.headers.get('Authorization', '').split(" ")[1]
25
+ is_custom_auth = len(token) < 500
26
+ decoded_data = None
27
+
28
+ if token and is_custom_auth:
29
+ secret_key = os.getenv('JWT_SECRET')
30
+ decoded_data = jwt.decode(token, secret_key, algorithms=["HS256"])
31
+ print(decoded_data)
32
+ dat = decoded_data.get('sub')
33
+ request.email = dat.get('email')
34
+ request.userId = dat.get('id')
35
+ else:
36
+ #google auth
37
+ decoded_data = jwt.decode(token)
38
+ request.email = decoded_data.get('email')
39
+ request.userId = decoded_data.get('sub')
40
+
41
+ return next_function(*args, **kwargs)
42
+
43
+ except Exception as e:
44
+ print(e) # Log the error if needed
45
+ return jsonify({"error": "Unauthorized"}), 401
46
+
47
+ return middleware_function
48
+
49
+ def get_initial_data():
50
+ initial_nodes = [
51
+ {
52
+ "id": "data-input",
53
+ "position": {"x": 0, "y": 0},
54
+ "data": {"label": "Data Input"}
55
+ },
56
+ {
57
+ "id": "data-preprocessing",
58
+ "position": {"x": 200, "y": 0},
59
+ "data": {"label": "Data Preprocessing"}
60
+ },
61
+ {
62
+ "id": "model-training",
63
+ "position": {"x": 400, "y": 0},
64
+ "data": {"label": "Model Training"}
65
+ },
66
+ {
67
+ "id": "model-evaluation",
68
+ "position": {"x": 0, "y": 200},
69
+ "data": {"label": "Model Evaluation"}
70
+ },
71
+ {
72
+ "id": "prediction",
73
+ "position": {"x": 200, "y": 200},
74
+ "data": {"label": "Prediction"}
75
+ },
76
+ {
77
+ "id": "data-visualization",
78
+ "position": {"x": 400, "y": 200},
79
+ "data": {"label": "Data Visualization"}
80
+ },
81
+ ]
82
+
83
+ initial_edges = [
84
+ {"id": "data-input-to-preprocessing", "source": "data-input", "target": "data-preprocessing"},
85
+ {"id": "preprocessing-to-training", "source": "data-preprocessing", "target": "model-training"},
86
+ {"id": "training-to-evaluation", "source": "model-training", "target": "model-evaluation"},
87
+ {"id": "training-to-prediction", "source": "model-training", "target": "prediction"},
88
+ {"id": "evaluation-to-visualization", "source": "model-evaluation", "target": "data-visualization"},
89
+ {"id": "prediction-to-visualization", "source": "prediction", "target": "data-visualization"}
90
+ ]
91
+
92
+ return jsonify({
93
+ "initialNodes": initial_nodes,
94
+ "initialEdges": initial_edges
95
+ })
96
+
97
+ def saveMindmap(data, userId, savedMindmap):
98
+ try:
99
+ result = savedMindmap.insert_one({
100
+ "userId": userId,
101
+ "data": data,
102
+ })
103
+
104
+ print(result.inserted_id)
105
+ return jsonify({"result": str(result.inserted_id)}), 201
106
+ except Exception as e:
107
+ print(e)
108
+ return jsonify({"error": "An error occurred"}), 500
109
+
110
+ def getMindmap(userId, savedMindmap):
111
+ try:
112
+ results = savedMindmap.find({"userId": userId})
113
+ mindmaps = [{"_id": str(result["_id"]), "data": result["data"]} for result in results]
114
+
115
+ if mindmaps:
116
+ # Convert ObjectId to string for JSON serialization
117
+ return json_util.dumps({"data": mindmaps}), 200
118
+ else:
119
+ return jsonify({"msg": "No Mindmap stored"}), 404
120
+ except Exception as e:
121
+ print(e)
122
+ return jsonify({"error": "An error occurred"}), 500
123
+
124
+ def getMindmapByid(userId, id, savedMindmap):
125
+ try:
126
+ object_id = ObjectId(id)
127
+ result = savedMindmap.find_one({"userId": userId, "_id": object_id})
128
+ if result:
129
+ return json_util.dumps({"data": result}), 200
130
+ else:
131
+ return jsonify({"msg": "Mindmap not found"}), 404
132
+ except Exception as e:
133
+ print(e)
134
+ return jsonify({"error": "An error occurred"}), 500
135
+
136
+ def deleteMindmap(userId, data, savedMindmap):
137
+ try:
138
+ object_id = ObjectId(data["_id"])
139
+ result = savedMindmap.delete_one({"userId": userId, "_id": object_id})
140
+ if result.deleted_count == 1:
141
+ return jsonify({"result": True}), 200
142
+ else:
143
+ return jsonify({"result": False, "message": "Mindmap not found"}), 404
144
+ except Exception as e:
145
+ print(e)
146
+ return jsonify({"message": "Something went wrong"}), 500
147
 
148
  app = Flask(__name__)
149
 
 
285
  # return temp
286
 
287
 
 
 
288
  @app.route('/tree/demo', methods=["POST"])
289
  def treeDemo():
290
  if request.method == 'POST':
 
609
  "password": hashed_password
610
  })
611
 
612
+ print(result)
613
 
614
  expires = timedelta(days=7)
615
  access_token = create_access_token(identity={"email": email, "id": str(result.inserted_id)}, expires_delta=expires)
 
686
  @app.route('/mindmap/demo', methods=['POST'])
687
  def mindmapDemo():
688
  data = request.json
689
+ print(data)
690
  return get_initial_data(), 200
691
 
692