Esempio n. 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)
    story_is_visible = story.is_visible_for(request.user)
    comments = Comment.objects\
        .filter(story=story, status=Comment.PUBLISHED, owner__is_active=True)\
        .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})
Esempio n. 2
0
def question(request, base62_id, order=None, show_delete=False, **kwargs):
    qmeta = get_object_or_404(QuestionMeta, id=base62.to_decimal(base62_id))

    order = {
        'popular': '-like_count',
        'featured': 'is_featured',
        'recent': '-created_at'
    }.get(order, '-created_at')

    stories = Story.objects\
        .from_question_meta(qmeta)\
        .filter(status=Story.PUBLISHED,
                visible_for=Story.VISIBLE_FOR_EVERYONE).order_by(order)

    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,
            'stories': paginated(request, stories, settings.STORIES_PER_PAGE)
        })
Esempio n. 3
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.status == QuestionMeta.REDIRECTED and 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)
        })
Esempio n. 4
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)
    story_is_visible = story.is_visible_for(request.user)
    comments = Comment.objects\
        .filter(story=story, status=Comment.PUBLISHED, owner__is_active=True)\
        .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
        })
Esempio n. 5
0
def _index(request, stories, extra):
    """
    If user is authenticated and not registered email we will show
    Register your email message
    """
    show_email_message = request.user.is_authenticated() and \
        not request.user.email
    stories = paginated(request, stories, settings.STORIES_PER_PAGE)
    recommended_questions = QuestionMeta.objects.\
        filter(is_featured=True).order_by('?')[:10]
    ctx = {
        'stories': stories,
        'recommended_questions': recommended_questions,
        'show_email_message': show_email_message
    }
    if extra:
        ctx.update(extra)
    return render(request, "index2.html", ctx)
Esempio n. 6
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)})
Esempio n. 7
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)
Esempio n. 8
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)})
Esempio n. 9
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)
Esempio n. 10
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)
        })