Я успешно принимаю участие в хештеге @ username в новой форме сообщения, когда я обновляю его другим содержимым @ username , которое не дает пользователю никаких сигналов. И я использую django -notifications , чтобы уведомить пользователя. Я использую django представление обновлений на основе Claass.
my models.py:
class post(models.Model):
parent = models.ForeignKey("self", on_delete=models.CASCADE, blank=True, null=True)
title = models.CharField(max_length=100)
image = models.ImageField(upload_to='post_pics', null=True, blank=True)
video = models.FileField(upload_to='post_videos', null=True, blank=True)
content = models.TextField()
likes = models.ManyToManyField(User, related_name='likes', blank=True)
date_posted = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(User, on_delete=models.CASCADE)
objects = postManager()
def __str__(self):
return self.title
class Meta:
ordering = ['-date_posted', 'title']
def get_absolute_url(self):
return reverse ('blog-home')
def total_likes(self):
return self.likes.count()
def post_save_receiver(sender, instance, created, *args,**kwargs):
if created and not instance.parent:
user_regex = r'@(?P<username>[\w.@+-]+)'
m = re.search(user_regex, instance.content)
if m:
try:
recipient = User.objects.get(username=m.group('username'))
except (User.DoesNotExist, User.MultipleObjectsReturned):
pass
else:
notify.send(instance.author, recipient=recipient, actor=instance.author, verb='mention you in a post', target=instance, nf_type='tagged_by_one_user')
post_save.connect(post_save_receiver, sender=post)
и мой обзор обновлений в views.py:
class PostUpdateView(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
model = post
fields = ['content']
def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)
def test_func(self):
post = self.get_object()
if self.request.user == post.author:
return True
return False
def update_save_receiver(sender, object, created, *args,**kwargs):
if created:
user_regex = r'@(?P<username>[\w.@+-]+)'
m = re.search(user_regex, object.content)
if m:
try:
recipient = User.objects.get(username=m.group('username'))
except (User.DoesNotExist, User.MultipleObjectsReturned):
pass
else:
notify.send(object.author, recipient=recipient, actor=object.author, verb='mention you in a post', nf_type='tagged_by_one_user')
post_save.connect(update_save_receiver, sender=object)