flask app
This commit is contained in:
parent
4592672dce
commit
fce0dde1fc
@ -66,4 +66,17 @@ CREATE INDEX "idx_rules_rules" ON "rules_rules" ("rules_2");
|
||||
|
||||
ALTER TABLE "rules_rules" ADD CONSTRAINT "fk_rules_rules__rules" FOREIGN KEY ("rules") REFERENCES "rules" ("id");
|
||||
|
||||
ALTER TABLE "rules_rules" ADD CONSTRAINT "fk_rules_rules__rules_2" FOREIGN KEY ("rules_2") REFERENCES "rules" ("id")
|
||||
ALTER TABLE "rules_rules" ADD CONSTRAINT "fk_rules_rules__rules_2" FOREIGN KEY ("rules_2") REFERENCES "rules" ("id")
|
||||
|
||||
|
||||
1. Александр Козырев 23.11.1958
|
||||
2. Ян Лаврентьев 16.10.1971
|
||||
3. Чертков Виталий 04.08.1978
|
||||
4. Пащенко Антон 26.04.1987
|
||||
5. Антон Ларин 13.05.2000
|
||||
|
||||
|
||||
|
||||
2.
|
||||
3. Александр Козырев 23.11.1958
|
||||
4.Пащенко Антон: 26.04.1987 год рождения.
|
||||
|
83
code/python/flask/app.py
Normal file
83
code/python/flask/app.py
Normal file
@ -0,0 +1,83 @@
|
||||
import database.database as database
|
||||
from flask import Flask
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
@app.route('/')
|
||||
def hello_world():
|
||||
return 'Hello, World!'
|
||||
|
||||
|
||||
#todo:
|
||||
@app.route('/auth/login/<login>/<passwd>', methods=['POST'])
|
||||
def login(login, passwd):
|
||||
print(f"{login}")
|
||||
return f'Data {login} {passwd}'
|
||||
|
||||
|
||||
# ***************** CRUD OPERATION [ROLE]*************************************
|
||||
# Method Endpoint Описание Операция CRUD
|
||||
#------------------------------------------------------------------------------
|
||||
# GET /api/v0.1/role/<id> Одну роль Read
|
||||
# GET /api/v0.1/role/list Список ролей Read
|
||||
# POST /api/v0.1/role/<name> Добавили роль Create
|
||||
# PUT /api/v0.1/role/<id>/<name> Изменение роли Update
|
||||
# DELETE /api/v0.1/role/<id> Удаление роли Delete
|
||||
|
||||
# todo: Получить роль по ID
|
||||
@app.route('/api/v0.1/role/<id>', methods=['GET'])
|
||||
def get_route(id):
|
||||
return f'ID:{id}'
|
||||
|
||||
|
||||
# todo: Получить список ролей из БД
|
||||
@app.route('/api/v0.1/role/list', methods=['GET'])
|
||||
def get_list():
|
||||
return "list: {roles}"
|
||||
|
||||
|
||||
@app.route('/api/v0.1/role/<name>', methods=['POST'])
|
||||
def add_role(name):
|
||||
"""
|
||||
Функция добавляет роль
|
||||
- args
|
||||
- name
|
||||
"""
|
||||
try:
|
||||
id = database.add_role(name)
|
||||
return f"Запись добавлена! ID={id}"
|
||||
except Exception as e:
|
||||
print(e)
|
||||
|
||||
|
||||
|
||||
# todo: Дописать изменение роли
|
||||
@app.route('/api/v0.1/role/<id>/<name>', methods=['PUT'])
|
||||
def edit_role(id, name):
|
||||
"""
|
||||
Функция изменение роли
|
||||
- args
|
||||
- name
|
||||
"""
|
||||
|
||||
return f"id: {id}, name: {name}"
|
||||
|
||||
|
||||
|
||||
@app.route('/api/v0.1/role/<id>', methods=['DELETE'])
|
||||
def delete_role(id):
|
||||
"""
|
||||
Функция удаления роли
|
||||
- args
|
||||
- name
|
||||
"""
|
||||
database.delete_role(id)
|
||||
return f"id: {id}"
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(debug=True)
|
||||
|
||||
|
||||
|
||||
|
96
code/python/flask/database/database.py
Normal file
96
code/python/flask/database/database.py
Normal file
@ -0,0 +1,96 @@
|
||||
import psycopg2
|
||||
|
||||
try:
|
||||
conn = psycopg2.connect("dbname='auth' user='auth' port='5432' host='localhost' password='123'")
|
||||
except:
|
||||
print("I am unable to connect to the database")
|
||||
|
||||
def add_account(login, passwd):
|
||||
|
||||
SQL_INSERT_ACCOUNT = f"""INSERT INTO account
|
||||
(login, pswd, is_block, dt_create, "token", "refresh", dt_)
|
||||
VALUES('{login}', '{passwd}', false, now(), '', '', now()); """
|
||||
|
||||
with conn.cursor() as curs:
|
||||
try:
|
||||
# simple single row system query
|
||||
# conn.start()
|
||||
# print(dir(conn))
|
||||
curs.execute(SQL_INSERT_ACCOUNT)
|
||||
conn.commit()
|
||||
# returns a single row as a tuple
|
||||
#single_row = curs.fetchone()
|
||||
|
||||
# use an f-string to print the single tuple returned
|
||||
#print(f"{single_row}")
|
||||
|
||||
# simple multi row system query
|
||||
# curs.execute("SELECT query, backend_type FROM pg_stat_activity")
|
||||
|
||||
# a default install should include this query and some backend workers
|
||||
# use the * unpack operator to print many_rows which is a Python list
|
||||
|
||||
# a more robust way of handling errors
|
||||
except (Exception, psycopg2.DatabaseError) as error:
|
||||
print(error)
|
||||
|
||||
finally:
|
||||
curs.close()
|
||||
conn.close()
|
||||
|
||||
|
||||
|
||||
def get_list_account(_login):
|
||||
SQL_SELECT_ACCOUNT = f"""SELECT login, pswd
|
||||
FROM account
|
||||
WHERE login like '{_login}'"""
|
||||
|
||||
with conn.cursor() as curs:
|
||||
try:
|
||||
|
||||
curs.execute(SQL_SELECT_ACCOUNT)
|
||||
# returns a single row as a tuple
|
||||
single_row = curs.fetchone()
|
||||
print(f"{single_row}")
|
||||
return single_row
|
||||
|
||||
except (Exception, psycopg2.DatabaseError) as error:
|
||||
print(error)
|
||||
|
||||
finally:
|
||||
curs.close()
|
||||
conn.close()
|
||||
|
||||
|
||||
|
||||
def delete_role(id):
|
||||
id = int(id)
|
||||
SQL_DELETE_ROLE = f"DELETE FROM role where ID = {id}"
|
||||
with conn.cursor() as curs:
|
||||
try:
|
||||
curs.execute(SQL_DELETE_ROLE)
|
||||
conn.commit()
|
||||
except (Exception, psycopg2.DatabaseError) as error:
|
||||
print(error)
|
||||
finally:
|
||||
curs.close()
|
||||
|
||||
|
||||
|
||||
def add_role(name):
|
||||
SQL_INSERT_ROLE = f"INSERT INTO role (name) VALUES('{name}') RETURNING id"
|
||||
|
||||
with conn.cursor() as curs:
|
||||
try:
|
||||
curs.execute(SQL_INSERT_ROLE)
|
||||
last_id = curs.fetchone()[0]
|
||||
print(f"last_id:{last_id}")
|
||||
conn.commit()
|
||||
except (Exception, psycopg2.DatabaseError) as error:
|
||||
print(error)
|
||||
finally:
|
||||
curs.close()
|
||||
# conn.close()
|
||||
return last_id
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user