def personal(request, form_class=TweetForm, template_name="microblogging/personal.html", success_url=None): """ just the tweets the current user is following """ following_list, followers_list = get_following_followers_lists(request.user) if request.method == "POST": form = form_class(request.user, request.POST) if form.is_valid(): text = form.cleaned_data['text'] tweet = form.save() if request.is_ajax(): return HttpResponse("ok") else: if success_url is None: success_url = reverse('microblogging.views.personal') return HttpResponseRedirect(success_url) reply = None else: reply = request.GET.get("reply", None) form = form_class() if reply: form.fields['text'].initial = u"@%s " % reply else: form.fields['text'].initial = u"" tweets = TweetInstance.objects.tweets_for(request.user).order_by("-sent") return render_to_response(template_name, { "form": form, "reply": reply, "tweets": tweets, "following_list": following_list, "followers_list": followers_list }, context_instance=RequestContext(request))
def friends_objects(request, template_name, friends_objects_function, extra_context={}): """ Display friends' objects. This view takes a template name and a function. The function should take an iterator over users and return an iterator over objects belonging to those users. This iterator over objects is then passed to the template of the given name as ``object_list``. The template is also passed variable defined in ``extra_context`` which should be a dictionary of variable names to functions taking a request object and returning the value for that variable. """ friends = friend_set_for(request.user) following_list, followers_list = get_following_followers_lists(request.user) dictionary = { "object_list": friends_objects_function(friends), "following_list": following_list, "followers_list": followers_list } for name, func in extra_context.items(): dictionary[name] = func(request) return render(request, template_name, dictionary)
def get_context_data(self, **kwargs): context = super(FollowingPageView, self).get_context_data(**kwargs) # TODO use a query parameter for the time delta. here is 3 months ago = datetime.datetime.now() - datetime.timedelta(30) following_list, followers_list = get_following_followers_lists( self.request.user) tweets = [ TimeLineItem(item, item.sent, item.sender, "timeline/_tweet.html") for item in Tweet.objects.all().filter( sent__gte=ago, sender_id__in=[user.id for user in following_list], sender_type__model="user").order_by("-sent") ] posts = [ TimeLineItem(item, item.updated_at, item.author, "timeline/_post.html") for item in Post.objects.all().filter( publish__gte=ago, status=2, author__in=following_list).order_by("-publish") ] images = [ TimeLineItem(item, item.date_added, item.member, "timeline/_photo.html") for item in Image.objects.all().filter( Q(is_public=True) | Q(member__in=friend_set_for(self.request.user))).filter( date_added__gte=ago, member__in=following_list).order_by( "-date_added") ] tracks = [ TimeLineItem(item, item.updated_at, item.user, "timeline/_track.html") for item in Track.objects.all().filter( created_at__gte=ago, user__in=following_list).order_by( "-created_at") ] comments = [ TimeLineItem(item, item.date_submitted, item.user, "timeline/_comment.html") for item in ThreadedComment.objects.all().filter( date_submitted__gte=ago, user__in=following_list).order_by( "-date_submitted") ] items = merge(tweets, images, posts, tracks, comments, field="date") for index, item in enumerate(items): item.index = index + 1 context['timelineitems'] = group_comments(items) context['prefix_sender'] = True return context
def public(request, template_name="microblogging/public.html"): """ all the tweets """ tweets = Tweet.objects.all().order_by("-sent") following_list, followers_list = get_following_followers_lists(request.user) return render_to_response(template_name, { "tweets": tweets, "following_list": following_list, "followers_list": followers_list }, context_instance=RequestContext(request))
def public(request, template_name="microblogging/public.html"): """ all the tweets """ tweets = Tweet.objects.all().order_by("-sent") following_list, followers_list = get_following_followers_lists(request.user) return render(request, template_name, { "tweets": tweets, "following_list": following_list, "followers_list": followers_list })
def get_context_data(self, **kwargs): context = super(FollowingPageView, self).get_context_data(**kwargs) # TODO use a query parameter for the time delta. here is 3 months ago = datetime.datetime.now() - datetime.timedelta(30) following_list, followers_list = get_following_followers_lists(self.request.user) tweets = [ TimeLineItem(item, item.sent, item.sender, "timeline/_tweet.html") for item in Tweet.objects.all().filter(sent__gte=ago, sender_id__in=[user.id for user in following_list], sender_type__model="user").order_by("-sent") ] posts = [ TimeLineItem(item, item.updated_at, item.author, "timeline/_post.html") for item in Post.objects.all().filter(publish__gte=ago, status=2, author__in=following_list).order_by("-publish") ] images = [ TimeLineItem(item, item.date_added, item.member, "timeline/_photo.html") for item in Image.objects.all().filter(Q(is_public=True) | Q(member__in=friend_set_for(self.request.user))).filter(date_added__gte=ago, member__in=following_list).order_by("-date_added") ] tracks = [ TimeLineItem(item, item.updated_at, item.user, "timeline/_track.html") for item in Track.objects.all().filter(created_at__gte=ago, user__in=following_list).order_by("-created_at") ] comments = [ TimeLineItem(item, item.date_submitted, item.user, "timeline/_comment.html") for item in ThreadedComment.objects.all().filter(date_submitted__gte=ago, user__in=following_list).order_by("-date_submitted") ] items = merge(tweets, images, posts, tracks, comments, field="date") for index, item in enumerate(items): item.index = index + 1 context['timelineitems'] = group_comments(items) context['prefix_sender'] = True return context