blender-studio/blog/signals.py
Anna Sirota e7669f6e8c Move Profile fields into a custom User model [D9913]
- Moves all columns of `Profile` model to the custom `users.User` model;
- Moves `Notification` model to `users` app without touching its table's content;
- Unifies `Profile`/`User` admin;
- Moves/renames a lot of modules and templates;
- Makes email lookup in unsubscribe handler case-insensitive (bugfix).

- Having all custom profile data on `User` makes it easy to use it:
   Every template/module that needs to do something with profile data has to reference it with `[request].user.profile.field`, potentially introducing extra joins, requiring extra checks and so on;
- Having all custom profile data on `User` makes implementing deletion more straightforward, if not simpler:
   The only way to reliably override the way `User` records are deleted is to override methods of the model/queryset, which is not possible while it's governed by Django's `auth` app. Having User and Profile separate also introduces a layer of hacks in the admin, which we could do without.
2020-12-22 10:28:57 +01:00

40 lines
1.1 KiB
Python

import logging
from django.db.models.signals import post_save
from django.dispatch import receiver
from blog.models import PostComment, Like
from users.queries import create_action_from_like
logger = logging.getLogger(__name__)
@receiver(post_save, sender=PostComment)
def notify_about_comment(
sender: object, instance: PostComment, created: bool, **kwargs: object
) -> None:
"""
Generate notifications about comments under blog posts.
Because asset <-> comment relation uses a custom through model (PostComment),
film asset is not accessible in post_save of a Comment, only in post_save of the through model.
"""
if not created:
return
instance.comment.create_action()
@receiver(post_save, sender=Like)
def notify_about_like(sender: object, instance: Like, created: bool, **kwargs: object) -> None:
"""Generate notifications about blog post likes."""
if not created:
return
# Don't notify when one likes their own blog post
if instance.user == instance.post.author:
return
create_action_from_like(actor=instance.user, target=instance.post)