-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
executable file
·301 lines (250 loc) · 9.89 KB
/
app.py
File metadata and controls
executable file
·301 lines (250 loc) · 9.89 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/usr/bin/env python
import os
import bcrypt
import re
from datetime import datetime, date
from flask import Flask, abort, request, json, jsonify, g, url_for
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.sql import func
from flask_httpauth import HTTPBasicAuth
from flask_migrate import Migrate
# initialization
app = Flask(__name__)
app.config['SECRET_KEY'] = 'somerandomstring'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite3'
app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
# extensions
db = SQLAlchemy(app)
auth = HTTPBasicAuth()
migrate = Migrate(app, db)
class User(db.Model):
__tablename__ = 'user'
_id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(32), index=True, unique=True)
password = db.Column(db.String(64))
first_name = db.Column(db.String(16))
last_name = db.Column(db.String(16))
email = db.Column(db.String(32), unique=True)
phone = db.Column(db.String(13))
date_joined = db.Column(db.DateTime, default=datetime.utcnow)
is_admin = db.Column(db.Boolean, default=0)
feedback = db.relationship('Feedback', backref='User', uselist=False)
def __init__(self, username, first_name, email, last_name, phone):
self.username = username
self.email = email
self.first_name = first_name
self.last_name = last_name
self.phone = phone
def hash_password(self, password):
self.password = bcrypt.hashpw(password, bcrypt.gensalt())
def check_password(self, password):
return bcrypt.checkpw(password, str(self.password))
def full_name(self):
return self.first_name + ' ' + self.last_name
def __str__(self):
return '<User: %s>' % str(self.username)
class Feedback(db.Model):
__tablename__='feedback'
_id = db.Column(db.Integer, primary_key=True)
stars = db.Column(db.Integer)
comment = db.Column(db.String(50))
ts_submit = db.Column(db.DateTime, default=datetime.utcnow())
user_id = db.Column(db.Integer, db.ForeignKey('user._id'))
def __init__(self, stars, comment):
self.stars = stars
self.comment = comment
def __str__(self):
return '<Feedback: %s>' % str(self.comment)
event_user = db.Table('event_user',
db.Column('event_id', db.Integer, db.ForeignKey('event._id')),
db.Column('user_id', db.Integer, db.ForeignKey('user._id')),
db.Column('interested', db.Boolean)
)
class Event(db.Model):
__tablename__='event'
_id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(32))
location = db.Column(db.String(32))
schedule = db.Column(db.DateTime)
logo_url = db.Column(db.String(128), default="assets/logo.png")
requirements = db.Column(db.String(64))
refreshment = db.Column(db.Boolean)
contact = db.Column(db.String(13))
contact_alt = db.Column(db.String(12))
event_user = db.relationship(
'User',
secondary=event_user,
backref=db.backref('events', lazy='dynamic')
)
def __init__(self, title, location, schedule, contact):
self.title = title
self.location= location
self.schedule = schedule
self.contact = contact
def to_json(self):
return {
'_id' : self._id,
'title': self.title,
'location': self.location,
'schedule': self.schedule,
'logo_url': self.logo_url,
'requirements': self.requirements,
'contact' : self.contact,
'contact_alt' : self.contact_alt,
'going': len(self.event_user)
}
def __str__(self):
return '<Event: %s>' % str(self.title)
def custom_error(status_code, err_msg):
response = {
'status': status_code,
'message': err_msg
}
return jsonify(response)
@auth.verify_password
def verify_password(username, password):
user = User.query.filter_by(username=str(username)).first()
if not user or not user.check_password(str(password)):
return False
g.user = user
return True
@app.route('/api/v1/users', methods=['POST'])
def new_user():
username = str(request.json.get('username'))
password = str(request.json.get('password'))
first_name = str(request.json.get('first_name'))
last_name = str(request.json.get('last_name'))
email = str(request.json.get('email'))
phone = str(request.json.get('phone'))
if re.match(r"^[a-zA-Z0-9_]+$", username) is None:
return custom_error(400, 'username format is lowercase and numeric')
if first_name is None or len(first_name) < 3:
return custom_error(400, 'first_name cannot be null')
if re.match(r"[^@]+@[^@]+\.[^@]+", email) is None:
return custom_error(400, 'email is not correct')
if password is None or len(password) < 6:
return custom_error(400, 'password length must be > 6')
user = User.query.filter_by(username=username).first()
if user is not None:
return custom_error(409, 'username already exists ! try differenr')
user = User(username, first_name, email, last_name, phone)
user.hash_password(password)
# First user is always Admin
u = User.query.all()
if u is None or len(u) == 0:
user.is_admin = True
db.session.add(user)
db.session.commit()
return (jsonify({'username': user.username}), 201, {'Location': url_for('get_user', _id=user._id, _external=True)})
@app.route('/api/v1/users/<int:_id>', methods=['GET'])
def get_user(_id):
user = User.query.get(_id)
if not user:
custom_error(404, 'User not found')
return jsonify({'full_name': user.full_name(), 'email': user.email, 'phone': user.phone})
@app.route('/api/v1/users', methods=['GET'])
@auth.login_required
def get_all_user():
users = User.query.all()
if not users:
return jsonify({'status':'success', 'count':0})
else:
res = {'status':'success', 'count': len(users), 'users': []}
for user in users:
res['users'].append({'_id': user._id,'full_name': user.full_name(), 'email': user.email, 'phone': user.phone})
return jsonify(res)
@app.route('/api/v1/feedback', methods=['POST'])
@auth.login_required
def submit_feedback():
stars = int(request.json.get('stars'))
comment = str(request.json.get('comment'))
f = Feedback.query.filter_by(user_id=g.user._id).first()
if f is not None:
return jsonify({'status':'error', 'message':'Feedback Already Submitted !'})
feedback = Feedback(stars, comment)
feedback.user_id = g.user._id
db.session.add(feedback)
db.session.commit()
return jsonify({'status':'success', 'message':'Feedback Submitted !'})
@app.route('/api/v1/events/<int:_id>', methods=['GET'])
@auth.login_required
def get_event(_id):
event = Event.query.get(_id)
if event is None:
return custom_error(404, 'Event not found')
return jsonify({'status': 'success', 'event': event.to_json()})
@app.route('/api/v1/events', methods=['GET'])
@auth.login_required
def get_all_event():
when = int(request.args.get('when'))
if when == 0:
events = Event.query.filter(func.date(Event.schedule) < date.today())
elif when == 1:
events = Event.query.filter(func.date(Event.schedule) == date.today())
elif when == 2:
events = Event.query.filter(func.date(Event.schedule) > date.today())
else:
return custom_error(400, 'Invalid query body')
if events is None:
return custom_error(404, 'Event not found')
response = {'status': 'success', 'events': []}
for event in events:
response['events'].append(event.to_json())
return jsonify(response)
@app.route('/api/v1/events', methods=['POST'])
@auth.login_required
def create_event():
if g.user.is_admin == False:
return custom_error(400, 'You are not an admin')
title = str(request.json.get('title'))
location = str(request.json.get('location'))
schedule = datetime.strptime(str(request.json.get('schedule')), '%Y-%m-%d %H:%M:%S')
logo_url = str(request.json.get('logo_url'))
contact = str(request.json.get('contact'))
contact_alt = str(request.json.get('contact_alt'))
requirements = str(request.json.get('requirements'))
refreshment = int(request.json.get('refreshment'))
event = Event(title, location, schedule, contact)
event.logo_url = logo_url
event.contact_alt = contact_alt
event.requirements = requirements
event.refreshment = refreshment
db.session.add(event)
db.session.commit()
return (jsonify({'title': event.title}), 201, {'Location': url_for('get_event', _id=event._id, _external=True)})
@app.route('/api/v1/admin/<int:_id>', methods=['PUT'])
@auth.login_required
def create_admin(_id):
if g.user.is_admin is None or g.user.is_admin == False:
return custom_error(400, 'You are not an admin')
user = User.query.get(_id)
if user is None:
return custom_error(404, 'User not found')
user.is_admin = True
db.session.commit()
print user.username, ' made admin by ', g.user.username
return jsonify({"status":"success", "message":"new admin created"})
@app.route('/api/v1/join/<int:_id>', methods=['POST'])
@auth.login_required
def join_event(_id):
event = Event.query.get(_id)
if event is None:
return custom_error(404, "Event not found !")
event.event_user.append(g.user)
db.session.commit()
return jsonify({"status":"success", "message":"Successfully Joined"})
@app.route('/api/v1/going/<int:_id>', methods=['GET'])
@auth.login_required
def going_user(_id):
event = Event.query.get(_id)
if event is None:
return custom_error(404, "Event not found !")
response = {"status":"success", "count": len(event.event_user), "users": []}
for user in event.event_user:
response["users"].append(user.full_name())
return jsonify(response)
if __name__=='__main__':
if not os.path.exists('db.sqlite3'):
db.create_all()
app.run(debug=True)