-
Notifications
You must be signed in to change notification settings - Fork 45
/
model.py
115 lines (99 loc) · 2.49 KB
/
model.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
import math, hashlib, json, os, random
from datetime import datetime, timedelta
import sqlalchemy as sa
from sqlalchemy.orm import relationship
from sqlalchemy.types import *
from metamodel import *
import bcrypt
@Model
def Log():
target_id = ForeignKey(Integer, 'Target.id')
message = Unicode(65536)
at = DateTime
@staticmethod
def add(target, data):
with transact:
return Log.create(
target_id=target.id,
message=data,
at=datetime.now()
)
@Model
def Hit():
target_id = ForeignKey(Integer, 'Target.id')
request = Unicode(65536)
at = DateTime
@staticmethod
def add(target, request):
with transact:
return Hit.create(
target_id=target.id,
request=request,
at=datetime.now()
)
@Model
def Target():
user_id = ForeignKey(Integer, 'User.id')
enabled = Boolean
link = String(5)
name = Unicode
logs = Log.relation(backref='target')
hits = Hit.relation(backref='target')
@staticmethod
def add(user, link, name):
with transact:
return Target.create(
enabled=True,
user_id=user.id,
link=link,
name=name
)
@Model
def User():
enabled = Boolean
admin = Boolean
username = Unicode(255)
password = String(88)
email = String
registrationDate = DateTime
targets = Target.relation(backref='user')
def setPassword(self, password):
with transact:
self.password = User.hash(password)
@staticmethod
def hash(password):
return bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt(12))
@staticmethod
def checkHash(hash, password):
try:
hash = hash.encode('utf-8')
return bcrypt.hashpw(password.encode('utf-8'), hash) == hash
except:
return False
@staticmethod
def add(username, password, email, admin):
if User.one(enabled=True, username=username) or User.one(enabled=True, email=email):
return None
with transact:
return User.create(
enabled=True,
username=username,
password=User.hash(password),
email=email,
admin=admin,
registrationDate=datetime.now()
)
@staticmethod
def find(username, password):
if username == None or password == None:
return None
user = User.one(enabled=True, username=username)
if user and User.checkHash(user.password, password):
return user
if not user and len(User.all()) == 0:
return User.add(username, password, 'admin@admin', True)
return None
db = 'postgresql://postgres:dbpassword@db/postgres'
@setup(db)
def init():
pass