Ejemplo n.º 1
0
def save_snippet(node, page):

    snippet = Snippet.create(
        page=page,
        text_above=extract_text_above(node),
        text_below=extract_text_below(node),
        header=extract_header_above(node),
        code=extract_code(node),
        line_count=len(node.text.split('\n')),
    )

    for tok_str in extract_tokens(node):
        token, _ = Token.get_or_create(string=tok_str)
        SnippetToken.create(
            snippet=snippet,
            token=token,
        )

    for comment_str in extract_comments(node):
        comment, _ = Comment.get_or_create(string=comment_str)
        SnippetComment.create(
            snippet=snippet,
            comment=comment,
        )

    return snippet
Ejemplo n.º 2
0
def comment(request, pk=None):
    """ Create a new comment. Django comments framework sucks! """

    if request.GET.get('delete'):
        if request.user.is_staff:
            get_object_or_404(SnippetComment, pk=pk).delete()
            request.session['flash'] = ['Comment deleted succesfully',
                                        'success']
        else:
            request.session['flash'] = ['Permission denied', 'error']
        return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))
    else:
        data = {}
        snippet = get_object_or_404(Snippet, pk=pk)
        if request.user.is_authenticated():
            body = request.POST.get('body')
            if body:
                comment = SnippetComment(snippet=snippet, user=request.user,
                                         body=body)
                comment.save()
                # send notification if you are not the author
                # TODO: This code should be catched by an event handler
                if snippet.author != request.user:
                    profile = UserProfile.objects.get(user=snippet.author)
                    #User wants to recieve a notification
                    if profile.user_commented:
                        queue = EmailQueue(
                            mail_to=snippet.author.email,
                            mail_subject="Your snippet has been commented",
                            mail_body=render_to_string(
                                'emails/user_commented.txt', {
                                    'user': snippet.author,
                                    'username': request.user.username,
                                    'comment': comment,
                                    'snippet': snippet,
                                    'SITE': request.META.get('HTTP_HOST',
                                        'localhost')
                                }
                            )
                        )
                        queue.save()
                data['content'] = render_to_string('elements/comment.html',
                                                   {'comment': comment})
            else:
                data['error'] = 'Body field is required'
        else:
            data['error'] = 'You must login to post a comment'
        return JsonResponse(data)
Ejemplo n.º 3
0
def comment(request, id = None):
    """
    Create a new comment.
    Django comments framework (v1.2) doesn't have a simple ajax api.
    Only staff users can delete comments. Also only logged users can post.
    """
    if request.GET.get('delete'):
        if request.user.is_staff:
            get_object_or_404(SnippetComment, pk=id).delete()
            request.session['flash'] = ['Comment deleted succesfully', 'success'];
        else:
            request.session['flash'] = ['Permission denied', 'error'];
        return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/accounts/profile/'))
    else:
        data = {}
        snippet = get_object_or_404(Snippet, pk=id)
        if request.user.is_authenticated:
            body = request.POST.get('body')
            if body:
                comment = SnippetComment(snippet = snippet, user = request.user, body = body)
                comment.save()
                if snippet.author != request.user: # send notification if you are not the author
                    profile = UserProfile.objects.get(user=snippet.author)
                    if profile.user_commented: #User wants to recieve a notification
                        queue = EmailQueue(
                            mail_to=snippet.author.email,
                            mail_subject="Your snippet has been commented",
                            mail_body=render_to_string('emails/user_commented.txt', {
                                'user': snippet.author,
                                'username': request.user.username,
                                'comment': comment,
                                'snippet': snippet,
                                'SITE': request.META['HTTP_HOST']}
                            )
                        )
                        queue.save()
                data['content'] = render_to_string('elements/comment.html', {'comment': comment})
            else:
                data['error'] = 'Body field is required'
        else:
            data['error'] = 'You must login to post a comment'
        return JsonResponse(data)