Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Tobias Geisler
commited on
Commit
·
10aad52
1
Parent(s):
c91ccb6
refactoring db calls
Browse files- database.py +15 -12
database.py
CHANGED
@@ -3,6 +3,7 @@ import os
|
|
3 |
from sqlalchemy import create_engine, Column, Integer, String, Text, Boolean, DateTime, func
|
4 |
from sqlalchemy.ext.declarative import declarative_base
|
5 |
from sqlalchemy.orm import sessionmaker
|
|
|
6 |
from utils.utils import get_secret
|
7 |
import random
|
8 |
from better_profanity import profanity
|
@@ -82,30 +83,32 @@ def generate_chatbot_id():
|
|
82 |
|
83 |
return f"{random.choice(adjectives)}-{random.choice(nouns)}-{random.randint(100, 999)}"
|
84 |
|
85 |
-
|
|
|
86 |
db = SessionLocal()
|
87 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
chatbot_id = generate_chatbot_id()
|
89 |
new_chatbot = Chatbot(chatbot_id=chatbot_id, name=name, custom_instruction=custom_instruction)
|
90 |
db.add(new_chatbot)
|
91 |
db.commit()
|
92 |
db.refresh(new_chatbot)
|
93 |
return new_chatbot
|
94 |
-
except SQLAlchemyError as e:
|
95 |
-
db.rollback()
|
96 |
-
raise e
|
97 |
-
finally:
|
98 |
-
db.close()
|
99 |
|
100 |
def get_chatbot(chatbot_id):
|
101 |
-
|
102 |
-
try:
|
103 |
return db.query(Chatbot).filter(Chatbot.chatbot_id == chatbot_id, Chatbot.is_active == True).first()
|
104 |
-
except SQLAlchemyError as e:
|
105 |
-
raise e
|
106 |
-
finally:
|
107 |
-
db.close()
|
108 |
|
|
|
|
|
109 |
def update_chatbot(chatbot_id, name=None, custom_instruction=None, is_active=None):
|
110 |
db = SessionLocal()
|
111 |
try:
|
|
|
3 |
from sqlalchemy import create_engine, Column, Integer, String, Text, Boolean, DateTime, func
|
4 |
from sqlalchemy.ext.declarative import declarative_base
|
5 |
from sqlalchemy.orm import sessionmaker
|
6 |
+
from contextlib import contextmanager
|
7 |
from utils.utils import get_secret
|
8 |
import random
|
9 |
from better_profanity import profanity
|
|
|
83 |
|
84 |
return f"{random.choice(adjectives)}-{random.choice(nouns)}-{random.randint(100, 999)}"
|
85 |
|
86 |
+
@contextmanager
|
87 |
+
def get_db_session():
|
88 |
db = SessionLocal()
|
89 |
try:
|
90 |
+
yield db
|
91 |
+
except SQLAlchemyError as e:
|
92 |
+
db.rollback()
|
93 |
+
raise e
|
94 |
+
finally:
|
95 |
+
db.close()
|
96 |
+
|
97 |
+
def create_chatbot(name, custom_instruction):
|
98 |
+
with get_db_session() as db:
|
99 |
chatbot_id = generate_chatbot_id()
|
100 |
new_chatbot = Chatbot(chatbot_id=chatbot_id, name=name, custom_instruction=custom_instruction)
|
101 |
db.add(new_chatbot)
|
102 |
db.commit()
|
103 |
db.refresh(new_chatbot)
|
104 |
return new_chatbot
|
|
|
|
|
|
|
|
|
|
|
105 |
|
106 |
def get_chatbot(chatbot_id):
|
107 |
+
with get_db_session() as db:
|
|
|
108 |
return db.query(Chatbot).filter(Chatbot.chatbot_id == chatbot_id, Chatbot.is_active == True).first()
|
|
|
|
|
|
|
|
|
109 |
|
110 |
+
|
111 |
+
### These functions need to be rewritten to use the get_db_session context manager
|
112 |
def update_chatbot(chatbot_id, name=None, custom_instruction=None, is_active=None):
|
113 |
db = SessionLocal()
|
114 |
try:
|