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

@ -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