-
Notifications
You must be signed in to change notification settings - Fork 14
[#H24] Docker_Flask_HW #100
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
Nikita5390
wants to merge
5
commits into
Chudopal:main
Choose a base branch
from
Nikita5390:flask_hw2
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
Changes from all commits
Commits
Show all changes
5 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
6 changes: 6 additions & 0 deletions
6
Shops/building_materials_store/app/Homework/flask_hw2/app/Dockerfile
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 python:3.10 | ||
| COPY . /app | ||
| WORKDIR /app | ||
| RUN pip install -r requirements.txt | ||
| CMD ["python3", "run.py"] | ||
| EXPOSE 5000 |
7 changes: 7 additions & 0 deletions
7
Shops/building_materials_store/app/Homework/flask_hw2/app/config.py
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,7 @@ | ||
| from flask import Flask | ||
| from storage import Storage | ||
|
|
||
| storage = Storage("storage.json") | ||
| app = Flask(__name__) | ||
|
|
||
| secret_key = 'qwerty' |
Binary file added
BIN
+472 Bytes
Shops/building_materials_store/app/Homework/flask_hw2/app/requirements.txt
Binary file not shown.
4 changes: 4 additions & 0 deletions
4
Shops/building_materials_store/app/Homework/flask_hw2/app/run.py
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,4 @@ | ||
| from views import app | ||
|
|
||
| if __name__ == "__main__": | ||
| app.run(debug=True, host='0.0.0.0', port=5000) |
30 changes: 30 additions & 0 deletions
30
Shops/building_materials_store/app/Homework/flask_hw2/app/service.py
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,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: | ||
| return make_jwt(data) | ||
|
|
||
|
|
||
| def make_jwt(data): | ||
| return jwt.encode(data, secret_key, algorithm='HS256') | ||
Empty file.
22 changes: 22 additions & 0 deletions
22
Shops/building_materials_store/app/Homework/flask_hw2/app/storage.py
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,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
33
Shops/building_materials_store/app/Homework/flask_hw2/app/views.py
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,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)) |
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.
в если условие не срабатывает то возвращаем None? Не надо так, лучше всегда что-то возвращать, например сообщение какое-то или типа того