From c9f0192a2cfdc3f2a0495b9b064c1dc08d35a829 Mon Sep 17 00:00:00 2001 From: vabene1111 Date: Sun, 6 May 2018 22:12:40 +0200 Subject: [PATCH] import process --- cookbook/forms.py | 41 ++++++------------- cookbook/tables.py | 2 +- cookbook/templates/batch/edit.html | 3 +- cookbook/templates/batch/import.html | 19 --------- cookbook/templates/batch/monitor.html | 2 + .../templates/forms/edit_import_recipe.html | 24 +++++++++++ cookbook/urls.py | 3 +- cookbook/views/new.py | 34 +++++++++++++++ 8 files changed, 76 insertions(+), 52 deletions(-) delete mode 100644 cookbook/templates/batch/import.html create mode 100644 cookbook/templates/forms/edit_import_recipe.html diff --git a/cookbook/forms.py b/cookbook/forms.py index 943a5099..cd781604 100644 --- a/cookbook/forms.py +++ b/cookbook/forms.py @@ -5,24 +5,6 @@ from django import forms from .models import * -class RecipeForm(forms.ModelForm): - class Meta: - model = Recipe - fields = ('name', 'category', 'keywords') - - labels = { - 'name': _('Name'), - 'category': _('Category'), - 'keywords': _('Keywords'), - } - - def __init__(self, *args, **kwargs): - super(RecipeForm, self).__init__(*args, **kwargs) - self.helper = FormHelper() - self.helper.form_method = 'post' - self.helper.add_input(Submit('save', _('Save'), css_class='btn-primary')) - - class EditRecipeForm(forms.ModelForm): class Meta: model = Recipe @@ -45,20 +27,21 @@ class EditRecipeForm(forms.ModelForm): class MonitorForm(forms.Form): path = forms.CharField(label=_('Path')) - def __init__(self, *args, **kwargs): - super(MonitorForm, self).__init__(*args, **kwargs) - self.helper = FormHelper() - self.helper.form_method = 'post' - self.helper.add_input(Submit('import', _('Sync'), css_class='btn-primary')) - class BatchEditForm(forms.Form): search = forms.CharField(label=_('Search String')) category = forms.ModelChoiceField(queryset=Category.objects.all().order_by('id'), required=False) keyword = forms.ModelMultipleChoiceField(queryset=Keyword.objects.all().order_by('id'), required=False) - def __init__(self, *args, **kwargs): - super(BatchEditForm, self).__init__(*args, **kwargs) - self.helper = FormHelper() - self.helper.form_method = 'post' - self.helper.add_input(Submit('update', _('Update'), css_class='btn-primary')) + +class ImportRecipeForm(forms.ModelForm): + class Meta: + model = Recipe + fields = ('name', 'category', 'keywords', 'path') + + labels = { + 'name': _('Name'), + 'category': _('Category'), + 'keywords': _('Keywords'), + 'path': _('Path'), + } diff --git a/cookbook/tables.py b/cookbook/tables.py index 76b95e81..6f30fde2 100644 --- a/cookbook/tables.py +++ b/cookbook/tables.py @@ -61,7 +61,7 @@ class MonitoredPathTable(tables.Table): class ImportTable(tables.Table): - id = tables.LinkColumn('edit_import', args=[A('id')]) + id = tables.LinkColumn('new_recipe_import', args=[A('id')]) delete = tables.TemplateColumn("" + _('Delete') + "") class Meta: diff --git a/cookbook/templates/batch/edit.html b/cookbook/templates/batch/edit.html index d60b9721..61c61608 100644 --- a/cookbook/templates/batch/edit.html +++ b/cookbook/templates/batch/edit.html @@ -14,6 +14,7 @@
{% csrf_token %} {% crispy form %} +
- + {% endblock %} \ No newline at end of file diff --git a/cookbook/templates/batch/import.html b/cookbook/templates/batch/import.html deleted file mode 100644 index f2732f76..00000000 --- a/cookbook/templates/batch/import.html +++ /dev/null @@ -1,19 +0,0 @@ -{% extends "base.html" %} -{% load i18n %} -{% load django_tables2 %} - -{% block title %}{% trans 'Imported Recipes' %}{% endblock %} - -{% block content %} - -

{% trans 'Imported Recipes' %}

-
-
- {% csrf_token %} - -
-
- - {% render_table imported_recipes %} - -{% endblock %} \ No newline at end of file diff --git a/cookbook/templates/batch/monitor.html b/cookbook/templates/batch/monitor.html index f466731d..c17b2803 100644 --- a/cookbook/templates/batch/monitor.html +++ b/cookbook/templates/batch/monitor.html @@ -16,7 +16,9 @@
{% csrf_token %} {% crispy form %} +
+

{% render_table monitored_paths %} diff --git a/cookbook/templates/forms/edit_import_recipe.html b/cookbook/templates/forms/edit_import_recipe.html new file mode 100644 index 00000000..f077fd36 --- /dev/null +++ b/cookbook/templates/forms/edit_import_recipe.html @@ -0,0 +1,24 @@ +{% extends "base.html" %} +{% load crispy_forms_tags %} +{% load i18n %} + +{% block title %}{% trans 'Import new Recipe' %}{% endblock %} + +{% block content %} + +

{% trans 'Import new Recipe' %}

+ +
+ {% csrf_token %} + {{ form|crispy }} + +
+ + +{% endblock %} \ No newline at end of file diff --git a/cookbook/urls.py b/cookbook/urls.py index d43cb444..373f1427 100644 --- a/cookbook/urls.py +++ b/cookbook/urls.py @@ -9,6 +9,7 @@ urlpatterns = [ path('test', views.test, name='test'), path('new/recipe', new.RecipeCreate.as_view(), name='new_recipe'), + path('new/recipe_import//', new.create_new_recipe, name='new_recipe_import'), path('new/category', new.CategoryCreate.as_view(), name='new_category'), path('new/keyword', new.KeywordCreate.as_view(), name='new_keyword'), @@ -37,6 +38,4 @@ urlpatterns = [ path('api/sync_all/', api.dropbox_sync, name='api_dropbox_sync'), path('dal/keyword/', dal.KeywordAutocomplete.as_view(), name='dal_keyword'), - ] - diff --git a/cookbook/views/new.py b/cookbook/views/new.py index 45bb5f5d..211e7653 100644 --- a/cookbook/views/new.py +++ b/cookbook/views/new.py @@ -1,8 +1,12 @@ +from django.contrib import messages +from django.contrib.auth.decorators import login_required from django.contrib.auth.mixins import LoginRequiredMixin +from django.shortcuts import render, redirect from django.urls import reverse_lazy from django.utils.translation import gettext as _ from django.views.generic import CreateView +from cookbook.forms import ImportRecipeForm, NewRecipe from cookbook.models import Category, Keyword, Recipe @@ -12,6 +16,9 @@ class RecipeCreate(LoginRequiredMixin, CreateView): # this exists for completen fields = ['name', 'category', 'keywords'] success_url = reverse_lazy('list_category') + def get_initial(self): + return {'value1': self.request.user} + def get_context_data(self, **kwargs): context = super(RecipeCreate, self).get_context_data(**kwargs) context['title'] = _("Recipe") @@ -40,3 +47,30 @@ class KeywordCreate(LoginRequiredMixin, CreateView): context = super(KeywordCreate, self).get_context_data(**kwargs) context['title'] = _("Keyword") return context + + +@login_required +def create_new_recipe(request, import_id): + if request.method == "POST": + form = ImportRecipeForm(request.POST) + if form.is_valid(): + recipe = Recipe() + recipe.name = form.cleaned_data['name'] + recipe.path = form.cleaned_data['path'] + recipe.category = form.cleaned_data['category'] + + recipe.save() + + recipe.keywords.set(form.cleaned_data['keywords']) + + NewRecipe.objects.get(id=import_id).delete() + + messages.add_message(request, messages.SUCCESS, _('Imported new recipe!')) + return redirect('list_import') + else: + messages.add_message(request, messages.ERROR, _('There was an error importing this recipe!')) + else: + new_recipe = NewRecipe.objects.get(id=import_id) + form = ImportRecipeForm(initial={'path': new_recipe.path, 'name': new_recipe.name}) + + return render(request, 'forms/edit_import_recipe.html', {'form': form})