cleand up new views
This commit is contained in:
@ -4,9 +4,6 @@ from django.utils.translation import gettext as _
|
|||||||
from django import forms
|
from django import forms
|
||||||
from .models import *
|
from .models import *
|
||||||
|
|
||||||
# noinspection PyPackageRequirements
|
|
||||||
from dal import autocomplete
|
|
||||||
|
|
||||||
|
|
||||||
class RecipeForm(forms.ModelForm):
|
class RecipeForm(forms.ModelForm):
|
||||||
class Meta:
|
class Meta:
|
||||||
@ -19,10 +16,6 @@ class RecipeForm(forms.ModelForm):
|
|||||||
'keywords': _('Keywords'),
|
'keywords': _('Keywords'),
|
||||||
}
|
}
|
||||||
|
|
||||||
help_texts = {
|
|
||||||
'keywords': _('Ctrl+Click to select multiple keywords'),
|
|
||||||
}
|
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super(RecipeForm, self).__init__(*args, **kwargs)
|
super(RecipeForm, self).__init__(*args, **kwargs)
|
||||||
self.helper = FormHelper()
|
self.helper = FormHelper()
|
||||||
@ -30,40 +23,6 @@ class RecipeForm(forms.ModelForm):
|
|||||||
self.helper.add_input(Submit('save', _('Save'), css_class='btn-primary'))
|
self.helper.add_input(Submit('save', _('Save'), css_class='btn-primary'))
|
||||||
|
|
||||||
|
|
||||||
class CategoryForm(forms.ModelForm):
|
|
||||||
class Meta:
|
|
||||||
model = Category
|
|
||||||
fields = ('name', 'description')
|
|
||||||
|
|
||||||
labels = {
|
|
||||||
'name': _('Name'),
|
|
||||||
'description': _('Description'),
|
|
||||||
}
|
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
|
||||||
super(CategoryForm, self).__init__(*args, **kwargs)
|
|
||||||
self.helper = FormHelper()
|
|
||||||
self.helper.form_method = 'post'
|
|
||||||
self.helper.add_input(Submit('save', _('Save'), css_class='btn-primary'))
|
|
||||||
|
|
||||||
|
|
||||||
class KeywordForm(forms.ModelForm):
|
|
||||||
class Meta:
|
|
||||||
model = Keyword
|
|
||||||
fields = ('name', 'description')
|
|
||||||
|
|
||||||
labels = {
|
|
||||||
'name': _('Name'),
|
|
||||||
'description': _('Description'),
|
|
||||||
}
|
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
|
||||||
super(KeywordForm, 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 EditRecipeForm(forms.ModelForm):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Recipe
|
model = Recipe
|
||||||
|
@ -53,8 +53,8 @@
|
|||||||
{% trans 'New' %}
|
{% trans 'New' %}
|
||||||
</a>
|
</a>
|
||||||
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
|
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
|
||||||
<a class="dropdown-item" href="{% url 'new_recipe' %}"><i
|
<!--<a class="dropdown-item" href="{% url 'new_recipe' %}"><i
|
||||||
class="fas fa-book"></i> {% trans 'Recipe' %}</a>
|
class="fas fa-book"></i> {% trans 'Recipe' %}</a>-->
|
||||||
<a class="dropdown-item" href="{% url 'new_category' %}"><i
|
<a class="dropdown-item" href="{% url 'new_category' %}"><i
|
||||||
class="fas fa-archive"></i> {% trans 'Category' %}</a>
|
class="fas fa-archive"></i> {% trans 'Category' %}</a>
|
||||||
<a class="dropdown-item" href="{% url 'new_keyword' %}"><i
|
<a class="dropdown-item" href="{% url 'new_keyword' %}"><i
|
||||||
|
25
cookbook/templates/generic/new_template.html
Normal file
25
cookbook/templates/generic/new_template.html
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
{% extends "base.html" %}
|
||||||
|
{% load crispy_forms_tags %}
|
||||||
|
{% load i18n %}
|
||||||
|
|
||||||
|
{% block title %}{% trans 'New' %} - {{ title }}{% endblock %}
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
|
||||||
|
<h3>{% trans 'New' %} {{ title }}</h3>
|
||||||
|
|
||||||
|
<form action="." method="post">
|
||||||
|
{% csrf_token %}
|
||||||
|
{{ form|crispy }}
|
||||||
|
<input type="submit" value="Submit" class="btn btn-success">
|
||||||
|
</form>
|
||||||
|
|
||||||
|
|
||||||
|
<script>
|
||||||
|
//converts multiselct in recipe edit to searchable multiselect
|
||||||
|
//shitty solution that needs to be redone at some point
|
||||||
|
$(document).ready(function () {
|
||||||
|
$('#id_keywords').select2();
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
{% endblock %}
|
@ -8,9 +8,9 @@ urlpatterns = [
|
|||||||
path('', views.index, name='index'),
|
path('', views.index, name='index'),
|
||||||
path('test', views.test, name='test'),
|
path('test', views.test, name='test'),
|
||||||
|
|
||||||
path('new/recipe', new.recipe, name='new_recipe'),
|
path('new/recipe', new.RecipeCreate.as_view(), name='new_recipe'),
|
||||||
path('new/category', new.category, name='new_category'),
|
path('new/category', new.CategoryCreate.as_view(), name='new_category'),
|
||||||
path('new/keyword', new.keyword, name='new_keyword'),
|
path('new/keyword', new.KeywordCreate.as_view(), name='new_keyword'),
|
||||||
|
|
||||||
path('list/keyword', lists.keyword_list, name='list_keyword'),
|
path('list/keyword', lists.keyword_list, name='list_keyword'),
|
||||||
path('list/category', lists.category_list, name='list_category'),
|
path('list/category', lists.category_list, name='list_category'),
|
||||||
|
@ -1,64 +1,42 @@
|
|||||||
from django.contrib import messages
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||||
from django.contrib.auth.decorators import login_required
|
from django.urls import reverse_lazy
|
||||||
from django.shortcuts import redirect, render
|
|
||||||
from django_tables2 import RequestConfig
|
|
||||||
from django.utils.translation import gettext as _
|
from django.utils.translation import gettext as _
|
||||||
|
from django.views.generic import CreateView
|
||||||
|
|
||||||
from cookbook.forms import CategoryForm, KeywordForm, RecipeForm
|
from cookbook.models import Category, Keyword, Recipe
|
||||||
from cookbook.models import Category, Keyword
|
|
||||||
from cookbook.tables import CategoryTable, KeywordTable
|
|
||||||
|
|
||||||
|
|
||||||
@login_required
|
class RecipeCreate(LoginRequiredMixin, CreateView): # this exists for completeness but is not in use at the moment
|
||||||
def recipe(request):
|
template_name = "generic\\new_template.html"
|
||||||
if request.method == "POST":
|
model = Recipe
|
||||||
form = RecipeForm(request.POST)
|
fields = ['name', 'category', 'keywords']
|
||||||
if form.is_valid():
|
success_url = reverse_lazy('list_category')
|
||||||
recipe_obj = form.save(commit=False)
|
|
||||||
recipe_obj.created_by = request.user.id
|
|
||||||
recipe_obj.save()
|
|
||||||
form.save_m2m()
|
|
||||||
messages.add_message(request, messages.SUCCESS, _('Recipe saved!'))
|
|
||||||
return redirect('index')
|
|
||||||
else:
|
|
||||||
form = RecipeForm()
|
|
||||||
|
|
||||||
return render(request, 'new/recipe.html', {'form': form})
|
def get_context_data(self, **kwargs):
|
||||||
|
context = super(RecipeCreate, self).get_context_data(**kwargs)
|
||||||
|
context['title'] = _("Recipe")
|
||||||
|
return context
|
||||||
|
|
||||||
|
|
||||||
@login_required
|
class CategoryCreate(LoginRequiredMixin, CreateView):
|
||||||
def category(request):
|
template_name = "generic\\new_template.html"
|
||||||
if request.method == "POST":
|
model = Category
|
||||||
form = CategoryForm(request.POST)
|
fields = ['name', 'description']
|
||||||
if form.is_valid():
|
success_url = reverse_lazy('list_category')
|
||||||
category_obj = form.save(commit=False)
|
|
||||||
category_obj.created_by = request.user.id
|
|
||||||
category_obj.save()
|
|
||||||
messages.add_message(request, messages.SUCCESS, _('Category saved!'))
|
|
||||||
return redirect('new_category')
|
|
||||||
else:
|
|
||||||
form = CategoryForm()
|
|
||||||
|
|
||||||
table = CategoryTable(Category.objects.all())
|
def get_context_data(self, **kwargs):
|
||||||
RequestConfig(request, paginate={'per_page': 25}).configure(table)
|
context = super(CategoryCreate, self).get_context_data(**kwargs)
|
||||||
|
context['title'] = _("Category")
|
||||||
return render(request, 'new/category.html', {'form': form, 'table': table})
|
return context
|
||||||
|
|
||||||
|
|
||||||
@login_required
|
class KeywordCreate(LoginRequiredMixin, CreateView):
|
||||||
def keyword(request):
|
template_name = "generic\\new_template.html"
|
||||||
if request.method == "POST":
|
model = Keyword
|
||||||
form = KeywordForm(request.POST)
|
fields = ['name', 'description']
|
||||||
if form.is_valid():
|
success_url = reverse_lazy('list_keyword')
|
||||||
keyword_obj = form.save(commit=False)
|
|
||||||
keyword_obj.created_by = request.user.id
|
|
||||||
keyword_obj.save()
|
|
||||||
messages.add_message(request, messages.SUCCESS, _('Keyword saved!'))
|
|
||||||
return redirect('new_keyword')
|
|
||||||
else:
|
|
||||||
form = KeywordForm()
|
|
||||||
|
|
||||||
table = KeywordTable(Keyword.objects.all())
|
def get_context_data(self, **kwargs):
|
||||||
RequestConfig(request, paginate={'per_page': 25}).configure(table)
|
context = super(KeywordCreate, self).get_context_data(**kwargs)
|
||||||
|
context['title'] = _("Keyword")
|
||||||
return render(request, 'new/keyword.html', {'form': form, 'table': table})
|
return context
|
||||||
|
Reference in New Issue
Block a user