-
Notifications
You must be signed in to change notification settings - Fork 0
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
Conversation
def _save_m2m(self): | ||
super()._save_m2m() | ||
# update document text | ||
self.instance.update_text_content() | ||
|
There was a problem hiding this comment.
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
if not self.filename: | ||
self.filename = self.file.name | ||
if not self.size: | ||
self.size = self.file.size | ||
if not self.mimetype: | ||
self.file.seek(0) | ||
self.mimetype = magic.from_buffer(self.file.read(), mime=True) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this prevents us from updating the source file details
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This messes with setting these fields explicitly. Instead, clear them in the admin form so that they get reset here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small change needed
peachjam/forms.py
Outdated
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 |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks, updated
peachjam/forms.py
Outdated
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 | ||
|
||
return cleaned_data |
There was a problem hiding this comment.
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.
Suspect IssuesThis pull request was deployed and Sentry observed the following issues:
Did you find this useful? React with a 👍 or 👎 |
This reverts commit 9e18d6b.
#1737