diff --git a/shimatta_kenkyusho/parts/forms.py b/shimatta_kenkyusho/parts/forms.py index 4080327..81f6c0f 100644 --- a/shimatta_kenkyusho/parts/forms.py +++ b/shimatta_kenkyusho/parts/forms.py @@ -3,15 +3,56 @@ from django.forms import widgets from django.core.exceptions import ValidationError from parts import models as parts_models from shimatta_modules.EngineeringNumberConverter import EngineeringNumberConverter +import uuid + +class AutoCompleteWidget(widgets.Input): + template_name = 'widgets/autocomplete-foreign-key.html' + + def __init__(self, api_search_url, image_field_name, foreign_model, *args, **kwargs): + super().__init__(*args, **kwargs) + self.image_field_name = image_field_name + self.foreign_model = foreign_model + self.api_search_url = api_search_url + + def get_context(self, name, value, attrs): + context = super().get_context(name, value, attrs) + try: + instance = self.foreign_model.objects.get(id=uuid.UUID(value)) + except Exception as ex: + print(ex) + instance = None + + image = None + if instance is not None: + image = getattr(instance, self.image_field_name) -class AutoCompleteWidget(widgets.TextInput): - template_name = 'widgets/autrocomplete-foreign-key.html' + context['custom'] = { + 'search_url': self.api_search_url, + 'image_field_name': self.image_field_name, + 'current_instance': instance, + 'image': image, + } + return context + class AutocompleteForeingKeyField(forms.UUIDField): - def __init__(self, foreign_model=None, api_search_url=None, autocomplete_media_div=False, **kwargs): - kwargs['widget'] = AutoCompleteWidget + def __init__(self, foreign_model=None, api_search_url=None, image_field_name='image', **kwargs): super().__init__(**kwargs) + self.widget = AutoCompleteWidget(api_search_url, image_field_name, foreign_model) + self.foreign_model = foreign_model + + + def clean(self, value): + pre_cleaned_uuid = super().clean(value) + if pre_cleaned_uuid is None and not self.required: + return None + try: + obj = self.foreign_model.objects.get(id=pre_cleaned_uuid) + except self.foreign_model.DoesNotExist: + raise ValidationError('Given element does not exist') + return obj + class MyTestForm(forms.Form): pass @@ -118,8 +159,9 @@ class AddStockForm(forms.Form): new_stock.save() class ComponentForm(forms.ModelForm): - manufacturer = AutocompleteForeingKeyField(widget=AutoCompleteWidget) - component_type = AutocompleteForeingKeyField() + manufacturer = AutocompleteForeingKeyField(api_search_url='foo', foreign_model=parts_models.Manufacturer) + component_type = AutocompleteForeingKeyField(api_search_url='bar', foreign_model=parts_models.ComponentType, required=False, image_field_name=None) + package = AutocompleteForeingKeyField(api_search_url='pkgurl', foreign_model=parts_models.Package, required=False) class Meta: model = parts_models.Component fields = '__all__' diff --git a/shimatta_kenkyusho/parts/views.py b/shimatta_kenkyusho/parts/views.py index 8058f88..17d52e6 100644 --- a/shimatta_kenkyusho/parts/views.py +++ b/shimatta_kenkyusho/parts/views.py @@ -521,7 +521,7 @@ class ComponentDetailView(LoginRequiredMixin, BaseTemplateMixin, DetailView): context['edit_form'] = EditComponentForm(instance=self.object) context['stocks'] = Stock.objects.filter(component=self.object) - context['comp_form'] = ComponentForm() + context['comp_form'] = ComponentForm(instance=self.object) return context @@ -546,10 +546,25 @@ class ComponentDetailView(LoginRequiredMixin, BaseTemplateMixin, DetailView): context['edit_form'] = form return self.render_to_response(context) + def handle_submit_edit_component_post(self, request, **kwargs): + context = self.get_context_data(**kwargs) + + cform = ComponentForm(instance=self.object, data=request.POST, files=request.FILES) + if cform.is_valid(): + print('valid') + cform.save() + else: + print('invalid') + context['comp_form'] = cform + + return self.render_to_response(context) + def post(self, request, *args, **kwargs): self.object = self.get_object() if 'submit-edit-comp' in request.POST: return self.handle_submit_edit_post(request, **kwargs) + if 'submit-edit-component' in request.POST: + return self.handle_submit_edit_component_post(request, **kwargs) return super().post(request, *args, **kwargs) diff --git a/shimatta_kenkyusho/templates/parts/components-detail.html b/shimatta_kenkyusho/templates/parts/components-detail.html index bd16760..e31073a 100644 --- a/shimatta_kenkyusho/templates/parts/components-detail.html +++ b/shimatta_kenkyusho/templates/parts/components-detail.html @@ -119,9 +119,11 @@ - - {{comp_form|crispy}} - +
+ {% csrf_token %} + {{comp_form|crispy}} + +
{% if component.get_resolved_image %} diff --git a/shimatta_kenkyusho/templates/widgets/autocomplete-foreign-key.html b/shimatta_kenkyusho/templates/widgets/autocomplete-foreign-key.html new file mode 100644 index 0000000..e7f3b0f --- /dev/null +++ b/shimatta_kenkyusho/templates/widgets/autocomplete-foreign-key.html @@ -0,0 +1,28 @@ + \ No newline at end of file diff --git a/shimatta_kenkyusho/templates/widgets/autrocomplete-foreign-key.html b/shimatta_kenkyusho/templates/widgets/autrocomplete-foreign-key.html deleted file mode 100644 index 8d25d3e..0000000 --- a/shimatta_kenkyusho/templates/widgets/autrocomplete-foreign-key.html +++ /dev/null @@ -1,7 +0,0 @@ - \ No newline at end of file