Skip to content

Commit

Permalink
Merge pull request #56 from Alschn/chore/rename-serializers
Browse files Browse the repository at this point in the history
Rename model serializers so that they all start with model's name
  • Loading branch information
Alschn authored Apr 28, 2024
2 parents fe067bb + a5e1f2a commit f8f1278
Show file tree
Hide file tree
Showing 15 changed files with 139 additions and 105 deletions.
10 changes: 5 additions & 5 deletions backend/beers/serializers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
from .beer import (
BeerSerializer,
BeerCreateSerializer,
SimplifiedBeerSerializer,
BeerSimplifiedSerializer,
BeerRepresentationalSerializer,
BeerWithResultsSerializer,
DetailedBeerSerializer,
BeerDetailedSerializer,
)
from .beer_style import (
BeerStyleListSerializer,
BeerStyleDetailSerializer,
EmbeddedBeerStyleSerializer
BeerStyleEmbeddedSerializer
)
from .brewery import (
BrewerySerializer, EmbeddedBrewerySerializer,
BrewerySerializer, BreweryEmbeddedSerializer,
)
from .hop import (
HopSerializer, EmbeddedHopsSerializer
HopSerializer, HopEmbeddedSerializer
)
75 changes: 44 additions & 31 deletions backend/beers/serializers/beer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
from rest_framework.relations import StringRelatedField

from beers.models import Beer, Hop
from beers.serializers.beer_style import EmbeddedBeerStyleSerializer
from beers.serializers.brewery import EmbeddedBrewerySerializer
from beers.serializers.hop import EmbeddedHopsSerializer
from beers.serializers.beer_style import BeerStyleEmbeddedSerializer
from beers.serializers.brewery import BreweryEmbeddedSerializer
from beers.serializers.hop import HopEmbeddedSerializer
from rooms.models import BeerInRoom


Expand Down Expand Up @@ -41,22 +41,18 @@ def to_representation(self, instance: Beer) -> dict:
return representation


class SimplifiedBeerSerializer(serializers.ModelSerializer):
class BeerSimplifiedSerializer(serializers.ModelSerializer):
brewery = StringRelatedField(read_only=True)
style = StringRelatedField(read_only=True)

class Meta:
model = Beer
fields = ('id', 'image', 'name', 'brewery', 'style')


class BeerRepresentationalSerializer(SimplifiedBeerSerializer):
class Meta:
model = Beer
fields = (
'id', 'name', 'brewery', 'style', 'percentage',
'hop_rate', 'extract', 'IBU',
'image', 'description'
'id',
'name',
'brewery',
'style',
'image',
)

def to_representation(self, instance):
Expand All @@ -65,34 +61,39 @@ def to_representation(self, instance):
return representation


class BeerWithResultsSerializer(serializers.ModelSerializer):
beer = SimplifiedBeerSerializer()
average_rating = serializers.DecimalField(
max_digits=4, decimal_places=2,
min_value=Decimal(0), max_value=Decimal(10),
)

class BeerRepresentationalSerializer(BeerSimplifiedSerializer):
class Meta:
model = BeerInRoom
fields = ('order', 'beer', 'average_rating')
model = Beer
fields = (
'id',
'name',
'brewery',
'style',
'percentage',
'hop_rate',
'extract',
'IBU',
'image',
'description'
)


class DetailedBeerSerializer(BeerSerializer):
class BeerDetailedSerializer(BeerSerializer):
"""
BeerSerializer with serialized relationships fields.
Used when handling GET method (list/retrieve action).
"""
hops = EmbeddedHopsSerializer(many=True, read_only=True)
style = EmbeddedBeerStyleSerializer(read_only=True)
brewery = EmbeddedBrewerySerializer(read_only=True)
hops = HopEmbeddedSerializer(many=True, read_only=True)
style = BeerStyleEmbeddedSerializer(read_only=True)
brewery = BreweryEmbeddedSerializer(read_only=True)


class BeerEmbeddedSerializer(BeerSerializer):
"""
BeerSerializer with serialized brewery and style related fields.
"""
style = EmbeddedBeerStyleSerializer(read_only=True)
brewery = EmbeddedBrewerySerializer(read_only=True)
style = BeerStyleEmbeddedSerializer(read_only=True)
brewery = BreweryEmbeddedSerializer(read_only=True)


class BeerCreateSerializer(serializers.ModelSerializer):
Expand Down Expand Up @@ -123,9 +124,21 @@ def create(self, validated_data: dict) -> Beer:
return instance


class BeerInRatingSerializer(SimplifiedBeerSerializer):
# todo: separate serializer for beer embedded in rating
pass
class BeerWithResultsSerializer(serializers.ModelSerializer):
beer = BeerSimplifiedSerializer()
average_rating = serializers.DecimalField(
max_digits=4, decimal_places=2,
min_value=Decimal(0), max_value=Decimal(10),
)

class Meta:
model = BeerInRoom
fields = (
'id',
'order',
'beer',
'average_rating'
)


def build_file_url(url: str | None, request: WSGIRequest) -> str | None:
Expand Down
2 changes: 1 addition & 1 deletion backend/beers/serializers/beer_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Meta:
)


class EmbeddedBeerStyleSerializer(serializers.ModelSerializer):
class BeerStyleEmbeddedSerializer(serializers.ModelSerializer):
class Meta:
model = BeerStyle
fields = (
Expand Down
17 changes: 13 additions & 4 deletions backend/beers/serializers/brewery.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,24 @@ class Meta:
)


class SimplifiedBrewerySerializer(serializers.ModelSerializer):
class BrewerySimplifiedSerializer(serializers.ModelSerializer):
country = CountryField()

class Meta:
model = Brewery
fields = ('id', 'name', 'city', 'country', 'image')
fields = (
'id',
'name',
'city',
'country',
'image'
)


class EmbeddedBrewerySerializer(serializers.ModelSerializer):
class BreweryEmbeddedSerializer(serializers.ModelSerializer):
class Meta:
model = Brewery
fields = ('id', 'name')
fields = (
'id',
'name'
)
2 changes: 1 addition & 1 deletion backend/beers/serializers/hop.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Meta:
)


class EmbeddedHopsSerializer(serializers.ModelSerializer):
class HopEmbeddedSerializer(serializers.ModelSerializer):
class Meta:
model = Hop
fields = (
Expand Down
4 changes: 2 additions & 2 deletions backend/beers/tests/test_beers_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
Hop, Brewery
)
from beers.serializers import (
DetailedBeerSerializer,
BeerDetailedSerializer,
BeerSerializer,
)
from core.shared.unit_tests import APITestCase
Expand Down Expand Up @@ -57,7 +57,7 @@ def test_list_beers(self):
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(json_response['count'], beers.count())
self.assertEqual(
first=DetailedBeerSerializer(beers, many=True).data,
first=BeerDetailedSerializer(beers, many=True).data,
second=json_response['results']
)

Expand Down
4 changes: 2 additions & 2 deletions backend/beers/views/beers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from beers.serializers import (
BeerSerializer,
BeerCreateSerializer,
DetailedBeerSerializer,
BeerDetailedSerializer,
)
from core.shared.pagination import page_number_pagination_factory

Expand Down Expand Up @@ -45,7 +45,7 @@ def get_queryset(self) -> QuerySet[Beer]:

def get_serializer_class(self):
if self.action in ['list', 'retrieve']:
return DetailedBeerSerializer
return BeerDetailedSerializer

if self.action == 'create':
return BeerCreateSerializer
Expand Down
25 changes: 11 additions & 14 deletions backend/ratings/serializers/rating.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@

from beers.models import Beer
from beers.serializers.beer import (
DetailedBeerSerializer,
BeerInRatingSerializer,
SimplifiedBeerSerializer
BeerDetailedSerializer,
BeerSimplifiedSerializer
)
from purchases.models import BeerPurchase
from purchases.serializers import BeerPurchaseSimplifiedSerializer
Expand All @@ -30,22 +29,20 @@ class Meta:
)

def to_representation(self, instance: Rating):
my_fields = self.Meta.fields
data = super().to_representation(instance)

# Replace None with empty string, so that client does not receive nulls
for field in my_fields:
try:
if data[field] is None:
data[field] = ""
except KeyError:
pass
# todo: move this elsewhere
for key, value in data.items():
if value is None:
data[key] = ""

return data


class RatingListSerializer(serializers.ModelSerializer):
added_by = UserSerializer()
beer = BeerInRatingSerializer()
beer = BeerSimplifiedSerializer()
beer_purchase = BeerPurchaseSimplifiedSerializer()
room = RoomListSerializer()

Expand All @@ -70,7 +67,7 @@ class Meta:

class RatingDetailSerializer(serializers.ModelSerializer):
added_by = UserSerializer()
beer = DetailedBeerSerializer()
beer = BeerDetailedSerializer()
beer_purchase = BeerPurchaseSimplifiedSerializer()
room = RoomListSerializer()

Expand Down Expand Up @@ -136,7 +133,7 @@ def validate(self, attrs: dict) -> dict:
class RatingUpdateSerializer(serializers.ModelSerializer):
# same as in RatingListSerializer, so that the types match
added_by = UserSerializer()
beer = BeerInRatingSerializer()
beer = BeerSimplifiedSerializer()
beer_purchase = BeerPurchaseSimplifiedSerializer()
room = RoomListSerializer()

Expand Down Expand Up @@ -169,7 +166,7 @@ class Meta:


class RatingWithSimplifiedBeerSerializer(serializers.ModelSerializer):
beer = SimplifiedBeerSerializer()
beer = BeerSimplifiedSerializer()

class Meta:
model = Rating
Expand Down
Loading

0 comments on commit f8f1278

Please sign in to comment.