-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
77 lines (66 loc) · 2.77 KB
/
models.py
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from flask import jsonify, request, session, redirect, url_for
from passlib.hash import pbkdf2_sha256
import uuid
from user.db import get_db
import datetime
class User:
def start_session(self, user):
del user['Password']
session['logged_in'] = True
session['user'] = user
return jsonify(user), 200
def login(self):
email = request.form.get('email')
password = request.form.get('password')
user = get_db().your_db.find_one({"Email": email})
if user and pbkdf2_sha256.verify(password, user['Password']):
del user['Password']
session['logged_in'] = True
session['user'] = user
return redirect('/dashboard')
else:
return jsonify({"error": "Invalid email or password"}), 401
def signup(self):
user_data = {
"_id": uuid.uuid4().hex,
"Name": request.form.get('name'),
"Surname": request.form.get('surname'),
"Email": request.form.get('email'),
"Password": request.form.get('password'),
"TCKNO": request.form.get('tckimlik'),
"DateOfBirth": request.form.get('birthday'),
"PhoneNumber": request.form.get('phone')
}
existing_user = get_db().your_db.find_one({
"$or": [
{"Email": user_data["Email"]},
{"TCKNO": user_data["TCKNO"]},
{"PhoneNumber": user_data["PhoneNumber"]}
]
})
if existing_user:
existing_field = next(field for field in ["Email", "TCKNO", "PhoneNumber"] if existing_user.get(field) == user_data[field])
return jsonify({"error": f"{existing_field} already exists"}), 400
user_data['Password'] = pbkdf2_sha256.encrypt(user_data['Password'])
db = get_db()
if db.your_db.insert_one(user_data):
return self.start_session(user_data)
def signout(self):
session.clear()
return redirect('/')
def create_post(self):
if 'user' not in session:
return jsonify({"error": "Unauthorized access"}), 403
post_data = {
"_id": uuid.uuid4().hex,
"title": request.form.get('title'),
"description": request.form.get('description'),
"content": request.form.get('content'),
"author": session['user']['Email'],
"created_at": datetime.datetime.utcnow()
}
db = get_db()
if db.blog_db.insert_one(post_data): # Save to blog_db collection
return redirect(url_for('dashboard')) # Redirect to the dashboard
else:
return jsonify({"error": "Failed to create post"}), 500