def _notify(request): prof.start('comment-notify') comments = Comments.objects.filter(news=request.GET['news_id']).exclude(user=request.user) # Notify all the people we need to news = News.objects.filter(id=request.GET['news_id']).select_related('user', 'about_id') for n in news: # Should be only one actually if n.user != request.user: comments = comments.exclude(user=n.user) NotificationSystem.create(n.user, const.NotifConst.NEW_COMMENT_GOSSIP_MINE, {'username': request.user.username, 'name': prettyuser.user_or_first_name(request.user), 'news_id': request.GET['news_id'], }) elif n.about_id and n.about_id != request.user: comments = comments.exclude(user=n.about_id) NotificationSystem.create(n.about_id, const.NotifConst.NEW_COMMENT_GOSSIP_ME, {'username': request.user.username, 'name': prettyuser.user_or_first_name(request.user), 'news_id': request.GET['news_id'], }) participants = User.objects.filter(id__in=comments.distinct().values('user')) for participant in participants: NotificationSystem.create(participant, const.NotifConst.NEW_COMMENT_GOSSIP, {'username': request.user.username, 'name': prettyuser.user_or_first_name(request.user), 'news_id': request.GET['news_id'], }) prof.stop('comment-notify')
def invite(request): wrong = False if request.method == "POST": if "send" in request.GET: # final step, actually send e-mails form_people = InviteFormPeople(request.POST) for email in request.POST.getlist("people"): InviteSystem.invite( email, { "username": request.user.username, "name": prettyuser.user_or_first_name(request.user), "email": email, }, ) return HttpResponseRedirect("/friends") else: # get contacts and make him choose form_cred = InviteFormCredentials(request.POST) if form_cred.is_valid(): contacts = InviteSystem.getContacts( form_cred.cleaned_data["username"], form_cred.cleaned_data["password"], form_cred.cleaned_data["type"], ) if contacts is None: # wrong credentials wrong = True step = 0 else: form_people = InviteFormPeople(choices=contacts) step = 1 else: step = 0 else: form_cred = InviteFormCredentials() step = 0 return render_to_response( "friends/invite.html", {"step": step, "wrong": wrong, "form": form_cred if 0 == step else form_people}, context_instance=RequestContext(request), )
def news_scroll(request): text = cache.get('news_scroll') if text is None: prof.start('news-scroll-no-cache') # Hit the database baby - Uh. text = [] friendships = Friends.objects.filter(accepted=True).order_by('-modified_on')[:const.NewsConst.Scroll.NEW_FRIEND_MAX] for friendship in friendships: text.append(const.NewsConst.Scroll.NEW_FRIEND % ( '/' + util.escape_html(friendship.friend.username), prettyuser.user_or_first_name(friendship.friend), '/' + util.escape_html(friendship.user.username), prettyuser.user_or_first_name(friendship.user))) gossips = News.objects.filter(about_id__isnull=False).order_by('-time')[:const.NewsConst.Scroll.NEW_GOSSIP_MAX] for gossip in gossips: if gossip.anonymous: text.append(const.NewsConst.Scroll.NEW_GOSSIP_ANONYMOUS % ( '/' + util.escape_html(gossip.about_id.username), util.escape_html(prettyuser.user_or_first_name(gossip.about_id)), '/profile/?n=' + util.escape_html(str(gossip.id)), util.escape_html(gossip.title) )) else: text.append(const.NewsConst.Scroll.NEW_GOSSIP % ( '/' + util.escape_html(gossip.user.username), util.escape_html(prettyuser.user_or_first_name(gossip.user)), '/' + util.escape_html(gossip.about_id.username), util.escape_html(prettyuser.user_or_first_name(gossip.about_id)), '/profile/?n=' + util.escape_html(str(gossip.id)), util.escape_html(gossip.title) )) new_users = User.objects.order_by('-date_joined')[:const.NewsConst.Scroll.NEW_USER_MAX] for new_user in new_users: text.append(const.NewsConst.Scroll.NEW_USER % ( '/' + util.escape_html(new_user.username), util.escape_html(prettyuser.user_or_first_name(new_user)), )) # Save it in cache for 30 seconds cache.set('news_scroll', text, 30) prof.stop('news-scroll-no-cache') # Randomize it each time random.shuffle(text) return {'news_scroll': text}
def _add(request, form): # Check if the user is active or not if not request.user.is_active: return # Save news prof.start('news-add') news = form.save(commit=False) news.user = request.user news.save() prof.stop('news-add') # Publish on wall? if not('anonymous' in form.cleaned_data and form.cleaned_data['anonymous']): if 'fb_wall' in form.cleaned_data and form.cleaned_data['fb_wall']: try: FacebookSystem.post_gossip_to_wall(request.COOKIES, news, 'me') except facebook.GraphAPIError: # TODO: Error when the user logs out from facebook between # loading /news/add and submiting it pass # Save images if any prof.start('news-add-save-images') for image in request.FILES.getlist('images'): img = Image(news=news, image=image) img.save() prof.stop('news-add-save-images') # Send e-mail if news.about_id: NotificationSystem.create(news.about_id, const.NotifConst.NEW_GOSSIP_ME, {'username': request.user.username, 'anonymous': news.anonymous, 'name': prettyuser.user_or_first_name(request.user), })
def confirm(request): if request.is_ajax() or True: if not ("confirm" in request.GET and "id" in request.GET): raise Http404 try: friend = User.objects.get(pk=request.GET["id"]) except User.DoesNotExist: raise Http404 try: friendship = Friends.objects.get(user=friend, friend=request.user) if friendship.accepted or friendship.ignored: return HttpResponse("ERR") if request.GET["confirm"] == "accept": friendship.accepted = True friendship.ignored = False friendship.email_sent = True # Send an e-mail NotificationSystem.create( friend, const.NotifConst.FRIEND_CONFIRM, {"username": friend.username, "name": prettyuser.user_or_first_name(friend)}, ) else: friendship.accepted = False friendship.ignored = True friendship.email_sent = True friendship.save() except Friends.DoesNotExist: raise Http404 return HttpResponse("OK")