Example #1
0
def story(request, base62_id):
    action_keys = filter(lambda i: i in ['delete', 'publish'], request.POST)
    action_key = action_keys[0] if action_keys else None
    if action_key:
        method = {
            u'delete': _delete_story,
            u'publish': _publish_story
        }.get(action_key)
        if method:
            story = get_object_or_404(Story,
                                      id=base62.to_decimal(base62_id),
                                      owner=request.user)
            return method(request, story)
    statuses_in = [Story.DRAFT, Story.PUBLISHED] if \
        request.user.is_authenticated() else [Story.PUBLISHED]
    story = get_object_or_404(Story,
                              id=base62.to_decimal(base62_id),
                              status__in=statuses_in,
                              owner__is_active=True)
    blocked_user_ids = compute_blocked_user_ids_for(request.user)
    story_is_visible = story.is_visible_for(request.user,
                                            blocked_user_ids=blocked_user_ids)
    comments = Comment.objects\
        .filter(story=story, status=Comment.PUBLISHED)\
        .from_active_owners()\
        .visible_for(request.user)\
        .select_related('owner__userprofile')
    comments = paginated(request, comments, settings.COMMENTS_PER_PAGE)
    if request.user.is_authenticated():
        if request.method == 'POST' and request.POST.get('action') ==\
           'create_comment':
            comment_form = CommentForm(request.POST,
                                       owner=request.user,
                                       story=story)
            if comment_form.is_valid():
                comment = comment_form.save()
                return HttpResponseRedirect(comment.get_absolute_url())
        else:
            comment_form = CommentForm(owner=request.user, story=story)
    else:
        comment_form = None
    return render(
        request, 'story/story_detail.html', {
            'story': story,
            'current_site': Site.objects.get_current(),
            'story_is_visible': story_is_visible,
            'comments': comments,
            'comment_form': comment_form
        })
Example #2
0
def followers(request, username):
    if request.method == 'POST':
        action_form = FollowerActionForm(request.POST, username=username)
        if action_form.is_valid():
            result = action_form.save()
            if result:
                messages.warning(request, result)
    else:
        action_form = FollowerActionForm(username=username)
    user = get_object_or_404(User, username=username)
    user_follows = UserFollow.objects.filter(target=user)\
        .prefetch_related('follower__userprofile')
    return render(
        request,
        'auth/followers.html',
        {'profile_user': user,
         'action_form': action_form,
         'user_follows': paginated(request, user_follows,
                                   settings.QUESTIONS_PER_PAGE)})
Example #3
0
def index(request, listing='public'):
    """
    If user is authenticated and not registered email we will show
    Register your email message
    """
    if not request.user.is_authenticated():
        listing = 'public'
    show_email_message = request.user.is_authenticated() and \
        not request.user.email
    stories = Story.objects.build(
        requested_user=request.user, listing=listing)
    stories = paginated(request, stories, settings.STORIES_PER_PAGE)
    recommended_questions = QuestionMeta.objects.\
        filter(is_featured=True).order_by('?')[:10]
    ctx = {'stories': stories,
           'listing': listing,
           'recommended_questions': recommended_questions,
           'show_email_message': show_email_message}
    return render(request, "index2.html", ctx)
Example #4
0
def question(request, base62_id, ordering=None, show_delete=False, **kwargs):
    qmeta = get_object_or_404(QuestionMeta, id=base62.to_decimal(base62_id))

    if qmeta.redirected_to:
        return HttpResponseRedirect(qmeta.redirected_to.get_absolute_url())

    stories = Story.objects.build(frm=qmeta, ordering=ordering)

    if not ordering:
        ordering = 'recent'

    if request.user.is_authenticated():
        request_answer_form = RequestAnswerForm(questioner=request.user,
                                                qmeta=qmeta)
        if request.method == 'POST':
            request_answer_form = RequestAnswerForm(
                request.POST, questioner=request.user, qmeta=qmeta,)
            if request_answer_form.is_valid():
                usernames = request_answer_form.save()
                if usernames:
                    messages.success(request, 'Your question sent to: %s' %
                                              ', '.join(usernames))
                else:
                    messages.error(request, 'Could\'t find any users to send.')
                return HttpResponseRedirect(qmeta.get_absolute_url())
    else:
        request_answer_form = None

    if request.user.is_authenticated():
        is_following = QuestionFollow.objects\
            .filter(target=qmeta,
                    follower=request.user,
                    status=QuestionFollow.FOLLOWING).exists()
    else:
        is_following = False

    return render(request, "question/question_detail.html", {
        'question': qmeta,
        'is_following': is_following,
        'request_answer_form': request_answer_form,
        'ordering': ordering,
        'stories': paginated(request, stories, settings.STORIES_PER_PAGE)})
Example #5
0
def followers(request, username):
    if request.method == 'POST':
        action_form = FollowerActionForm(request.POST, username=username)
        if action_form.is_valid():
            result = action_form.save()
            if result:
                messages.warning(request, result)
    else:
        action_form = FollowerActionForm(username=username)
    user = get_object_or_404(User, username=username)
    user_follows = UserFollow.objects\
        .filter(target=user, follower__is_active=True)\
        .prefetch_related('follower__userprofile')
    return render(
        request,
        'auth/followers.html',
        {'profile_user': user,
         'action_form': action_form,
         'user_follows': paginated(request, user_follows,
                                   settings.QUESTIONS_PER_PAGE)})
Example #6
0
def story(request, base62_id):
    action_keys = filter(lambda i: i in ["delete", "publish"], request.POST)
    action_key = action_keys[0] if action_keys else None
    if action_key:
        method = {u"delete": _delete_story, u"publish": _publish_story}.get(action_key)
        if method:
            story = get_object_or_404(Story, id=base62.to_decimal(base62_id), owner=request.user)
            return method(request, story)
    statuses_in = [Story.DRAFT, Story.PUBLISHED] if request.user.is_authenticated() else [Story.PUBLISHED]
    story = get_object_or_404(Story, id=base62.to_decimal(base62_id), status__in=statuses_in, owner__is_active=True)
    blocked_user_ids = compute_blocked_user_ids_for(request.user)
    story_is_visible = story.is_visible_for(request.user, blocked_user_ids=blocked_user_ids)
    comments = (
        Comment.objects.filter(story=story)
        .from_active_owners()
        .visible_for(request.user)
        .select_related("owner__profile")
    )
    comments = paginated(request, comments, settings.COMMENTS_PER_PAGE)
    if request.user.is_authenticated():
        if request.method == "POST" and request.POST.get("action") == "create_comment":
            comment_form = CommentForm(request.POST, owner=request.user, story=story)
            if comment_form.is_valid():
                comment = comment_form.save()
                return HttpResponseRedirect(comment.get_absolute_url())
        else:
            comment_form = CommentForm(owner=request.user, story=story)
    else:
        comment_form = None
    return render(
        request,
        "story/story_detail.html",
        {
            "story": story,
            "current_site": Site.objects.get_current(),
            "story_is_visible": story_is_visible,
            "comments": comments,
            "comment_form": comment_form,
        },
    )
Example #7
0
def story(request, base62_id):
    action_keys = filter(lambda i: i in ['delete', 'publish'], request.POST)
    action_key = action_keys[0] if action_keys else None
    if action_key:
        method = {u'delete': _delete_story,
                  u'publish': _publish_story}.get(action_key)
        if method:
            story = get_object_or_404(Story, id=base62.to_decimal(base62_id),
                                      owner=request.user)
            return method(request, story)
    statuses_in = [Story.DRAFT, Story.PUBLISHED] if \
        request.user.is_authenticated() else [Story.PUBLISHED]
    story = get_object_or_404(Story, id=base62.to_decimal(base62_id),
                              status__in=statuses_in, owner__is_active=True)
    blocked_user_ids = compute_blocked_user_ids_for(request.user)
    story_is_visible = story.is_visible_for(request.user,
                                            blocked_user_ids=blocked_user_ids)
    comments = Comment.objects\
        .filter(story=story)\
        .from_active_owners()\
        .visible_for(request.user)\
        .select_related('owner__profile')
    comments = paginated(request, comments, settings.COMMENTS_PER_PAGE)
    if request.user.is_authenticated():
        if request.method == 'POST' and request.POST.get('action') ==\
           'create_comment':
            comment_form = CommentForm(request.POST, owner=request.user,
                                       story=story)
            if comment_form.is_valid():
                comment = comment_form.save()
                return HttpResponseRedirect(comment.get_absolute_url())
        else:
            comment_form = CommentForm(owner=request.user, story=story)
    else:
        comment_form = None
    return render(request, 'story/story_detail.html',
                  {'story': story, 'current_site': Site.objects.get_current(),
                   'story_is_visible': story_is_visible, 'comments': comments,
                   'comment_form': comment_form})
Example #8
0
def profile(request, username=None, listing='public', action=None):

    user = get_object_or_404(User, username=username, is_active=True) if \
        username else request.user

    user_is_blocked_me, user_is_blocked_by_me,\
        i_am_follower_of_user, have_pending_follow_request \
        = False, False, False, False

    if request.user.is_authenticated():
        user_is_blocked_me = user.is_blocked_by(request.user)
        user_is_blocked_by_me = user.is_blocked_by(request.user)
        i_am_follower_of_user = request.user.is_following(user)
        have_pending_follow_request = \
            request.user.has_pending_follow_request(user)

    ctx = {'profile_user': user,
           'listing': listing,
           'user_is_blocked_by_me': user_is_blocked_by_me,
           'user_is_blocked_me': user_is_blocked_me,
           'have_pending_follow_request': have_pending_follow_request,
           'i_am_follower_of_user': i_am_follower_of_user}

    # If there are not blocks, fill ctx with answers
    if not (user_is_blocked_me or user_is_blocked_by_me):
        stories = Story.objects.build(
            frm=user, requested_user=request.user, listing=listing)
        ctx['stories'] = paginated(request, stories,
                                   settings.STORIES_PER_PAGE)
    if request.POST:
        question_form = QuestionForm(request.POST, questioner=request.user,
                                     questionee=user)
        if question_form.is_valid():
            question_form.save()
            messages.success(request, _('Your question sent to user.'))
            return HttpResponseRedirect(user.get_absolute_url())
    else:
        question_form = QuestionForm(questioner=request.user,
                                     questionee=user)

    if action:
        ctx['action'] = action
        follow_form = FollowForm(follower=request.user,
                                 target=user,
                                 action=action)
        if request.POST:
            follow_form = FollowForm(request.POST,
                                     follower=request.user,
                                     target=user,
                                     action=action)
            if follow_form.is_valid():
                follow_form.save()
                if action == 'follow':
                    messages.success(
                        request, _('Follow request sent to user'))
                elif action == 'unfollow':
                    messages.success(
                        request, _('You are not a follower anymore'))
                elif action == 'block':
                    messages.success(
                        request, _('You have blocked this user'))
                elif action == 'unblock':
                    messages.success(
                        request, _('You have unblocked this user'))
                return HttpResponseRedirect(user.get_absolute_url())

        ctx['follow_form'] = follow_form
    ctx['question_form'] = question_form

    return render(request, "auth/user_detail.html", ctx)
Example #9
0
def profile(request, username=None, listing='public', action=None):

    user = get_object_or_404(User, username=username, is_active=True) if \
        username else request.user

    user_is_blocked_me, user_is_blocked_by_me,\
        i_am_follower_of_user, have_pending_follow_request \
        = False, False, False, False

    if request.user.is_authenticated():
        user_is_blocked_me = user.is_blocked_by(request.user)
        user_is_blocked_by_me = user.is_blocked_by(request.user)
        i_am_follower_of_user = request.user.is_following(user)
        have_pending_follow_request = \
            request.user.has_pending_follow_request(user)

    ctx = {'profile_user': user,
           'listing': listing,
           'user_is_blocked_by_me': user_is_blocked_by_me,
           'user_is_blocked_me': user_is_blocked_me,
           'have_pending_follow_request': have_pending_follow_request,
           'i_am_follower_of_user': i_am_follower_of_user}

    # If there are not blocks, fill ctx with answers
    if not (user_is_blocked_me or user_is_blocked_by_me):
        stories = Story.objects.build(
            frm=user, requested_user=request.user, listing=listing)
        ctx['stories'] = paginated(request, stories,
                                   settings.STORIES_PER_PAGE)
    if request.POST:
        question_form = QuestionForm(request.POST, questioner=request.user,
                                     questionee=user)
        if question_form.is_valid():
            question_form.save()
            messages.success(request, _('Your question sent to user.'))
            return HttpResponseRedirect(user.get_absolute_url())
    else:
        question_form = QuestionForm(questioner=request.user,
                                     questionee=user)

    if action:
        ctx['action'] = action
        follow_form = FollowForm(follower=request.user,
                                 target=user,
                                 action=action)
        if request.POST:
            follow_form = FollowForm(request.POST,
                                     follower=request.user,
                                     target=user,
                                     action=action)
            if follow_form.is_valid():
                follow_form.save()
                if action == 'follow':
                    messages.success(
                        request, _('Follow request sent to user'))
                elif action == 'unfollow':
                    messages.success(
                        request, _('You are not a follower anymore'))
                elif action == 'block':
                    messages.success(
                        request, _('You have blocked this user'))
                elif action == 'unblock':
                    messages.success(
                        request, _('You have unblocked this user'))
                return HttpResponseRedirect(user.get_absolute_url())

        ctx['follow_form'] = follow_form
    ctx['question_form'] = question_form

    return render(request, "auth/user_detail.html", ctx)