获取/显示来自另一个应用程序的帖子的作者通知系统

时间:2017-10-17 17:43:32

标签: python django

所以我正在建立这个通知系统,该系统应该在其他人喜欢或评论他们的帖子时显示/告诉用户。但是,不是告诉用户何时有人喜欢您的帖子,而是记录您的个人活动并显示您自己所做的所有喜欢和评论。我知道为什么会发生这种情况(毕竟我设置了它),我只是不知道如何反转它以便后期作者得到通知而不是当前用户。

我想我拥有所需的所有信息,我只是不知道如何进入其他应用程序模型,获取我没有的额外信息,然后显示/的帖子的作者的通知。请帮忙。

另外,请忽略马虎进口,我知道需要清理。

通知应用视图:

from django.core.paginator import Paginator,EmptyPage,PageNotAnInteger
from django.shortcuts import render,get_object_or_404,redirect
from django.utils import timezone
from feed.models import UserPost,UserComment
from users.models import UserProfile
from notify.models import UserNotification
from feed.forms import PostForm,CommentForm
from django.urls import reverse_lazy
from django.contrib.auth.decorators import login_required

from django.contrib.auth import get_user_model

from django.contrib.auth.mixins import LoginRequiredMixin
from braces.views import SelectRelatedMixin

from django.views.generic import (TemplateView,ListView,
                                    DetailView,CreateView,
                                    UpdateView,DeleteView,
                                    RedirectView,)

User = get_user_model()

class UserNotifications(LoginRequiredMixin,ListView):
    login_url = 'account_login'
    model = UserNotification
    template_name = 'notify/usernotification_list.html'
    context_object_name = 'notifies'
#   paginate_by = 25

    def get_queryset(self):
        return UserNotification.objects.filter(user=self.request.user)

class NotifyMarkRead(RedirectView):
    def get_redirect_url(self,pk):
        obj = get_object_or_404(UserNotification,pk=pk)
        if obj.read == False:
            obj.read = True
            obj.save()
        else:
            obj.read = False
            obj.save()
        return obj.get_absolute_url()

通知应用模型:

from django.db import models
from django.contrib.auth import get_user_model
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.core.urlresolvers import reverse

User = get_user_model()

# Create your models here.
class UserNotification(models.Model):
    user = models.ForeignKey(User,related_name='user',null=True)
    post = models.ForeignKey('feed.UserPost',related_name='post')
    timestamp = models.DateTimeField(auto_now_add=True)
    notify_type = models.CharField(max_length=6)
    read = models.BooleanField(default=False)

    def get_absolute_url(self):
        return reverse('notify:user_notifications')

    def __str__(self):
        return str(self.user)

据推测,我设置它的方式,我需要根据UserNotification模型中记录的帖子进入并获取作者?

Feed应用模型:

from django.db import models
from django.core.urlresolvers import reverse
from django.conf import settings

from django.contrib.auth import get_user_model
User = get_user_model()

# Create your models here.
class UserPost(models.Model):
    author = models.ForeignKey(User,related_name='userpost',null=True)
    post_date = models.DateTimeField(auto_now_add=True)
    title = models.CharField(max_length=150,blank=False)
    post_body = models.TextField(max_length=1000,blank=False)
    image = models.ImageField(upload_to='post_pics',blank=True)
    likes = models.ManyToManyField(User,blank=True,related_name='post_likes')

    class Meta:
        ordering = ['-post_date']

    def publish(self):
        self.save()

    def get_absolute_url(self):
        return reverse('index')

    def likes_as_flat_user_id_list(self):
        return self.likes.values_list('id', flat=True)

    def __str__(self):
        return self.title

更新 像按钮视图(在Feed应用程序中):

class LikePostToggle(RedirectView):
    def get_redirect_url(self,pk):
        obj = get_object_or_404(UserPost,pk=pk)
        user = self.request.user
        if user.is_authenticated():
            if user in obj.likes.all():
                obj.likes.remove(user)
            else:
                obj.likes.add(user)
                notification = UserNotification.objects.create(
                    user = self.request.user,
                    post = obj,
                    notify_type = "like",
                )
        return obj.get_absolute_url()

“喜欢”按钮有效,因此我没有显示任何导入

1 个答案:

答案 0 :(得分:1)

您应该让代码更容易,通知系统太容易了。 如果您希望在有人喜欢他的帖子时通知用户,您可以在类似视图中执行此操作

def like(request, post):
    post.likes += 1
    post.save()
    notify = post.author
    Notifications_model.objects.create(fromUser=request.user, toUser=notify, content="liked your post")

然后在你的模板中,你可以循环通过这样的通知:

{% for notification in filterd_notifications%}
<p>{{notification.fromUser}} {{notification.content}}</p>
{% endfor %}
filterd_notifications为notifications.objects.filter(toUser=request.user)