-
Notifications
You must be signed in to change notification settings - Fork 14
[#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
Open
Denis9505
wants to merge
3
commits into
Chudopal:main
Choose a base branch
from
Denis9505:flask_hw
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[#H24] Flask app #101
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 = '' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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): | ||
| 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') | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
22
Shops/sport_atribute_shop/home_work/flask_hw/[#H17]Flask_HW.py
This file was deleted.
Oops, something went wrong.
66 changes: 0 additions & 66 deletions
66
Shops/sport_atribute_shop/home_work/flask_hw/[#H17]Flask_HW2.py
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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, если не совпадают