forked from pemami2/twitterLite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_bottle.py
executable file
·163 lines (111 loc) · 3.76 KB
/
user_bottle.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
#!/usr/bin/python
import sys
import textwrap
import logging.config
import sqlite3
import bottle
from bottle import get, post, delete, error, abort, request, response, HTTPResponse
from bottle.ext import sqlite
if not sys.warnoptions:
import warnings
for warning in [DeprecationWarning, ResourceWarning]:
warnings.simplefilter('ignore', warning)
app = bottle.default_app()
plugin = bottle.ext.sqlite.Plugin(dbfile ='db/Users.db')
app.install(plugin)
def json_error_handler(res):
if res.content_type == 'application/json':
return res.body
res.content_type = 'application/json'
if res.body == 'Unknown Error.':
res.body = bottle.HTTP_CODES[res.status_code]
return bottle.json_dumps({'error': res.body})
app.default_error_handler = json_error_handler
def query(db, sql, args=(), one=False):
cur = db.execute(sql, args)
rv = [dict((cur.description[idx][0], value)
for idx, value in enumerate(row))
for row in cur.fetchall()]
cur.close()
return (rv[0] if rv else None) if one else rv
def missingFields(required, posted):
if not required <= posted:
return f'Missing fields: {required - posted}'
else:
return False
@post('/users/')
def createUser(db):
'''Handles name creation'''
data = request.json
missing = missingFields({'username', 'password', 'email'}, data.keys())
if missing:
abort(400, missing)
username = data['username']
password = data['password']
email = data['email']
try:
result = query(db, 'INSERT INTO Users (username, email, password) VALUES (?, ?, ?);', [username, email, password])
except:
abort(500)
response.status = "200 OK"
return response.status
@post('/users/checkPassword/<username>/')
def checkPassword(username, db):
data = request.json
if data is not None:
keys = data.keys()
else:
keys = set()
missing = missingFields({ 'password'}, keys)
if missing:
abort(400, missing)
password = data['password']
try:
db_pass = query(db, 'SELECT password FROM Users WHERE username = ?;', [username], one=True)
except:
abort(500)
if db_pass['Password'] == password:
response.status = 200
else:
response.status = 400
response.body = "Incorrect Password"
return response
@post('/users/<username>/follow/')
def addFollower(username, db):
data = request.json
if data is not None:
keys = data.keys()
else:
keys = set()
missing = missingFields({'usernameToFollow'}, keys)
if missing:
abort(400, missing)
usernameToFollow = data['usernameToFollow']
try:
result = query(db, 'INSERT INTO Following (username, follow) VALUES ( ?, ? );', [username, usernameToFollow])
response.body = result
response.status = 200
except:
abort(500)
return response
@delete('/users/<username>/follow/')
def removeFollower(username, db):
data = request.json
if data is not None:
keys = data.keys()
else:
keys = set()
missing = missingFields({'usernameToUnfollow'}, keys)
if missing:
abort(400, missing)
usernameToUnfollow = data['usernameToUnfollow']
result = query(db, 'DELETE FROM Following WHERE username = :username AND follow = :follow;', {'username': username, 'follow': usernameToUnfollow}, one = True)
response.body = result
response.status = 200
return response.status
@get('/users/<username>/follow/')
def getFollowers(username, db):
row = query(db, 'SELECT follow FROM Following WHERE username = ?;', [username])
return {'followers' : row}
#if __name__ == '__main__':
# bottle.run(host = 'localhost', port = 8000)