added comment field and a recipe filter to cook log

This commit is contained in:
vabene1111 2024-02-24 13:18:08 +01:00
parent ae70064c06
commit 59ecc40dc6
4 changed files with 47 additions and 4 deletions

View File

@ -0,0 +1,33 @@
# Generated by Django 4.2.7 on 2024-02-24 12:09
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('cookbook', '0213_remove_property_property_unique_import_food_per_space_and_more'),
]
operations = [
migrations.AddField(
model_name='cooklog',
name='comment',
field=models.TextField(blank=True, null=True),
),
migrations.AddField(
model_name='cooklog',
name='updated_at',
field=models.DateTimeField(auto_now=True),
),
migrations.AlterField(
model_name='cooklog',
name='rating',
field=models.IntegerField(blank=True, null=True),
),
migrations.AlterField(
model_name='cooklog',
name='servings',
field=models.IntegerField(blank=True, null=True),
),
]

View File

@ -1293,10 +1293,13 @@ class TelegramBot(models.Model, PermissionModelMixin):
class CookLog(ExportModelOperationsMixin('cook_log'), models.Model, PermissionModelMixin): class CookLog(ExportModelOperationsMixin('cook_log'), models.Model, PermissionModelMixin):
recipe = models.ForeignKey(Recipe, on_delete=models.CASCADE) recipe = models.ForeignKey(Recipe, on_delete=models.CASCADE)
rating = models.IntegerField(null=True, blank=True)
servings = models.IntegerField(null=True, blank=True)
comment = models.TextField(null=True, blank=True)
created_by = models.ForeignKey(User, on_delete=models.CASCADE) created_by = models.ForeignKey(User, on_delete=models.CASCADE)
created_at = models.DateTimeField(default=timezone.now) created_at = models.DateTimeField(default=timezone.now)
rating = models.IntegerField(null=True) updated_at = models.DateTimeField(auto_now=True)
servings = models.IntegerField(default=0)
space = models.ForeignKey(Space, on_delete=models.CASCADE) space = models.ForeignKey(Space, on_delete=models.CASCADE)
objects = ScopedManager(space='space') objects = ScopedManager(space='space')

View File

@ -873,7 +873,7 @@ class CommentSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = Comment model = Comment
fields = '__all__' fields = '__all__'
read_only_fields = ['id', 'created_at', 'created_by', 'updated_at',] read_only_fields = ['id', 'created_at', 'created_by', 'updated_at', ]
class RecipeOverviewSerializer(RecipeBaseSerializer): class RecipeOverviewSerializer(RecipeBaseSerializer):
@ -1200,6 +1200,8 @@ class ShareLinkSerializer(SpacedModelSerializer):
class CookLogSerializer(serializers.ModelSerializer): class CookLogSerializer(serializers.ModelSerializer):
created_by = UserSerializer(read_only=True)
def create(self, validated_data): def create(self, validated_data):
validated_data['created_by'] = self.context['request'].user validated_data['created_by'] = self.context['request'].user
validated_data['space'] = self.context['request'].space validated_data['space'] = self.context['request'].space
@ -1207,7 +1209,7 @@ class CookLogSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = CookLog model = CookLog
fields = ('id', 'recipe', 'servings', 'rating', 'created_by', 'created_at') fields = ('id', 'recipe', 'servings', 'rating', 'comment', 'created_by', 'created_at', 'updated_at')
read_only_fields = ('id', 'created_by') read_only_fields = ('id', 'created_by')

View File

@ -1270,8 +1270,13 @@ class CookLogViewSet(viewsets.ModelViewSet):
serializer_class = CookLogSerializer serializer_class = CookLogSerializer
permission_classes = [CustomIsOwner & CustomTokenHasReadWriteScope] permission_classes = [CustomIsOwner & CustomTokenHasReadWriteScope]
pagination_class = DefaultPagination pagination_class = DefaultPagination
query_params = [
QueryParam(name='recipe', description=_('Filter for entries with the given recipe'), qtype='integer'),
]
def get_queryset(self): def get_queryset(self):
if self.request.query_params.get('recipe', None):
self.queryset = self.queryset.filter(recipe=self.request.query_params.get('recipe'))
return self.queryset.filter(space=self.request.space) return self.queryset.filter(space=self.request.space)