Changeset View
Changeset View
Standalone View
Standalone View
comments/models.py
| from typing import Optional, List, Dict, Any | from typing import Optional, List, Dict, Any | ||||
| from actstream import action | |||||
| from django.contrib.auth.models import User | from django.contrib.auth.models import User | ||||
| from django.db import models | from django.db import models | ||||
| from django.urls.base import reverse | from django.urls.base import reverse | ||||
| from django.utils import timezone | from django.utils import timezone | ||||
| from common import mixins | from common import mixins | ||||
| ▲ Show 20 Lines • Show All 148 Lines • ▼ Show 20 Lines | def to_dict(self) -> Dict[str, Any]: | ||||
| 'message_html': with_shortcodes(self.message_html), | 'message_html': with_shortcodes(self.message_html), | ||||
| 'like_url': self.like_url, | 'like_url': self.like_url, | ||||
| 'liked': False, | 'liked': False, | ||||
| 'likes': 0, | 'likes': 0, | ||||
| 'edit_url': self.edit_url, | 'edit_url': self.edit_url, | ||||
| 'delete_url': self.delete_url, | 'delete_url': self.delete_url, | ||||
| } | } | ||||
| def get_action_target(self): | |||||
| """Return an object this comment is relevant to, e.g. a blog post or a training section.""" | |||||
| for field in ('section', 'post', 'asset'): | |||||
| target = getattr(self, field, None) | |||||
| if target and target.all(): | |||||
| return target.first() | |||||
| def create_action(self): | |||||
| """Create an activity action from this comment. | |||||
| Used to notify users about comments in various pages, such as blog posts or film assets. | |||||
| """ | |||||
| target = self.get_action_target() | |||||
| return action.send(self.user, verb='commented', action_object=self, target=target) | |||||
| @property | |||||
| def anchor(self): | |||||
| """Return an anchor for referencing the comment in a page, e.g. blog post or film asset.""" | |||||
| return f'comment-{self.pk}' | |||||
| def get_absolute_url(self): | |||||
| """Return a URL to the comment within a specific page, e.g. blog post or film asset.""" | |||||
| action_target = self.get_action_target() | |||||
| if action_target: | |||||
| return action_target.get_absolute_url() + f'#{self.anchor}' | |||||
| class Like(mixins.CreatedUpdatedMixin, models.Model): | class Like(mixins.CreatedUpdatedMixin, models.Model): | ||||
| class Meta: | class Meta: | ||||
| constraints = [ | constraints = [ | ||||
| models.UniqueConstraint( | models.UniqueConstraint( | ||||
| fields=['user', 'comment'], name='only_one_like_per_comment_and_user' | fields=['user', 'comment'], name='only_one_like_per_comment_and_user' | ||||
| ) | ) | ||||
| ] | ] | ||||
| Show All 13 Lines | |||||