From 3818acbb50991ea3dea9a711407f5e427eecd7ed Mon Sep 17 00:00:00 2001 From: Aleksandr_Tsimbulov <36223069+AleksandrTsimbulov@users.noreply.github.com> Date: Tue, 10 Jul 2018 12:04:41 +0500 Subject: [PATCH 01/10] Update urls.py --- blog/articles/urls.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/blog/articles/urls.py b/blog/articles/urls.py index e69de29..83e5f6f 100644 --- a/blog/articles/urls.py +++ b/blog/articles/urls.py @@ -0,0 +1,6 @@ +from django.urls import path +from blog.articles.view import home + +urlpatterns = [ + path('', home), +] From 5a08b940d0ab8bf4b368bb944b516374ac6f40aa Mon Sep 17 00:00:00 2001 From: Aleksandr_Tsimbulov <36223069+AleksandrTsimbulov@users.noreply.github.com> Date: Tue, 10 Jul 2018 12:05:47 +0500 Subject: [PATCH 02/10] Update urls.py --- blog/urls.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/blog/urls.py b/blog/urls.py index dfc7362..be8664c 100644 --- a/blog/urls.py +++ b/blog/urls.py @@ -1,6 +1,7 @@ from django.contrib import admin -from django.urls import path +from django.urls import path, include urlpatterns = [ + path('', include('blog.articles.urls')), path('admin/', admin.site.urls), ] From daedbce36a80b1b90bda98251d96ce461ab30355 Mon Sep 17 00:00:00 2001 From: Aleksandr_Tsimbulov <36223069+AleksandrTsimbulov@users.noreply.github.com> Date: Tue, 10 Jul 2018 12:08:19 +0500 Subject: [PATCH 03/10] Update views.py --- blog/articles/views.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/blog/articles/views.py b/blog/articles/views.py index e69de29..d44dca8 100644 --- a/blog/articles/views.py +++ b/blog/articles/views.py @@ -0,0 +1,7 @@ +from django.http import HttpResponse, HttpRequest +import datetime + + +def home(request: HttpRequest) -> HttpResponse: + current_time = datetime.date.today().isoformat() + return HttpResponse(current_time, content_type='text/plain') From 32b62a6d7e3201ecb4a030da27e143150328f639 Mon Sep 17 00:00:00 2001 From: Aleksandr_Tsimbulov <36223069+AleksandrTsimbulov@users.noreply.github.com> Date: Tue, 10 Jul 2018 12:09:30 +0500 Subject: [PATCH 04/10] Update urls.py --- blog/articles/urls.py | 1 + 1 file changed, 1 insertion(+) diff --git a/blog/articles/urls.py b/blog/articles/urls.py index 83e5f6f..d7c624c 100644 --- a/blog/articles/urls.py +++ b/blog/articles/urls.py @@ -3,4 +3,5 @@ urlpatterns = [ path('', home), + path('calculate/', calculate), ] From 77c235e5c150a5d34ea792e79b2434d5d020e941 Mon Sep 17 00:00:00 2001 From: Aleksandr_Tsimbulov <36223069+AleksandrTsimbulov@users.noreply.github.com> Date: Tue, 10 Jul 2018 12:12:21 +0500 Subject: [PATCH 05/10] Update views.py --- blog/articles/views.py | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/blog/articles/views.py b/blog/articles/views.py index d44dca8..8025d84 100644 --- a/blog/articles/views.py +++ b/blog/articles/views.py @@ -1,7 +1,32 @@ -from django.http import HttpResponse, HttpRequest import datetime +from django.http import HttpResponse, HttpRequest +from django.views.decorators.http import require_http_methods + def home(request: HttpRequest) -> HttpResponse: current_time = datetime.date.today().isoformat() return HttpResponse(current_time, content_type='text/plain') + + +def handler400(request, *args, **kwargs): + return HttpResponse('Неизвестная операция или деление на ноль', status=400) + + +@require_http_methods(['GET']) +def calculate(request): + operation = request.GET['op'] + left_operand = int(request.GET['left']) + right_operand = int(request.GET['right']) + print(request.GET, operation, left_operand, right_operand) + if operation == '+': + result = left_operand + right_operand + elif operation == '-': + result = left_operand - right_operand + elif operation == '*': + result = left_operand * right_operand + elif operation == '/' and right_operand != 0: + result = left_operand / right_operand + else: + return handler400(request) + return HttpResponse(result) From 55c0415b921a0abb813d5c37d183dda9a376d557 Mon Sep 17 00:00:00 2001 From: Aleksandr_Tsimbulov <36223069+AleksandrTsimbulov@users.noreply.github.com> Date: Tue, 10 Jul 2018 12:16:11 +0500 Subject: [PATCH 06/10] Update urls.py --- blog/articles/urls.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/blog/articles/urls.py b/blog/articles/urls.py index d7c624c..af469e6 100644 --- a/blog/articles/urls.py +++ b/blog/articles/urls.py @@ -1,7 +1,11 @@ from django.urls import path from blog.articles.view import home +from blog.articles.views import show_all, show_specified_article, show_articles_by_year urlpatterns = [ path('', home), path('calculate/', calculate), + path('articles/', show_all), + path('articles//', show_specified_article), + path('articles/archive//', show_articles_by_year), ] From db2fc09f3ae9026164f0559bb6b3d044dd0ce96d Mon Sep 17 00:00:00 2001 From: Aleksandr_Tsimbulov <36223069+AleksandrTsimbulov@users.noreply.github.com> Date: Tue, 10 Jul 2018 12:18:09 +0500 Subject: [PATCH 07/10] Update views.py --- blog/articles/views.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/blog/articles/views.py b/blog/articles/views.py index 8025d84..477fa50 100644 --- a/blog/articles/views.py +++ b/blog/articles/views.py @@ -1,7 +1,8 @@ import datetime -from django.http import HttpResponse, HttpRequest +from django.http import HttpResponse, HttpRequest, Http404 from django.views.decorators.http import require_http_methods +from blog.articles.models import ARTICLES def home(request: HttpRequest) -> HttpResponse: @@ -30,3 +31,24 @@ def calculate(request): else: return handler400(request) return HttpResponse(result) + +def show_all(request): + return HttpResponse('\n'.join((article['title'] for article in ARTICLES)), content_type='text/plain; charset=utf-8') + + +def show_specified_article(request, id): + article_name = '' + for article in ARTICLES: + if article['id'] == id: + article_name = article['title'] + if article_name: + return HttpResponse(article_name) + raise Http404 + + +def show_articles_by_year(request, year): + over_a_year_articles = '\n'.join((article['title'] for article in ARTICLES if article['year'] == year)) + print(over_a_year_articles) + if over_a_year_articles: + return HttpResponse(over_a_year_articles, content_type='text/plain; charset=UTF-8') + raise Http404 From 889da686be6399a651223dc163501b53c7b77c42 Mon Sep 17 00:00:00 2001 From: Aleksandr_Tsimbulov <36223069+AleksandrTsimbulov@users.noreply.github.com> Date: Tue, 10 Jul 2018 12:22:36 +0500 Subject: [PATCH 08/10] Update urls.py --- blog/articles/urls.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/blog/articles/urls.py b/blog/articles/urls.py index af469e6..1613a94 100644 --- a/blog/articles/urls.py +++ b/blog/articles/urls.py @@ -1,5 +1,5 @@ from django.urls import path -from blog.articles.view import home +from blog.articles.views import home from blog.articles.views import show_all, show_specified_article, show_articles_by_year urlpatterns = [ From 5abe6b5f3947527fb5156d4f5fbf17c1bbcf44c9 Mon Sep 17 00:00:00 2001 From: Aleksandr_Tsimbulov <36223069+AleksandrTsimbulov@users.noreply.github.com> Date: Tue, 10 Jul 2018 12:41:52 +0500 Subject: [PATCH 09/10] Update urls.py --- blog/articles/urls.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/blog/articles/urls.py b/blog/articles/urls.py index 1613a94..20eab43 100644 --- a/blog/articles/urls.py +++ b/blog/articles/urls.py @@ -1,5 +1,5 @@ from django.urls import path -from blog.articles.views import home +from blog.articles.views import home, calculate from blog.articles.views import show_all, show_specified_article, show_articles_by_year urlpatterns = [ From 432456a0336eb785f29853e120b6520c4af93430 Mon Sep 17 00:00:00 2001 From: Aleksandr_Tsimbulov <36223069+AleksandrTsimbulov@users.noreply.github.com> Date: Tue, 10 Jul 2018 12:49:46 +0500 Subject: [PATCH 10/10] Update views.py --- blog/articles/views.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/blog/articles/views.py b/blog/articles/views.py index 477fa50..22a158d 100644 --- a/blog/articles/views.py +++ b/blog/articles/views.py @@ -47,8 +47,4 @@ def show_specified_article(request, id): def show_articles_by_year(request, year): - over_a_year_articles = '\n'.join((article['title'] for article in ARTICLES if article['year'] == year)) - print(over_a_year_articles) - if over_a_year_articles: - return HttpResponse(over_a_year_articles, content_type='text/plain; charset=UTF-8') - raise Http404 + return HttpResponse('\n'.join((article['title'] for article in ARTICLES if article['year'] == year)))