Add community section where public functions can be viewed
This commit is contained in:
68
db.py
68
db.py
@@ -61,37 +61,55 @@ class DataBase():
|
||||
if search_query:
|
||||
search_pattern = f"%{search_query}%"
|
||||
http_functions = self.execute(
|
||||
'SELECT id, user_id, NAME, path, script_content, invoked_count, environment_info, is_public, log_request, log_response, version_number, runtime FROM http_functions WHERE user_id=%s AND (NAME ILIKE %s OR path ILIKE %s) ORDER by id DESC',
|
||||
'SELECT id, user_id, NAME, path, script_content, invoked_count, environment_info, is_public, log_request, log_response, version_number, runtime, description FROM http_functions WHERE user_id=%s AND (NAME ILIKE %s OR path ILIKE %s) ORDER by id DESC',
|
||||
[user_id, search_pattern, search_pattern]
|
||||
)
|
||||
else:
|
||||
http_functions = self.execute(
|
||||
'SELECT id, user_id, NAME, path, script_content, invoked_count, environment_info, is_public, log_request, log_response, version_number, runtime FROM http_functions WHERE user_id=%s ORDER by id DESC',
|
||||
'SELECT id, user_id, NAME, path, script_content, invoked_count, environment_info, is_public, log_request, log_response, version_number, runtime, description FROM http_functions WHERE user_id=%s ORDER by id DESC',
|
||||
[user_id]
|
||||
)
|
||||
return http_functions
|
||||
|
||||
def get_public_http_functions(self, search_query=None):
|
||||
if search_query:
|
||||
search_pattern = f"%{search_query}%"
|
||||
http_functions = self.execute(
|
||||
'SELECT h.id, h.user_id, h.NAME, h.path, h.script_content, h.invoked_count, h.environment_info, h.is_public, h.log_request, h.log_response, h.version_number, h.runtime, h.description, h.created_at, u.username FROM http_functions h JOIN users u ON h.user_id = u.id WHERE h.is_public=TRUE AND (h.NAME ILIKE %s OR h.description ILIKE %s) ORDER by h.created_at DESC',
|
||||
[search_pattern, search_pattern]
|
||||
)
|
||||
else:
|
||||
http_functions = self.execute(
|
||||
'SELECT h.id, h.user_id, h.NAME, h.path, h.script_content, h.invoked_count, h.environment_info, h.is_public, h.log_request, h.log_response, h.version_number, h.runtime, h.description, h.created_at, u.username FROM http_functions h JOIN users u ON h.user_id = u.id WHERE h.is_public=TRUE ORDER by h.created_at DESC'
|
||||
)
|
||||
return http_functions
|
||||
|
||||
def get_http_function(self, user_id, name):
|
||||
http_function = self.execute(
|
||||
'SELECT id, user_id, NAME, path, script_content, invoked_count, environment_info, is_public, log_request, log_response, version_number, created_at, runtime FROM http_functions WHERE user_id=%s AND NAME=%s', [user_id, name], one=True)
|
||||
'SELECT id, user_id, NAME, path, script_content, invoked_count, environment_info, is_public, log_request, log_response, version_number, created_at, runtime, description FROM http_functions WHERE user_id=%s AND NAME=%s', [user_id, name], one=True)
|
||||
return http_function
|
||||
|
||||
def get_http_function_by_id(self, user_id, http_function_id):
|
||||
http_function = self.execute(
|
||||
'SELECT id, user_id, NAME, path, script_content, invoked_count, environment_info, is_public, log_request, log_response, version_number, created_at, runtime FROM http_functions WHERE user_id=%s AND id=%s', [user_id, http_function_id], one=True)
|
||||
'SELECT id, user_id, NAME, path, script_content, invoked_count, environment_info, is_public, log_request, log_response, version_number, created_at, runtime, description FROM http_functions WHERE user_id=%s AND id=%s', [user_id, http_function_id], one=True)
|
||||
return http_function
|
||||
|
||||
def get_public_http_function_by_id(self, http_function_id):
|
||||
http_function = self.execute(
|
||||
'SELECT h.id, h.user_id, h.NAME, h.path, h.script_content, h.invoked_count, h.environment_info, h.is_public, h.log_request, h.log_response, h.version_number, h.created_at, h.runtime, h.description, u.username FROM http_functions h JOIN users u ON h.user_id = u.id WHERE h.id=%s AND h.is_public=TRUE', [http_function_id], one=True)
|
||||
return http_function
|
||||
|
||||
def create_new_http_function(self, user_id, name, path, script_content, environment_info, is_public, log_request, log_response, runtime):
|
||||
def create_new_http_function(self, user_id, name, path, script_content, environment_info, is_public, log_request, log_response, runtime, description=""):
|
||||
self.execute(
|
||||
'INSERT INTO http_functions (user_id, NAME, path, script_content, environment_info, is_public, log_request, log_response, runtime) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)',
|
||||
[user_id, name, path, script_content, environment_info, is_public, log_request, log_response, runtime],
|
||||
'INSERT INTO http_functions (user_id, NAME, path, script_content, environment_info, is_public, log_request, log_response, runtime, description) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)',
|
||||
[user_id, name, path, script_content, environment_info, is_public, log_request, log_response, runtime, description],
|
||||
commit=True
|
||||
)
|
||||
|
||||
def edit_http_function(self, user_id, function_id, name, path, script_content, environment_info, is_public, log_request, log_response, runtime):
|
||||
def edit_http_function(self, user_id, function_id, name, path, script_content, environment_info, is_public, log_request, log_response, runtime, description=""):
|
||||
updated_version = self.execute(
|
||||
'UPDATE http_functions SET NAME=%s, path=%s, script_content=%s, environment_info=%s, is_public=%s, log_request=%s, log_response=%s, runtime=%s WHERE user_id=%s AND id=%s RETURNING version_number',
|
||||
[name, path, script_content, environment_info, is_public, log_request, log_response, runtime, user_id, function_id],
|
||||
'UPDATE http_functions SET NAME=%s, path=%s, script_content=%s, environment_info=%s, is_public=%s, log_request=%s, log_response=%s, runtime=%s, description=%s WHERE user_id=%s AND id=%s RETURNING version_number',
|
||||
[name, path, script_content, environment_info, is_public, log_request, log_response, runtime, description, user_id, function_id],
|
||||
commit=True, one=True
|
||||
)
|
||||
return updated_version
|
||||
@@ -115,6 +133,36 @@ FROM http_function_invocations
|
||||
WHERE http_function_id=%s
|
||||
ORDER BY invocation_time DESC""", [http_function_id])
|
||||
return http_function_invocations
|
||||
|
||||
def fork_http_function(self, user_id, function_id):
|
||||
# Get the original function
|
||||
original = self.execute(
|
||||
'SELECT NAME, path, script_content, environment_info, runtime, description FROM http_functions WHERE id=%s',
|
||||
[function_id],
|
||||
one=True
|
||||
)
|
||||
if not original:
|
||||
raise Exception("Function not found")
|
||||
|
||||
new_name = original['name']
|
||||
# Check if name exists for this user
|
||||
exists = self.execute('SELECT 1 FROM http_functions WHERE user_id=%s AND NAME=%s', [user_id, new_name], one=True)
|
||||
if exists:
|
||||
new_name = f"{new_name}-fork"
|
||||
|
||||
self.create_new_http_function(
|
||||
user_id,
|
||||
new_name,
|
||||
original['path'],
|
||||
original['script_content'],
|
||||
original['environment_info'],
|
||||
False, # is_public
|
||||
True, # log_request
|
||||
False, # log_response
|
||||
original['runtime'],
|
||||
original['description']
|
||||
)
|
||||
return new_name
|
||||
|
||||
def get_user(self, user_id):
|
||||
user = self.execute(
|
||||
|
||||
Reference in New Issue
Block a user