-
Notifications
You must be signed in to change notification settings - Fork 3
/
models.py
196 lines (157 loc) · 6.5 KB
/
models.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#models.py
import os
import datetime
import random
import json
from extensions import db
from flask_login import UserMixin
from werkzeug.security import generate_password_hash, check_password_hash
from itsdangerous.url_safe import URLSafeSerializer as Serializer
from flask import current_app
def add_monthly_credits():
users = User.query.all()
for user in users:
user.credits += 10
db.session.commit()
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(64), index=True, unique=True)
email = db.Column(db.String(120), unique=True, nullable=False)
password_hash = db.Column(db.String(256))
agents_data = db.Column(db.JSON)
images_data = db.Column(db.JSON, default={})
thumbnail_images_data = db.Column(db.JSON, default={})
credits = db.Column(db.Integer, default=100)
meetings = db.relationship('Meeting', backref='creator', lazy=True)
api_keys = db.relationship('APIKey', backref='owner', lazy='dynamic')
timeframes = db.relationship('Timeframe', backref='user', lazy=True)
agent_type = db.Column(db.String(20), default='user')
@property
def folder_path(self):
user_folder = f"user_{self.id}"
folder_path = os.path.join(current_app.root_path, 'user_data', user_folder)
os.makedirs(folder_path, exist_ok=True)
return folder_path
def generate_api_key(self, expiration=None):
s = Serializer(current_app.config['SECRET_KEY'])
random_data = random.randint(1000, 9999) # Random number for uniqueness
timestamp = datetime.datetime.utcnow().isoformat() # Current timestamp
payload = {'user_id': self.id, 'timestamp': timestamp, 'rnd': random_data}
token = s.dumps(payload)
new_key = APIKey(key=token, owner=self)
try:
db.session.add(new_key)
db.session.commit()
return token
except Exception as e:
db.session.rollback(
) # Rollback if there's an issue, such as a non-unique key
return None
def set_password(self, password):
self.password_hash = generate_password_hash(password)
def check_password(self, password):
return check_password_hash(self.password_hash, password)
def create_user_data(self):
self.agents_data = []
self.images_data = {}
# Create user folder
os.makedirs(self.folder_path, exist_ok=True)
def update_agents_data(self, agents_data):
self.agents_data = agents_data
db.session.commit()
class APIKey(db.Model):
id = db.Column(db.Integer, primary_key=True)
key = db.Column(db.String(256), unique=True, nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
class Agent(db.Model):
id = db.Column(db.String, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
data = db.Column(db.JSON)
user = db.relationship('User', backref=db.backref('agents', lazy=True))
agent_type = db.Column(db.String(20), default='agent')
voice = db.Column(db.String(20), default='echo')
@property
def persona(self):
return self.data.get('persona', '')
@property
def summary(self):
return self.data.get('summary', '')
@property
def keywords(self):
return self.data.get('keywords', [])
@property
def image_prompt(self):
return self.data.get('image_prompt', '')
@property
def relationships(self):
return self.data.get('relationships', [])
class Image(db.Model):
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
filename = db.Column(db.String(255), nullable=False)
data = db.Column(db.LargeBinary)
user = db.relationship('User', backref=db.backref('images', lazy=True))
class MainAgent(db.Model):
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
data = db.Column(db.Text, nullable=False)
image_data = db.Column(db.Text)
user = db.relationship('User', backref=db.backref('main_agents', lazy=True))
class Survey(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
survey_data = db.Column(db.JSON)
is_public = db.Column(db.Boolean, default=False)
public_url = db.Column(db.String(255), unique=True)
user = db.relationship('User', backref=db.backref('surveys', lazy=True))
class Timeframe(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
agents_data = db.Column(db.Text, nullable=False)
images_data = db.Column(db.Text, nullable=False)
thumbnail_images_data = db.Column(db.Text, nullable=False)
summary = db.Column(db.Text)
summary_image_data = db.Column(db.Text)
summary_thumbnail_image_data = db.Column(db.Text)
@property
def agents_count(self):
if self.agents_data:
agents = json.loads(self.agents_data)
return len(agents)
return 0
class Meeting(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
agents = db.Column(db.JSON)
questions = db.Column(db.JSON)
answers = db.Column(db.JSON)
is_public = db.Column(db.Boolean, default=False)
public_url = db.Column(db.String(255), unique=True, nullable=True)
original_name = db.Column(db.String(100))
summary = db.Column(db.Text)
image_data = db.Column(db.Text)
thumbnail_image_data = db.Column(db.Text)
class Conversation(db.Model):
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
name = db.Column(db.String(100), nullable=False)
agents = db.Column(db.JSON)
messages = db.Column(db.JSON)
timestamp = db.Column(db.DateTime, default=datetime.datetime.utcnow)
url = db.Column(db.String(255), unique=True, nullable=True) # Added URL field
user = db.relationship('User', backref=db.backref('conversations', lazy=True))
def __init__(self, user_id, name, agents, messages, url=None):
self.user_id = user_id
self.name = name
self.agents = agents
self.messages = messages
self.url = url
class Document(db.Model):
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
filename = db.Column(db.String(255), nullable=False)
data = db.Column(db.JSON)
user = db.relationship('User', backref=db.backref('documents', lazy=True))