Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update source file details on file change #1760

Merged
merged 3 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions peachjam/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,11 +322,6 @@ def clean_content_html(self):
return self.instance.content_html
return self.cleaned_data["content_html"]

def _save_m2m(self):
super()._save_m2m()
# update document text
self.instance.update_text_content()

Comment on lines -325 to -329
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@longhotsummer this is getting called before the source file is saved and updated so we can move it to after the save_related method


class AttachedFilesInline(BaseAttachmentFileInline):
model = AttachedFiles
Expand Down Expand Up @@ -504,7 +499,9 @@ def save_related(self, request, form, formsets, change):
cp.queue_re_extract_citations(form.instance.date)

super().save_related(request, form, formsets, change)

form.instance.save()
form.instance.update_text_content()

def get_urls(self):
return [
Expand Down
9 changes: 9 additions & 0 deletions peachjam/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,15 @@ def _save_m2m(self):
self.instance.file_as_pdf.delete()
self.instance.ensure_file_as_pdf()

def clean(self):
cleaned_data = super().clean()
if "file" in self.changed_data:
# clear all the fields that are derived from the file
for field in ["mimetype", "size", "file_as_pdf", "filename"]:
self.instance.__dict__[field] = None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setattr is the preferred method: setattr(self.instance, field, None)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that is exactly what self.instance.field = None does

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or just do it explicitly: self.instance.mimetype = None etc. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, updated


return cleaned_data
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@longhotsummer clearing the fields on the instance from the clean method allows us to compute them again on save.



class AttachedFilesForm(AttachmentFormMixin, forms.ModelForm):
class Meta:
Expand Down
Loading