25 Commits

Author SHA1 Message Date
Kirill 12be29340b feat(api/ws.py): WebSoket 2025-10-31 13:47:55 +03:00
Kirill 9d01ead120 feat(dependencies.txt): Update libs
Added Libs for work with webSockets
2025-10-31 13:46:23 +03:00
Kirill 5bac4c720b feat(api/robots.py): Robots Api 2025-10-31 13:45:30 +03:00
Sweetbread cdf195306e feat(jwt): add iat field 2025-10-26 17:27:24 +03:00
Sweetbread 90f1a6245c feat(log): use loguru 2025-10-26 16:23:37 +03:00
Sweetbread 532266c98e refactor(auth): remove /api/auth/* handlers to api/auth.py 2025-10-26 16:23:37 +03:00
Sweetbread f99c0e8148 refactor(user): rename to toJson back 2025-10-26 16:23:37 +03:00
Sweetbread 2a7c74286c feat(auth): add data validation 2025-10-26 16:23:37 +03:00
Sweetbread 920c51b4f0 refactor(user): change class name to uppercase 2025-10-26 16:23:37 +03:00
Sweetbread 2b7fecb248 fix(env): ensure the .env is loaded 2025-10-26 14:55:43 +03:00
Kirill ce48859079 fix(api/auth/loginapi.py, app.py, utils/PostgressConnect.py, utils/createLogger.py, utils/loadDotEnv.py): Review fix
https://g.codrs.ru/Hackaton/Backend/pulls/2#issuecomment-23
https://g.codrs.ru/Hackaton/Backend/pulls/2#issuecomment-31
2025-10-26 14:10:10 +03:00
Kirill f93b94531c fix(utils/token.py): Code review fix
https://g.codrs.ru/Hackaton/Backend/pulls/2#issuecomment-24
2025-10-25 21:01:45 +03:00
Kirill 22da586aec fix(app.py): Code review fix
https://g.codrs.ru/Hackaton/Backend/pulls/2#issuecomment-30
2025-10-25 20:59:41 +03:00
Kirill 5058df8b7d fix(model/user.py): Code review fix
https://g.codrs.ru/Hackaton/Backend/pulls/2#issuecomment-20
https://g.codrs.ru/Hackaton/Backend/pulls/2#issuecomment-25
https://g.codrs.ru/Hackaton/Backend/pulls/2#issuecomment-21
2025-10-25 20:57:23 +03:00
Kirill 3a118c51b5 fix(api/auth/loginapi.py): Pull request review
https://g.codrs.ru/Hackaton/Backend/pulls/2#issuecomment-26
https://g.codrs.ru/Hackaton/Backend/pulls/2#issuecomment-20
https://g.codrs.ru/Hackaton/Backend/pulls/2#issuecomment-35
2025-10-25 20:53:48 +03:00
Kirill 7ccc9385e6 feat(app.py): Include Blueprint to app 2025-10-25 11:26:46 +03:00
Kirill 1bf9c69c2b feat(api/loginapi.py): Login Api
Blueprint with api login page with is get respons (form-data) and send JVT token and user id name and role
2025-10-25 11:24:31 +03:00
Kirill 2098719c82 feat(model/user.py): User model
User Dataclass with is get all user respons data
2025-10-25 11:23:01 +03:00
Kirill 0e668e1993 feat(utils/token.py): Create JVT token
Added function with is creating jvt tocken for user
2025-10-25 11:21:32 +03:00
Kirill da05ae96d2 fix(controllers/controller.py; model/example.py; utils/InitalizeDB.py): deleted exaple files 2025-10-25 11:19:20 +03:00
Kirill 061b7a86a3 fix(dependencies.txt): JVT tocken libs 2025-10-25 11:18:06 +03:00
Kirill eabc193d8b feat(utils/InitalizeDB.py): PSQL initializing
Add script with is connected to db and initialize it
2025-10-23 23:01:22 +03:00
Kirill 35f18853fc feat(utils/PostgressConnect.py): Postgress connect Functional
Added Functions with connect to postgress db by link from enviroment
2025-10-23 23:00:00 +03:00
Kirill a062d24b24 feat(utils/loadDotEnv.py): Enviroment
Function for download enviroment variables from .env to Enviroment
2025-10-23 22:57:07 +03:00
Kirill a87656274b feat(dependencies.txt): Lib fix
Added libs for working with PSQL and dot-env
2025-10-23 22:55:05 +03:00
12 changed files with 148 additions and 0 deletions
+2
View File
@@ -0,0 +1,2 @@
KEY= # Key for JWT token
POSTGRES_URL=postgresql://
+28
View File
@@ -0,0 +1,28 @@
from flask import Blueprint, request, jsonify
from model.user import User
auth = Blueprint("auth", __name__)
@auth.route('/login', methods = ['POST'])
def login():
if request.is_json:
req = request.json
email = req.get('email')
password = req.get('password')
if not email or not password:
return "Request must have email and password", 400
if len(email.strip()) < 4 or '@' not in email or '.' not in email:
return "Email is incorrect", 400
if len(password.strip()) < 8:
return "Password is too short", 400
user = User(email, password)
return jsonify(user.toJson())
else:
return "Request is not a json", 400
+24
View File
@@ -0,0 +1,24 @@
from flask import Blueprint, request, jsonify
from model.user import User
#from module.db.repositories.robot_reposytory import update_robot get_by_id
from loguru import logger as log
robots = Blueprint("robots", __name__)
robots.route('/data', methods = ['POST'])
def data():
if request.headers.get("Authorization"):
if request.is_json:
req = request.json
id = req.get('robot_id')
tms = req.get('timestamp')
loc = req.get('location')
scanRes = req.get('scan_results')
battery = req.get('battery_level')
if update_robot(id, "received", battery, loc["zone"], loc["row"], loc["self"]):
res = {"status": "received", "message_id": "123"} #for that moment i don`t now what is message
return jsonify(res)
else:
log.error('failed to update robot data')
return("Server error")
+26
View File
@@ -0,0 +1,26 @@
from app import app
from flask_socketio import SocketIO, emit
from loguru import logger as log
#from module.db.repositories.robot_reposytory import update_robot get_by_id get_all
#from module.db.repositories.inventory_repository import get_all as getRecords
ws = socketIO(app)
@ws.on('connect')
def is_connect():
log.info('client is connected')
@ws.on('disconnect')
def is_disconnect():
log.info('client is disconnected')
@ws.on('robot_update')
def robotUpdate():
robots = get_all()
emit('response', jsonify({"type": "robot_update", "data": {robots.toJSON()}})
@ws.on('inventory_alert')
def robotUpdate():
records = getRecords()
emit('response', jsonify({"type": "inventory_alert", "data": {records.toJSON()}})
+11
View File
@@ -1,4 +1,15 @@
from sys import exit
from flask import Flask
from api.auth import auth
from utils.loadDotEnv import initializeENV
from utils.PostgressConnect import PSQLConnect, PSQLCursor
if not initializeENV():
exit(-1)
#conn = PSQLConnect()
#cur = PSQLCursor(conn)
app = Flask(__name__)
app.register_blueprint(auth, url_prefix='/api/auth')
View File
+5
View File
@@ -1 +1,6 @@
flask==3.1.2
flask-socketio
python-dotenv
psycopg2-binary
pyjwt
loguru
View File
+20
View File
@@ -0,0 +1,20 @@
from dataclasses import dataclass
import json
from utils.token import generateKey
@dataclass
class User:
id: int
name: str
role: str
token: str
def __init__(self, email: str, passwd: str):
#us = getUsModel() #возвращает словарь
self.id = 1#us['id']
self.name = 'Bob'#us['name']
self.role = 'Backend'#us['role']
self.token = generateKey(email, passwd)
def toJson(self):
return {"user": {"id": self.id, "name": self.name, "role": self.role}, "token": self.token}
+10
View File
@@ -0,0 +1,10 @@
import psycopg2
import os
def PSQLConnect():
conn = psycopg2.connect(os.getenv('POSTGRES_URL'))
return conn
def PSQLCursor(conn):
cur = conn.cursor()
return cur
+14
View File
@@ -0,0 +1,14 @@
import os
from dotenv import load_dotenv
from loguru import logger as log
DOTENV_PATH = '.env'
def initializeENV() -> bool:
if os.path.exists(DOTENV_PATH):
load_dotenv(DOTENV_PATH)
log.info('.env is loaded')
return True
else:
log.error('.env isn`t loaded')
return False
+8
View File
@@ -0,0 +1,8 @@
import jwt
import os
from time import time
def generateKey(email, passwd):
key = os.getenv('KEY')
encoded = jwt.encode({email: passwd, 'iat': time()}, key, algorithm="HS256")
return encoded