Skip to content

Commit

Permalink
Merge pull request #9 from bobiboMC/fastapi_api_revised
Browse files Browse the repository at this point in the history
Fastapi Api Routes
  • Loading branch information
moshe742 authored Sep 4, 2023
2 parents 2286402 + 263e943 commit 7510b50
Show file tree
Hide file tree
Showing 4 changed files with 3,763 additions and 177 deletions.
144 changes: 106 additions & 38 deletions api/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ def get_settings():
return config.Settings()


def get_db_connection(settings: Annotated[config.Settings, Depends(get_settings)]):
def get_db_connection(
settings: Annotated[config.Settings, Depends(get_settings)]
):
return psycopg2.connect(
host=settings.oknesset_db_host,
port=settings.oknesset_db_port,
Expand All @@ -39,71 +41,139 @@ def get_db_cursor():
finally:
cur.close()
conn.close()


def get_data(sql):

# get table names from database
def get_data_tables(limit, offset):
sql = (
f"SELECT table_name FROM information_schema.tables "
f"WHERE table_schema NOT IN ('pg_catalog', 'information_schema') "
f"LIMIT {limit} OFFSET {offset}"
)
print(sql)
with get_db_cursor() as cur:
cur.execute(f"{sql} LIMIT 10;")
cur.execute(sql)
data = cur.fetchall()
return data


def get_committee(table, field, value):
sql = f'select * from {table} where "{field}"={value}'
# get all columns from some table
def get_data_columns(table_name):
sql = (
f"SELECT column_name FROM information_schema.columns"
f"WHERE table_name = '{table_name}'"
)
with get_db_cursor() as cur:
cur.execute(sql)
data = cur.fetchall()
return data

#get all known info about current minister or kns member
def get_fully_today_member(query,value):

# get single data from table
def get_single_data(table, field, value):
sql = f'select * from {table} where "{field}"={value}'
with get_db_cursor() as cur:
cur.execute(sql)
data = cur.fetchone()
return data


# get all known info about current minister or kns member
def get_fully_today_member(query, value):
try:
with get_db_cursor() as cur:
cur.execute(query,value)
cur.execute(query, value)
data = cur.fetchone()
column_names = [desc[0] for desc in cur.description]
result = {column_names[i]: data[i] for i in range(len(column_names))}
except Exception as e:
except Exception:
return ValueError('No such member exist!')
return result
return data


# get list data
def get_data_list(start_query, limit, offset, order_by, qs):
result = create_query_list(start_query, limit, offset, order_by, qs)
if isinstance(result, Exception):
return result
query=result[0]
values=result[1]
query = result[0]
values = result[1]
try:
with get_db_cursor() as cur:
cur.execute(query,tuple(values))
cur.execute(query, tuple(values))
return cur.fetchall()
except Exception as e:
return e


# create query that returns data list
def create_query_list(start_query, limit: int = 0, offset: int = 0, order_by=None, qs: str | None = None):
def create_query_list(
start_query, limit: int = 0, offset: int = 0,
order_by=None, qs: str | None = None
):
# options: limit, offset, order_by
where_optional_args = []
# options: limit, offset, order_by
other_optional_args = []
# values to put in placeholders in query
values = []

used_clause = ["limit", "offset", "order_by"]

crc32c = False
# add arguments to where clause
if qs is not None:
if qs:
qs = qs.split('&')
for item in qs:
# dealing with crc32c format
if item[-2:] == '==':
item = item[:-2]
crc32c = True
key, val = item.split('=')
where_optional_args.append('"{0}" = %s'.format(key))
values.append(val)
# for key, val in request.args.items():
# if key not in used_clause:
# where_optional_args.append('"{0}" = %s'.format(key))
# values.append(val)
# checking if it's crc32c format
if crc32c:
val += '=='
crc32c = False
# checks if it's simple array of strings or integers
if '[' in val:
val_splited = val[1:-1].split(',')
print(val_splited)
# checks if it's integers array
try:
val = [int(element) for element in val_splited]
print('here3')
val = str(val)
# if here, it's strings array
except Exception:
val = str(val).replace("'", '"')

val = val.replace('\\\\', '\\')
print(val)
values.append(val)
where_optional_args.append('"{0}" @> %s'.format(key))
# checks if it's array of objects
elif '<' in val:
key_splited = key.split('_')
object_key = key_splited[0]
# checks if field of object has two words combine
if len(key_splited) == 3:
object_field = '{0}_{1}'.format(
key_splited[1],
key_splited[2]
)
# if here, field of object is one word
else:
object_field = key_splited[1]

# checks if field of object is integer
if not (val[1:-1].isdigit()):
val = '[{{"{0}": "{1}"}}]'.format(object_field, val[1:-1])
# if here, field of object is string
else:
val = '[{{"{0}": {1}}}]'.format(object_field, val[1:-1])
values.append(val)
where_optional_args.append(
'"{0}" @> %s'.format(object_key)
)
# if here, it's simple data type integer, string or bool
else:
where_optional_args.append('"{0}" = %s'.format(key))
values.append(val)

# No arguments for where clause
if not where_optional_args:
Expand All @@ -112,9 +182,10 @@ def create_query_list(start_query, limit: int = 0, offset: int = 0, order_by=Non
# add arguments to order by clause
if order_by is not None:
order_by_clause = ''
pattern = r'^(\w+\s+(?i)(?:asc|desc))(?:,\s*\w+\s+(?i)(?:asc|desc))*\s*$'
if not re.match(pattern, order_by):
return ValueError('Must be this format: column1 asc/desc,column2 asc/desc..')
pattern = r'^(\w+\s+(?:asc|desc))(?:,\s*\w+\s+(?:asc|desc))*\s*$'
if not re.match(pattern, order_by, re.IGNORECASE):
return ValueError('Must be this format:'
'column1 asc/desc,column2 asc/desc..')

for elemt in order_by.split(','):
parts = elemt.split(' ')
Expand All @@ -125,16 +196,10 @@ def create_query_list(start_query, limit: int = 0, offset: int = 0, order_by=Non
order_by_clause = order_by_clause[:-1]
other_optional_args.append(f" ORDER BY {order_by_clause}")
# add arguments to limit clause
# if limit is not None:
# if not limit.isdigit() and not limit[1:].isdigit():
# return ValueError('Limit Must be an Integer!')
if limit > 0:
other_optional_args.append(" LIMIT %s")
values.append(int(limit))
# add arguments to offset clause
# if offset is not None:
# if not offset.isdigit() and not offset[1:].isdigit():
# return ValueError('Offset Must be an Integer!')
if offset > 0:
other_optional_args.append(" OFFSET %s")
values.append(int(offset))
Expand All @@ -144,12 +209,15 @@ def create_query_list(start_query, limit: int = 0, offset: int = 0, order_by=Non
+ " AND ".join(where_optional_args)
+ "".join(other_optional_args)
)
return [query,values]
return [query, values]


def get_discribe(table):
with get_db_cursor() as cur:
sql = f"SELECT * FROM information_schema.columns WHERE table_name = '{table}'"
sql = (
f"SELECT * FROM information_schema.columns"
f"WHERE table_name = '{table}'"
)
cur.execute(sql)
data = cur.fetchall()
return data
Loading

0 comments on commit 7510b50

Please sign in to comment.