-
Notifications
You must be signed in to change notification settings - Fork 1
/
db.py
104 lines (86 loc) · 2.72 KB
/
db.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
from datetime import datetime
from random import choice
from emoji import emojize
from pymongo import MongoClient
import settings
client = MongoClient(settings.MONGO_LINK)
db = client[settings.MONGO_DB]
def get_or_create_user(db, effective_user, chat_id):
user = db.users.find_one({"user_id": effective_user.id})
if not user:
user = {
"user_id": effective_user.id,
"first_name": effective_user.first_name,
"last_name": effective_user.last_name,
"username": effective_user.username,
"chat_id": chat_id,
"emoji": emojize(choice(settings.USER_EMOJI), use_aliases=True)
}
db.users.insert_one(user)
return user
def save_anketa(db, user_id, anketa_data):
user = db.users.find_one({"user_id": user_id})
anketa_data['created'] = datetime.now()
if 'anketa' not in user:
db.users.update_one(
{'_id': user['_id']},
{'$set': {'anketa': [anketa_data]}}
)
else:
db.users.update_one(
{'_id': user['_id']},
{'$push': {'anketa': anketa_data}}
)
def subscribe_user(db, user_data):
if not user_data.get('subscribed'):
db.users.update_one(
{'_id': user_data['_id']},
{'$set': {'subscribed': True}}
)
def unsubscribe_user(db, user_data):
db.users.update_one(
{'_id': user_data['_id']},
{'$set': {'subscribed': False}}
)
def get_subscribed(db):
return db.users.find({"subscribed": True})
def save_cat_image_vote(db, user_data, image_name, vote):
image = db.images.find_one({"image_name": image_name})
if not image:
image = {
"image_name": image_name,
"votes": [{"user_id": user_data["user_id"], "vote": vote}]
}
db.images.insert_one(image)
elif not user_voted(db, image_name, user_data["user_id"]):
db.images.update_one(
{"image_name": image_name},
{"$push": {"votes": {"user_id": user_data["user_id"], "vote": vote}}}
)
def user_voted(db, image_name, user_id):
if db.images.find_one({"image_name": image_name, "votes.user_id": user_id}):
return True
return False
def get_image_rating(db, image_name):
rating = db.images.aggregate([
{
'$match': {
'image_name': image_name
}
}, {
'$unwind': {
'path': '$votes'
}
}, {
'$group': {
'_id': '$image_name',
'rating': {
'$sum': '$votes.vote'
}
}
}
])
rating = next(rating, None)
if rating:
return rating['rating']
return 0