-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmanage.py
More file actions
99 lines (75 loc) · 2.66 KB
/
manage.py
File metadata and controls
99 lines (75 loc) · 2.66 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import os
import sys
import click
from bottle import static_file, Bottle, run, TEMPLATE_PATH, request
from beaker.middleware import SessionMiddleware
from requester import settings
from requester.routes import Routes
from requester.utils import validate_input, hash_password
import sqlite3
TEMPLATE_PATH.insert(0, settings.TEMPLATE_PATH)
session_opts = {
'session.type': 'cookie',
'session.cookie_expires': True,
'session.httponly': True,
'session.timeout': 3600 * 24, # 1 day
'session.validate_key': True,
'session.auto': True,
}
app = SessionMiddleware(Bottle(), session_opts)
# Bottle Routes
app.wrap_app.merge(Routes)
@app.wrap_app.route('/assets/<path:path>', name='assets')
def assets(path):
yield static_file(path, root=settings.STATIC_PATH)
@click.group()
def cmds():
pass
@cmds.command()
@click.option('--port', default=os.environ.get('PORT', 8080), type=int,
help=u'Set application server port!')
@click.option('--ip', default='0.0.0.0', type=str,
help=u'Set application server ip!')
@click.option('--debug', default=False,
help=u'Set application server debug!')
def runserver(port, ip, debug):
click.echo('Start server at: {}:{}'.format(ip, port))
run(app=app, host=ip, port=port, debug=debug, reloader=debug)
@cmds.command()
def test():
import unittest
loader = unittest.TestLoader()
tests = loader.discover('tests')
testRunner = unittest.runner.TextTestRunner()
testRunner.run(tests)
@cmds.command()
def createadmin():
fname = input("Firstname: ")
lname = input("Surname: ")
email = input("Email: ")
phonenumber = input("Phone number: ")
password = input("Password: ")
if validate_input(email, r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)") == False:
print("Please enter a valid email address!")
sys.exit(0)
if validate_input(phonenumber, r"^0\d{9}$") == False:
print("Please enter a valid phone number!")
sys.exit(0)
hashed_password = hash_password(password)
data = (fname, lname, email, phonenumber, hashed_password, "admin")
connection = sqlite3.connect(settings.DB_FILE)
cursorObj = connection.cursor()
try:
cursorObj.execute(
'''INSERT INTO users(fname, lname, email, phonenumber, password, role) VALUES(?, ?, ?, ?, ?, ?)''', data)
connection.commit()
print("Admin user has been successfully created!")
except sqlite3.Error as error:
print("An error occured: ", error.args[0])
sys.exit()
connection.close()
if __name__ == "__main__":
cmds()