Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM python:3.10
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
CMD ["python3", "run.py"]
EXPOSE 5000
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from flask import Flask
from storage import Storage

storage = Storage("storage.json")
app = Flask(__name__)

secret_key = 'qwerty'
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from views import app

if __name__ == "__main__":
app.run(debug=True, host='0.0.0.0', port=5000)
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import hashlib
from config import storage, secret_key
import jwt


def hash_md5(data):
return hashlib.md5(data.encode('utf-8')).hexdigest()


def registrate(account):
account['password'] = hash_md5(account.get('password'))
email = account.get("email")
db = storage.get_by_email(email)
saved_email = db.get("email")
if email != saved_email:
return storage.add(account)


def login(account):
email = account.get("email")
data = storage.get_by_email(email)
password = hash_md5(account.get("password"))
saved_password = data.get("password")
saved_email = data.get("email")
if password == saved_password and email == saved_email:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

в если условие не срабатывает то возвращаем None? Не надо так, лучше всегда что-то возвращать, например сообщение какое-то или типа того

return make_jwt(data)


def make_jwt(data):
return jwt.encode(data, secret_key, algorithm='HS256')
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import json


class Storage:

def __init__(self, path):
self.path = path

def get(self, *args, **kwargs):
with open(self.path, "r") as file:
data = json.load(file)
return data

def get_by_email(self, email):
data = self.get()
return list(filter(lambda acc: acc.get("email") == email, data))[0]

def add(self, data):
storage_data = self.get()
storage_data.append(data)
with open(self.path, "w") as file:
json.dump(storage_data, file)
33 changes: 33 additions & 0 deletions Shops/building_materials_store/app/Homework/flask_hw2/app/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from config import app
from flask import request, jsonify, make_response
from service import registrate, login as log


@app.route('/login', methods=["POST"])
def login():
data = request.get_json()
try:
token = log(data)
result = ({"token": token,
"status": "ok"}, 200)
except Exception as e:
result = ({
"status": "error",
"detail": "Passwords or email didn't match.",
}, 400)
return make_response(jsonify(result))


@app.route('/reg', methods=["POST"])
def reg():
data = request.get_json()
try:
registrate(data)
result = ({
"status": "ok"}, 200)
except Exception as e:
result = ({
"status": "error",
"detail": "User already exists. "
}, 400)
return make_response(jsonify(result))