Refactor BuildAttachment views

This commit is contained in:
Oliver 2021-06-30 11:05:35 +10:00
parent a7d60cf5ad
commit 9ea3e511b9
3 changed files with 22 additions and 89 deletions

View File

@ -22,7 +22,7 @@
enableDragAndDrop(
'#attachment-dropzone',
'{% url "build-attachment-create" %}',
'{% url "api-build-attachment-list" %}',
{
data: {
build: {{ build.id }},
@ -36,29 +36,34 @@ enableDragAndDrop(
// Callback for creating a new attachment
$('#new-attachment').click(function() {
launchModalForm(
'{% url "build-attachment-create" %}',
{
reload: true,
data: {
build: {{ build.pk }},
constructForm('{% url "api-build-attachment-list" %}', {
fields: {
attachment: {},
comment: {},
build: {
value: {{ build.pk }},
hidden: true,
}
}
);
},
method: 'POST',
reload: true,
title: '{% trans "Add Attachment" %}',
});
});
// Callback for editing an attachment
$("#attachment-table").on('click', '.attachment-edit-button', function() {
var pk = $(this).attr('pk');
var url = `/build/attachment/${pk}/edit/`;
launchModalForm(
url,
{
reload: true,
}
);
constructForm(`/api/build/attachment/${pk}/`, {
fields: {
attachment: {},
comment: {},
},
reload: true,
title: '{% trans "Edit Attachment" %}',
});
});
// Callback for deleting an attachment

View File

@ -37,8 +37,6 @@ build_urls = [
])),
url('^attachment/', include([
url('^new/', views.BuildAttachmentCreate.as_view(), name='build-attachment-create'),
url(r'^(?P<pk>\d+)/edit/', views.BuildAttachmentEdit.as_view(), name='build-attachment-edit'),
url(r'^(?P<pk>\d+)/delete/', views.BuildAttachmentDelete.as_view(), name='build-attachment-delete'),
])),

View File

@ -1060,76 +1060,6 @@ class BuildItemEdit(AjaxUpdateView):
return form
class BuildAttachmentCreate(AjaxCreateView):
"""
View for creating a BuildAttachment
"""
model = BuildOrderAttachment
form_class = forms.EditBuildAttachmentForm
ajax_form_title = _('Add Build Order Attachment')
def save(self, form, **kwargs):
"""
Add information on 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):
"""
Get initial data for creating an attachment
"""
initials = super().get_initial()
try:
initials['build'] = Build.objects.get(pk=self.request.GET.get('build', -1))
except (ValueError, Build.DoesNotExist):
pass
return initials
def get_form(self):
"""
Hide the 'build' field if specified
"""
form = super().get_form()
form.fields['build'].widget = HiddenInput()
return form
class BuildAttachmentEdit(AjaxUpdateView):
"""
View for editing a BuildAttachment object
"""
model = BuildOrderAttachment
form_class = forms.EditBuildAttachmentForm
ajax_form_title = _('Edit Attachment')
def get_form(self):
form = super().get_form()
form.fields['build'].widget = HiddenInput()
return form
def get_data(self):
return {
'success': _('Attachment updated')
}
class BuildAttachmentDelete(AjaxDeleteView):
"""
View for deleting a BuildAttachment