From 4b9a9fe013b9701648556c410e7299e83c04cf9b Mon Sep 17 00:00:00 2001 From: "Uladzislau Shedko(Foranx team)" Date: Wed, 24 Aug 2022 19:14:06 +0300 Subject: [PATCH 1/4] [H25].(First shit version) --- .../homeworks/django_todo_front/manage.py | 22 ++++ .../django_todo_front/templates/base.html | 16 +++ .../templates/plan/event_add.html | 52 ++++++++ .../templates/plan/event_analytics.html | 19 +++ .../templates/plan/event_delete.html | 20 +++ .../templates/plan/event_detail.html | 48 +++++++ .../templates/plan/event_list.html | 39 ++++++ .../templates/plan/event_update.html | 52 ++++++++ .../django_todo_front/todo_front/__init__.py | 0 .../django_todo_front/todo_front/admin.py | 3 + .../django_todo_front/todo_front/apps.py | 6 + .../todo_front/migrations/__init__.py | 0 .../django_todo_front/todo_front/models.py | 12 ++ .../todo_front/request_service.py | 12 ++ .../django_todo_front/todo_front/tests.py | 3 + .../django_todo_front/todo_front/urls.py | 6 + .../django_todo_front/todo_front/views.py | 17 +++ .../todo_service/__init__.py | 0 .../django_todo_front/todo_service/asgi.py | 16 +++ .../todo_service/settings.py | 124 ++++++++++++++++++ .../django_todo_front/todo_service/urls.py | 22 ++++ .../django_todo_front/todo_service/wsgi.py | 16 +++ 22 files changed, 505 insertions(+) create mode 100644 Shops/household_shop/homeworks/django_todo_front/manage.py create mode 100644 Shops/household_shop/homeworks/django_todo_front/templates/base.html create mode 100644 Shops/household_shop/homeworks/django_todo_front/templates/plan/event_add.html create mode 100644 Shops/household_shop/homeworks/django_todo_front/templates/plan/event_analytics.html create mode 100644 Shops/household_shop/homeworks/django_todo_front/templates/plan/event_delete.html create mode 100644 Shops/household_shop/homeworks/django_todo_front/templates/plan/event_detail.html create mode 100644 Shops/household_shop/homeworks/django_todo_front/templates/plan/event_list.html create mode 100644 Shops/household_shop/homeworks/django_todo_front/templates/plan/event_update.html create mode 100644 Shops/household_shop/homeworks/django_todo_front/todo_front/__init__.py create mode 100644 Shops/household_shop/homeworks/django_todo_front/todo_front/admin.py create mode 100644 Shops/household_shop/homeworks/django_todo_front/todo_front/apps.py create mode 100644 Shops/household_shop/homeworks/django_todo_front/todo_front/migrations/__init__.py create mode 100644 Shops/household_shop/homeworks/django_todo_front/todo_front/models.py create mode 100644 Shops/household_shop/homeworks/django_todo_front/todo_front/request_service.py create mode 100644 Shops/household_shop/homeworks/django_todo_front/todo_front/tests.py create mode 100644 Shops/household_shop/homeworks/django_todo_front/todo_front/urls.py create mode 100644 Shops/household_shop/homeworks/django_todo_front/todo_front/views.py create mode 100644 Shops/household_shop/homeworks/django_todo_front/todo_service/__init__.py create mode 100644 Shops/household_shop/homeworks/django_todo_front/todo_service/asgi.py create mode 100644 Shops/household_shop/homeworks/django_todo_front/todo_service/settings.py create mode 100644 Shops/household_shop/homeworks/django_todo_front/todo_service/urls.py create mode 100644 Shops/household_shop/homeworks/django_todo_front/todo_service/wsgi.py diff --git a/Shops/household_shop/homeworks/django_todo_front/manage.py b/Shops/household_shop/homeworks/django_todo_front/manage.py new file mode 100644 index 0000000..1729a26 --- /dev/null +++ b/Shops/household_shop/homeworks/django_todo_front/manage.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +"""Django's command-line utility for administrative tasks.""" +import os +import sys + + +def main(): + """Run administrative tasks.""" + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'todo_service.settings') + try: + from django.core.management import execute_from_command_line + except ImportError as exc: + raise ImportError( + "Couldn't import Django. Are you sure it's installed and " + "available on your PYTHONPATH environment variable? Did you " + "forget to activate a virtual environment?" + ) from exc + execute_from_command_line(sys.argv) + + +if __name__ == '__main__': + main() diff --git a/Shops/household_shop/homeworks/django_todo_front/templates/base.html b/Shops/household_shop/homeworks/django_todo_front/templates/base.html new file mode 100644 index 0000000..dd9ad01 --- /dev/null +++ b/Shops/household_shop/homeworks/django_todo_front/templates/base.html @@ -0,0 +1,16 @@ + + + + + + + + {% block title %}Base title{% endblock %} + + + + + + + + \ No newline at end of file diff --git a/Shops/household_shop/homeworks/django_todo_front/templates/plan/event_add.html b/Shops/household_shop/homeworks/django_todo_front/templates/plan/event_add.html new file mode 100644 index 0000000..ffbf2d7 --- /dev/null +++ b/Shops/household_shop/homeworks/django_todo_front/templates/plan/event_add.html @@ -0,0 +1,52 @@ +{% extends "base.html" %} + +{% block bar_add %}class="nav-link active" {% endblock %} + + +{% block content %} + +
+ {% csrf_token %} + +
+

Добавить событие

+ +
+ Название + {{ form.title }} +
+ +
+ Описание + {{ form.description }} +
+ +
+ Статус + {{ form.status }} +
+ +
+ Зависит от + {{ form.depends_on }} +
+ +
+ Старт + {{ form.started_at }} + Дэдлайн + {{ form.finished_at }} +
+ + {{form.started_at.errors}} + {{form.finished_at.errors}} + {{form.status.errors}} + + + +
+
+ +{% endblock %} \ No newline at end of file diff --git a/Shops/household_shop/homeworks/django_todo_front/templates/plan/event_analytics.html b/Shops/household_shop/homeworks/django_todo_front/templates/plan/event_analytics.html new file mode 100644 index 0000000..1944b0b --- /dev/null +++ b/Shops/household_shop/homeworks/django_todo_front/templates/plan/event_analytics.html @@ -0,0 +1,19 @@ +{% extends "base.html" %} + +{% block bar_analitics %} class="nav-link active" {% endblock %} + +{% block content %} +
+ +

Аналитика событий:

+ +
+ + {% for status, percentage in events.items %} + {{ status }} - {{ percentage }}% + {% endfor %} + +
+ +
+{% endblock %} diff --git a/Shops/household_shop/homeworks/django_todo_front/templates/plan/event_delete.html b/Shops/household_shop/homeworks/django_todo_front/templates/plan/event_delete.html new file mode 100644 index 0000000..d5bd79a --- /dev/null +++ b/Shops/household_shop/homeworks/django_todo_front/templates/plan/event_delete.html @@ -0,0 +1,20 @@ +{% extends "base.html" %} + + +{% block content %} + + +
{% csrf_token %} +
+

А ты уверен, что хочешь удалить событие "{{ object.title }}"?

+ + {{ form }} + + + +
+
+ +{% endblock %} \ No newline at end of file diff --git a/Shops/household_shop/homeworks/django_todo_front/templates/plan/event_detail.html b/Shops/household_shop/homeworks/django_todo_front/templates/plan/event_detail.html new file mode 100644 index 0000000..4a126e0 --- /dev/null +++ b/Shops/household_shop/homeworks/django_todo_front/templates/plan/event_detail.html @@ -0,0 +1,48 @@ +{% extends "base.html" %} + +{% block content %} + +
+ +

{{ object.title }} ({{ object.status }})

+ + +
+
+
+

{{ object.description }}

+ +
+ + +
+
+
+
Старт: {{ object.started_at }}
+
+
+
Истекает: {{ object.finished_at }}
+
+
+
+ + {% if object.depends_on %} +
+

Зависит от {{ object.depends_on.title }}

+
+ {% endif %} +
+
+ + + +
+ +{% endblock %} \ No newline at end of file diff --git a/Shops/household_shop/homeworks/django_todo_front/templates/plan/event_list.html b/Shops/household_shop/homeworks/django_todo_front/templates/plan/event_list.html new file mode 100644 index 0000000..8207c6f --- /dev/null +++ b/Shops/household_shop/homeworks/django_todo_front/templates/plan/event_list.html @@ -0,0 +1,39 @@ +{% extends "base.html" %} + +{% block bar_list %}class="nav-link active" {% endblock %} + +{% block content %} +
+ +

Твои события:

+ +
+ + In progress + Finished + Waiting + Expired + Blocked + +
+ +
+ + {% for event in object_list %} + {% if event.status == "expired" %} + {{ event.title }} + {% elif event.status == "finished" %} + {{ event.title }} + {% elif event.status == "in progress" %} + {{ event.title }} + {% else %} + {{ event.title }} + {% endif %} + {% empty %} + Попробуй добавь какое-нибудь событие... + {% endfor %} + +
+ +
+{% endblock %} diff --git a/Shops/household_shop/homeworks/django_todo_front/templates/plan/event_update.html b/Shops/household_shop/homeworks/django_todo_front/templates/plan/event_update.html new file mode 100644 index 0000000..e98aef7 --- /dev/null +++ b/Shops/household_shop/homeworks/django_todo_front/templates/plan/event_update.html @@ -0,0 +1,52 @@ +{% extends "base.html" %} + +{% block bar_add %}class="nav-link active" {% endblock %} + + +{% block content %} + +
+ {% csrf_token %} + +
+ +

Обновить событие

+ +
+ Название + {{ form.title }} +
+ +
+ Описание + {{ form.description }} +
+ +
+ Статус + {{ form.status }} +
+ +
+ Зависит от + {{ form.depends_on }} +
+ +
+ Старт + {{ form.started_at }} + Дэдлайн + {{ form.finished_at }} +
+ + {{form.started_at.errors}} + {{form.finished_at.errors}} + {{form.status.errors}} + + +
+
+ +{% endblock %} \ No newline at end of file diff --git a/Shops/household_shop/homeworks/django_todo_front/todo_front/__init__.py b/Shops/household_shop/homeworks/django_todo_front/todo_front/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/Shops/household_shop/homeworks/django_todo_front/todo_front/admin.py b/Shops/household_shop/homeworks/django_todo_front/todo_front/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/Shops/household_shop/homeworks/django_todo_front/todo_front/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/Shops/household_shop/homeworks/django_todo_front/todo_front/apps.py b/Shops/household_shop/homeworks/django_todo_front/todo_front/apps.py new file mode 100644 index 0000000..0733e16 --- /dev/null +++ b/Shops/household_shop/homeworks/django_todo_front/todo_front/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class TodoFrontConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'todo_front' diff --git a/Shops/household_shop/homeworks/django_todo_front/todo_front/migrations/__init__.py b/Shops/household_shop/homeworks/django_todo_front/todo_front/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/Shops/household_shop/homeworks/django_todo_front/todo_front/models.py b/Shops/household_shop/homeworks/django_todo_front/todo_front/models.py new file mode 100644 index 0000000..3989c61 --- /dev/null +++ b/Shops/household_shop/homeworks/django_todo_front/todo_front/models.py @@ -0,0 +1,12 @@ +from pydantic import BaseModel +from typing import Optional + + +class Event(BaseModel): + created_at = str + started_at = str + finished_at = str + title = str + description = str + depends_on = Optional[None] + status = str diff --git a/Shops/household_shop/homeworks/django_todo_front/todo_front/request_service.py b/Shops/household_shop/homeworks/django_todo_front/todo_front/request_service.py new file mode 100644 index 0000000..8336188 --- /dev/null +++ b/Shops/household_shop/homeworks/django_todo_front/todo_front/request_service.py @@ -0,0 +1,12 @@ +import requests +from .models import Event + +class ConnectService: + def __init__(self, url) -> None: + self.url = url + + def get_data(self): + return requests.get(url=self.url).json() + + def post_data(self): + return requests.post(url=self.url, json=Event.json()) diff --git a/Shops/household_shop/homeworks/django_todo_front/todo_front/tests.py b/Shops/household_shop/homeworks/django_todo_front/todo_front/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/Shops/household_shop/homeworks/django_todo_front/todo_front/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/Shops/household_shop/homeworks/django_todo_front/todo_front/urls.py b/Shops/household_shop/homeworks/django_todo_front/todo_front/urls.py new file mode 100644 index 0000000..ce642e7 --- /dev/null +++ b/Shops/household_shop/homeworks/django_todo_front/todo_front/urls.py @@ -0,0 +1,6 @@ +from django.urls import path +from .views import get_front_view + +urlpatterns = [ + path('front/', get_front_view), +] diff --git a/Shops/household_shop/homeworks/django_todo_front/todo_front/views.py b/Shops/household_shop/homeworks/django_todo_front/todo_front/views.py new file mode 100644 index 0000000..5ee3e9f --- /dev/null +++ b/Shops/household_shop/homeworks/django_todo_front/todo_front/views.py @@ -0,0 +1,17 @@ +from django.shortcuts import render +from django.http import HttpRequest +from .request_service import ConnectService +from .models import Event + + +def get_front_view(request): + template_name = 'plan/event_list.html' + data = ConnectService('http://127.0.0.1:5000/api/events/').get_data() + + context = {'events': [Event(**item) for item in data]} + render(request, template_name, context=context) + + +def post_front_view(request): + data = ConnectService(url='http://127.0.0.1:5000/api/events/') + return HttpRequest(data.post_data()) diff --git a/Shops/household_shop/homeworks/django_todo_front/todo_service/__init__.py b/Shops/household_shop/homeworks/django_todo_front/todo_service/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/Shops/household_shop/homeworks/django_todo_front/todo_service/asgi.py b/Shops/household_shop/homeworks/django_todo_front/todo_service/asgi.py new file mode 100644 index 0000000..11f79e8 --- /dev/null +++ b/Shops/household_shop/homeworks/django_todo_front/todo_service/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for todo_service project. + +It exposes the ASGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/4.1/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'todo_service.settings') + +application = get_asgi_application() diff --git a/Shops/household_shop/homeworks/django_todo_front/todo_service/settings.py b/Shops/household_shop/homeworks/django_todo_front/todo_service/settings.py new file mode 100644 index 0000000..d7db8a5 --- /dev/null +++ b/Shops/household_shop/homeworks/django_todo_front/todo_service/settings.py @@ -0,0 +1,124 @@ +""" +Django settings for todo_service project. + +Generated by 'django-admin startproject' using Django 4.1. + +For more information on this file, see +https://docs.djangoproject.com/en/4.1/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/4.1/ref/settings/ +""" + +from pathlib import Path + +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = Path(__file__).resolve().parent.parent + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'django-insecure-y8y(9i5!pake(in$ass%-)50dq3(eoinpvj9x8fd9u@p7r2pj9' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = [] + + +# Application definition + +INSTALLED_APPS = [ + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', + 'todo_front', +] + +MIDDLEWARE = [ + 'django.middleware.security.SecurityMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', +] + +ROOT_URLCONF = 'todo_service.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [BASE_DIR.joinpath('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 = 'todo_service.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/4.1/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': BASE_DIR / 'db.sqlite3', + } +} + + +# Password validation +# https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + }, +] + + +# Internationalization +# https://docs.djangoproject.com/en/4.1/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'UTC' + +USE_I18N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/4.1/howto/static-files/ + +STATIC_URL = 'static/' + +# Default primary key field type +# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field + +DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' diff --git a/Shops/household_shop/homeworks/django_todo_front/todo_service/urls.py b/Shops/household_shop/homeworks/django_todo_front/todo_service/urls.py new file mode 100644 index 0000000..df5749a --- /dev/null +++ b/Shops/household_shop/homeworks/django_todo_front/todo_service/urls.py @@ -0,0 +1,22 @@ +"""todo_service URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/4.1/topics/http/urls/ +Examples: +Function views + 1. Add an import: from my_app import views + 2. Add a URL to urlpatterns: path('', views.home, name='home') +Class-based views + 1. Add an import: from other_app.views import Home + 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') +Including another URLconf + 1. Import the include() function: from django.urls import include, path + 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) +""" +from django.contrib import admin +from django.urls import path, include + +urlpatterns = [ + path('admin/', admin.site.urls), + path('', include('todo_front.urls')), +] diff --git a/Shops/household_shop/homeworks/django_todo_front/todo_service/wsgi.py b/Shops/household_shop/homeworks/django_todo_front/todo_service/wsgi.py new file mode 100644 index 0000000..c645aaa --- /dev/null +++ b/Shops/household_shop/homeworks/django_todo_front/todo_service/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for todo_service project. + +It exposes the WSGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/4.1/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'todo_service.settings') + +application = get_wsgi_application() From 08778106a72e44f2101ebf5abed818629965dd6e Mon Sep 17 00:00:00 2001 From: "Uladzislau Shedko(Foranx team)" Date: Mon, 29 Aug 2022 12:40:23 +0300 Subject: [PATCH 2/4] [H25].(Correct GET request, added view detail) --- .../django_todo_front/templates/base.html | 15 ++++++++++ .../templates/plan/event_detail.html | 12 ++++---- .../templates/plan/event_list.html | 27 ++++------------- .../django_todo_front/todo_front/models.py | 15 +++++----- .../todo_front/request_service.py | 10 +++++-- .../django_todo_front/todo_front/urls.py | 7 +++-- .../django_todo_front/todo_front/views.py | 30 ++++++++++++------- .../todo_service/settings.py | 4 ++- 8 files changed, 69 insertions(+), 51 deletions(-) diff --git a/Shops/household_shop/homeworks/django_todo_front/templates/base.html b/Shops/household_shop/homeworks/django_todo_front/templates/base.html index dd9ad01..bffeeed 100644 --- a/Shops/household_shop/homeworks/django_todo_front/templates/base.html +++ b/Shops/household_shop/homeworks/django_todo_front/templates/base.html @@ -12,5 +12,20 @@ + +
+ + + {% block sidebar %}{% endblock %} + {% block content %}{% endblock %} +
+ \ No newline at end of file diff --git a/Shops/household_shop/homeworks/django_todo_front/templates/plan/event_detail.html b/Shops/household_shop/homeworks/django_todo_front/templates/plan/event_detail.html index 4a126e0..34a5584 100644 --- a/Shops/household_shop/homeworks/django_todo_front/templates/plan/event_detail.html +++ b/Shops/household_shop/homeworks/django_todo_front/templates/plan/event_detail.html @@ -5,15 +5,15 @@

{{ object.title }} ({{ object.status }})

- +

{{ object.description }}

- +
- +
@@ -36,13 +36,13 @@

{{ object.title }} ({{ object.status }})

-{% endblock %} \ No newline at end of file +{% endblock %} \ No newline at end of file diff --git a/Shops/household_shop/homeworks/django_todo_front/templates/plan/event_list.html b/Shops/household_shop/homeworks/django_todo_front/templates/plan/event_list.html index 8207c6f..0ec5e2d 100644 --- a/Shops/household_shop/homeworks/django_todo_front/templates/plan/event_list.html +++ b/Shops/household_shop/homeworks/django_todo_front/templates/plan/event_list.html @@ -1,36 +1,19 @@ {% extends "base.html" %} {% block bar_list %}class="nav-link active" {% endblock %} +{% csrf_token %} {% block content %}

Твои события:

- -
- {% for event in object_list %} - {% if event.status == "expired" %} - {{ event.title }} - {% elif event.status == "finished" %} - {{ event.title }} - {% elif event.status == "in progress" %} - {{ event.title }} - {% else %} - {{ event.title }} - {% endif %} - {% empty %} - Попробуй добавь какое-нибудь событие... + {% for event in events %} +
  • + {{event.title}} +
  • {% endfor %}
    diff --git a/Shops/household_shop/homeworks/django_todo_front/todo_front/models.py b/Shops/household_shop/homeworks/django_todo_front/todo_front/models.py index 3989c61..4865a2d 100644 --- a/Shops/household_shop/homeworks/django_todo_front/todo_front/models.py +++ b/Shops/household_shop/homeworks/django_todo_front/todo_front/models.py @@ -3,10 +3,11 @@ class Event(BaseModel): - created_at = str - started_at = str - finished_at = str - title = str - description = str - depends_on = Optional[None] - status = str + id: str + created_at: str + started_at: str + finished_at: str + title: str + description: str + depends_on: Optional[str] = '' + status: str diff --git a/Shops/household_shop/homeworks/django_todo_front/todo_front/request_service.py b/Shops/household_shop/homeworks/django_todo_front/todo_front/request_service.py index 8336188..3b8977e 100644 --- a/Shops/household_shop/homeworks/django_todo_front/todo_front/request_service.py +++ b/Shops/household_shop/homeworks/django_todo_front/todo_front/request_service.py @@ -5,8 +5,12 @@ class ConnectService: def __init__(self, url) -> None: self.url = url - def get_data(self): + def get_all_events(self): return requests.get(url=self.url).json() - def post_data(self): - return requests.post(url=self.url, json=Event.json()) + def add_event(self): + headers = {"Content-type": "application/json"} + return requests.post(url=self.url, json=Event.json(), headers=headers) + + def get_event_detail(self): + return requests.get(url=self.url).json() diff --git a/Shops/household_shop/homeworks/django_todo_front/todo_front/urls.py b/Shops/household_shop/homeworks/django_todo_front/todo_front/urls.py index ce642e7..d575ea9 100644 --- a/Shops/household_shop/homeworks/django_todo_front/todo_front/urls.py +++ b/Shops/household_shop/homeworks/django_todo_front/todo_front/urls.py @@ -1,6 +1,9 @@ from django.urls import path -from .views import get_front_view +from .views import get_events_list, create_new_event, get_event_detail + urlpatterns = [ - path('front/', get_front_view), + path('front/', get_events_list), + path('front/add', create_new_event), + path('front//', get_event_detail), ] diff --git a/Shops/household_shop/homeworks/django_todo_front/todo_front/views.py b/Shops/household_shop/homeworks/django_todo_front/todo_front/views.py index 5ee3e9f..caf15da 100644 --- a/Shops/household_shop/homeworks/django_todo_front/todo_front/views.py +++ b/Shops/household_shop/homeworks/django_todo_front/todo_front/views.py @@ -1,17 +1,27 @@ +from django.conf import settings from django.shortcuts import render -from django.http import HttpRequest -from .request_service import ConnectService +from django.views.decorators.csrf import csrf_exempt from .models import Event +from .request_service import ConnectService -def get_front_view(request): - template_name = 'plan/event_list.html' - data = ConnectService('http://127.0.0.1:5000/api/events/').get_data() - +def get_events_list(request): + data = ConnectService(settings.SERVICE_URL).get_all_events() context = {'events': [Event(**item) for item in data]} - render(request, template_name, context=context) + return render(request, 'plan/event_list.html', context=context) + +@csrf_exempt +def create_new_event(request): + if request.method == "POST": + event = Event(**request.POST) + data = ConnectService(settings.SERVICE_URL).add_event(event.json()) + return render(request, 'plan/event_add.html') + else: + return render(request, 'plan/event_add.html') + -def post_front_view(request): - data = ConnectService(url='http://127.0.0.1:5000/api/events/') - return HttpRequest(data.post_data()) +def get_event_detail(request, event_id): + data = ConnectService(settings.SERVICE_URL + event_id).get_event_detail() + context = {"object": Event(**data)} + return render(request, 'plan/event_detail.html', context=context) diff --git a/Shops/household_shop/homeworks/django_todo_front/todo_service/settings.py b/Shops/household_shop/homeworks/django_todo_front/todo_service/settings.py index d7db8a5..0b47542 100644 --- a/Shops/household_shop/homeworks/django_todo_front/todo_service/settings.py +++ b/Shops/household_shop/homeworks/django_todo_front/todo_service/settings.py @@ -20,7 +20,7 @@ # See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = 'django-insecure-y8y(9i5!pake(in$ass%-)50dq3(eoinpvj9x8fd9u@p7r2pj9' +SECRET_KEY = '' # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True @@ -122,3 +122,5 @@ # https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' + +SERVICE_URL = 'http://127.0.0.1:5000/api/events/' From fe5d03159152892b1d636098562d017984dacb30 Mon Sep 17 00:00:00 2001 From: "Uladzislau Shedko(Foranx team)" Date: Tue, 30 Aug 2022 11:08:08 +0300 Subject: [PATCH 3/4] [H25].(Completed task) --- .../django_todo_front/templates/base.html | 4 ++-- .../templates/plan/event_list.html | 23 +++++++++++++++---- .../todo_front/request_service.py | 6 ++--- .../django_todo_front/todo_front/urls.py | 6 ++--- .../django_todo_front/todo_front/views.py | 21 +++++++++-------- .../todo_service/settings.py | 2 +- 6 files changed, 40 insertions(+), 22 deletions(-) diff --git a/Shops/household_shop/homeworks/django_todo_front/templates/base.html b/Shops/household_shop/homeworks/django_todo_front/templates/base.html index bffeeed..6f60e3f 100644 --- a/Shops/household_shop/homeworks/django_todo_front/templates/base.html +++ b/Shops/household_shop/homeworks/django_todo_front/templates/base.html @@ -17,10 +17,10 @@ diff --git a/Shops/household_shop/homeworks/django_todo_front/templates/plan/event_list.html b/Shops/household_shop/homeworks/django_todo_front/templates/plan/event_list.html index 0ec5e2d..99ca3ae 100644 --- a/Shops/household_shop/homeworks/django_todo_front/templates/plan/event_list.html +++ b/Shops/household_shop/homeworks/django_todo_front/templates/plan/event_list.html @@ -1,19 +1,34 @@ {% extends "base.html" %} {% block bar_list %}class="nav-link active" {% endblock %} -{% csrf_token %} {% block content %}

    Твои события:

    +
    {% for event in events %} -
  • - {{event.title}} -
  • + {% if event.status == "expired" %} + {{ event.title }} + {% elif event.status == "finished" %} + {{ event.title }} + {% elif event.status == "in progress" %} + {{ event.title }} + {% else %} + {{ event.title }} + {% endif %} + {% empty %} + Попробуй добавь какое-нибудь событие... {% endfor %}
    diff --git a/Shops/household_shop/homeworks/django_todo_front/todo_front/request_service.py b/Shops/household_shop/homeworks/django_todo_front/todo_front/request_service.py index 3b8977e..f0c8b01 100644 --- a/Shops/household_shop/homeworks/django_todo_front/todo_front/request_service.py +++ b/Shops/household_shop/homeworks/django_todo_front/todo_front/request_service.py @@ -1,5 +1,5 @@ import requests -from .models import Event + class ConnectService: def __init__(self, url) -> None: @@ -8,9 +8,9 @@ def __init__(self, url) -> None: def get_all_events(self): return requests.get(url=self.url).json() - def add_event(self): + def add_event(self, data): headers = {"Content-type": "application/json"} - return requests.post(url=self.url, json=Event.json(), headers=headers) + return requests.post(url=self.url, data=data, headers=headers) def get_event_detail(self): return requests.get(url=self.url).json() diff --git a/Shops/household_shop/homeworks/django_todo_front/todo_front/urls.py b/Shops/household_shop/homeworks/django_todo_front/todo_front/urls.py index d575ea9..ec1b7c5 100644 --- a/Shops/household_shop/homeworks/django_todo_front/todo_front/urls.py +++ b/Shops/household_shop/homeworks/django_todo_front/todo_front/urls.py @@ -3,7 +3,7 @@ urlpatterns = [ - path('front/', get_events_list), - path('front/add', create_new_event), - path('front//', get_event_detail), + path('front/', get_events_list, name='event-list'), + path('front/add', create_new_event, name='event-add'), + path('front//', get_event_detail, name='event-detail'), ] diff --git a/Shops/household_shop/homeworks/django_todo_front/todo_front/views.py b/Shops/household_shop/homeworks/django_todo_front/todo_front/views.py index caf15da..3d1f80b 100644 --- a/Shops/household_shop/homeworks/django_todo_front/todo_front/views.py +++ b/Shops/household_shop/homeworks/django_todo_front/todo_front/views.py @@ -1,7 +1,8 @@ +import json +from .models import Event +from .forms import EventForm from django.conf import settings from django.shortcuts import render -from django.views.decorators.csrf import csrf_exempt -from .models import Event from .request_service import ConnectService @@ -11,15 +12,17 @@ def get_events_list(request): return render(request, 'plan/event_list.html', context=context) -@csrf_exempt def create_new_event(request): - if request.method == "POST": - event = Event(**request.POST) - data = ConnectService(settings.SERVICE_URL).add_event(event.json()) - return render(request, 'plan/event_add.html') - else: - return render(request, 'plan/event_add.html') + template_name = 'plan/event_add.html' + form = EventForm + context = {"form": form} + if request.method == "POST": + event = json.dumps(request.POST) + ConnectService(settings.SERVICE_URL).add_event(event) + + return render(request, template_name, context=context) + def get_event_detail(request, event_id): data = ConnectService(settings.SERVICE_URL + event_id).get_event_detail() diff --git a/Shops/household_shop/homeworks/django_todo_front/todo_service/settings.py b/Shops/household_shop/homeworks/django_todo_front/todo_service/settings.py index 0b47542..9a122e6 100644 --- a/Shops/household_shop/homeworks/django_todo_front/todo_service/settings.py +++ b/Shops/household_shop/homeworks/django_todo_front/todo_service/settings.py @@ -20,7 +20,7 @@ # See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = '' +SECRET_KEY = 'django-insecure-y8y(9i5!pake(in$ass%-)50dq3(eoinpvj9x8fd9u@p7r2pj9' # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True From b8ee8077b2ebf4ab014f12b2ac01a2d740e5c31a Mon Sep 17 00:00:00 2001 From: "Uladzislau Shedko(Foranx team)" Date: Tue, 30 Aug 2022 14:20:31 +0300 Subject: [PATCH 4/4] [H25].(Refactoring) --- .../templates/plan/event_detail.html | 14 ++++---- .../templates/plan/event_list.html | 18 +++++----- .../todo_front/request_service.py | 14 +++++--- .../django_todo_front/todo_front/urls.py | 10 +++--- .../django_todo_front/todo_front/views.py | 35 ++++++++++++++----- 5 files changed, 60 insertions(+), 31 deletions(-) diff --git a/Shops/household_shop/homeworks/django_todo_front/templates/plan/event_detail.html b/Shops/household_shop/homeworks/django_todo_front/templates/plan/event_detail.html index 34a5584..e32c3e6 100644 --- a/Shops/household_shop/homeworks/django_todo_front/templates/plan/event_detail.html +++ b/Shops/household_shop/homeworks/django_todo_front/templates/plan/event_detail.html @@ -5,15 +5,15 @@

    {{ object.title }} ({{ object.status }})

    - +

    {{ object.description }}

    - +
    - +
    @@ -28,7 +28,7 @@

    {{ object.title }} ({{ object.status }})

    {% if object.depends_on %}
    -

    Зависит от {{ object.depends_on.title }}

    +

    Зависит от {{ object.depends_on.title }}

    {% endif %}
    @@ -36,13 +36,13 @@

    {{ object.title }} ({{ object.status }})

    -{% endblock %} \ No newline at end of file +{% endblock %} \ No newline at end of file diff --git a/Shops/household_shop/homeworks/django_todo_front/templates/plan/event_list.html b/Shops/household_shop/homeworks/django_todo_front/templates/plan/event_list.html index 99ca3ae..969f30f 100644 --- a/Shops/household_shop/homeworks/django_todo_front/templates/plan/event_list.html +++ b/Shops/household_shop/homeworks/django_todo_front/templates/plan/event_list.html @@ -6,13 +6,15 @@

    Твои события:

    -
    - Waiting - In Progress - Finished - Expired - Blocked - All events + +
    @@ -23,7 +25,7 @@

    Твои события:

    {% elif event.status == "finished" %} {{ event.title }} {% elif event.status == "in progress" %} - {{ event.title }} + {{ event.title }} {% else %} {{ event.title }} {% endif %} diff --git a/Shops/household_shop/homeworks/django_todo_front/todo_front/request_service.py b/Shops/household_shop/homeworks/django_todo_front/todo_front/request_service.py index f0c8b01..2a57263 100644 --- a/Shops/household_shop/homeworks/django_todo_front/todo_front/request_service.py +++ b/Shops/household_shop/homeworks/django_todo_front/todo_front/request_service.py @@ -4,13 +4,19 @@ class ConnectService: def __init__(self, url) -> None: self.url = url - - def get_all_events(self): + self.headers = {"Content-type": "application/json"} + + def get_list_events(self): return requests.get(url=self.url).json() def add_event(self, data): - headers = {"Content-type": "application/json"} - return requests.post(url=self.url, data=data, headers=headers) + return requests.post(url=self.url, data=data, headers=self.headers) def get_event_detail(self): return requests.get(url=self.url).json() + + def delete_event(self): + return requests.delete(url=self.url) + + def update_event(self, data): + return requests.put(url=self.url, data=data, headers=self.headers) diff --git a/Shops/household_shop/homeworks/django_todo_front/todo_front/urls.py b/Shops/household_shop/homeworks/django_todo_front/todo_front/urls.py index ec1b7c5..8742061 100644 --- a/Shops/household_shop/homeworks/django_todo_front/todo_front/urls.py +++ b/Shops/household_shop/homeworks/django_todo_front/todo_front/urls.py @@ -1,9 +1,11 @@ from django.urls import path -from .views import get_events_list, create_new_event, get_event_detail +from .views import get_events_list, create_new_event, get_event_detail, delete_event, update_event urlpatterns = [ - path('front/', get_events_list, name='event-list'), - path('front/add', create_new_event, name='event-add'), - path('front//', get_event_detail, name='event-detail'), + path('list/', get_events_list, name='event-list'), + path('add/', create_new_event, name='event-add'), + path('/', get_event_detail, name='event-detail'), + path('/delete/', delete_event, name='event-delete'), + path('/update/', update_event, name='event-update'), ] diff --git a/Shops/household_shop/homeworks/django_todo_front/todo_front/views.py b/Shops/household_shop/homeworks/django_todo_front/todo_front/views.py index 3d1f80b..def4c86 100644 --- a/Shops/household_shop/homeworks/django_todo_front/todo_front/views.py +++ b/Shops/household_shop/homeworks/django_todo_front/todo_front/views.py @@ -7,24 +7,43 @@ def get_events_list(request): - data = ConnectService(settings.SERVICE_URL).get_all_events() + template_name = 'plan/event_list.html' + data = ConnectService(settings.SERVICE_URL).get_list_events() context = {'events': [Event(**item) for item in data]} - return render(request, 'plan/event_list.html', context=context) + + return render(request=request, template_name=template_name, context=context) def create_new_event(request): template_name = 'plan/event_add.html' - form = EventForm - context = {"form": form} + context = {"form": EventForm} if request.method == "POST": event = json.dumps(request.POST) ConnectService(settings.SERVICE_URL).add_event(event) - return render(request, template_name, context=context) + return render(request=request, template_name=template_name, context=context) -def get_event_detail(request, event_id): - data = ConnectService(settings.SERVICE_URL + event_id).get_event_detail() +def get_event_detail(request, pk): + template_name = 'plan/event_detail.html' + data = ConnectService(settings.SERVICE_URL + pk).get_event_detail() context = {"object": Event(**data)} - return render(request, 'plan/event_detail.html', context=context) + + return render(request=request, template_name=template_name, context=context) + + +def delete_event(request, pk): + template_name = 'plan/event_delete.html' + ConnectService(settings.SERVICE_URL + pk).delete_event() + + return render(request=request, template_name=template_name) + + +def update_event(request, pk): + template_name = 'plan/event_update.html' + context = {"form": EventForm} + event = json.dumps(request.POST) + ConnectService(settings.SERVICE_URL + pk).update_event(event) + + return render(request=request, template_name=template_name, context=context)