diff --git a/InvenTree/InvenTree/forms.py b/InvenTree/InvenTree/forms.py index 80df8914d3..707c0c5c79 100644 --- a/InvenTree/InvenTree/forms.py +++ b/InvenTree/InvenTree/forms.py @@ -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', + ), + ) diff --git a/InvenTree/InvenTree/urls.py b/InvenTree/InvenTree/urls.py index 9b74aabe5f..8006d633b5 100644 --- a/InvenTree/InvenTree/urls.py +++ b/InvenTree/InvenTree/urls.py @@ -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'), diff --git a/InvenTree/InvenTree/views.py b/InvenTree/InvenTree/views.py index 57f80f1be7..465b0dc194 100644 --- a/InvenTree/InvenTree/views.py +++ b/InvenTree/InvenTree/views.py @@ -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 """ diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index 2278993bf7..37788ae56a 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -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): diff --git a/InvenTree/templates/InvenTree/settings/category.html b/InvenTree/templates/InvenTree/settings/category.html index b643af53dd..3dc702adc1 100644 --- a/InvenTree/templates/InvenTree/settings/category.html +++ b/InvenTree/templates/InvenTree/settings/category.html @@ -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 %} -