-
Notifications
You must be signed in to change notification settings - Fork 1
/
models.py
139 lines (110 loc) · 3.5 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
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
"""
models file create models to be used in application,
we use mongodb for persistance layer
"""
from functools import wraps
from pymongo import MongoClient
from bson.objectid import ObjectId
# mongodb connection handler
try:
mongo = MongoClient("localhost", 27017)
print("MongoDB Connected successfully!!!")
except pymongo.errors.ConnectionFailure as e:
raise Exception("Could not connect to MongoDB: %s" % e)
# Initialize myson mongodb database
db = mongo.myson
class MissingFields(Exception):
"""
missing model field exception
"""
pass
class UnknownField(Exception):
"""
unknown model field exception
"""
pass
def validate(func):
"""
validate fields and raise unknown field exception if fields do not match
"""
@wraps(func)
def wrapper(self, *args, **kwargs):
for key, value in kwargs.items():
if key not in self.fields:
raise UnknownField("unknown field {0}".format(key))
return func(self, *args, **kwargs)
return wrapper
class Model(object):
"""
base model class for mongo collections
"""
@validate
def create(self, *args, **kwargs):
if not all(map(lambda each: each in kwargs.keys(), self.required_fields)):
raise MissingFields(
"required fields are missing uid & phone number")
result = self.collection.insert(kwargs)
kwargs['_id'] = result
return kwargs
@validate
def get(self, *args, **kwargs):
"""
get mongodb document for given kwargs
"""
return self.collection.find_one(kwargs)
@validate
def filter(self, *args, **kwargs):
"""
filter mongodb collection for given kwargs
"""
result = self.collection.find(kwargs)
return list(result)
@validate
def count(self, *args, **kwargs):
"""
get mongodb document count for given kwargs
"""
return self.collection.count(kwargs)
@validate
def update(self, *args, **kwargs):
"""
update mongodb collection for given kwargs
"""
return self.collection.find_one_and_update({"_id": ObjectId(args[0])}, {"$set": kwargs})
class User(Model):
"""
user collection to store user details
"""
def __init__(self):
"""
initialize mongodb user collection with fields and required fileds
"""
self.collection = db.user
self.fields = ["_id", "uid", "phone_num", "first_name",
"last_name", "username", "photo", "is_admin"]
self.required_fields = ["uid", "phone_num"]
@property
def admins(self):
"""
method to get list of admins in user models
"""
users = self.filter(is_admin=True)
return [i['uid'] for i in users]
class Group(Model):
"""
group collection to store telegram group details
"""
def __init__(self):
self.collection = db.tele_group
self.fields = ["_id", "gid", "group_name", "members", "photo"]
self.required_fields = ["gid", "group_name"]
class ServerInfo(Model):
"""
server info collection to store server credentials and access user
"""
def __init__(self):
self.collection = db.serverinfo
self.fields = ["_id", "server_ip", "server_name",
"server_username", "server_password", "users"]
self.required_fields = ["server_ip", "server_name",
"server_username", "server_password", "users"]