SciPost Code Repository
Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
SciPost
Manage
Activity
Members
Labels
Plan
Issues
118
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
SciPost
SciPost
Commits
a4cba341
Commit
a4cba341
authored
5 years ago
by
Jean-Sébastien Caux
Browse files
Options
Downloads
Patches
Plain Diff
Add management command to delete orphaned attachment files
parent
ff890521
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
apimail/management/commands/delete_orphaned_attachment_files.py
+41
-0
41 additions, 0 deletions
...l/management/commands/delete_orphaned_attachment_files.py
apimail/models/attachment.py
+3
-0
3 additions, 0 deletions
apimail/models/attachment.py
with
44 additions
and
0 deletions
apimail/management/commands/delete_orphaned_attachment_files.py
0 → 100644
+
41
−
0
View file @
a4cba341
__copyright__
=
"
Copyright © Stichting SciPost (SciPost Foundation)
"
__license__
=
"
AGPL v3
"
import
os
from
django.core.management
import
BaseCommand
from
...models
import
AttachmentFile
,
ComposedMessage
,
StoredMessage
class
Command
(
BaseCommand
):
def
handle
(
self
,
*
args
,
**
options
):
uuids_in_cm
=
[
att
.
uuid
for
cm
in
ComposedMessage
.
objects
.
all
()
for
att
in
cm
.
attachment_files
.
all
()]
uuids_in_sm
=
[
att
.
uuid
for
sm
in
StoredMessage
.
objects
.
all
()
for
att
in
sm
.
attachment_files
.
all
()]
uuids_in_use
=
set
(
uuids_in_cm
+
uuids_in_sm
)
print
(
uuids_in_use
)
orphaned_att
=
AttachmentFile
.
objects
.
exclude
(
uuid__in
=
uuids_in_use
)
print
(
orphaned_att
)
for
orphan_att
in
orphaned_att
:
# We double-check that we're not deleting any used attachment
# since the 'exclude' logic above is risky
# (any mistake in uuids_in_use would lead to unwanted deletion)
try
:
ComposedMessage
.
objects
.
filter
(
attachment_files__uuid
=
orphan_att
.
uuid
).
first
()
except
ComposedMessage
.
DoesNotExist
:
try
:
StoredMessage
.
objects
.
filter
(
attachment_files__uuid
=
orphan_att
.
uuid
).
first
()
except
StoredMessage
.
DoesNotExist
:
print
(
'
Deleting %s
'
%
orphan_att
.
uuid
)
if
orphan_att
.
file
:
if
os
.
path
.
isfile
(
orphan_att
.
file
.
path
):
os
.
remove
(
orphan_att
.
file
.
path
)
orphan_att
.
delete
()
This diff is collapsed.
Click to expand it.
apimail/models/attachment.py
+
3
−
0
View file @
a4cba341
...
...
@@ -29,6 +29,9 @@ class AttachmentFile(models.Model):
validators
=
[
validate_max_email_attachment_file_size
,],
storage
=
SecureFileStorage
())
def
__str__
(
self
):
return
'
%s (%s, %s)
'
%
(
self
.
data
[
'
name
'
],
self
.
data
[
'
content-type
'
],
self
.
file
.
size
)
def
get_absolute_url
(
self
):
return
reverse
(
'
apimail:attachment_file
'
,
kwargs
=
{
'
uuid
'
:
self
.
uuid
})
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment