Make it possible to fully delete unlisted/unrated extensions #81

Merged
Anna Sirota merged 24 commits from fully-delete-extension into main 2024-04-19 11:00:19 +02:00
6 changed files with 47 additions and 9 deletions
Showing only changes of commit 8d690c5c4b - Show all commits

View File

@ -59,19 +59,23 @@ class TrackChangesMixin(models.Model):
track_changes_to_fields: Set[str] track_changes_to_fields: Set[str]
def _compare(self, old_instance: object) -> bool: def _was_modified(self, old_instance: object, update_fields=None) -> bool:
"""Returns True if model fields have changed. """Returns True if the record is modified.
Only checks fields listed in self.track_changes_to_fields. Only checks fields listed in self.track_changes_to_fields.
""" """
for field in self.track_changes_to_fields: for field in self.track_changes_to_fields:
# If update_fields was given and this field was NOT in it,
# its value definitely won't be changed:
if update_fields is not None and field not in update_fields:
continue
old_val = getattr(old_instance, field, ...) old_val = getattr(old_instance, field, ...)
new_val = getattr(self, field, ...) new_val = getattr(self, field, ...)
if old_val != new_val: if old_val != new_val:
return True return True
return False return False
def pre_save_record(self) -> Tuple[bool, OldStateType]: def pre_save_record(self, *args, **kwargs) -> Tuple[bool, OldStateType]:
"""Tracks the previous state of this object. """Tracks the previous state of this object.
Only records fields listed in self.track_changes_to_fields. Only records fields listed in self.track_changes_to_fields.
@ -86,7 +90,8 @@ class TrackChangesMixin(models.Model):
except type(self).DoesNotExist: except type(self).DoesNotExist:
return True, {} return True, {}
was_modified = self._compare(db_instance) update_fields = kwargs.get('update_fields')
was_modified = self._was_modified(db_instance, update_fields=update_fields)
old_instance_data = { old_instance_data = {
attr: copy.deepcopy(getattr(db_instance, attr)) for attr in self.track_changes_to_fields attr: copy.deepcopy(getattr(db_instance, attr)) for attr in self.track_changes_to_fields
} }

View File

@ -44,9 +44,10 @@ def _delete_file(sender: object, instance: object, **kwargs: object) -> None:
def _record_changes( def _record_changes(
sender: object, sender: object,
instance: Union[extensions.models.Extension, extensions.models.Version], instance: Union[extensions.models.Extension, extensions.models.Version],
update_fields: object,
**kwargs: object, **kwargs: object,
) -> None: ) -> None:
was_changed, old_state = instance.pre_save_record() was_changed, old_state = instance.pre_save_record(update_fields=update_fields)
if hasattr(instance, 'name'): if hasattr(instance, 'name'):
instance.sanitize('name', was_changed, old_state, **kwargs) instance.sanitize('name', was_changed, old_state, **kwargs)

View File

@ -10,8 +10,10 @@ logger = logging.getLogger(__name__)
@receiver(pre_save, sender=files.models.File) @receiver(pre_save, sender=files.models.File)
def _record_changes(sender: object, instance: files.models.File, **kwargs: object) -> None: def _record_changes(
was_changed, old_state = instance.pre_save_record() sender: object, instance: files.models.File, update_fields: object, **kwargs: object
) -> None:
was_changed, old_state = instance.pre_save_record(update_fields=update_fields)
instance.record_status_change(was_changed, old_state, **kwargs) instance.record_status_change(was_changed, old_state, **kwargs)

View File

@ -0,0 +1,20 @@
[Unit]
Description=Send notification emails for {{ project_name }} {{ env|capitalize }}
[Service]
Type=oneshot
User={{ user }}
Group={{ group }}
EnvironmentFile={{ env_file }}
ExecStart={{ dir.source }}/.venv/bin/python {{ dir.source }}/manage.py send_notification_emails
SyslogIdentifier={{ service_name }}
NotifyAccess=all
WorkingDirectory={{ dir.source }}
PrivateTmp=true
ProtectHome=true
ProtectSystem=full
CapabilityBoundingSet=~CAP_SYS_ADMIN
[Install]
WantedBy=multi-user.target

View File

@ -0,0 +1,8 @@
[Timer]
OnCalendar=*-*-* *:*:00
RandomizedDelaySec=3
AccuracySec=1us
Persistent=true
[Install]
WantedBy=timer.target

View File

@ -19,8 +19,10 @@ logger = logging.getLogger(__name__)
@receiver(pre_save, sender=User) @receiver(pre_save, sender=User)
def _record_changes(sender: object, instance: User, **kwargs: object) -> None: def _record_changes(
was_changed, old_state = instance.pre_save_record() sender: object, instance: User, update_fields: object, **kwargs: object
) -> None:
was_changed, old_state = instance.pre_save_record(update_fields=update_fields)
instance.record_status_change(was_changed, old_state, **kwargs) instance.record_status_change(was_changed, old_state, **kwargs)