-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
182 lines (152 loc) · 5.51 KB
/
app.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
"""This Module contains the backend structure of the project"""
from flask import Flask, request, jsonify, redirect, url_for
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
from flask_cors import CORS
import os
from helper import signup_func, signin_func
app = Flask(__name__)
cors = CORS(app, resources={r"/*": {"origins": "*"}})
basedir = os.path.abspath(os.path.dirname(__file__))
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'test.sqlite')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
ma = Marshmallow(app)
#class for users and drafts
class User(db.Model):
"""Class that defines the user attributes
"""
__tablename__ = 'User'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(200), nullable=False)
email = db.Column(db.String(200), nullable=False)
password = db.Column(db.String(100), nullable=False)
def __init__(self, name, email, password):
self.name = name
self.email = email
self.password = password
class Draft(db.Model):
"""Class that defines the Draft attributes"""
__tablename__ = 'Draft'
draft_id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('User.id'))
draft_name = db.Column(db.String(300), nullable=False)
draft_txt = db.Column(db.Text)
def __init__(self, draft_name, draft_txt, user_id):
self.draft_name = draft_name
self.draft_txt = draft_txt
self.user_id = user_id
class UserSchema(ma.Schema):
"""Class That handles serialization of User"""
class Meta:
fields = ('id', 'name', 'email', 'password')
class DraftSchema(ma.Schema):
"""Class that handles serialization of Drafts"""
class Meta:
fields = ('draft_id', 'user_id', 'draft_txt', "draft_name")
include_relationships = True
load_instance = True
user_schema = UserSchema()
users_schema = UserSchema(many=True)
draft_schema = DraftSchema()
drafts_schema = DraftSchema(many=True)
#create user
@app.route('/signup', methods=["POST"])
def sign_up():
"""Signup route"""
name = request.json['name']
email = request.json['email']
password = request.json['password']
check = signup_func(User, email)
if check == False:
new_user = User(name, email, password)
db.session.add(new_user)
db.session.commit()
return user_schema.jsonify(new_user)
else:
return {"id": -1}
@app.route('/signin', methods=['POST'])
def sign_in():
"""Sigin route"""
email = request.json['email']
password = request.json['password']
users = signin_func(User)
for user in users:
if (user.email == email) and (user.password == password):
"""url = url_for('user_page', id = user.id)
return redirect(url, code=303)"""
return user_page(user.id)
return {"id": -1}
@app.route('/user', methods=["GET"])
def total_users():
"""Returns total users in the database"""
all_users = User.query.all()
total = users_schema.dump(all_users)
return jsonify(total)
@app.route('/user/<id>', methods=["GET", "POST"])
def user_page(id):
"""handles user data retrival"""
user = User.query.get(id)
#return user_schema.jsonify(user)
return user_schema.jsonify(user)
@app.route('/user/<id>', methods=["PUT"])
def update_user(id):
"""Performs user update"""
user = User.query.get(id)
name = request.json['name']
email = request.json['email']
password = request.json['password']
user.name = name
user.email = email
user.password = password
db.session.commit()
return user_schema.jsonify(user)
@app.route('/user/<id>', methods=["DELETE"])
def delete_user(id):
"""Delete user from database"""
user = User.query.get(id)
Draft.query.filter_by(user_id=id).delete()
db.session.delete(user)
db.session.commit()
return user_schema.jsonify(user)
@app.route('/user/draft/<id>', methods=["POST"])
def create_draft(id):
"""Creates a new draft for user"""
draft_name = request.json['draft_name']
draft_txt = request.json['draft_txt']
user_id = id
new_draft = Draft(draft_name, draft_txt, user_id)
db.session.add(new_draft)
db.session.commit()
print(new_draft.draft_txt)
return draft_schema.jsonify(new_draft)
@app.route('/user/draft/<id>', methods=["GET"])
def all_user_drafts(id):
"""Retrives all data to be displayed"""
all_drafts = Draft.query.filter_by(user_id=id).all()
total = drafts_schema.dump(all_drafts)
return jsonify(total)
@app.route('/user/draft/<id>/<d_id>', methods=["GET"])
def one_draft(id, d_id):
"""Retrieves one draft for user viewing"""
draft = Draft.query.filter_by(user_id=id).filter_by(draft_id=d_id).first()
return draft_schema.jsonify(draft)
@app.route('/user/draft/<id>/<d_id>', methods=["PUT"])
def update(id, d_id):
"""Updates drafts in database"""
draft = Draft.query.filter_by(user_id=id).filter_by(draft_id=d_id).first()
draft_name = request.json['draft_name']
draft_txt = request.json['draft_txt']
draft.draft_name = draft_name
draft.draft_txt = draft_txt
db.session.commit()
return draft_schema.jsonify(draft)
@app.route('/user/draft/<id>/<d_id>', methods=["DELETE"])
def delete_draft(id, d_id):
"""Delete draft"""
draft = Draft.query.filter_by(user_id=id).filter_by(draft_id=d_id).first()
db.session.delete(draft)
db.session.commit()
return draft_schema.jsonify(draft)
if __name__ == "__main__":
app.run(debug=True)