Notification emails #80
@ -7,22 +7,22 @@ from django.db.models.signals import m2m_changed, pre_save, post_save, post_dele
|
|||||||
from django.dispatch import receiver
|
from django.dispatch import receiver
|
||||||
|
|
||||||
from constants.activity import Flag
|
from constants.activity import Flag
|
||||||
from extensions.models import Extension, Preview, Version
|
import extensions.models
|
||||||
import files.models
|
import files.models
|
||||||
|
|
||||||
User = get_user_model()
|
User = get_user_model()
|
||||||
|
|
||||||
|
|
||||||
@receiver(post_delete, sender=Preview)
|
@receiver(post_delete, sender=extensions.models.Preview)
|
||||||
def _delete_file(sender: object, instance: Preview, **kwargs: object) -> None:
|
def _delete_file(sender: object, instance: extensions.models.Preview, **kwargs: object) -> None:
|
||||||
instance.file.delete()
|
instance.file.delete()
|
||||||
|
|
||||||
|
|
||||||
@receiver(pre_save, sender=Extension)
|
@receiver(pre_save, sender=extensions.models.Extension)
|
||||||
@receiver(pre_save, sender=Version)
|
@receiver(pre_save, sender=extensions.models.Version)
|
||||||
def _record_changes(
|
def _record_changes(
|
||||||
sender: object,
|
sender: object,
|
||||||
instance: Union[Extension, Version],
|
instance: Union[extensions.models.Extension, extensions.models.Version],
|
||||||
**kwargs: object,
|
**kwargs: object,
|
||||||
) -> None:
|
) -> None:
|
||||||
was_changed, old_state = instance.pre_save_record()
|
was_changed, old_state = instance.pre_save_record()
|
||||||
@ -35,7 +35,7 @@ def _record_changes(
|
|||||||
instance.record_status_change(was_changed, old_state, **kwargs)
|
instance.record_status_change(was_changed, old_state, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
@receiver(post_save, sender=Extension)
|
@receiver(post_save, sender=extensions.models.Extension)
|
||||||
def _update_search_index(sender, instance, **kw):
|
def _update_search_index(sender, instance, **kw):
|
||||||
pass # TODO: update search index
|
pass # TODO: update search index
|
||||||
|
|
||||||
@ -49,18 +49,18 @@ def extension_should_be_listed(extension):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@receiver(post_save, sender=Extension)
|
@receiver(post_save, sender=extensions.models.Extension)
|
||||||
@receiver(post_save, sender=Version)
|
@receiver(post_save, sender=extensions.models.Version)
|
||||||
@receiver(post_save, sender=files.models.File)
|
@receiver(post_save, sender=files.models.File)
|
||||||
def _set_is_listed(
|
def _set_is_listed(
|
||||||
sender: object,
|
sender: object,
|
||||||
instance: Union[Extension, Version, files.models.File],
|
instance: Union[extensions.models.Extension, extensions.models.Version, files.models.File],
|
||||||
*args: object,
|
*args: object,
|
||||||
**kwargs: object,
|
**kwargs: object,
|
||||||
) -> None:
|
) -> None:
|
||||||
if isinstance(instance, Extension):
|
if isinstance(instance, extensions.models.Extension):
|
||||||
extension = instance
|
extension = instance
|
||||||
elif isinstance(instance, Version):
|
elif isinstance(instance, extensions.models.Version):
|
||||||
extension = instance.extension
|
extension = instance.extension
|
||||||
else:
|
else:
|
||||||
# Some file types (e.g., image or video) have no version associated to them.
|
# Some file types (e.g., image or video) have no version associated to them.
|
||||||
@ -76,17 +76,17 @@ def _set_is_listed(
|
|||||||
if old_is_listed == new_is_listed:
|
if old_is_listed == new_is_listed:
|
||||||
return
|
return
|
||||||
|
|
||||||
if extension.status == Extension.STATUSES.APPROVED and not new_is_listed:
|
if extension.status == extensions.models.Extension.STATUSES.APPROVED and not new_is_listed:
|
||||||
extension.status = Extension.STATUSES.INCOMPLETE
|
extension.status = extensions.models.Extension.STATUSES.INCOMPLETE
|
||||||
|
|
||||||
extension.is_listed = new_is_listed
|
extension.is_listed = new_is_listed
|
||||||
extension.save()
|
extension.save()
|
||||||
|
|
||||||
|
|
||||||
@receiver(post_save, sender=Extension)
|
@receiver(post_save, sender=extensions.models.Extension)
|
||||||
def _setup_followers(
|
def _setup_followers(
|
||||||
sender: object,
|
sender: object,
|
||||||
instance: Extension,
|
instance: extensions.models.Extension,
|
||||||
created: bool,
|
created: bool,
|
||||||
**kwargs: object,
|
**kwargs: object,
|
||||||
) -> None:
|
) -> None:
|
||||||
@ -99,20 +99,20 @@ def _setup_followers(
|
|||||||
follow(user, instance, send_action=False, flag=Flag.MODERATOR)
|
follow(user, instance, send_action=False, flag=Flag.MODERATOR)
|
||||||
|
|
||||||
|
|
||||||
@receiver(m2m_changed, sender=Extension.authors.through)
|
@receiver(m2m_changed, sender=extensions.models.Extension.authors.through)
|
||||||
def _update_authors_follow(instance, action, model, reverse, pk_set, **kwargs):
|
def _update_authors_follow(instance, action, model, reverse, pk_set, **kwargs):
|
||||||
if action not in ['post_add', 'post_remove']:
|
if action not in ['post_add', 'post_remove']:
|
||||||
return
|
return
|
||||||
|
|
||||||
if model == Extension and not reverse:
|
if model == extensions.models.Extension and not reverse:
|
||||||
|
targets = extensions.models.Extension.objects.filter(pk__in=pk_set)
|
||||||
users = [instance]
|
users = [instance]
|
||||||
extensions = Extension.objects.filter(pk__in=pk_set)
|
|
||||||
else:
|
else:
|
||||||
extensions = [instance]
|
targets = [instance]
|
||||||
users = User.objects.filter(pk__in=pk_set)
|
users = User.objects.filter(pk__in=pk_set)
|
||||||
|
|
||||||
for user in users:
|
for user in users:
|
||||||
for extension in extensions:
|
for extension in targets:
|
||||||
if action == 'post_remove':
|
if action == 'post_remove':
|
||||||
unfollow(user, extension, send_action=False, flag=Flag.AUTHOR)
|
unfollow(user, extension, send_action=False, flag=Flag.AUTHOR)
|
||||||
elif action == 'post_add':
|
elif action == 'post_add':
|
||||||
|
Loading…
Reference in New Issue
Block a user