-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroute.py
166 lines (133 loc) · 4.26 KB
/
route.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
import sys
import os
PACKAGE_PARENT = '..'
SCRIPT_DIR = os.path.dirname(os.path.realpath(os.path.join(os.getcwd(), os.path.expanduser(__file__))))
sys.path.append(os.path.normpath(os.path.join(SCRIPT_DIR, PACKAGE_PARENT)))
from oira import RouterController, Endpoint, Application, json, html, setup_jinja, jinja, redirect
from oira.status import forbidden, ok
from oira.static import serve_static
from oira.exceptions import UnauthorizedError
setup_jinja("./templates")
control = RouterController(debug=True, langs=['fa', 'en'])
@control.route(method=["GET", "PUT", "INSERT"], route="/user/cookie")
class UserEndpoint(Endpoint):
@html
def get(request, response):
if not request.lang:
print("No lang is set")
else:
print("responsig from /", request.lang, "/user")
body = '''
<title>user</title>
<h1>Hello World</h1>
'''
print(request.URI)
response.cookie("key", "value", path='/user')
return body
@redirect("https://github.com/")
def insert(request, response):
pass
@control.route(method=["GET"], route="/logout")
class UserEndpoint(Endpoint):
@html
def get(request, response):
body = '''
<title>remove cookie</title>
<h1>Hello World!!</h1>
'''
response.remove_cookie("key", path='/user')
return body
@control.route(method=["GET", "POST", "HEAD"], route="/")
class DashboardEndpoint(Endpoint):
@json
def get(request, response):
data = {
"name": "shayan",
"family_name": "shafaghi",
"age": 21,
"phonenumber": "09197304252"
}
return data
@control.route(method=["GET", "POST"], route="/user/profile")
class DashboardEndpoint(Endpoint):
@html
def get(request, response):
body = '''
<title>profile</title>
<body>
<h1>This is user profile</h1>
</body>
'''
return body
@control.route(method=["GET", "POST"], route="/user/$id")
class DashboardEndpoint(Endpoint):
@jinja("base.html")
def get(request, response, id):
params = {"my_string": id, "my_list": [0, 1, 2]}
return params
@control.route(method=["GET", "POST"], route="/user_all/*id")
class DashboardEndpoint(Endpoint):
@json
def get(request, response, id):
print(id)
params = {"my_string": id, "my_list": [0, 1, 2]}
return params
@control.route(method=["GET", "POST"], route="/user/profile")
class DashboardEndpoint(Endpoint):
@html
def get(request, response):
body = '''
<title>profile</title>
<body>
<h1>This is user profile</h1>
</body>
'''
return body
@control.route(method=['GET', 'POST'], route="/bug")
class bug(Endpoint):
def post(request, response):
raise Exception("This is an exception")
return b"302 Moved Temporarily"
def get(request, response):
raise UnauthorizedError('abbas is here')
@control.route(method=['GET', 'INSERT'], route="/foo")
class RedirectEndpoint(Endpoint):
def get(request, response):
response.temp_redirect("/")
return b"302 Moved Temporarily"
@json
def insert(request, response):
response.status = ok()
body = request.body
return body
@control.route(method=['POST', 'GET'], route='/file')
class PostFile(Endpoint):
def post(request, response):
form = request.body
fileitem = form['userfile']
filename = fileitem.filename.replace('\\','/').split('/')[-1].strip()
with open(filename, 'wb') as f:
while True:
data = fileitem.file.read(1024)
if not data:
break
f.write(data)
print('name: ', form['fname'].value)
print('last name: ', form['lname'].value)
print('None', form.getvalue('lnam'))
return "hello"
def get(request, response):
file = serve_static("<path-to-file>", response)
return file
@control.default()
@html
def not_found(request, response):
body = '''
<title>Not Found</title>
<body>
<h1>404 Not Found</h1>
</body>
'''
response.status = forbidden()
return body
app = Application(control)