-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdb.py
38 lines (33 loc) · 1.41 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
# 2020 Col·lectivaT
#
# DB class for queries
#
import psycopg2
import queries
import pandas
from datetime import datetime
class DB(object):
def __init__(self, config, date_config):
server, to_user, to_password, to_database = config
self.connection = psycopg2.connect(host=server,
database=to_database,
user=to_user,
password=to_password)
self.date_start, self.date_end, self.date_active_member = date_config
self.active_users = None
def get_active_users(self, organization_id=None):
active_date_start = min(self.date_start,
self.date_active_member)
active_users_query = queries.ACTIVE_USERS%(
datetime.strftime(active_date_start, '%Y-%m-%d'),
datetime.strftime(self.date_end, '%Y-%m-%d'))
print(active_users_query)
if organization_id == None:
# get the whole active users in order not to query multiple times
if not self.active_users:
self.active_users = pandas.read_sql_query(active_users_query,
self.connection)
else:
if not self.active_users:
# get the whole active users
self.get_active_users()