From 9afccdc511fa98eecec1c6f4325cc04af48116b8 Mon Sep 17 00:00:00 2001 From: Termuellinator Date: Thu, 7 Dec 2023 17:42:43 +0100 Subject: [PATCH 1/5] Add remove userprofile from UserLanguageModelSerializer and add UserLanguageModelAdminSerializer --- apps/user/serializers.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/apps/user/serializers.py b/apps/user/serializers.py index 72e7936..b3e8d36 100644 --- a/apps/user/serializers.py +++ b/apps/user/serializers.py @@ -40,6 +40,14 @@ class Meta: class UserLanguageModelSerializer(serializers.ModelSerializer): language = serializers.StringRelatedField() + class Meta: + model = models.UserLanguage + fields = ('id', 'language', 'level') + + +class UserLanguageModelAdminSerializer(serializers.ModelSerializer): + language = serializers.StringRelatedField() + class Meta: model = models.UserLanguage fields = ('id', 'userprofile', 'language', 'level') From 6d3ae8795aea3440312e46d9950b6925b9acfe53 Mon Sep 17 00:00:00 2001 From: Termuellinator Date: Thu, 7 Dec 2023 17:43:37 +0100 Subject: [PATCH 2/5] Use UserLanguageModelAdminSerializer if user is superuser --- apps/user/views.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/apps/user/views.py b/apps/user/views.py index 2ce8530..d0d730e 100644 --- a/apps/user/views.py +++ b/apps/user/views.py @@ -103,7 +103,9 @@ class UserLanguageViewSet(viewsets.ModelViewSet): permission_classes = [IsAuthenticated] def get_serializer_class(self): - if self.action in ("create", "update"): + if self.request.user.is_superuser: + return serializers.UserLanguageModelAdminSerializer + elif self.action in ("create", "update"): return serializers.UserLanguageCreateUpdateSerializer return serializers.UserLanguageModelSerializer From 5e415e0a4423640cd6206ec2f02cf437c73feb24 Mon Sep 17 00:00:00 2001 From: Termuellinator Date: Thu, 7 Dec 2023 17:44:28 +0100 Subject: [PATCH 3/5] Adapt test to new Serializer layout --- apps/user/tests/ut/test_endpoints.py | 1 - 1 file changed, 1 deletion(-) diff --git a/apps/user/tests/ut/test_endpoints.py b/apps/user/tests/ut/test_endpoints.py index c810c43..3cfd23a 100644 --- a/apps/user/tests/ut/test_endpoints.py +++ b/apps/user/tests/ut/test_endpoints.py @@ -280,7 +280,6 @@ def test_list_user_languages_authenticated(self): self.assert_status_code(response_list, status.HTTP_200_OK) self.assertEqual(len(response_list.data["results"]), 1) - self.assertEqual(response_list.data["results"][0]['userprofile'], self.profile.id) self.assertEqual(response_list.data["results"][0]['language'], 'English') self.assertEqual(response_list.data["results"][0]['level'], 'Fluent') From 375bf31f7aed654170461d5cf13ecb11fccee06d Mon Sep 17 00:00:00 2001 From: Termuellinator Date: Fri, 8 Dec 2023 16:01:50 +0100 Subject: [PATCH 4/5] Add more information to TimePlaceMatchSerializer --- apps/timeplace/serializers.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/apps/timeplace/serializers.py b/apps/timeplace/serializers.py index f650b4a..b8d62b2 100644 --- a/apps/timeplace/serializers.py +++ b/apps/timeplace/serializers.py @@ -1,8 +1,8 @@ -from datetime import datetime, timezone +from datetime import datetime, timezone, date import pytz from rest_framework import serializers -from apps.user.serializers import UserModelSerializer +from apps.user.serializers import UserModelSerializer, UserLanguageModelSerializer from apps.core.utils import get_nearest_city from . import models @@ -162,6 +162,11 @@ class TimePlaceMatchSerializer(serializers.ModelSerializer): interests = InterestModelSerializer(many=True) activities = ActivityModelSerializer(many=True) username = serializers.CharField(source="user.userprofile.name") + gender = serializers.CharField(source="user.userprofile.gender") + slogan = serializers.CharField(source="user.userprofile.slogan") + languages = UserLanguageModelSerializer(many=True, + source="user.userprofile.user_language") + age = serializers.SerializerMethodField() class Meta: model = models.TimePlace @@ -169,7 +174,18 @@ class Meta: "id", "user", "username", + "age", + "languages", + "gender", + "slogan", "description", "interests", "activities", ] + + def get_age(self, obj) -> int: + today = date.today() + birthdate = obj.user.userprofile.birthday + age = today.year - birthdate.year - \ + ((today.month, today.day) < (birthdate.month, birthdate.day)) + return age From d345f9a44146bd64aa001d58df56338f05dd11bb Mon Sep 17 00:00:00 2001 From: Termuellinator Date: Fri, 8 Dec 2023 16:02:12 +0100 Subject: [PATCH 5/5] Update OpenAPI Schema to account for changes in TimePlaceMatchSerializer. --- apps/match/serializers.py | 30 +++++++++++ config/settings/dev.py | 2 +- schema.yml | 105 ++++++++++++++++++++++++++++++++++---- 3 files changed, 127 insertions(+), 10 deletions(-) diff --git a/apps/match/serializers.py b/apps/match/serializers.py index 5597246..744e7e6 100644 --- a/apps/match/serializers.py +++ b/apps/match/serializers.py @@ -17,6 +17,21 @@ "id": 13, "user": 3, "username": "Anna", + "age": 19, + "languages": [ + { + "id": 4, + "language": "Albanian", + "level": "Fluent" + }, + { + "id": 6, + "language": "German", + "level": "Learning" + } + ], + "gender": "D", + "slogan": "Christmas is coming to town", "description": "Want to go to the Train Museum", "interests": [ { @@ -35,6 +50,21 @@ "id": 2, "user": 2, "username": "Betty", + "age": 25, + "languages": [ + { + "id": 2, + "language": "Albanian", + "level": "Fluent" + }, + { + "id": 16, + "language": "English", + "level": "Learning" + } + ], + "gender": "F", + "slogan": "Every API has a call!", "description": "Want to visit some kind of Museum", "interests": [ { diff --git a/config/settings/dev.py b/config/settings/dev.py index b834599..119431a 100644 --- a/config/settings/dev.py +++ b/config/settings/dev.py @@ -40,7 +40,7 @@ SPECTACULAR_SETTINGS = { 'TITLE': 'SameTimeSamePlace API', 'DESCRIPTION': 'Find friends for an adventure.', - 'VERSION': '0.3.1', + 'VERSION': '0.3.2', 'SERVE_INCLUDE_SCHEMA': False, # Split components into request and response parts where appropriate 'COMPONENT_SPLIT_REQUEST': True, diff --git a/schema.yml b/schema.yml index 63f7a50..51a56fc 100644 --- a/schema.yml +++ b/schema.yml @@ -1,7 +1,7 @@ openapi: 3.0.3 info: title: SameTimeSamePlace API - version: 0.3.1 + version: 0.3.2 description: Find friends for an adventure. paths: /api/v1/activity/: @@ -504,6 +504,16 @@ paths: id: 13 user: 3 username: Anna + age: 19 + languages: + - id: 4 + language: Albanian + level: Fluent + - id: 6 + language: German + level: Learning + gender: D + slogan: Christmas is coming to town description: Want to go to the Train Museum interests: - id: 2 @@ -515,6 +525,16 @@ paths: id: 2 user: 2 username: Betty + age: 25 + languages: + - id: 2 + language: Albanian + level: Fluent + - id: 16 + language: English + level: Learning + gender: F + slogan: Every API has a call! description: Want to visit some kind of Museum interests: - id: 2 @@ -556,6 +576,16 @@ paths: id: 13 user: 3 username: Anna + age: 19 + languages: + - id: 4 + language: Albanian + level: Fluent + - id: 6 + language: German + level: Learning + gender: D + slogan: Christmas is coming to town description: Want to go to the Train Museum interests: - id: 2 @@ -567,6 +597,16 @@ paths: id: 2 user: 2 username: Betty + age: 25 + languages: + - id: 2 + language: Albanian + level: Fluent + - id: 16 + language: English + level: Learning + gender: F + slogan: Every API has a call! description: Want to visit some kind of Museum interests: - id: 2 @@ -625,6 +665,16 @@ paths: id: 13 user: 3 username: Anna + age: 19 + languages: + - id: 4 + language: Albanian + level: Fluent + - id: 6 + language: German + level: Learning + gender: D + slogan: Christmas is coming to town description: Want to go to the Train Museum interests: - id: 2 @@ -636,6 +686,16 @@ paths: id: 2 user: 2 username: Betty + age: 25 + languages: + - id: 2 + language: Albanian + level: Fluent + - id: 16 + language: English + level: Learning + gender: F + slogan: Every API has a call! description: Want to visit some kind of Museum interests: - id: 2 @@ -878,6 +938,16 @@ paths: id: 13 user: 3 username: Anna + age: 19 + languages: + - id: 4 + language: Albanian + level: Fluent + - id: 6 + language: German + level: Learning + gender: D + slogan: Christmas is coming to town description: Want to go to the Train Museum interests: - id: 2 @@ -889,6 +959,16 @@ paths: id: 2 user: 2 username: Betty + age: 25 + languages: + - id: 2 + language: Albanian + level: Fluent + - id: 16 + language: English + level: Learning + gender: F + slogan: Every API has a call! description: Want to visit some kind of Museum interests: - id: 2 @@ -1788,8 +1868,6 @@ components: PatchedUserLanguageModelRequest: type: object properties: - userprofile: - type: integer level: $ref: '#/components/schemas/LevelEnum' PatchedUserModelRequest: @@ -1868,6 +1946,17 @@ components: type: integer username: type: string + age: + type: integer + readOnly: true + languages: + type: array + items: + $ref: '#/components/schemas/UserLanguageModel' + gender: + type: string + slogan: + type: string description: type: string maxLength: 500 @@ -1881,9 +1970,13 @@ components: $ref: '#/components/schemas/ActivityModel' required: - activities + - age - description + - gender - id - interests + - languages + - slogan - user - username TimePlaceModelCreateRequest: @@ -2091,8 +2184,6 @@ components: id: type: integer readOnly: true - userprofile: - type: integer language: type: string readOnly: true @@ -2102,17 +2193,13 @@ components: - id - language - level - - userprofile UserLanguageModelRequest: type: object properties: - userprofile: - type: integer level: $ref: '#/components/schemas/LevelEnum' required: - level - - userprofile UserLogin: type: object description: ensures that both the username and password fields are present