From 9bd5e737a9b345faa612f3e78932168af098cd54 Mon Sep 17 00:00:00 2001 From: vabene1111 Date: Fri, 15 Nov 2019 14:33:24 +0100 Subject: [PATCH] recipe conversion + cleanup --- cookbook/migrations/0010_recipe_internal.py | 18 ++++++++ cookbook/models.py | 1 + cookbook/templates/recipe_view.html | 50 +++++++++++++++++---- cookbook/urls.py | 3 ++ cookbook/views/edit.py | 45 +++++++++++++++++-- cookbook/views/new.py | 1 + 6 files changed, 105 insertions(+), 13 deletions(-) create mode 100644 cookbook/migrations/0010_recipe_internal.py diff --git a/cookbook/migrations/0010_recipe_internal.py b/cookbook/migrations/0010_recipe_internal.py new file mode 100644 index 000000000..e1a1de5b8 --- /dev/null +++ b/cookbook/migrations/0010_recipe_internal.py @@ -0,0 +1,18 @@ +# Generated by Django 2.2.7 on 2019-11-15 12:55 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('cookbook', '0009_auto_20191114_1800'), + ] + + operations = [ + migrations.AddField( + model_name='recipe', + name='internal', + field=models.BooleanField(default=False), + ), + ] diff --git a/cookbook/models.py b/cookbook/models.py index e6b2a8301..dc8721942 100644 --- a/cookbook/models.py +++ b/cookbook/models.py @@ -61,6 +61,7 @@ class Recipe(models.Model): file_path = models.CharField(max_length=512, default="") link = models.CharField(max_length=512, default="") keywords = models.ManyToManyField(Keyword, blank=True) + internal = models.BooleanField(default=False) created_by = models.ForeignKey(User, on_delete=models.PROTECT) created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) diff --git a/cookbook/templates/recipe_view.html b/cookbook/templates/recipe_view.html index 20af3846d..09b7fa577 100644 --- a/cookbook/templates/recipe_view.html +++ b/cookbook/templates/recipe_view.html @@ -10,11 +10,15 @@

{{ recipe.name }}

{% if recipe.storage %} {% trans 'in' %} {{ recipe.storage.name }}

- {% else %} - {% trans 'by' %} {{ recipe.created_by.username }}

+ href="{% url 'edit_storage' recipe.storage.pk %}">{{ recipe.storage.name }}
{% endif %} + {% if recipe.internal %} + {% trans 'by' %} {{ recipe.created_by.username }}
+ {% endif %} + +
+ {% if recipe.all_tags %} {{ recipe.all_tags }}
@@ -42,10 +46,32 @@ {% endif %} {% if recipe.storage %} - {% trans 'Open recipe' %} {% trans 'View external recipe' %} {% endif %} + {% if not recipe.internal %} +
+
+
+
+
+
{% trans 'External recipe' %}
+

+ {% blocktrans %} + This is an external recipe, which means you can only view it by opening the link above. + You can convert this recipe to a fancy recipe by pressing the convert button. The original file + will still be accessible. + {% endblocktrans %}. +
+
+ {% trans 'Convert now!' %} +

+
+
+ {% endif %} +

@@ -53,21 +79,27 @@
{% csrf_token %} - {{ form|crispy }} - +
+ +
+ +
+
{% for c in comments %}
- {{ c.updated_at }} {% trans 'by' %} {{ c.created_by.username }}
+ {{ c.updated_at }} {% trans 'by' %} {{ c.created_by.username }}
{{ c.text }}
-
+
{% endfor %} - {% if not ingredients %} + {% if recipe.storage %} {% include 'include/recipe_open_modal.html' %} {% endif %} {% endblock %} \ No newline at end of file diff --git a/cookbook/urls.py b/cookbook/urls.py index 4ebf44da2..9c314d53c 100644 --- a/cookbook/urls.py +++ b/cookbook/urls.py @@ -23,11 +23,13 @@ urlpatterns = [ path('edit/recipe//', edit.switch_recipe, name='edit_recipe'), path('edit/recipe/internal//', edit.internal_recipe_update, name='edit_internal_recipe'), # for internal use only path('edit/recipe/external//', edit.RecipeUpdate.as_view(), name='edit_external_recipe'), # for internal use only + path('edit/recipe/convert//', edit.convert_recipe, name='edit_convert_recipe'), # for internal use only path('edit/keyword//', edit.KeywordUpdate.as_view(), name='edit_keyword'), path('edit/sync//', edit.SyncUpdate.as_view(), name='edit_sync'), path('edit/import//', edit.ImportUpdate.as_view(), name='edit_import'), path('edit/storage//', edit.StorageUpdate.as_view(), name='edit_storage'), + path('edit/comment//', edit.CommentUpdate.as_view(), name='edit_comment'), path('redirect/delete///', edit.delete_redirect, name='redirect_delete'), @@ -36,6 +38,7 @@ urlpatterns = [ path('delete/sync//', edit.MonitorDelete.as_view(), name='delete_sync'), path('delete/import//', edit.ImportDelete.as_view(), name='delete_import'), path('delete/storage//', edit.StorageDelete.as_view(), name='delete_storage'), + path('delete/comment//', edit.CommentDelete.as_view(), name='delete_comment'), path('data/sync', data.sync, name='data_sync'), # TODO move to generic "new" view path('data/batch/edit', data.batch_edit, name='data_batch_edit'), diff --git a/cookbook/views/edit.py b/cookbook/views/edit.py index e8178af5a..131871ee5 100644 --- a/cookbook/views/edit.py +++ b/cookbook/views/edit.py @@ -7,16 +7,25 @@ from django.urls import reverse_lazy, reverse from django.utils.translation import gettext as _ from django.views.generic import UpdateView, DeleteView -from cookbook.forms import ExternalRecipeForm, KeywordForm, StorageForm, SyncForm, InternalRecipeForm -from cookbook.models import Recipe, Sync, Keyword, RecipeImport, Storage +from cookbook.forms import ExternalRecipeForm, KeywordForm, StorageForm, SyncForm, InternalRecipeForm, CommentForm +from cookbook.models import Recipe, Sync, Keyword, RecipeImport, Storage, Comment @login_required def switch_recipe(request, pk): recipe = get_object_or_404(Recipe, pk=pk) - if recipe.storage and not recipe.instructions: - return HttpResponseRedirect(reverse('edit_external_recipe', args=[pk])) + if recipe.internal: + return HttpResponseRedirect(reverse('edit_internal_recipe', args=[pk])) else: + return HttpResponseRedirect(reverse('edit_external_recipe', args=[pk])) + + +@login_required +def convert_recipe(request, pk): + recipe = get_object_or_404(Recipe, pk=pk) + if not recipe.internal: + recipe.internal = True + recipe.save() return HttpResponseRedirect(reverse('edit_internal_recipe', args=[pk])) @@ -93,6 +102,23 @@ class StorageUpdate(LoginRequiredMixin, UpdateView): return context +class CommentUpdate(LoginRequiredMixin, UpdateView): + template_name = "generic/edit_template.html" + model = Comment + form_class = CommentForm + + # TODO add msg box + + def get_success_url(self): + return reverse('edit_comment', kwargs={'pk': self.object.pk}) + + def get_context_data(self, **kwargs): + context = super(CommentUpdate, self).get_context_data(**kwargs) + context['title'] = _("Comment") + context['view_url'] = reverse('view_recipe', args=[self.object.recipe.pk]) + return context + + class ImportUpdate(LoginRequiredMixin, UpdateView): template_name = "generic/edit_template.html" model = RecipeImport @@ -191,3 +217,14 @@ class StorageDelete(LoginRequiredMixin, DeleteView): context = super(StorageDelete, self).get_context_data(**kwargs) context['title'] = _("Storage Backend") return context + + +class CommentDelete(LoginRequiredMixin, DeleteView): + template_name = "generic/delete_template.html" + model = Comment + success_url = reverse_lazy('index') + + def get_context_data(self, **kwargs): + context = super(CommentDelete, self).get_context_data(**kwargs) + context['title'] = _("Comment") + return context diff --git a/cookbook/views/new.py b/cookbook/views/new.py index 3437c11b7..5ffa26b0f 100644 --- a/cookbook/views/new.py +++ b/cookbook/views/new.py @@ -19,6 +19,7 @@ class RecipeCreate(LoginRequiredMixin, CreateView): def form_valid(self, form): obj = form.save(commit=False) obj.created_by = self.request.user + obj.internal = True obj.save() return HttpResponseRedirect(reverse('edit_recipe', kwargs={'pk': obj.pk}))