From 64b9cb90c3898bc53d6854b37f464d6c989548b0 Mon Sep 17 00:00:00 2001 From: Alex <48474870+aon2003@users.noreply.github.com> Date: Wed, 5 Jan 2022 21:14:29 +0200 Subject: [PATCH] urls.py - version update fixes api_processor.py - fixed (the functions didn't really return the longest post title and comment, they actually sorted them alphabetically) --- mysite/api/urls.py | 5 ++--- mysite/elsys/processors/api_processor.py | 19 +++++++++++++++---- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/mysite/api/urls.py b/mysite/api/urls.py index de14d94..55539ad 100644 --- a/mysite/api/urls.py +++ b/mysite/api/urls.py @@ -1,13 +1,12 @@ -from django.urls import include, path +from django.urls import include, path, re_path from api import views from api.views import CarsView from rest_framework_swagger.views import get_swagger_view -from django.conf.urls import url schema_view = get_swagger_view(title='Cars API') urlpatterns = [ - url(r'docs/', schema_view), + re_path(r'docs/', schema_view), path('cars/get', views.cars_json), path('cars/create', views.create_car), path('view_cars/', CarsView.as_view()), diff --git a/mysite/elsys/processors/api_processor.py b/mysite/elsys/processors/api_processor.py index 11614da..e70d1f4 100644 --- a/mysite/elsys/processors/api_processor.py +++ b/mysite/elsys/processors/api_processor.py @@ -3,15 +3,26 @@ COMMENTS_URL = "https://jsonplaceholder.typicode.com/posts/1/comments" POSTS_URL = "https://jsonplaceholder.typicode.com/posts" + +def sort_func(json): + try: + if 'title' in json: + return len(json['title']) + else: + return len(json['body']) + except KeyError: + return 0 + + class ApiProcessor: @staticmethod def longest_comment(): data = requests.get(COMMENTS_URL) - comments = sorted(data.json(), key=lambda d: d['body'], reverse=True) + comments = sorted(data.json(), key=sort_func, reverse=True) return comments[0] - + @staticmethod def post_with_longest_title(): data = requests.get(POSTS_URL) - posts = sorted(data.json(), key=lambda d: d['title'], reverse=True) - return posts[0] \ No newline at end of file + posts = sorted(data.json(), key=sort_func, reverse=True) + return posts[0]