Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mat matchserializer #74

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
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
30 changes: 30 additions & 0 deletions apps/match/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
{
Expand All @@ -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": [
{
Expand Down
20 changes: 18 additions & 2 deletions apps/timeplace/serializers.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -162,14 +162,30 @@ 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
fields = [
"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
8 changes: 8 additions & 0 deletions apps/user/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
1 change: 0 additions & 1 deletion apps/user/tests/ut/test_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down
4 changes: 3 additions & 1 deletion apps/user/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion config/settings/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
105 changes: 96 additions & 9 deletions schema.yml
Original file line number Diff line number Diff line change
@@ -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/:
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -1788,8 +1868,6 @@ components:
PatchedUserLanguageModelRequest:
type: object
properties:
userprofile:
type: integer
level:
$ref: '#/components/schemas/LevelEnum'
PatchedUserModelRequest:
Expand Down Expand Up @@ -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
Expand All @@ -1881,9 +1970,13 @@ components:
$ref: '#/components/schemas/ActivityModel'
required:
- activities
- age
- description
- gender
- id
- interests
- languages
- slogan
- user
- username
TimePlaceModelCreateRequest:
Expand Down Expand Up @@ -2091,8 +2184,6 @@ components:
id:
type: integer
readOnly: true
userprofile:
type: integer
language:
type: string
readOnly: true
Expand All @@ -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
Expand Down