ParthCodes commited on
Commit
22b6105
·
verified ·
1 Parent(s): 83d4944

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +59 -18
main.py CHANGED
@@ -1,4 +1,4 @@
1
- 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
@@ -14,7 +14,11 @@ from controllers.demo import get_initial_data
14
  from controllers.mindmap import saveMindmap, getMindmap, getMindmapHistory, deleteMindmap, getMindmapByid, saveMindmapById, saveGeneratedData
15
  import json
16
  from bson import ObjectId, json_util
 
 
 
17
  from dotenv import load_dotenv
 
18
  import os
19
  import re
20
  import requests
@@ -44,6 +48,7 @@ users_collection = db["users"]
44
  interviews_collection = db["interviews"]
45
  savedMindmap = db["savedMindmap"]
46
  roadmap_collection = db['roadmaps']
 
47
 
48
  # Send a ping to confirm a successful connection
49
  try:
@@ -83,9 +88,7 @@ safety_settings = [
83
  },
84
  ]
85
 
86
- model = genai.GenerativeModel('gemini-1.0-pro',
87
- generation_config=generation_config,
88
- safety_settings=safety_settings)
89
 
90
  # Caches to reduce no of queries to MongoDB...
91
  user_id_ping = {'current': 0}
@@ -378,6 +381,7 @@ def interview():
378
 
379
  - It is required that the response should be an object. Don't include any other verbose explanations and don't include the markdown syntax anywhere.''')
380
  print(response.text)
 
381
  temp = json.loads(response.text)
382
  user_chats[user_id]['qa'] = [{'question': temp['question']}]
383
  return jsonify({'success': True, 'data': response.text})
@@ -530,12 +534,11 @@ def useridping():
530
 
531
 
532
  # User Routes
533
- @app.route('/user/signup', methods=['POST'])
534
- def signup():
535
  data = request.json
536
  name = data.get('name')
537
  email = data.get('email')
538
- password = data.get('password')
539
 
540
  if not email:
541
  return jsonify({"error": "Invalid email"}), 400
@@ -544,23 +547,61 @@ def signup():
544
 
545
  if existing_user:
546
  return jsonify({"message": "User already exists"}), 404
 
 
547
 
548
- hashed_password = bcrypt.generate_password_hash(password).decode('utf-8')
 
 
 
549
 
550
- result = users_collection.insert_one({
551
- "name": name,
552
- "email": email,
553
- "password": hashed_password
554
- })
555
 
556
- print(result)
557
 
558
- expires = timedelta(days=7)
559
- access_token = create_access_token(identity={"email": email, "id": str(result.inserted_id)}, expires_delta=expires)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
560
 
561
- res = {"name": name, "email": email, "userId": str(result.inserted_id)}
 
562
 
563
- return jsonify({"result": res, "token": access_token}), 201
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
564
 
565
  @app.route('/user/signin', methods=['POST'])
566
  def signin():
 
1
+ from flask import Flask, jsonify, render_template, request
2
  from flask_cors import CORS
3
  from pymongo.mongo_client import MongoClient
4
  from pymongo.server_api import ServerApi
 
14
  from controllers.mindmap import saveMindmap, getMindmap, getMindmapHistory, deleteMindmap, getMindmapByid, saveMindmapById, saveGeneratedData
15
  import json
16
  from bson import ObjectId, json_util
17
+ from email.message import EmailMessage
18
+ import smtplib
19
+ import ssl
20
  from dotenv import load_dotenv
21
+ import random
22
  import os
23
  import re
24
  import requests
 
48
  interviews_collection = db["interviews"]
49
  savedMindmap = db["savedMindmap"]
50
  roadmap_collection = db['roadmaps']
51
+ otps_collection = db['otps']
52
 
53
  # Send a ping to confirm a successful connection
54
  try:
 
88
  },
89
  ]
90
 
91
+ model = genai.GenerativeModel('gemini-1.0-pro', generation_config=generation_config, safety_settings=safety_settings)
 
 
92
 
93
  # Caches to reduce no of queries to MongoDB...
94
  user_id_ping = {'current': 0}
 
381
 
382
  - It is required that the response should be an object. Don't include any other verbose explanations and don't include the markdown syntax anywhere.''')
383
  print(response.text)
384
+ print(response.text)
385
  temp = json.loads(response.text)
386
  user_chats[user_id]['qa'] = [{'question': temp['question']}]
387
  return jsonify({'success': True, 'data': response.text})
 
534
 
535
 
536
  # User Routes
537
+ @app.route('/user/verifymail', methods=['POST'])
538
+ def verifymail():
539
  data = request.json
540
  name = data.get('name')
541
  email = data.get('email')
 
542
 
543
  if not email:
544
  return jsonify({"error": "Invalid email"}), 400
 
547
 
548
  if existing_user:
549
  return jsonify({"message": "User already exists"}), 404
550
+
551
+ msg = EmailMessage()
552
 
553
+ otp = str(random.randint(100000, 999999))
554
+ msg["Subject"] = "GenUP Nexus Verification"
555
+ msg["From"] = "GenUP Nexus Team"
556
+ msg["To"] = email
557
 
558
+ html_content = render_template('email.html', name=name, otp=otp)
559
+ msg.set_content(html_content, subtype='html')
 
 
 
560
 
561
+ context = ssl.create_default_context()
562
 
563
+ with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
564
+ smtp.login('[email protected]', os.getenv('GMAIL_SSL_KEY'))
565
+ smtp.send_message(msg)
566
+
567
+ otps_collection.insert_one({"email": email, "otp": otp})
568
+
569
+ return jsonify({"success": True}), 200
570
+
571
+
572
+ @app.route('/user/signup', methods=['POST'])
573
+ def signup():
574
+ data = request.json
575
+ print(data)
576
+ form = data.get('form')
577
+ name = form['name']
578
+ email = form['email']
579
+ password = form['password']
580
+ otp = data.get('otp')
581
 
582
+ if not email:
583
+ return jsonify({"error": "Invalid email"}), 400
584
 
585
+ stored_otp = otps_collection.find_one({"email": email}, sort=[('_id', -1)])
586
+
587
+ if stored_otp['otp'] == otp:
588
+ hashed_password = bcrypt.generate_password_hash(password).decode('utf-8')
589
+
590
+ result = users_collection.insert_one({
591
+ "name": name,
592
+ "email": email,
593
+ "password": hashed_password
594
+ })
595
+
596
+ expires = timedelta(days=7)
597
+ access_token = create_access_token(identity={"email": email, "id": str(result.inserted_id)}, expires_delta=expires)
598
+
599
+ res = {"name": name, "email": email, "userId": str(result.inserted_id)}
600
+
601
+ return jsonify({"result": res, "token": access_token}), 201
602
+
603
+ else:
604
+ return jsonify({"error": "Invalid otp"}), 400
605
 
606
  @app.route('/user/signin', methods=['POST'])
607
  def signin():