forked from dualboot-partners/eu-python-learn-challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
Flask exercise #4
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
alex-solovyev
wants to merge
2
commits into
feature/filter_map
Choose a base branch
from
feature/flask
base: feature/filter_map
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 1 commit
Commits
Show all changes
2 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 |
|---|---|---|
| @@ -1,31 +1,44 @@ | ||
| from flask import Flask | ||
| from flask import Flask, abort, request | ||
| from http import HTTPStatus | ||
|
|
||
|
|
||
| class FlaskExercise: | ||
| """ | ||
| Вы должны создать API для обработки CRUD запросов. | ||
| В данной задаче все пользователи хранятся в одном словаре, где ключ - это имя пользователя, | ||
| а значение - его параметры. {"user1": {"age": 33}, "user2": {"age": 20}} | ||
| Словарь (dict) хранить в памяти, он должен быть пустым при старте flask. | ||
|
|
||
| POST /user - создание пользователя. | ||
| В теле запроса приходит JSON в формате {"name": <имя пользователя>}. | ||
| Ответ должен вернуться так же в JSON в формате {"data": "User <имя пользователя> is created!"} | ||
| со статусом 201. | ||
| Если в теле запроса не было ключа "name", то в ответ возвращается JSON | ||
| {"errors": {"name": "This field is required"}} со статусом 422 | ||
|
|
||
| GET /user/<name> - чтение пользователя | ||
| В ответе должен вернуться JSON {"data": "My name is <name>"}. Статус 200 | ||
|
|
||
| PATCH /user/<name> - обновление пользователя | ||
| В теле запроса приходит JSON в формате {"name": <new_name>}. | ||
| В ответе должен вернуться JSON {"data": "My name is <new_name>"}. Статус 200 | ||
|
|
||
| DELETE /user/<name> - удаление пользователя | ||
| В ответ должен вернуться статус 204 | ||
| """ | ||
| users: dict = {} | ||
|
|
||
| @staticmethod | ||
| def configure_routes(app: Flask) -> None: | ||
| pass | ||
| @app.post("/user") | ||
| def user_post() -> tuple: | ||
| name = request.json.get("name") | ||
| if name is None: | ||
| return { | ||
| "errors": {"name": "This field is required"} | ||
| }, HTTPStatus.UNPROCESSABLE_ENTITY | ||
|
|
||
| FlaskExercise.users[name] = {} | ||
| return {"data": f"User {name} is created!"}, HTTPStatus.CREATED | ||
|
|
||
| @app.get("/user/<string:name>") | ||
| def show_user(name: str) -> dict: | ||
| FlaskExercise.abort_if_user_not_found(name) | ||
| return {"data": f"My name is {name}"} | ||
|
|
||
| @app.patch("/user/<string:name>") | ||
| def update_user(name: str) -> dict: | ||
| FlaskExercise.abort_if_user_not_found(name) | ||
| new_name = request.json.get("name") | ||
| user = FlaskExercise.users.pop(name) | ||
| FlaskExercise.users[new_name] = user | ||
| return {"data": f"My name is {new_name}"} | ||
|
|
||
| @app.delete("/user/<string:name>") | ||
| def delete_user(name: str) -> tuple: | ||
| FlaskExercise.abort_if_user_not_found(name) | ||
| FlaskExercise.users.pop(name) | ||
| return "", HTTPStatus.NO_CONTENT | ||
|
|
||
| @staticmethod | ||
| def abort_if_user_not_found(name: str) -> None: | ||
| name = FlaskExercise.users.get(name) | ||
| if name is None: | ||
| abort(HTTPStatus.NOT_FOUND) | ||
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
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.
pop() выполняет две операции - del и возврат удаленного. Здесь нужна только первая
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.
replaced pop with del