-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviews.py
More file actions
74 lines (58 loc) · 2.07 KB
/
views.py
File metadata and controls
74 lines (58 loc) · 2.07 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
from aiohttp import web
def to_json(string):
a = ['id', 'name', 'surname']
string = string.split()
data = dict(zip(a, string))
return (data)
async def postinfo(request):
data = await request.post()
name = data['name']
surname = data['surname']
return name, surname
def homepagehandler(request):
return web.FileResponse('website/index.html')
def gethandler(request):
with open('DataBase.txt') as dbfile:
data = list()
for user in dbfile:
data.append(to_json(user))
return web.json_response(data)
def getidhandler(request):
with open('DataBase.txt') as dbfile:
for user in dbfile:
data = to_json(user)
if user.split()[0] == request.match_info['id']:
return web.json_response(data)
async def postidhandler(request):
a = ''
with open('DataBase.txt') as dbfile:
for user in dbfile:
a += user
if user.split()[0] == request.match_info['id']:
return web.Response(
status=409,
text='user with this id exists'
)
with open('DataBase.txt', 'w') as dbfilewr:
data = await postinfo(request)
newstud = request.match_info['id'] + ' ' + data[0] + ' ' + data[1]
print(a + newstud, file=dbfilewr)
with open('DataBase.txt') as dbfile:
return web.json_response(to_json(dbfile.readlines()[-1]))
def deleteidhandler(request):
a = ''
with open('DataBase.txt') as dbfile:
for user in dbfile:
if user[0] == request.match_info['id']:
with open('DataBase.txt', 'w') as dbfilewr:
b = dbfile.read()
print((a + b)[:-1], file=dbfilewr)
a += user
return
async def putidhandler(request):
id = request.match_info['id']
with open('DataBase.txt') as dbfile:
for user in dbfile:
if user.split()[0] == id:
deleteidhandler(request)
return await postidhandler(request)