Skip to content

Commit

Permalink
docs: updated docstrings in goods app
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-lymar committed Jun 29, 2024
1 parent b7548c6 commit 844589b
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 1 deletion.
2 changes: 2 additions & 0 deletions clients/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class DirectorPosition(models.Model):
"""
Model representing a director's position.
"""

director_position = models.CharField(max_length=40, verbose_name="Должность директора")

class Meta:
Expand All @@ -22,6 +23,7 @@ class Client(models.Model):
"""
Model representing a client organization.
"""

# Main Information
client_name = models.CharField(
max_length=100, verbose_name="Наименование организации", db_index=True
Expand Down
4 changes: 3 additions & 1 deletion clients/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class ClientSerializer(serializers.ModelSerializer[Client]):
"""
Serializer for the Client model.
Serializes all fields of the Client model including nested serialization of 'director_position'.
Serializes all fields of the Client model
including nested serialization of 'director_position'.
Adds 'destination_city' and 'railway_station' as CharField serializers.
Sets the current authenticated user as the value for the 'user' field using HiddenField.
Expand All @@ -32,6 +33,7 @@ class ClientSerializer(serializers.ModelSerializer[Client]):
Note: 'user' field is automatically populated with the current user making the request.
"""

director_position = DirectorPositionSerializer()
destination_city: serializers.CharField = serializers.CharField()
railway_station: serializers.CharField = serializers.CharField()
Expand Down
4 changes: 4 additions & 0 deletions clients/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class ClientAPIView(generics.ListCreateAPIView[Client]):
Retrieves a list of clients with related director position, destination city,
and railway station. Caches the client list for 15 minutes if not already cached.
"""

queryset = Client.objects.select_related(
"director_position", "destination_city", "railway_station"
).all()
Expand Down Expand Up @@ -41,6 +42,7 @@ class ClientAPIUpdateView(generics.RetrieveUpdateAPIView[Client]):
Retrieves and updates a specific client record based on its primary key.
Requires ClientAccessPermission for authorization.
"""

queryset = Client.objects.all()
serializer_class = ClientSerializer
permission_classes = (ClientAccessPermission,)
Expand All @@ -53,6 +55,7 @@ class ClientAPIDeleteView(generics.DestroyAPIView[Client]):
Deletes a specific client record based on its primary key.
Requires ClientAccessPermission for authorization.
"""

queryset = Client.objects.all()
serializer_class = ClientSerializer
permission_classes = (ClientAccessPermission,)
Expand All @@ -65,6 +68,7 @@ class DirectorPositionListView(generics.ListAPIView[DirectorPosition]):
Retrieves a list of all available director positions.
Requires authentication (IsAuthenticated).
"""

queryset = DirectorPosition.objects.all()
serializer_class = DirectorPositionSerializer
permission_classes = (IsAuthenticated,)
17 changes: 17 additions & 0 deletions goods/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@


class Product(models.Model):
"""
A model representing a product with specific
attributes like flour type, brand, package, and price.
"""

flour_name = models.ForeignKey(
"Flour", on_delete=models.PROTECT, related_name="flour_goods", db_index=True
)
Expand All @@ -28,6 +33,10 @@ def __str__(self) -> str:


class Flour(models.Model):
"""
A model representing the type of flour used in products.
"""

flour_name = models.CharField(max_length=255, blank=False)

class Meta:
Expand All @@ -40,6 +49,10 @@ def __str__(self) -> str:


class Brand(models.Model):
"""
A model representing a brand associated with products.
"""

brand = models.CharField(max_length=100, verbose_name="Брэнд", blank=True)

class Meta:
Expand All @@ -52,6 +65,10 @@ def __str__(self) -> str:


class Package(models.Model):
"""
A model representing packaging details associated with products.
"""

package = models.IntegerField(verbose_name="Тара")
factory = models.ForeignKey(Factory, on_delete=models.PROTECT)
pallet_weight = models.PositiveIntegerField(verbose_name="Вес на паллете")
Expand Down
4 changes: 4 additions & 0 deletions goods/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@


class GoodsSerializer(serializers.ModelSerializer[Product]):
"""
Serializer for serializing/deserializing Product objects.
"""

flour_name: serializers.CharField = serializers.CharField()
brand: serializers.CharField = serializers.CharField()
package: serializers.CharField = serializers.CharField()
Expand Down
8 changes: 8 additions & 0 deletions goods/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,19 @@


class GoodsViewSet(viewsets.ModelViewSet[Product]):
"""
A viewset for managing CRUD operations for products.
Retrieves a list of products with caching enabled for 30 minutes.
"""

queryset = Product.objects.select_related("flour_name", "brand", "package").all()
serializer_class = GoodsSerializer
permission_classes = (IsAuthenticated,)

def get_queryset(self):
"""
Override the default queryset to utilize caching for better performance.
"""
cached_goods = cache.get("goods_list")
if cached_goods:
return cached_goods
Expand Down

0 comments on commit 844589b

Please sign in to comment.