diff --git a/shimatta_kenkyusho/parts/forms.py b/shimatta_kenkyusho/parts/forms.py index f0ac349..fbbf9fd 100644 --- a/shimatta_kenkyusho/parts/forms.py +++ b/shimatta_kenkyusho/parts/forms.py @@ -91,7 +91,7 @@ class AutocompleteForeingKeyField(forms.UUIDField): class MyTestForm(forms.Form): pass -class AddSubStorageForm(forms.Form): +class ChangeStorageForm(forms.Form): storage_name = forms.CharField(label="storage_name", initial='') responsible = AutocompleteForeingKeyField(api_search_url='user-list', image_field_name=None, @@ -100,6 +100,8 @@ class AddSubStorageForm(forms.Form): prepend='@') is_template = forms.BooleanField(label='is_template', required=False) + +class AddSubStorageForm(ChangeStorageForm): template = AutocompleteForeingKeyField(api_search_url='storage-template-list', image_field_name=None, foreign_model=parts_models.Storage, diff --git a/shimatta_kenkyusho/parts/views.py b/shimatta_kenkyusho/parts/views.py index c3482db..81d8a25 100644 --- a/shimatta_kenkyusho/parts/views.py +++ b/shimatta_kenkyusho/parts/views.py @@ -482,6 +482,11 @@ class StockViewDetail(LoginRequiredMixin, BaseTemplateMixin, DetailView): add_storage_form = AddSubStorageForm() add_storage_form.fields['responsible'].initial = self.request.user.id context['add_storage_form'] = add_storage_form + change_storage_form = ChangeStorageForm() + change_storage_form.fields['storage_name'].initial = self.object.name + change_storage_form.fields['responsible'].initial = self.object.responsible.id + change_storage_form.fields['is_template'].initial = self.object.is_template + context['change_storage_form'] = change_storage_form context['delete_storage_error'] = None context['add_stock_form'] = AddStockForm() @@ -503,6 +508,21 @@ class StockViewDetail(LoginRequiredMixin, BaseTemplateMixin, DetailView): context['add_storage_form'] = f return self.render_to_response(context) + def handle_change_storage_post(self, request, **kwargs): + f = ChangeStorageForm(data=request.POST) + if f.is_valid(): + sub_name = f.cleaned_data['storage_name'] + try: + self.object.name = f.cleaned_data['storage_name'] + self.object.responsible = f.cleaned_data['responsible'] + self.object.is_template = f.cleaned_data['is_template'] + self.object.save() + except ValidationError as v_err: + f.add_error('storage_name', '. '.join(v_err.messages)) + context = self.get_context_data(**kwargs) + context['change_storage_form'] = f + return self.render_to_response(context) + def handle_del_storage_post(self, request, **kwargs): parent = self.object.parent_storage try: @@ -578,6 +598,8 @@ class StockViewDetail(LoginRequiredMixin, BaseTemplateMixin, DetailView): if 'submit-add-storage' in request.POST: return self.handle_add_storage_post(request, **kwargs) + if 'submit-change-storage' in request.POST: + return self.handle_change_storage_post(request, **kwargs) elif 'submit-delete-storage' in request.POST: return self.handle_del_storage_post(request, **kwargs) elif 'submit-delete-stock' in request.POST: diff --git a/shimatta_kenkyusho/templates/parts/modals/substorage-modal.html b/shimatta_kenkyusho/templates/parts/modals/add-substorage-modal.html similarity index 100% rename from shimatta_kenkyusho/templates/parts/modals/substorage-modal.html rename to shimatta_kenkyusho/templates/parts/modals/add-substorage-modal.html diff --git a/shimatta_kenkyusho/templates/parts/modals/change-storage-modal.html b/shimatta_kenkyusho/templates/parts/modals/change-storage-modal.html new file mode 100644 index 0000000..6a13675 --- /dev/null +++ b/shimatta_kenkyusho/templates/parts/modals/change-storage-modal.html @@ -0,0 +1,22 @@ +{% load static %} +{% load crispy_forms_tags %} + \ No newline at end of file diff --git a/shimatta_kenkyusho/templates/parts/stocks-detail.html b/shimatta_kenkyusho/templates/parts/stocks-detail.html index 80202ce..4276312 100644 --- a/shimatta_kenkyusho/templates/parts/stocks-detail.html +++ b/shimatta_kenkyusho/templates/parts/stocks-detail.html @@ -27,6 +27,7 @@ {% endif %} +
{% for storage in storages %} @@ -109,11 +110,11 @@ {% endfor %} {% with add_storage_form as form %} - {% include 'parts/modals/substorage-modal.html' %} + {% include 'parts/modals/add-substorage-modal.html' %} {% endwith %} {% with change_storage_form as form %} - {% include 'parts/modals/substorage-modal.html' %} + {% include 'parts/modals/change-storage-modal.html' %} {% endwith %} {% with delete_storage_errors as err_msgs %} diff --git a/shimatta_kenkyusho/templates/parts/stocks.html b/shimatta_kenkyusho/templates/parts/stocks.html index 23a8311..b0da901 100644 --- a/shimatta_kenkyusho/templates/parts/stocks.html +++ b/shimatta_kenkyusho/templates/parts/stocks.html @@ -53,8 +53,8 @@
- {% with add_storage_form as form %} - {% include 'parts/modals/substorage-modal.html' %} + {% with add_storage_form as form id as "add-sub-modal"%} + {% include 'parts/modals/add-substorage-modal.html' %} {% endwith %}