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

Update source file details on file change #1760

merged 3 commits into from
Mar 6, 2024

Conversation

actlikewill
Copy link
Contributor

@actlikewill actlikewill commented Mar 5, 2024

  • This allows us to update the source file meta data when the file is changed in the admin.

#1737

Comment on lines -325 to -329
def _save_m2m(self):
super()._save_m2m()
# update document text
self.instance.update_text_content()

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

Comment on lines 714 to 720
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)
Copy link
Contributor Author

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

Copy link
Contributor

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.

@actlikewill actlikewill marked this pull request as ready for review March 5, 2024 14:16
Copy link
Contributor

@longhotsummer longhotsummer left a comment

Choose a reason for hiding this comment

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

Small change needed

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

Comment on lines 191 to 198
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
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.

@actlikewill actlikewill merged commit 2a8a76d into main Mar 6, 2024
9 checks passed
@actlikewill actlikewill deleted the source-file branch March 6, 2024 09:27
Copy link

sentry-io bot commented Mar 6, 2024

Suspect Issues

This pull request was deployed and Sentry observed the following issues:

  • ‼️ CalledProcessError: Command '['/app/peachjam/../bin/pdfjs-to-text', '/tmp/tmpx3r504xe', '/tmp/tmpx9b_nwtl.txt']' retu... /admin/peachjam/judgment/add/ View Issue

Did you find this useful? React with a 👍 or 👎

longhotsummer added a commit that referenced this pull request Mar 6, 2024
This reverts commit 2a8a76d, reversing
changes made to 2078cf8.
longhotsummer added a commit that referenced this pull request Mar 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants