コード例 #1
0
ファイル: views.py プロジェクト: welbornprod/wp_site
def view_replies(request):
    """ View all replies for a paste. """
    pasteidarg = responses.get_request_arg(request, 'id')
    if pasteidarg is None:
        return responses.error404(request, 'No paste id given.')

    pasteobj = get_object(
        wp_paste.objects,
        paste_id=pasteidarg,
        disabled=False
    )
    if pasteobj is None:
        # No paste found.
        errmsg = 'Paste not found: {}'.format(pasteidarg)
        return responses.error404(request, errmsg)

    replies = pastetools.get_paste_children(pasteobj)
    context = {
        'paste': pasteobj,
        'replies': replies,
    }

    return responses.clean_response(
        'paste/replies.html',
        context=context,
        request=request)
コード例 #2
0
ファイル: views.py プロジェクト: welbornprod/wp_site
def paste_data(paste, doreplies=True, doparent=True):
    """ Build Paste JSON data for a valid paste. """
    resp = {
        'title': paste.title,
        'author': paste.author,
        'date': str(paste.publish_date),
        'content': paste.content,
        'id': paste.paste_id,
        'views': paste.view_count,
        'language': paste.language,
        'private': paste.private,
    }
    # Handle paste replies, by id.
    resp['replies'] = []
    if doreplies:
        resp['replies'] = [
            p.paste_id
            for p in pastetools.get_paste_children(paste).defer('content')
        ]
    resp['replycount'] = len(resp['replies'])
    # Get parent info, by id.
    resp['replyto'] = ''
    if doparent:
        parent = pastetools.get_paste_parent(paste)
        if parent:
            resp['replyto'] = parent.paste_id

    return resp
コード例 #3
0
def child_count(paste):
    """ Get count of children for this paste. """
    if paste and hasattr(paste, 'children'):
        # No content needed for the count.
        return (pastetools.get_paste_children(
            paste, order_by=None).defer('content').count())
    return 0
コード例 #4
0
def paste_data(paste, doreplies=True, doparent=True):
    """ Build Paste JSON data for a valid paste. """
    resp = {
        'title': paste.title,
        'author': paste.author,
        'date': str(paste.publish_date),
        'content': paste.content,
        'id': paste.paste_id,
        'views': paste.view_count,
        'language': paste.language,
        'private': paste.private,
    }
    # Handle paste replies, by id.
    resp['replies'] = []
    if doreplies:
        resp['replies'] = [
            p.paste_id
            for p in pastetools.get_paste_children(paste).defer('content')
        ]
    resp['replycount'] = len(resp['replies'])
    # Get parent info, by id.
    resp['replyto'] = ''
    if doparent:
        parent = pastetools.get_paste_parent(paste)
        if parent:
            resp['replyto'] = parent.paste_id

    return resp
コード例 #5
0
ファイル: paste_tags.py プロジェクト: welbornprod/wp_site
def child_count(paste):
    """ Get count of children for this paste. """
    if paste and hasattr(paste, 'children'):
        # No content needed for the count.
        return (
            pastetools.get_paste_children(paste, order_by=None)
            .defer('content')
            .count()
        )
    return 0
コード例 #6
0
def view_replies(request):
    """ View all replies for a paste. """
    pasteidarg = responses.get_request_arg(request, 'id')
    if pasteidarg is None:
        return responses.error404(request, 'No paste id given.')

    pasteobj = get_object(wp_paste.objects,
                          paste_id=pasteidarg,
                          disabled=False)
    if pasteobj is None:
        # No paste found.
        errmsg = 'Paste not found: {}'.format(pasteidarg)
        return responses.error404(request, errmsg)

    replies = pastetools.get_paste_children(pasteobj)
    context = {
        'paste': pasteobj,
        'replies': replies,
    }

    return responses.clean_response('paste/replies.html',
                                    context=context,
                                    request=request)
コード例 #7
0
ファイル: views.py プロジェクト: welbornprod/wp_site
def view_paste(request):
    """ View existing paste. """

    pasteidarg = responses.get_request_arg(request, 'id')
    replytoidarg = responses.get_request_arg(request, 'replyto')
    if pasteidarg and replytoidarg:
        # Can't have both id and replyto.
        return responses.error404(request, 'Invalid url.')

    # These are all optional, the template decides what to show,
    # based on what is available.
    pasteobj = None
    replytoobj = None
    replylast = None
    replies = None
    replycount = None

    if pasteidarg is not None:
        # Check for id aliases.
        id_alias = {
            'top': view_top,
            'latest': view_latest,
            'all': view_latest
        }
        id_view = id_alias.get(pasteidarg, None)
        if id_view is not None:
            return id_view(request)

        # Lookup existing paste by id.
        pasteobj = get_object(
            wp_paste.objects,
            paste_id=pasteidarg,
            disabled=False
        )

        if pasteobj is None:
            # Return a 404, that paste cannot be found.
            errmsg = 'Paste not found: {}'.format(pasteidarg)
            return responses.error404(request, errmsg)
        elif (not request.user.is_staff) and pasteobj.is_expired():
            errmsg = 'Paste is expired: {}'.format(pasteidarg)
            return responses.error404(request, errmsg)
        else:
            # Grab parent as the replyto object.
            replytoobj = pastetools.get_paste_parent(pasteobj)
            # Update some info about the paste.
            pasteobj.view_count += 1
            # Save changes.
            pasteobj.save()

    if replytoidarg is not None:
        # Lookup parent paste by id.
        replytoobj = get_object(
            wp_paste.objects,
            paste_id=replytoidarg
        )
        if replytoobj is None:
            # Return a 404, user is trying to reply to a dead paste.
            errmsg = 'Paste not found: {}'.format(replytoidarg)
            return responses.error404(request, errmsg)
        elif replytoobj.disabled:
            errmsg = 'Paste is disabled: {}'.format(replytoidarg)
            return responses.error_response(request, errmsg)

    # If this paste has a parent, get it and use it as the replyto.
    if pasteobj is not None:
        parent = pastetools.get_paste_parent(pasteobj)
        if parent is not None:
            replytoobj = parent

        replies = pastetools.get_paste_children(pasteobj)
        if replies:
            # Grab latest reply to this paste.
            replylast = replies[0]
            # Trim replies if they are too long to fit on the page.
            replycount = len(replies)
            replies = replies[:REPLYMAX]

    context = {
        'paste': pasteobj,
        'replyto': replytoobj,
        'replylast': replylast,
        'replies': replies,
        'replycount': replycount,
        'replymax': REPLYMAX,
    }
    return responses.clean_response(
        'paste/index.html',
        context=context,
        request=request)
コード例 #8
0
def paste_children(paste):
    """ Makes paste.children available in the templates, after passing
        through a gauntlet of checks (disabled, private, is_expired()).
    """
    return pastetools.get_paste_children(paste)
コード例 #9
0
ファイル: paste_tags.py プロジェクト: welbornprod/wp_site
def paste_children(paste):
    """ Makes paste.children available in the templates, after passing
        through a gauntlet of checks (disabled, private, is_expired()).
    """
    return pastetools.get_paste_children(paste)
コード例 #10
0
def view_paste(request):
    """ View existing paste. """

    pasteidarg = responses.get_request_arg(request, 'id')
    replytoidarg = responses.get_request_arg(request, 'replyto')
    if pasteidarg and replytoidarg:
        # Can't have both id and replyto.
        return responses.error404(request, 'Invalid url.')

    # These are all optional, the template decides what to show,
    # based on what is available.
    pasteobj = None
    replytoobj = None
    replylast = None
    replies = None
    replycount = None

    if pasteidarg is not None:
        # Check for id aliases.
        id_alias = {'top': view_top, 'latest': view_latest, 'all': view_latest}
        id_view = id_alias.get(pasteidarg, None)
        if id_view is not None:
            return id_view(request)

        # Lookup existing paste by id.
        pasteobj = get_object(wp_paste.objects,
                              paste_id=pasteidarg,
                              disabled=False)

        if pasteobj is None:
            # Return a 404, that paste cannot be found.
            errmsg = 'Paste not found: {}'.format(pasteidarg)
            return responses.error404(request, errmsg)
        elif (not request.user.is_staff) and pasteobj.is_expired():
            errmsg = 'Paste is expired: {}'.format(pasteidarg)
            return responses.error404(request, errmsg)
        else:
            # Grab parent as the replyto object.
            replytoobj = pastetools.get_paste_parent(pasteobj)
            # Update some info about the paste.
            pasteobj.view_count += 1
            # Save changes.
            pasteobj.save()

    if replytoidarg is not None:
        # Lookup parent paste by id.
        replytoobj = get_object(wp_paste.objects, paste_id=replytoidarg)
        if replytoobj is None:
            # Return a 404, user is trying to reply to a dead paste.
            errmsg = 'Paste not found: {}'.format(replytoidarg)
            return responses.error404(request, errmsg)
        elif replytoobj.disabled:
            errmsg = 'Paste is disabled: {}'.format(replytoidarg)
            return responses.error_response(request, errmsg)

    # If this paste has a parent, get it and use it as the replyto.
    if pasteobj is not None:
        parent = pastetools.get_paste_parent(pasteobj)
        if parent is not None:
            replytoobj = parent

        replies = pastetools.get_paste_children(pasteobj)
        if replies:
            # Grab latest reply to this paste.
            replylast = replies[0]
            # Trim replies if they are too long to fit on the page.
            replycount = len(replies)
            replies = replies[:REPLYMAX]

    context = {
        'paste': pasteobj,
        'replyto': replytoobj,
        'replylast': replylast,
        'replies': replies,
        'replycount': replycount,
        'replymax': REPLYMAX,
    }
    return responses.clean_response('paste/index.html',
                                    context=context,
                                    request=request)