ParthCodes commited on
Commit
03e8a43
·
verified ·
1 Parent(s): 7fada9f

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +36 -163
main.py CHANGED
@@ -2,148 +2,21 @@ 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 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
 
@@ -156,11 +29,11 @@ app.config['JWT_SECRET_KEY'] = os.getenv('JWT_SECRET')
156
  # MongoDB configuration
157
  username = urllib.parse.quote_plus(os.getenv('MONGO_USERNAME'))
158
  password = urllib.parse.quote_plus(os.getenv('MONGO_PASSWORD'))
159
- restUri = os.getenv('REST_URI');
160
 
161
- uri = f'mongodb+srv://{username}:{password}@cluster0.iidzcbc.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0'
162
 
163
- client = MongoClient(uri)
164
  db = client.GenUpNexus
165
  users_collection = db["users"]
166
  interviews_collection = db["interviews"]
@@ -191,7 +64,7 @@ def index():
191
  def index2():
192
  return "routes checking..."
193
 
194
- @app.route('/trees', methods=["POST", "GET"])
195
  def tree():
196
  if request.method == 'POST':
197
  data = request.get_json()
@@ -656,31 +529,31 @@ def delete_account():
656
  return jsonify({"message": "Something went wrong"}), 500
657
 
658
  # mindmap routes
659
- # @app.route('/mindmap/save', methods=['POST'])
660
- # @auth_user
661
- # def mindmapSave():
662
- # userId = request.userId
663
- # data = request.json
664
- # return saveMindmap(data, userId, savedMindmap)
665
-
666
- # @app.route('/mindmap/get', methods=['GET'])
667
- # @auth_user
668
- # def mindmapGet():
669
- # userId = request.userId
670
- # return getMindmap(userId, savedMindmap)
671
-
672
- # @app.route('/mindmap/get/<id>', methods=['GET'])
673
- # @auth_user
674
- # def mindmapGetById(id):
675
- # userId = request.userId
676
- # return getMindmapByid(userId, id, savedMindmap)
677
-
678
- # @app.route('/mindmap/delete', methods=['POST'])
679
- # @auth_user
680
- # def mindmapDelete():
681
- # userId = request.userId
682
- # data = request.json
683
- # return deleteMindmap(userId, data, savedMindmap)
684
 
685
 
686
  @app.route('/mindmap/demo', methods=['POST'])
 
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
 
 
29
  # MongoDB configuration
30
  username = urllib.parse.quote_plus(os.getenv('MONGO_USERNAME'))
31
  password = urllib.parse.quote_plus(os.getenv('MONGO_PASSWORD'))
32
+ restUri = os.getenv('REST_URI')
33
 
34
+ uri = f'mongodb+srv://{username}:{password}{restUri}'
35
 
36
+ client = MongoClient(uri, server_api=ServerApi('1'))
37
  db = client.GenUpNexus
38
  users_collection = db["users"]
39
  interviews_collection = db["interviews"]
 
64
  def index2():
65
  return "routes checking..."
66
 
67
+ @app.route('/tree', methods=["POST", "GET"])
68
  def tree():
69
  if request.method == 'POST':
70
  data = request.get_json()
 
529
  return jsonify({"message": "Something went wrong"}), 500
530
 
531
  # mindmap routes
532
+ @app.route('/mindmap/save', methods=['POST'])
533
+ @auth_user
534
+ def mindmapSave():
535
+ userId = request.userId
536
+ data = request.json
537
+ return saveMindmap(data, userId, savedMindmap)
538
+
539
+ @app.route('/mindmap/get', methods=['GET'])
540
+ @auth_user
541
+ def mindmapGet():
542
+ userId = request.userId
543
+ return getMindmap(userId, savedMindmap)
544
+
545
+ @app.route('/mindmap/get/<id>', methods=['GET'])
546
+ @auth_user
547
+ def mindmapGetById(id):
548
+ userId = request.userId
549
+ return getMindmapByid(userId, id, savedMindmap)
550
+
551
+ @app.route('/mindmap/delete', methods=['POST'])
552
+ @auth_user
553
+ def mindmapDelete():
554
+ userId = request.userId
555
+ data = request.json
556
+ return deleteMindmap(userId, data, savedMindmap)
557
 
558
 
559
  @app.route('/mindmap/demo', methods=['POST'])