diff --git a/shimatta_kenkyusho/api/serializers.py b/shimatta_kenkyusho/api/serializers.py index 183e134..93f785b 100644 --- a/shimatta_kenkyusho/api/serializers.py +++ b/shimatta_kenkyusho/api/serializers.py @@ -80,7 +80,7 @@ class StorageSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = parts_models.Storage - fields = ['url', 'id', 'name', 'verbose_name', 'parent_storage', 'responsible', 'template', 'full_path'] + fields = ['url', 'id', 'name', 'verbose_name', 'parent_storage', 'responsible', 'template', 'full_path', 'full_path_verbose'] class StorageSerializerStocksExpanded(StorageSerializer): ro_stocks = StockSerializerExpandComponent(many=True, read_only=True, source='stock_set') diff --git a/shimatta_kenkyusho/parts/forms.py b/shimatta_kenkyusho/parts/forms.py index d366146..8eaaa2f 100644 --- a/shimatta_kenkyusho/parts/forms.py +++ b/shimatta_kenkyusho/parts/forms.py @@ -157,6 +157,17 @@ class EditLotForm(EditStockBaseForm): stock.lot = lot stock.save() +class RelocateStockForm(forms.ModelForm): + storage = AutocompleteForeingKeyField(api_search_url='storage-list', + foreign_model=parts_models.Storage, + name_field_name='full_path_verbose', + image_field_name=None, + required=True) + + class Meta: + model = parts_models.Stock + fields = ['storage'] + class EditStockAmountForm(EditStockBaseForm): amount = forms.IntegerField(min_value=0) diff --git a/shimatta_kenkyusho/parts/models.py b/shimatta_kenkyusho/parts/models.py index 4355cdf..bb1280e 100644 --- a/shimatta_kenkyusho/parts/models.py +++ b/shimatta_kenkyusho/parts/models.py @@ -94,6 +94,14 @@ class Storage(models.Model): output = output + '/' + chain[i].name return output + @property + def full_path_verbose(self): + full_path = f'{self.get_full_path()} ({self.id})' + + if self.verbose_name: + full_path += f' ({self.verbose_name})' + return full_path + def get_qr_code(self): qrdata = '[stor_uuid]' + str(self.id) return qrdata @@ -108,8 +116,8 @@ class Storage(models.Model): return self.storage_set.all() def get_tree(self): - self.sub_storages = list(self.storage_set.all()) - for storage in self.sub_storages: + self.sub_storages = [self] + for storage in self.storage_set.all(): self.sub_storages += storage.get_tree() return self.sub_storages diff --git a/shimatta_kenkyusho/parts/views/storage_views.py b/shimatta_kenkyusho/parts/views/storage_views.py index 7ee8ed1..dc17ad4 100644 --- a/shimatta_kenkyusho/parts/views/storage_views.py +++ b/shimatta_kenkyusho/parts/views/storage_views.py @@ -121,14 +121,16 @@ class StockViewDetail(LoginRequiredMixin, BaseTemplateMixin, DetailView): context['storages'] = storage_paginator.get_page(storage_page) stocks = stock_paginator.get_page(componente_stock_page) context['stocks'] = stocks + context['stocks_with_forms'] = [{'object': s, 'relocate_form': RelocateStockForm(instance=s, prefix=str(s.id))} for s in stocks] context['stock_search'] = stock_search_input 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 = ChangeStorageForm(prefix='change_storage') change_storage_form.fields['storage_name'].initial = self.object.name change_storage_form.fields['verbose_name'].initial = self.object.verbose_name change_storage_form.fields['responsible'].initial = self.object.responsible.id + change_storage_form.fields['expand_sub_storage_stocks'].initial = self.object.expand_sub_storage_stocks change_storage_form.fields['is_template'].initial = self.object.is_template context['change_storage_form'] = change_storage_form context['delete_storage_error'] = None @@ -155,7 +157,7 @@ class StockViewDetail(LoginRequiredMixin, BaseTemplateMixin, DetailView): return self.render_to_response(context) def handle_change_storage_post(self, request, **kwargs): - f = ChangeStorageForm(data=request.POST) + f = ChangeStorageForm(data=request.POST, prefix='change_storage') if f.is_valid(): try: self.object.name = f.cleaned_data['storage_name'] @@ -223,6 +225,17 @@ class StockViewDetail(LoginRequiredMixin, BaseTemplateMixin, DetailView): context = self.get_context_data(**kwargs) return self.render_to_response(context) + def handle_relocate_stock(self, request, **kwargs): + instance = Stock.objects.get(id=request.POST['prefix']) + edit_form = RelocateStockForm(instance=instance, data=request.POST, prefix=request.POST['prefix']) + if edit_form.is_valid(): + edit_form.save() + else: + pass # Todo: Handle error + + context = self.get_context_data(**kwargs) + return self.render_to_response(context) + def handle_amount_change_post(self, request, increase, **kwargs): edit_form = EditStockAmountForm(data=request.POST) if edit_form.is_valid(): @@ -264,6 +277,8 @@ class StockViewDetail(LoginRequiredMixin, BaseTemplateMixin, DetailView): return self.handle_update_watermark(request, **kwargs) elif 'submit-edit-lot' in request.POST: return self.handle_update_lot(request, **kwargs) + elif 'submit-relocate-stock' in request.POST: + return self.handle_relocate_stock(request, **kwargs) elif 'submit-amount-reduce' in request.POST: return self.handle_amount_change_post(request, False, **kwargs) elif 'submit-amount-increase' in request.POST: diff --git a/shimatta_kenkyusho/templates/parts/modals/update-stock-modal.html b/shimatta_kenkyusho/templates/parts/modals/update-stock-modal.html index c15c067..50c6c0e 100644 --- a/shimatta_kenkyusho/templates/parts/modals/update-stock-modal.html +++ b/shimatta_kenkyusho/templates/parts/modals/update-stock-modal.html @@ -55,6 +55,12 @@ needs following context: +
diff --git a/shimatta_kenkyusho/templates/parts/stocks-detail.html b/shimatta_kenkyusho/templates/parts/stocks-detail.html index c0938c7..832c720 100644 --- a/shimatta_kenkyusho/templates/parts/stocks-detail.html +++ b/shimatta_kenkyusho/templates/parts/stocks-detail.html @@ -126,8 +126,8 @@ {% include 'paginator.html' with paginator=stocks get_param='stock_page' aria_label='Stock Page Navigation' %} - {% for stock in stocks %} - {% include 'parts/modals/update-stock-modal.html' with stock=stock form=change_stock_form %} + {% for stock in stocks_with_forms %} + {% include 'parts/modals/update-stock-modal.html' with stock=stock.object form=change_stock_form relocate_form=stock.relocate_form %} {% endfor %} {% with add_storage_form as form %}