fixes of storing users and apps

This commit is contained in:
LeonOstrez
2023-08-09 10:38:39 +02:00
parent e6769944a6
commit 1d5c01a707
6 changed files with 88 additions and 51 deletions

View File

@@ -27,10 +27,11 @@ def save_user(user_id, email, password):
return user
except DoesNotExist:
try:
return User.create(id=user_id, email=email, password=password)
except IntegrityError as e:
existing_user = User.get(User.email == email)
return existing_user
except DoesNotExist:
return User.create(id=user_id, email=email, password=password)
def get_user(user_id=None, email=None):
@@ -53,12 +54,27 @@ def get_user(user_id=None, email=None):
def save_app(args):
try:
app = App.get(App.id == args['app_id'])
for key, value in args.items():
if key != 'app_id' and value is not None:
setattr(app, key, value)
app.save()
except DoesNotExist:
try:
user = get_user(user_id=args['user_id'])
except ValueError:
user = save_user(args['user_id'], args['email'], args['password'])
app = App.create(id=args['app_id'], user=user, app_type=args['app_type'], name=args['name'])
if args.get('user_id') is not None:
try:
user = get_user(user_id=args['user_id'])
except ValueError:
user = save_user(args['user_id'], args['email'], args['password'])
args['user_id'] = user.id
args['email'] = user.email
else:
user = None
app = App.create(
id=args['app_id'],
user=user,
app_type=args.get('app_type'),
name=args.get('name')
)
return app
@@ -140,7 +156,7 @@ def save_development_step(app_id, prompt_path, prompt_data, llm_req_num, message
app = get_app(app_id)
hash_id = hash_data({
'prompt_path': prompt_path,
'prompt_data': {k: v for k, v in prompt_data.items() if k not in {"directory_tree"}},
'prompt_data': {k: v for k, v in (prompt_data.items() if prompt_data is not None else {}) if k not in {"directory_tree"}},
'llm_req_num': llm_req_num
})
try:
@@ -234,15 +250,22 @@ def get_user_input_from_hash_id(project, query):
def get_development_step_from_hash_id(app_id, prompt_path, prompt_data, llm_req_num):
if prompt_data is None:
prompt_data_dict = {}
else:
prompt_data_dict = {k: v for k, v in prompt_data.items() if k not in {"directory_tree"}}
hash_id = hash_data({
'prompt_path': prompt_path,
'prompt_data': {k: v for k, v in prompt_data.items() if k not in {"directory_tree"}},
'prompt_data': prompt_data_dict,
'llm_req_num': llm_req_num
})
try:
dev_step = DevelopmentSteps.get((DevelopmentSteps.hash_id == hash_id) & (DevelopmentSteps.app == app_id))
except DoesNotExist:
return None
return dev_step

View File

@@ -6,6 +6,6 @@ from database.models.user import User
class App(BaseModel):
user = ForeignKeyField(User, backref='apps')
app_type = CharField()
name = CharField()
app_type = CharField(null=True)
name = CharField(null=True)
status = CharField(default='started')

View File

@@ -16,6 +16,7 @@ class ProductOwner(Agent):
super().__init__('product_owner', project)
def get_project_description(self):
save_app(self.project.args)
self.project.current_step = 'project_description'
convo_project_description = AgentConvo(self)
@@ -28,13 +29,13 @@ class ProductOwner(Agent):
# PROJECT DESCRIPTION
self.project.args['app_type'] = ask_for_app_type()
self.project.args['name'] = clean_filename(ask_user('What is the project name?'))
self.project.args['name'] = clean_filename(ask_user(self.project, 'What is the project name?'))
setup_workspace(self.project.root_path, self.project.args['name'])
save_app(self.project.args)
main_prompt = ask_for_main_app_definition(self, project)
main_prompt = ask_for_main_app_definition(self.project)
high_level_messages = get_additional_info_from_openai(
self.project,

View File

@@ -4,7 +4,7 @@ from __future__ import print_function, unicode_literals
from dotenv import load_dotenv
from helpers.Project import Project
from utils.utils import get_arguments
from utils.arguments import get_arguments
from logger.logger import logger
def init():

50
euclid/utils/arguments.py Normal file
View File

@@ -0,0 +1,50 @@
import sys
import uuid
from database.database import get_app
def get_arguments():
# The first element in sys.argv is the name of the script itself.
# Any additional elements are the arguments passed from the command line.
args = sys.argv[1:]
# Create an empty dictionary to store the key-value pairs.
arguments = {}
# Loop through the arguments and parse them as key-value pairs.
for arg in args:
if '=' in arg:
key, value = arg.split('=', 1)
arguments[key] = value
else:
arguments[arg] = True
if 'app_id' in arguments:
try:
app = get_app(arguments['app_id'])
arguments['user_id'] = str(app.user.id)
arguments['app_type'] = app.app_type
arguments['name'] = app.name
# Add any other fields from the App model you wish to include
except ValueError as e:
print('fkldsjlfjsdlkfjkldsjflkdsjfljdsklfjdslk')
print(e)
# Handle the error as needed, possibly exiting the script
else:
arguments['app_id'] = str(uuid.uuid4())
if 'user_id' not in arguments:
arguments['user_id'] = str(uuid.uuid4())
if 'email' not in arguments:
arguments['email'] = str(uuid.uuid4())
if 'password' not in arguments:
arguments['password'] = 'password'
if 'step' not in arguments:
arguments['step'] = None
print(f"If you wish to continue with this project in future run 'python main.py app_id={arguments['app_id']}'")
return arguments

View File

@@ -1,10 +1,8 @@
# utils/utils.py
import sys
import os
import platform
import distro
import uuid
import json
import hashlib
import re
@@ -16,41 +14,6 @@ from const.common import ROLES, STEPS
from logger.logger import logger
def get_arguments():
# The first element in sys.argv is the name of the script itself.
# Any additional elements are the arguments passed from the command line.
args = sys.argv[1:]
# Create an empty dictionary to store the key-value pairs.
arguments = {}
# Loop through the arguments and parse them as key-value pairs.
for arg in args:
if '=' in arg:
key, value = arg.split('=', 1)
arguments[key] = value
else:
arguments[arg] = True
if 'user_id' not in arguments:
arguments['user_id'] = str(uuid.uuid4())
if 'email' not in arguments:
arguments['email'] = 'email'
if 'password' not in arguments:
arguments['password'] = 'password'
if 'app_id' not in arguments:
arguments['app_id'] = str(uuid.uuid4())
if 'step' not in arguments:
arguments['step'] = None
print(f"If you wish to continue with this project in future run 'python main.py app_id={arguments['app_id']}'")
return arguments
def capitalize_first_word_with_underscores(s):
# Split the string into words based on underscores.
words = s.split('_')