Beispiel #1
0
def article(request, article_number):
    if request.is_ajax():
        data = serializers.serialize('json', Article.objects.filter(order=article_number))
        return HttpResponse(
            data,
            content_type='application/json'
        )
    articles = get_articles(request.user)
    article_id = Article.objects.get(order=article_number).id
    if int(article_number) in [a.order for a in articles]:
        if request.POST:
            comment_form = MessageForm(request.POST)
            if comment_form.is_valid():
                comment_commit = comment_form.save(commit=False)
                comment_commit.ip = get_ip_address(request)
                comment_commit.time_date = now()
                comment_commit.is_comment = True
                comment_commit.save()
                comment = Comment(article_id=article_id, message_id=Message.objects.latest('id').id)
                comment.save()
                comment_form = make_comment(request)
        else:
            comment_form = make_comment(request)
        comments = Comment.objects.filter(article_id=article_id)
        comments_data = [Message.objects.get(id=item.message_id) for item in comments]
        for item in comments_data:
            try:
                item.user_id = User.objects.get(username=item.name).id
                break
            except User.DoesNotExist:
                item.user_id = None
        return render_to_response(
            'article.html',
            {
                'articles': articles,
                'showed_article': Article.objects.get(order=article_number),
                'username': auth.get_user(request).username,
                'comment_form': comment_form,
                'comments': comments_data
            },
            context_instance=RequestContext(request)
        )
    return render_to_response(
        'denied.html',
        {
            'articles': articles,
            'username': auth.get_user(request).username
        }
    )
Beispiel #2
0
def feedback(request, page_number):
    if request.POST:
        message_form = MessageForm(request.POST)
        if message_form.is_valid():
            message_form_commit = message_form.save(commit=False)
            message_form_commit.ip = get_ip_address(request)
            message_form_commit.time_date = now()
            message_form_commit.is_comment = 0
            message_form_commit.save()
            if request.is_ajax():
                return HttpResponse(
                    json.dumps({
                        "is_validate": True,
                        "html": message_form.cleaned_data["name"]
                    }),
                    content_type='application/json'
                )
            return render_to_response(
                'thanks.html',
                {
                    'your_name': message_form.cleaned_data['name'],
                    'articles': get_articles(request.user),
                    'username': auth.get_user(request).username
                },
                context_instance=RequestContext(request)
            )
    else:
        if request.user != AnonymousUser():
            message_form = MessageForm(
                initial={
                    'name': request.user.username,
                    'email': request.user.email
                }
            )
        else:
            message_form = MessageForm()
    answered_messages = Paginator(
        [(Message.objects.get(id=answer.message_id), answer.text) for answer in Answer.objects.all()],
        NUM_PAGES
    )
    html = render_to_response(
        'feedback_content.html',
        {
            'message_form': message_form,
            'answered_messages': answered_messages.page(page_number)
        },
        context_instance=RequestContext(request)
    ).serialize()[42:]
    if request.is_ajax():
        data = {
            "is_validate": False,
            "html": html.decode()
        }
        json_data = json.dumps(data)
        return HttpResponse(
            json_data,
            content_type='application/json'
        )
    return render_to_response(
        "feedback.html",
        {
            "feedback_content": html,
            'articles': get_articles(request.user),
            'username': auth.get_user(request).username
        },
        context_instance=RequestContext(request)
    )