模仿之前的Feedly框架,后台消息序列用django-rq,
整个过程就是简单滴生成一个活动-创建一个Activity类,一个用来把活动分发到用户friends的分发函数,然后在需要的时候,把活动传入分发函数中,由消息队列-这里
是django-rq,来调度它
Activity-活动类
from django.contrib.auth import get_user_model
class Activity(object):
def __init__(self, object_id, muser, user_name, c_date, thumbcontent, mobject_id, mtitle, verb):
self.object_id = object_id
self.muser = muser
self.user_name = user_name
self.date = c_date
self.thumbcontent = thumbcontent
self.mobject_id = mobject_id
self.mtitle = mtitle
self.verb = verb
def get_object_id(self):
return self.object_id
def get_muser(self):
return self.muser
def get_user_name(self):
return self.user_name
def get_date(self):
return self.c_date
def get_thumbcontent(self):
return self.thumbcontent
def get_mobject_id(self):
return self.mobject_id
def get_mtitle(self):
return self.mtitle
def set_real_user(self):
self.ruser = get_user_model().objects.get(pk=self.muser)
def get_verb(self):
return self.verb
在视图里面,将feed加入消息队列
feed_obj = tmp_obj.create_activity()
feed_list = self.request.user.friends.all()
redis_c = get_redis_connection('feed_storage')
feed_queue.enqueue(distr_object, feed_obj, feed_list = feed_list)
def distr_object(t_object, feed_list):
redis_c = get_redis_connection('feed_storage')
pickle_object = pickle.dumps(t_object)
for i in feed_list:
redis_c.lpush(i.id, pickle_object)
在用户的个人页面,简单滴从redis获取用户的feed,然后显示出来
class FeedPage(ListView):
template_name = 'myapp/index.html'
model = MyObject
paginate_by = 30
context_object_name = 'feedobject'
def get_queryset(self):
redis_c = get_redis_connection('feed_storage')
user_feed = []
for i in redis_c.lrange(self.request.user.id,0,30):
j = pickle.loads(i)
j.set_real_user()
user_feed.append(j)
return user_feed