-
Notifications
You must be signed in to change notification settings - Fork 0
/
business_logic.py
60 lines (44 loc) · 1.82 KB
/
business_logic.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
from app.data_access import User, Conversations, db
from flask import json
from sqlalchemy import desc
import inspect
# ======================= Queries =======================
def get_all_users():
return User.query.all()
def get_row(number):
return User.query.filter_by(cellphone=number).first()
def update_user_row(number, **kwargs):
user_state = get_row(number)
if not user_state:
user_state = User(cellphone=number, status="newclient")
db.session.add(user_state)
db.session.commit()
for key, value in kwargs.items():
setattr(user_state, key, value)
db.session.commit()
def get_all_convs():
return Conversations.query.all()
def get_conv_row(number):
#return Conversations.query.filter_by(cellphone=number).first()
return Conversations.query.filter_by(cellphone=number).order_by(desc(Conversations.id)).first()
def get_branch(number):
return Conversations.query.filter_by(cellphone=number).order_by(desc(Conversations.id)).with_entities(Conversations.branch).first()
def update_conversation_logs(number , **kwargs):
stack = inspect.stack()
contexts = {}
if len(stack) > 1:
parent_frame = stack[1]
contexts['parent'] = {
'function_name': parent_frame.function}
if len(stack) > 2:
grandparent_frame = stack[2]
contexts['grandparent'] = {
'function_name': grandparent_frame.function}
conv_state = get_conv_row(number)
if not conv_state:
conv_state = Conversations(cellphone=number, hlVl0 = 0, hlVl1 = 0, hlVl2 = 0, hlVl3 = 0, ex_funct = json.dumps(contexts), status = "OPEN")
db.session.add(conv_state)
db.session.commit()
for key, value in kwargs.items():
setattr(conv_state, key, value)
db.session.commit()