Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
10 changes: 10 additions & 0 deletions Shops/sport_atribute_shop/flask_app/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from flask import Flask
from storage import Storage

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


secret_key = 'hello'


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)
25 changes: 25 additions & 0 deletions Shops/sport_atribute_shop/flask_app/service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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
Copy Markdown
Owner

Choose a reason for hiding this comment

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

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

account['password'] = hash_md5(account.get('password'))
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")
if password == saved_password:
return make_jwt(data)


def make_jwt(data):
return jwt.encode(data, secret_key, algorithm='HS256')


Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

строки...

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 @@
[{"password": "900150983cd24fb0d6963f7d28e17f72", "email": "qwerty@qwerty.com"}, {"password": "3944b025c9ca7eec3154b44666ae04a0", "email": "qwerty1@qwerty.com"}, {"email": "denis@den.com", "password": "c3875d07f44c422f3b3bc019c23e16ae"}, {"email": "denis@den.com", "password": "c3875d07f44c422f3b3bc019c23e16ae"}, {"email": "denis@den.com", "password": "52103037f7230187f89ce58d5334cdb6"}]
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Это почистить надо, не нужно на ГХ лить какие-то данные, это сердить только для кода, json-ы всякие тут лучше хранить пустыми, если только в них не конфигурация какая-то(а это не твой случай, так что сделай его пустым)

23 changes: 23 additions & 0 deletions Shops/sport_atribute_shop/flask_app/storage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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)


Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Убрать лишние строки, в конце файла должна быть только одна пустая строка

16 changes: 16 additions & 0 deletions Shops/sport_atribute_shop/flask_app/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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()
token = log(data)
return make_response(jsonify({"token": token}), 200)


@app.route('/reg', methods=["POST"])
def reg():
data = request.get_json()
registrate(data)
return make_response(jsonify({"result": 'OK'}), 201)
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

тут добавить в конец файла пустую строку

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