Refactor PurchaseOrderAttachment views

This commit is contained in:
Oliver 2021-06-30 10:03:54 +10:00
parent b946aedb5c
commit 60d599b476
4 changed files with 36 additions and 86 deletions

View File

@ -554,12 +554,22 @@ class POAttachmentList(generics.ListCreateAPIView, AttachmentMixin):
serializer_class = POAttachmentSerializer
class POAttachmentDetail(generics.RetrieveUpdateDestroyAPIView, AttachmentMixin):
"""
Detail endpoint for a PurchaseOrderAttachment
"""
queryset = PurchaseOrderAttachment.objects.all()
serializer_class = POAttachmentSerializer
order_api_urls = [
# API endpoints for purchase orders
url(r'^po/(?P<pk>\d+)/$', PODetail.as_view(), name='api-po-detail'),
url(r'po/attachment/', include([
url(r'^(?P<pk>\d+)/$', POAttachmentDetail.as_view(), name='api-po-attachment-detail'),
url(r'^.*$', POAttachmentList.as_view(), name='api-po-attachment-list'),
])),
url(r'^po/(?P<pk>\d+)/$', PODetail.as_view(), name='api-po-detail'),
url(r'^po/.*$', POList.as_view(), name='api-po-list'),
# API endpoints for purchase order line items
@ -568,12 +578,11 @@ order_api_urls = [
# API endpoints for sales ordesr
url(r'^so/', include([
url(r'^(?P<pk>\d+)/$', SODetail.as_view(), name='api-so-detail'),
url(r'attachment/', include([
url(r'^.*$', SOAttachmentList.as_view(), name='api-so-attachment-list'),
])),
# List all sales orders
url(r'^(?P<pk>\d+)/$', SODetail.as_view(), name='api-so-detail'),
url(r'^.*$', SOList.as_view(), name='api-so-list'),
])),

View File

@ -22,7 +22,7 @@
enableDragAndDrop(
'#attachment-dropzone',
"{% url 'po-attachment-create' %}",
'{% url "api-po-attachment-list" %}',
{
data: {
order: {{ order.id }},
@ -35,20 +35,36 @@ enableDragAndDrop(
);
$("#new-attachment").click(function() {
launchModalForm("{% url 'po-attachment-create' %}?order={{ order.id }}",
{
reload: true,
}
);
constructForm('{% url "api-po-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/purchase-order/attachment/${button.attr('pk')}/edit/`;
var pk = button.attr('pk');
launchModalForm(url, {
var url = `/api/order/po/attachment/${pk}/`;
constructForm(url, {
fields: {
attachment: {},
comment: {},
},
reload: true,
title: '{% trans "Edit Attachment" %}',
});
});

View File

@ -46,8 +46,6 @@ purchase_order_urls = [
])),
url(r'^attachment/', include([
url(r'^new/', views.PurchaseOrderAttachmentCreate.as_view(), name='po-attachment-create'),
url(r'^(?P<pk>\d+)/edit/', views.PurchaseOrderAttachmentEdit.as_view(), name='po-attachment-edit'),
url(r'^(?P<pk>\d+)/delete/', views.PurchaseOrderAttachmentDelete.as_view(), name='po-attachment-delete'),
])),

View File

@ -96,58 +96,6 @@ class SalesOrderDetail(InvenTreeRoleMixin, DetailView):
template_name = 'order/sales_order_detail.html'
class PurchaseOrderAttachmentCreate(AjaxCreateView):
"""
View for creating a new PurchaseOrderAttachment
"""
model = PurchaseOrderAttachment
form_class = order_forms.EditPurchaseOrderAttachmentForm
ajax_form_title = _("Add Purchase Order Attachment")
ajax_template_name = "modal_form.html"
def save(self, form, **kwargs):
attachment = form.save(commit=False)
attachment.user = self.request.user
attachment.save()
def get_data(self):
return {
"success": _("Added attachment")
}
def get_initial(self):
"""
Get initial data for creating a new PurchaseOrderAttachment object.
- Client must request this form with a parent PurchaseOrder in midn.
- e.g. ?order=<pk>
"""
initials = super(AjaxCreateView, self).get_initial()
try:
initials["order"] = PurchaseOrder.objects.get(id=self.request.GET.get('order', -1))
except (ValueError, PurchaseOrder.DoesNotExist):
pass
return initials
def get_form(self):
"""
Create a form to upload a new PurchaseOrderAttachment
- Hide the 'order' field
"""
form = super(AjaxCreateView, self).get_form()
form.fields['order'].widget = HiddenInput()
return form
class SalesOrderAttachmentCreate(AjaxCreateView):
""" View for creating a new SalesOrderAttachment """
@ -188,27 +136,6 @@ class SalesOrderAttachmentCreate(AjaxCreateView):
return form
class PurchaseOrderAttachmentEdit(AjaxUpdateView):
""" View for editing a PurchaseOrderAttachment object """
model = PurchaseOrderAttachment
form_class = order_forms.EditPurchaseOrderAttachmentForm
ajax_form_title = _("Edit Attachment")
def get_data(self):
return {
'success': _('Attachment updated')
}
def get_form(self):
form = super(AjaxUpdateView, self).get_form()
# Hide the 'order' field
form.fields['order'].widget = HiddenInput()
return form
class SalesOrderAttachmentEdit(AjaxUpdateView):
""" View for editing a SalesOrderAttachment object """