-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
executable file
·97 lines (83 loc) · 2.88 KB
/
views.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
'''
View configuration:
- This program requires geruquote lib to be installed.
- Pyramid views are defined here for each kind of request:
- / -> intial webpage writen 'Desafio Web 1.0'
- /quotes -> quotes display as bullet points
- /quotes/{quote_number} -> quote number and its quote
- /quotes/random -> a random quote from quotes
- Pyramid endpoint returning database
'''
import geruquote
import uuid
import random
import json
import datetime
from db_setup import *
from logging_request import *
from pyramid.response import Response
from pyramid.view import view_config
# init_session creates a new unique id for new sessions
def init_session(request):
session = request.session
try:
session['id']
except KeyError as e:
session['id'] = uuid.uuid4().hex
return session
# route view config for home webpage
@view_config(route_name = 'home')
def home_view(request):
session = init_session(request)
logging_request(session['id'], request.path)
body = '<h1>Desafio Web 1.0</h1>'
return Response(body)
# route view config for quotes webpage
@view_config(route_name = 'quotes')
def quotes(request):
session = init_session(request)
logging_request(session['id'], request.path)
body = geruquote.get_quotes()['quotes']
bodyhtml = '<ul>'
for x in body:
bodyhtml += '<li>{}</li>'.format(x)
bodyhtml += '</ul>'
return Response(bodyhtml)
# route view config for quote_number or randomic quote_number
# If the request is random, select a random quote,
# otherwise select the quote referred to the given number
@view_config(route_name = 'pick_quote')
def pick_quote(request):
session = init_session(request)
logging_request(session['id'], request.path)
q = request.matchdict['quote_number']
bodytempl = '<p>{}</p>'
if q == 'random':
body = geruquote.get_quotes()['quotes']
index = random.randint(0, len(body) - 1)
bodyhtml = bodytempl.format(str(index) + ' ' + body[index])
else:
body = geruquote.get_quote(q)['quote']
bodyhtml = bodytempl.format(body)
return Response(bodyhtml)
# Below are define the endpoint views
def encode(obj):
if isinstance(obj, datetime.date):
return obj.isoformat()
else:
return obj
# whole database is shown
@view_config(route_name = 'log')
def log(request):
s = request_session.select()
rs = s.execute()
jd = json.dumps([dict(r) for r in rs], indent = 4, sort_keys = True, default = encode)
return Response(jd)
# only selected log_id is shown
@view_config(route_name = 'log_id')
def log_id(request):
q = request.matchdict['log_id']
s = request_session.select(request_session.c.request_id == q)
rs = s.execute()
jd = json.dumps([dict(r) for r in rs], indent = 4, sort_keys = True, default = encode)
return Response(jd)