-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
108 lines (85 loc) · 3.17 KB
/
models.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from common_exception import CommonException
from datetime import datetime, timedelta
from os import urandom
from hashlib import sha256
import bcrypt
from calendar import timegm
from time import time
class BaseModel(object):
def __init__(self, id):
super(BaseModel, self).__init__()
self.id = id
def __eq__(self, other):
return self.id == other.id
class User(BaseModel):
def __init__(self, id=None, name=None, password=None,
admin=False, session=None, channels=None):
super(User, self).__init__(id)
self.name = name
self.password = password
self.admin = admin
self.session = session
self.channels = channels if channels else []
def verify_password(self, password):
return bcrypt.checkpw(password.encode('utf-8'), self.password.encode('utf-8'))
@staticmethod
def new_password(password):
return bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
class Channel(BaseModel):
def __init__(self, id=None, name=None, users=None, messages=None):
super(Channel, self).__init__(id)
self.name = name
self.users = users if users else []
self.messages = messages if messages else []
def add_user(self, user):
if user in self.users:
raise CommonException('User already in chat')
self.users.append(user)
def delete_user(self, user):
del self.users[user]
def get_messages(self):
return sorted(self.messages, key=lambda x: x.timestamp)
class Message(BaseModel):
def __init__(self, id=None, user=None, channel=None, text=None, timestamp=None):
super(Message, self).__init__(id)
self.user = user
self.channel = channel
self.text = text
self.timestamp = timestamp if timestamp \
else datetime_to_timestamp(datetime.utcnow())
def get_dict(self):
return {
'timestamp': self.timestamp,
'text': self.text,
'user': self.user.name if self.user else None,
'id': self.id,
}
class Session(BaseModel):
def __init__(self, id=None, user=None, key=None, expires=None, timezone=0):
super(Session, self).__init__(id)
self.user = user
self.key = key if key else self.generate_session()
self.expires = expires if expires \
else datetime_to_timestamp(datetime.utcnow() + timedelta(days=1))
self.timezone = timezone
def generate_session(self):
parts = [
str(urandom(128)),
str(time()),
str(self.user.id)
]
result = sha256('_'.join(parts).encode('utf-8')).hexdigest()
return result
class ChannelUser(BaseModel):
def __init__(self, id=None, channel=None, user=None,
admin=False, subscribed=None):
super(ChannelUser, self).__init__(id)
self.channel = channel
self.user = user
self.admin = admin
self.subscribed = subscribed if subscribed \
else datetime_to_timestamp(datetime.utcnow())
def datetime_to_timestamp(dt):
return timegm(dt.timetuple())