diff --git a/cookbook/migrations/0042_cooklog.py b/cookbook/migrations/0042_cooklog.py new file mode 100644 index 00000000..a17bb216 --- /dev/null +++ b/cookbook/migrations/0042_cooklog.py @@ -0,0 +1,27 @@ +# Generated by Django 3.0.5 on 2020-05-02 14:47 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('cookbook', '0041_auto_20200502_1446'), + ] + + operations = [ + migrations.CreateModel( + name='CookLog', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('created_at', models.DateTimeField(auto_now_add=True)), + ('rating', models.IntegerField(null=True)), + ('servings', models.IntegerField(default=0)), + ('created_by', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ('recipe', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='cookbook.Recipe')), + ], + ), + ] diff --git a/cookbook/models.py b/cookbook/models.py index 7384f686..6a521c46 100644 --- a/cookbook/models.py +++ b/cookbook/models.py @@ -232,3 +232,13 @@ class MealPlan(models.Model): return self.title return str(self.recipe) + +class CookLog(models.Model): + recipe = models.ForeignKey(Recipe, on_delete=models.CASCADE) + created_by = models.ForeignKey(User, on_delete=models.CASCADE) + created_at = models.DateTimeField(auto_now_add=True) + rating = models.IntegerField(null=True) + servings = models.IntegerField(default=0) + + def __str__(self): + return self.recipe.name diff --git a/cookbook/tables.py b/cookbook/tables.py index a0466c9a..b9ace14b 100644 --- a/cookbook/tables.py +++ b/cookbook/tables.py @@ -26,7 +26,7 @@ class RecipeTableSmall(tables.Table): class RecipeTable(tables.Table): - edit = tables.TemplateColumn("" + _('Edit') + "") + edit = tables.TemplateColumn("" + _('Edit') + "") name = tables.LinkColumn('view_recipe', args=[A('id')]) all_tags = tables.Column( attrs={'td': {'class': 'd-none d-lg-table-cell'}, 'th': {'class': 'd-none d-lg-table-cell'}}) diff --git a/cookbook/templates/include/log_cooking.html b/cookbook/templates/include/log_cooking.html new file mode 100644 index 00000000..725f2213 --- /dev/null +++ b/cookbook/templates/include/log_cooking.html @@ -0,0 +1,78 @@ +{% load i18n %} + + + + \ No newline at end of file diff --git a/cookbook/templates/include/recipe_open_modal.html b/cookbook/templates/include/recipe_open_modal.html deleted file mode 100644 index 6cbb78ac..00000000 --- a/cookbook/templates/include/recipe_open_modal.html +++ /dev/null @@ -1,79 +0,0 @@ -{% load i18n %} - - - - - - - \ No newline at end of file diff --git a/cookbook/templates/index.html b/cookbook/templates/index.html index 2e1c547a..cee08e9c 100644 --- a/cookbook/templates/index.html +++ b/cookbook/templates/index.html @@ -85,4 +85,5 @@ {% endif %} + {% include 'include/log_cooking.html' %} {% endblock %} \ No newline at end of file diff --git a/cookbook/templates/recipes_table.html b/cookbook/templates/recipes_table.html index 2822861a..0966db4f 100644 --- a/cookbook/templates/recipes_table.html +++ b/cookbook/templates/recipes_table.html @@ -45,6 +45,7 @@ {% trans 'External' %} {% endif %} {{ row.cells.edit }} + {% trans 'Log' %}

diff --git a/cookbook/urls.py b/cookbook/urls.py index ae08750e..1d0f9c48 100644 --- a/cookbook/urls.py +++ b/cookbook/urls.py @@ -39,8 +39,8 @@ urlpatterns = [ path('api/get_external_file_link//', api.get_external_file_link, name='api_get_external_file_link'), path('api/get_recipe_file//', api.get_recipe_file, name='api_get_recipe_file'), - path('api/sync_all/', api.sync_all, name='api_sync'), + path('api/log_cooking//', api.log_cooking, name='api_log_cooking'), path('dal/keyword/', dal.KeywordAutocomplete.as_view(), name='dal_keyword'), path('dal/ingredient/', dal.IngredientsAutocomplete.as_view(), name='dal_ingredient'), diff --git a/cookbook/views/api.py b/cookbook/views/api.py index cabd66d4..ed7d6537 100644 --- a/cookbook/views/api.py +++ b/cookbook/views/api.py @@ -1,10 +1,14 @@ +import re + +from annoying.decorators import ajax_request +from annoying.functions import get_object_or_None from django.contrib import messages from django.http import HttpResponse from django.shortcuts import redirect from django.utils.translation import gettext as _ from cookbook.helper.permission_helper import group_required -from cookbook.models import Recipe, Sync, Storage +from cookbook.models import Recipe, Sync, Storage, CookLog from cookbook.provider.dropbox import Dropbox from cookbook.provider.nextcloud import Nextcloud @@ -64,3 +68,22 @@ def sync_all(request): else: messages.add_message(request, messages.ERROR, _('Error synchronizing with Storage')) return redirect('list_recipe_import') + + +@group_required('user') +@ajax_request +def log_cooking(request, recipe_id): + recipe = get_object_or_None(Recipe, id=recipe_id) + if recipe: + log = CookLog.objects.create(created_by=request.user, recipe=recipe) + servings = request.GET['s'] if 's' in request.GET else None + if servings and re.match(r'^([1-9])+$', servings): + log.servings = int(servings) + + rating = request.GET['r'] if 'r' in request.GET else None + if rating and re.match(r'^([1-9])+$', rating): + log.rating = int(rating) + log.save() + return {'msg': 'updated successfully'} + + return {'error': 'recipe does not exist'}