Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,6 @@ venv.bak/
# mypy
.mypy_cache/

# VS code
.vscode

.idea

.envrc
32 changes: 32 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Django",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}\\manage.py",
"args": [
"runserver",
"--no-color",
"--noreload"
],
"django": true,
"justMyCode": true
},
{
"name": "Python: shell",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}\\manage.py",
"args": [
"shell"
],
"django": true,
"justMyCode": true
}
]
}
10 changes: 10 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"flake8.args": [
"--max-line-length", "120",
"--extend-ignore", "E203"
],
"[python]": {
"editor.formatOnSave": true,
"editor.defaultFormatter": "charliermarsh.ruff"
}
}
70 changes: 35 additions & 35 deletions django_views_routing_homework/settings.py
Original file line number Diff line number Diff line change
@@ -1,83 +1,83 @@
from pathlib import Path

BASE_DIR = Path(__file__).resolve().parent.parent
SECRET_KEY = 'django-insecure-$$depm8wi$v*0y-)3#1&wlrwg$l%!pqa(x-t#z5gr^x=^vo#eo'
SECRET_KEY = "django-insecure-$$depm8wi$v*0y-)3#1&wlrwg$l%!pqa(x-t#z5gr^x=^vo#eo"

DEBUG = True

ALLOWED_HOSTS = []


INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
]

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
]

ROOT_URLCONF = 'django_views_routing_homework.urls'
ROOT_URLCONF = "django_views_routing_homework.urls"

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [str(BASE_DIR / 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [str(BASE_DIR / "templates")],
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
"django.template.context_processors.debug",
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
],
},
},
]

WSGI_APPLICATION = 'django_views_routing_homework.wsgi.application'
WSGI_APPLICATION = "django_views_routing_homework.wsgi.application"

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": BASE_DIR / "db.sqlite3",
}
}


AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
},
]


LANGUAGE_CODE = 'en-us'
LANGUAGE_CODE = "en-us"

TIME_ZONE = 'UTC'
TIME_ZONE = "UTC"

USE_I18N = True

USE_TZ = True


STATIC_URL = 'static/'
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
STATIC_URL = "static/"
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
61 changes: 43 additions & 18 deletions django_views_routing_homework/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,52 @@
from django.urls import path

from django_views_routing_homework.views.level_1.a_welcome_user import welcome_user_view
from django_views_routing_homework.views.level_1.c_baned_username import is_username_banned_view
from django_views_routing_homework.views.level_2.a_user_info_by_username import get_user_info_by_username_view
from django_views_routing_homework.views.level_1.b_bye_user import bye_user_view
from django_views_routing_homework.views.level_1.c_baned_username import (
is_username_banned_view,
)
from django_views_routing_homework.views.level_2.a_user_info_by_username import (
get_user_info_by_username_view,
)
from django_views_routing_homework.views.level_2.c_product_type import get_products_view
from django_views_routing_homework.views.level_2.d_authorization import authorization_view, process_authorization_view
from django_views_routing_homework.views.level_3.b_validate_user_data import validate_user_data_view
from django_views_routing_homework.views.level_3.c_github_full_name import fetch_name_from_github_view
from django_views_routing_homework.views.level_3.d_file_generation import generate_file_with_text_view
from django_views_routing_homework.views.level_2.b_greet_user_language import (
greet_user_in_different_languages_view,
)
from django_views_routing_homework.views.level_2.d_authorization import (
authorization_view,
process_authorization_view,
)
from django_views_routing_homework.views.level_3.b_validate_user_data import (
validate_user_data_view,
)
from django_views_routing_homework.views.level_3.c_github_full_name import (
fetch_name_from_github_view,
)
from django_views_routing_homework.views.level_3.d_file_generation import (
generate_file_with_text_view,
)

from django_views_routing_homework.views.level_3.a_user_ip import show_user_ip_view

from django_views_routing_homework.views.level_1.d_user_info import get_user_info_view
from django_views_routing_homework.views.level_1.e_month_title import (
get_month_title_view,
)

urlpatterns = [
path('admin/', admin.site.urls),
path('welcome/', welcome_user_view),
path('banned/<slug:username>/', is_username_banned_view),
path('user-info-by-username/<int:username>/', get_user_info_by_username_view),
path('products/', get_products_view),
path('authorization/', authorization_view),
path('process-authorization/', process_authorization_view),
path('me/ip/', show_user_ip_view),
path('user/validate/', validate_user_data_view),
path('user/github/<slug:github_username>/full-name/', fetch_name_from_github_view),
path('text/generate/', generate_file_with_text_view),
# добавлять пути тут
path("admin/", admin.site.urls),
path("welcome/", welcome_user_view),
path("bye/", bye_user_view),
path("banned/<slug:username>/", is_username_banned_view),
path("user-info-by-username/<slug:username>/", get_user_info_by_username_view),
path("greet/<str:name>/<slug:language>/", greet_user_in_different_languages_view),
path("products/", get_products_view),
path("authorization/", authorization_view),
path("process-authorization/", process_authorization_view),
path("me/ip/", show_user_ip_view),
path("user/validate/", validate_user_data_view),
path("user/github/<slug:github_username>/full-name/", fetch_name_from_github_view),
path("text/generate/", generate_file_with_text_view),
path("user-info/<int:user_id>/", get_user_info_view),
path("month-title/<int:month_number>/", get_month_title_view),
]
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@


def welcome_user_view(request):
welcome_message = 'Bye, user'
# welcome_message = 'Bye, user'
welcome_message = "hello, user"
return HttpResponse(welcome_message)
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
2. Результат проверяйте по ссылке http://127.0.0.1:8000/banned/тут интересующий юзернэйм/,
например http://127.0.0.1:8000/banned/any_username/
"""
BANNED_USERNAMES = ['red_dev', 'green_bear', 'monster']
BANNED_USERNAMES = ["red_dev", "green_bear", "monster"]


def is_username_banned_view(request, username: str):
# код писать тут
return HttpResponse('User not banned')
if username in BANNED_USERNAMES:
return HttpResponse("User is banned")
else:
return HttpResponse("User is not banned")
13 changes: 9 additions & 4 deletions django_views_routing_homework/views/level_1/e_month_title.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import calendar
from django.http import HttpResponse, HttpResponseNotFound


Expand All @@ -13,10 +14,14 @@
"""


def get_month_title_by_number(month_number: int):
pass # код писать тут
def get_month_title_by_number(month_number: int) -> str:
if 1 <= month_number <= 12:
return calendar.month_name[month_number]
return ""


def get_month_title_view(request, month_number: int):
# код писать тут
return HttpResponseNotFound('Месяца с таким номером не существует')
month_name = get_month_title_by_number(month_number)
if month_name:
return HttpResponse(month_name)
return HttpResponseNotFound("Месяца с таким номером не существует")
47 changes: 24 additions & 23 deletions django_views_routing_homework/views/level_2/c_product_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,32 @@
3. Файл urls.py трогать не нужно, праметры хранятся в объекте request.
"""
PRODUCTS = [
{'type': 'electronics', 'title': 'Smartphone', 'price': 500},
{'type': 'clothing', 'title': 'T-Shirt', 'price': 20},
{'type': 'books', 'title': 'Python for Beginners', 'price': 25},
{'type': 'electronics', 'title': 'Television', 'price': 300},
{'type': 'clothing', 'title': 'Sneakers', 'price': 50},
{'type': 'groceries', 'title': 'Milk', 'price': 2},
{'type': 'toys', 'title': 'Lego Set', 'price': 40},
{'type': 'books', 'title': 'Django for Dummies', 'price': 30},
{'type': 'electronics', 'title': 'Laptop', 'price': 1000},
{'type': 'clothing', 'title': 'Jeans', 'price': 40},
{'type': 'groceries', 'title': 'Eggs', 'price': 3},
{'type': 'toys', 'title': 'Action Figure', 'price': 15},
{'type': 'home & garden', 'title': 'Lawn Mower', 'price': 250},
{'type': 'electronics', 'title': 'Headphones', 'price': 100},
{'type': 'clothing', 'title': 'Jacket', 'price': 60},
{'type': 'home & garden', 'title': 'Chair', 'price': 80},
{'type': 'books', 'title': 'JavaScript: The Good Parts', 'price': 35},
{'type': 'groceries', 'title': 'Bread', 'price': 1},
{'type': 'toys', 'title': 'Board Game', 'price': 25},
{'type': 'home & garden', 'title': 'Table', 'price': 120}
{"type": "electronics", "title": "Smartphone", "price": 500},
{"type": "clothing", "title": "T-Shirt", "price": 20},
{"type": "books", "title": "Python for Beginners", "price": 25},
{"type": "electronics", "title": "Television", "price": 300},
{"type": "clothing", "title": "Sneakers", "price": 50},
{"type": "groceries", "title": "Milk", "price": 2},
{"type": "toys", "title": "Lego Set", "price": 40},
{"type": "books", "title": "Django for Dummies", "price": 30},
{"type": "electronics", "title": "Laptop", "price": 1000},
{"type": "clothing", "title": "Jeans", "price": 40},
{"type": "groceries", "title": "Eggs", "price": 3},
{"type": "toys", "title": "Action Figure", "price": 15},
{"type": "home & garden", "title": "Lawn Mower", "price": 250},
{"type": "electronics", "title": "Headphones", "price": 100},
{"type": "clothing", "title": "Jacket", "price": 60},
{"type": "home & garden", "title": "Chair", "price": 80},
{"type": "books", "title": "JavaScript: The Good Parts", "price": 35},
{"type": "groceries", "title": "Bread", "price": 1},
{"type": "toys", "title": "Board Game", "price": 25},
{"type": "home & garden", "title": "Table", "price": 120},
]


def get_products_view(request):
products = []
# код писать тут

products = PRODUCTS
product_type = request.GET.get("type")
if product_type:
products = [product for product in PRODUCTS if product["type"] == product_type]
return JsonResponse(data=products, safe=False)
35 changes: 21 additions & 14 deletions django_views_routing_homework/views/level_2/d_authorization.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,35 @@
5. На странице вы увидете сообщение об успехе или неудаче.
"""
USERNAME_TO_PASSWORD_MAPPER = {
'john_doe': 'password123',
'sarah_connor': 'terminator2',
'admin': 'admin_pass',
'coder2021': 'qwerty',
'happy_user': '12345',
'l33t_h4ck3r': 'leetpassword',
'music_lover': 'beethoven',
'sports_fan': 'goal2023',
'travel_guru': 'wanderlust',
"john_doe": "password123",
"sarah_connor": "terminator2",
"admin": "admin_pass",
"coder2021": "qwerty",
"happy_user": "12345",
"l33t_h4ck3r": "leetpassword",
"music_lover": "beethoven",
"sports_fan": "goal2023",
"travel_guru": "wanderlust",
}


def authorization(username: str, password: str) -> bool:
return USERNAME_TO_PASSWORD_MAPPER.get(username, "") == password


@csrf_exempt
def process_authorization_view(request):
if request.method == 'POST':
if request.method == "POST":
data = json.loads(request.body)
# код писать тут
if authorization(
username=data.get("username", ""), password=data.get("password", "")
):
return JsonResponse(data={}, status=200)
return JsonResponse(data={}, status=403)
else:
return HttpResponseNotAllowed(permitted_methods=['POST'])
return HttpResponseNotAllowed(permitted_methods=["POST"])


# не обращайте внимания на эту вьюху, она нужна лишь для отрисовки страницы авторизации
def authorization_view(request):
return render(request, 'authorization.html')

return render(request, "authorization.html")
Loading