Added form to select category in settings and update context data

This commit is contained in:
eeintech 2020-10-31 08:35:47 -05:00
parent 5310ce8465
commit 3e5d8d2b2d
5 changed files with 98 additions and 10 deletions

View File

@ -12,6 +12,7 @@ from crispy_forms.layout import Layout, Field
from crispy_forms.bootstrap import PrependedText, AppendedText, PrependedAppendedText, StrictButton, Div
from django.contrib.auth.models import User
from common.models import ColorTheme
from part.models import PartCategory
class HelperForm(forms.ModelForm):
@ -195,3 +196,36 @@ class ColorThemeSelectForm(forms.ModelForm):
css_class='row',
),
)
class SettingCategorySelectForm(forms.ModelForm):
""" Form for setting category settings """
name = forms.ChoiceField(choices=(), required=False)
class Meta:
model = PartCategory
fields = [
'name'
]
def __init__(self, *args, **kwargs):
super(SettingCategorySelectForm, self).__init__(*args, **kwargs)
# Populate category choices
self.fields['name'].choices = PartCategory.get_parent_categories()
self.helper = FormHelper()
# Form rendering
self.helper.form_show_labels = False
self.helper.layout = Layout(
Div(
Div(Field('name'),
css_class='col-sm-6',
style='width: 200px;'),
Div(StrictButton(_('Select Category'), css_class='btn btn-primary', type='submit'),
css_class='col-sm-6',
style='width: auto;'),
css_class='row',
),
)

View File

@ -36,7 +36,8 @@ from django.views.generic.base import RedirectView
from rest_framework.documentation import include_docs_urls
from .views import IndexView, SearchView, DatabaseStatsView
from .views import SettingsView, EditUserView, SetPasswordView, ColorThemeSelectView
from .views import SettingsView, EditUserView, SetPasswordView
from .views import ColorThemeSelectView, SettingCategorySelectView
from .views import DynamicJsView
from common.views import SettingEdit
@ -75,7 +76,7 @@ settings_urls = [
url(r'^global/?', SettingsView.as_view(template_name='InvenTree/settings/global.html'), name='settings-global'),
url(r'^currency/?', SettingsView.as_view(template_name='InvenTree/settings/currency.html'), name='settings-currency'),
url(r'^category/?', SettingsView.as_view(template_name='InvenTree/settings/category.html'), name='settings-category'),
url(r'^category/?', SettingCategorySelectView.as_view(), name='settings-category'),
url(r'^part/?', SettingsView.as_view(template_name='InvenTree/settings/part.html'), name='settings-part'),
url(r'^stock/?', SettingsView.as_view(template_name='InvenTree/settings/stock.html'), name='settings-stock'),
url(r'^build/?', SettingsView.as_view(template_name='InvenTree/settings/build.html'), name='settings-build'),

View File

@ -24,7 +24,8 @@ from stock.models import StockLocation, StockItem
from common.models import InvenTreeSetting, ColorTheme
from users.models import check_user_role, RuleSet
from .forms import DeleteForm, EditUserForm, SetPasswordForm, ColorThemeSelectForm
from .forms import DeleteForm, EditUserForm, SetPasswordForm
from .forms import ColorThemeSelectForm, SettingCategorySelectForm
from .helpers import str2bool
from rest_framework import views
@ -775,6 +776,44 @@ class ColorThemeSelectView(FormView):
return self.form_invalid(form)
class SettingCategorySelectView(FormView):
""" View for selecting categories in settings """
form_class = SettingCategorySelectForm
success_url = reverse_lazy('settings-category')
template_name = "InvenTree/settings/category.html"
def get_initial(self):
""" Set category selection """
initial = super(SettingCategorySelectView, self).get_initial()
category = self.request.GET.get('category', None)
if category:
initial['name'] = category
return initial
def post(self, request, *args, **kwargs):
""" Handle POST request (which contains category selection).
Pass the selected category to the page template
"""
form = self.get_form()
if form.is_valid():
category = form.cleaned_data['name']
context = self.get_context_data()
context['category'] = category
return super(SettingCategorySelectView, self).render_to_response(context)
return self.form_invalid(form)
class DatabaseStatsView(AjaxView):
""" View for displaying database statistics """

View File

@ -179,6 +179,11 @@ class PartCategory(InvenTreeTree):
return parent_categories
def get_parameter_templates(self):
""" Return parameter templates associated to category """
return PartCategoryParameterTemplate.objects.filter(category=self.id)
@receiver(pre_delete, sender=PartCategory, dispatch_uid='partcategory_delete_log')
def before_delete_part_category(sender, instance, using, **kwargs):

View File

@ -2,7 +2,7 @@
{% load i18n %}
{% block tabs %}
{% include "InvenTree/settings/tabs.html" with tab='part' %}
{% include "InvenTree/settings/tabs.html" with tab='category' %}
{% endblock %}
{% block subtitle %}
@ -11,9 +11,16 @@
{% block settings %}
<h4>{% trans "Category Parameter Templates" %}</h4>
<form action="{% url 'settings-category' %}" method="post">
{% csrf_token %}
{% load crispy_forms_tags %}
{% crispy form %}
</form>
<h5>{% trans "Category: XXX (id = 1)" %}</h5>
{% if category %}
<hr>
<h4>{% trans "Category Parameter Templates" %}</h4>
<div id='param-buttons'>
<button class='btn btn-success' id='new-param'>
@ -23,18 +30,20 @@
<table class='table table-striped table-condensed' id='param-table' data-toolbar='#param-buttons'>
</table>
{% endif %}
{% endblock %}
{% block js_ready %}
{{ block.super }}
{% if category %}
$("#new-param").click(function() {
launchModalForm("{% url 'category-param-template-create' 1 %}", {
launchModalForm("{% url 'category-param-template-create' category %}", {
success: function() {
$("#param-table").bootstrapTable('refresh');
},
});
});
{% endif %}
{% endblock %}