-
Notifications
You must be signed in to change notification settings - Fork 13
[#H24] Flask app #101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
[#H24] Flask app #101
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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' | ||
|
|
||
|
|
| 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) |
| 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): | ||
| 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') | ||
|
|
||
|
|
||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. строки... |
||
| 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"}] | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Это почистить надо, не нужно на ГХ лить какие-то данные, это сердить только для кода, json-ы всякие тут лучше хранить пустыми, если только в них не конфигурация какая-то(а это не твой случай, так что сделай его пустым) |
||
| 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) | ||
|
|
||
|
|
||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Убрать лишние строки, в конце файла должна быть только одна пустая строка |
||
| 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) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. тут добавить в конец файла пустую строку |
||
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
получить имэйл из запроса, получить имейл из storage, сравнить и занести в storage, если не совпадают