Refactor SalesOrderAttachment forms

This commit is contained in:
Oliver 2021-06-30 10:09:05 +10:00
parent 60d599b476
commit 712c9598d1
4 changed files with 34 additions and 70 deletions

View File

@ -259,6 +259,15 @@ class SOAttachmentList(generics.ListCreateAPIView, AttachmentMixin):
]
class SOAttachmentDetail(generics.RetrieveUpdateAPIView, AttachmentMixin):
"""
Detail endpoint for SalesOrderAttachment
"""
queryset = SalesOrderAttachment.objects.all()
serializer_class = SOAttachmentSerializer
class SOList(generics.ListCreateAPIView):
"""
API endpoint for accessing a list of SalesOrder objects.
@ -579,6 +588,7 @@ order_api_urls = [
# API endpoints for sales ordesr
url(r'^so/', include([
url(r'attachment/', include([
url(r'^(?P<pk>\d+)/$', SOAttachmentDetail.as_view(), name='api-so-attachment-detail'),
url(r'^.*$', SOAttachmentList.as_view(), name='api-so-attachment-list'),
])),

View File

@ -23,7 +23,7 @@
enableDragAndDrop(
'#attachment-dropzone',
"{% url 'so-attachment-create' %}",
'{% url "api-so-attachment-list" %}',
{
data: {
order: {{ order.id }},
@ -36,20 +36,36 @@ enableDragAndDrop(
);
$("#new-attachment").click(function() {
launchModalForm("{% url 'so-attachment-create' %}?order={{ order.id }}",
{
reload: true,
}
);
constructForm('{% url "api-so-attachment-list" %}', {
method: 'POST',
fields: {
attachment: {},
comment: {},
order: {
value: {{ order.pk }},
hidden: true
}
},
reload: true,
title: '{% trans "Add Attachment" %}'
});
});
$("#attachment-table").on('click', '.attachment-edit-button', function() {
var button = $(this);
var url = `/order/sales-order/attachment/${button.attr('pk')}/edit/`;
var pk = button.attr('pk');
launchModalForm(url, {
var url = `/api/order/so/attachment/${pk}/`;
constructForm(url, {
fields: {
attachment: {},
comment: {},
},
reload: true,
title: '{% trans "Edit Attachment" %}',
});
});

View File

@ -89,8 +89,6 @@ sales_order_urls = [
])),
url(r'^attachment/', include([
url(r'^new/', views.SalesOrderAttachmentCreate.as_view(), name='so-attachment-create'),
url(r'^(?P<pk>\d+)/edit/', views.SalesOrderAttachmentEdit.as_view(), name='so-attachment-edit'),
url(r'^(?P<pk>\d+)/delete/', views.SalesOrderAttachmentDelete.as_view(), name='so-attachment-delete'),
])),

View File

@ -96,66 +96,6 @@ class SalesOrderDetail(InvenTreeRoleMixin, DetailView):
template_name = 'order/sales_order_detail.html'
class SalesOrderAttachmentCreate(AjaxCreateView):
""" View for creating a new SalesOrderAttachment """
model = SalesOrderAttachment
form_class = order_forms.EditSalesOrderAttachmentForm
ajax_form_title = _('Add Sales Order Attachment')
def save(self, form, **kwargs):
"""
Save the user that uploaded the attachment
"""
attachment = form.save(commit=False)
attachment.user = self.request.user
attachment.save()
def get_data(self):
return {
'success': _('Added attachment')
}
def get_initial(self):
initials = super().get_initial().copy()
try:
initials['order'] = SalesOrder.objects.get(id=self.request.GET.get('order', None))
except (ValueError, SalesOrder.DoesNotExist):
pass
return initials
def get_form(self):
""" Hide the 'order' field """
form = super().get_form()
form.fields['order'].widget = HiddenInput()
return form
class SalesOrderAttachmentEdit(AjaxUpdateView):
""" View for editing a SalesOrderAttachment object """
model = SalesOrderAttachment
form_class = order_forms.EditSalesOrderAttachmentForm
ajax_form_title = _("Edit Attachment")
def get_data(self):
return {
'success': _('Attachment updated')
}
def get_form(self):
form = super().get_form()
form.fields['order'].widget = HiddenInput()
return form
class PurchaseOrderAttachmentDelete(AjaxDeleteView):
""" View for deleting a PurchaseOrderAttachment """