-
Notifications
You must be signed in to change notification settings - Fork 0
/
first_interaction.py
99 lines (82 loc) · 3.03 KB
/
first_interaction.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
def first_interaction_setup(update, mycursor):
'''Sets up tables for a new user'''
add_to_user_info(update, mycursor)
add_private_pack_to_pack_info(update, mycursor)
add_packs_to_user_packs(update, mycursor)
add_role_on_private_pack(update, mycursor)
def add_to_user_info(update, mycursor):
'''adds new user to user_info'''
user_data = {}
user_data['user_id'] = update.message.from_user.id
user_data['username'] = update.message.chat.username
user_data['language_code'] = update.message.from_user.language_code
user_data['call_dttm'] = update.message.date.strftime(
'%Y-%m-%d %H:%M:%S')
sql = '''insert into user_info
values (%s, %s, %s, %s);'''
val = (
user_data['user_id'], user_data['username'],
user_data['language_code'], user_data['call_dttm']
)
mycursor.execute(sql, val)
def add_private_pack_to_pack_info(update, mycursor):
'''creates private pack for a new user'''
user_data = {}
user_data['pack_name'] = 'private'
user_data['pack_author_id'] = update.message.from_user.id
user_data['create_dttm'] = update.message.date.strftime(
'%Y-%m-%d %H:%M:%S')
user_data['type'] = 'private'
sql = '''insert into pack_info (
pack_name, pack_author_id, create_dttm, type)
values (%s, %s, %s, %s)'''
val = (
user_data['pack_name'], user_data['pack_author_id'],
user_data['create_dttm'], user_data['type']
)
mycursor.execute(sql, val)
def add_packs_to_user_packs(update, mycursor):
'''adds default pack and user\'s private pack to user_packs'''
user_data = {}
user_data['user_id'] = update.message.from_user.id
user_data['added_dttm'] = update.message.date.strftime(
'%Y-%m-%d %H:%M:%S')
sql = '''
select pack_id from pack_info
where
pack_author_id = %s
and pack_name = "private"
'''
val = (update.message.from_user.id,)
mycursor.execute(sql, val)
result = mycursor.fetchall()
private_pack_id = result[0][0]
sql = '''insert into user_packs
values (%s, %s, %s)'''
val = [
[user_data['user_id'], 1, user_data['added_dttm']],
[user_data['user_id'], private_pack_id, user_data['added_dttm']]
]
mycursor.executemany(sql, val)
def add_role_on_private_pack(update, mycursor):
'''grants admin role on private pack'''
user_data = {}
user_data['user_id'] = update.message.from_user.id
user_data['added_dttm'] = update.message.date.strftime(
'%Y-%m-%d %H:%M:%S')
sql = '''
select pack_id from pack_info
where
pack_author_id = %s
and pack_name = "private"
'''
val = (update.message.from_user.id,)
mycursor.execute(sql, val)
result = mycursor.fetchall()
private_pack_id = result[0][0]
sql = '''insert into user_pack_roles
values (%s, %s, %s, %s, %s)'''
val = (
user_data['user_id'], private_pack_id, 'admin', -1,
user_data['added_dttm'])
mycursor.execute(sql, val)