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
8 changes: 8 additions & 0 deletions Shops/sport_atribute_shop/flask_app/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from flask import Flask
from storage import Storage

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


secret_key = ''
6 changes: 6 additions & 0 deletions Shops/sport_atribute_shop/flask_app/run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from views import app



if __name__ == "__main__":
app.run(debug=True, host='0.0.0.0', port=5000)
37 changes: 37 additions & 0 deletions Shops/sport_atribute_shop/flask_app/service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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):
Copy link
Owner

Choose a reason for hiding this comment

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

получить имэйл из запроса, получить имейл из storage, сравнить и занести в storage, если не совпадают

result = {
"error": True,
"message": "User already exists."
}
account['password'] = hash_md5(account.get('password'))
email = account.get("email")
storage_account = storage.get_by_email(email)
if storage_account is None:
storage.add(account)
result = {
'error': False,
'message': "OK!"
}
return result


def login(account):
email = account.get("email")
data = storage.get_by_email(email)
password = hash_md5(account.get("password"))
saved_password = data.get("password")
if password == saved_password:
return make_jwt(data)


def make_jwt(data):
return jwt.encode(data, secret_key, algorithm='HS256')
1 change: 1 addition & 0 deletions Shops/sport_atribute_shop/flask_app/storage.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
27 changes: 27 additions & 0 deletions Shops/sport_atribute_shop/flask_app/storage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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()
result = list(filter(lambda acc: acc.get("email") == email, data))
if not result:
result = None
else:
result = result[0]
return result

def add(self, data):
storage_data = self.get()
storage_data.append(data)
with open(self.path, "w") as file:
json.dump(storage_data, file)
29 changes: 29 additions & 0 deletions Shops/sport_atribute_shop/flask_app/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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()
code = 200
result = registrate(data)
if result.get('error'):
code = 422
return make_response(jsonify({"message": result.get("message")})), code

22 changes: 0 additions & 22 deletions Shops/sport_atribute_shop/home_work/flask_hw/[#H17]Flask_HW.py

This file was deleted.

66 changes: 0 additions & 66 deletions Shops/sport_atribute_shop/home_work/flask_hw/[#H17]Flask_HW2.py

This file was deleted.

Loading