-
Notifications
You must be signed in to change notification settings - Fork 1
/
db.py
76 lines (55 loc) · 1.77 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
import os
from datetime import datetime
import certifi
from pymongo import MongoClient
from copy import deepcopy
from dotenv import load_dotenv
import pytz
import constants
# Load environment variables from .env file
load_dotenv()
MONGO_DB = os.environ.get('MONGO_DB')
MONGO_URL = os.environ.get('MONGO_URL')
client = MongoClient(MONGO_URL, tlsCAFile=certifi.where())
database = client[MONGO_DB]
# MongoDB
all_profiles_col = database["all_profiles"]
all_messages_col = database["all_messages"]
all_teams_col = database["all_teams"]
def utc_to_eastern(naive, timezone="Canada/Eastern"):
return naive.replace(tzinfo=pytz.utc).astimezone(pytz.timezone(timezone))
def get_doc(id, col):
find = {constants.ID: id}
return col.find_one(find)
def get_messages_for_current_user(user_id):
find = {
constants.RECIPIENT_USER_ID: user_id,
}
return list(all_messages_col.find(find).sort(constants.TIME, -1))
def write_to_col(id=None, col=None, data=None, is_update=False, is_delete=False, upsert=False):
'''
If upsert == True, db will insert a document if no document exists and update if document exists
Note: id is NOT needed if inserting a document
'''
now = utc_to_eastern(datetime.utcnow())
time = f"{now.strftime('%B %d, %Y')} at {now.strftime('%H:%M:%S')}"
find = {constants.ID: id}
if is_update:
data.update(
{
constants.TIME: now,
constants.TIME_FORMATTED: time,
}
)
col.update_one(find, {'$set': data}, upsert=upsert)
return
if is_delete:
col.delete_many(find)
return
data.update(
{
constants.TIME_CREATED: now,
constants.TIME_CREATED_FORMATTED: time,
}
)
col.insert_one(data)