示例#1
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)
示例#2
0
def view_image_id(request, imageid):
    """ View a single image by id. """

    images = wp_image.objects.filter(disabled=False, image_id=imageid)
    if not images:
        return responses.error404(
            request,
            'Image not found: {}'.format(imageid)
        )

    image = images[0]
    image.view_count += 1
    image.save()
    log.debug('Image view_count incremented to {}: {}'.format(
        image.view_count,
        image.filename))
    # Reusing part of the index view template.
    # Needs to be more refined, in it's own template.
    context = {
        'image': image,
        'images': (image,),
        'album': None,
    }
    return responses.clean_response(
        template_name='img/image.html',
        context=context,
        request=request)
示例#3
0
def view_post(request, identifier):
    """ view a post by identifier.
        identifier can be:
            pk (id)
            slug
            title
    """

    post = blogtools.get_post_byany(identifier)

    if post is None:
        # No post found with that identifier
        return responses.error404(
            request,
            'Blog post not found: {}'.format(identifier)
        )

    # build blog post.

    # get short title for window-text
    if len(post.title) > 20:
        post_title_short = '..{}'.format(post.title[len(post.title) - 30:])
    else:
        post_title_short = post.title

    # no content found.
    if not blogtools.get_post_body(post):
        errmsg = 'Sorry, no content found for this post.'
        errlink = '\n'.join((
            '<a href=\'/blog\'><span>',
            'Click here to go back to the main blog page.',
            '</span></a>',
        ))
        return responses.alert_message(request, errmsg, body_message=errlink)

    # increment view count
    try:
        post.view_count += 1
        post.save()
    except Exception as exsave:
        log.error('Unable to increment view_count for: '
                  '{}\n{}'.format(post, exsave))

    # Build clean HttpResponse with post template...
    context = {
        'post_title_short': post_title_short,
        'enable_comments': post.enable_comments,
        'blog_post': post,
        'related_projects': post.get_projects(),
    }
    return responses.clean_response(
        'blogger/post.html',
        context=context,
        request=request,
    )
示例#4
0
def view_post(request, identifier):
    """ view a post by identifier.
        identifier can be:
            pk (id)
            slug
            title
    """

    post = blogtools.get_post_byany(identifier)

    if post is None:
        # No post found with that identifier
        return responses.error404(request,
                                  'Blog post not found: {}'.format(identifier))

    # build blog post.

    # get short title for window-text
    if len(post.title) > 20:
        post_title_short = '..{}'.format(post.title[len(post.title) - 30:])
    else:
        post_title_short = post.title

    # no content found.
    if not blogtools.get_post_body(post):
        errmsg = 'Sorry, no content found for this post.'
        errlink = '\n'.join((
            '<a href=\'/blog\'><span>',
            'Click here to go back to the main blog page.',
            '</span></a>',
        ))
        return responses.alert_message(request, errmsg, body_message=errlink)

    # increment view count
    try:
        post.view_count += 1
        post.save()
    except Exception as exsave:
        log.error('Unable to increment view_count for: '
                  '{}\n{}'.format(post, exsave))

    # Build clean HttpResponse with post template...
    context = {
        'post_title_short': post_title_short,
        'enable_comments': post.enable_comments,
        'blog_post': post,
        'related_projects': post.get_projects(),
    }
    return responses.clean_response(
        'blogger/post.html',
        context=context,
        request=request,
    )
示例#5
0
def view_paste_raw(request):
    """ View a paste as plain text. """
    pasteidarg = responses.get_request_arg(request, 'id')
    if not pasteidarg:
        # /paste/raw doesn't provide anything by itself.
        return responses.error404(request, 'No paste id provided.')

    try:
        pasteobj = wp_paste.objects.get(paste_id=pasteidarg, disabled=False)
    except wp_paste.DoesNotExist:
        return responses.error404(request, 'Paste not found.')
    except Exception as ex:
        log.error('Error retrieving paste: {}\n{}'.format(pasteidarg, ex))
        return responses.error404(request, 'Paste not found.')

    if pasteobj is None:
        return responses.error404(request, 'Paste content not found.')
    elif (not request.user.is_staff) and pasteobj.is_expired():
        # Staff may view expired pastes.
        return responses.error404(request, 'Paste is expired.')

    try:
        pasteobj.view_count += 1
        pasteobj.save()
    except Exception as ex:
        log.error('Unable to update view_count!\n{}'.format(ex))
    try:
        content = pasteobj.content
    except Exception as ex:
        log.error('paste.content is not available!\n{}'.format(ex))
        return responses.error404(request, 'Paste content not found.')

    # Valid paste, return the plain content.
    return responses.text_response(content)
示例#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
def view_misc_any(request, identifier):
    """ View a specific misc item. """

    misc = misctools.get_by_identifier(identifier)
    if not misc:
        # No misc item found by that identifier
        return responses.error404(
            request, 'Misc. object not found: {}'.format(identifier))

    context = {
        'misc': misc,
    }
    return responses.clean_response('misc/misc.html',
                                    context=context,
                                    request=request)
示例#8
0
def view_misc_any(request, identifier):
    """ View a specific misc item. """

    misc = misctools.get_by_identifier(identifier)
    if not misc:
        # No misc item found by that identifier
        return responses.error404(
            request,
            'Misc. object not found: {}'.format(identifier)
        )

    context = {
        'misc': misc,
    }
    return responses.clean_response(
        'misc/misc.html',
        context=context,
        request=request)
示例#9
0
def view_project(request, project, requested_page, source=None):
    """ Returns project page for individual project.
        Project object must be passed
        (usually from request_any() which retrieves projects by name,alias,id)
        If a list/tuple of projects is passed as 'project' then it will
        be used as 'possible matches' for bad project name.
    """

    # default flags
    use_screenshots = False

    # no project, no matches found (or error retrieving).
    if not project:
        return responses.error404(
            request, 'Project not found: {}'.format(requested_page))

    # possible matches passed?
    matches = project if isinstance(project, set) else None
    if matches:
        project = None

    # Grab project info
    if project:
        # this will tell the template to add the screenshots javascript.
        use_screenshots = project.screenshot_dir != ''
        # keep track of how many times this has been viewed.
        project.view_count += 1
        project.save()

    # Grab projects list for vertical menu
    all_projects = wp_project.objects.filter(disabled=False).order_by('name')
    context = {
        'requested_page': requested_page,
        'projects': all_projects,
        'project': project,
        'matches': matches,
        'use_screenshots': use_screenshots,
    }
    return responses.clean_response('projects/project.html',
                                    context=context,
                                    request=request)
示例#10
0
def view_image_id(request, imageid):
    """ View a single image by id. """

    images = wp_image.objects.filter(disabled=False, image_id=imageid)
    if not images:
        return responses.error404(request,
                                  'Image not found: {}'.format(imageid))

    image = images[0]
    image.view_count += 1
    image.save()
    log.debug('Image view_count incremented to {}: {}'.format(
        image.view_count, image.filename))
    # Reusing part of the index view template.
    # Needs to be more refined, in it's own template.
    context = {
        'image': image,
        'images': (image, ),
        'album': None,
    }
    return responses.clean_response(template_name='img/image.html',
                                    context=context,
                                    request=request)
示例#11
0
def view_paste_raw(request):
    """ View a paste as plain text. """
    pasteidarg = responses.get_request_arg(request, 'id')
    if not pasteidarg:
        # /paste/raw doesn't provide anything by itself.
        return responses.error404(request, 'No paste id provided.')

    try:
        pasteobj = wp_paste.objects.get(
            paste_id=pasteidarg,
            disabled=False
        )
    except wp_paste.DoesNotExist:
        return responses.error404(request, 'Paste not found.')
    except Exception as ex:
        log.error('Error retrieving paste: {}\n{}'.format(pasteidarg, ex))
        return responses.error404(request, 'Paste not found.')

    if pasteobj is None:
        return responses.error404(request, 'Paste content not found.')
    elif (not request.user.is_staff) and pasteobj.is_expired():
        # Staff may view expired pastes.
        return responses.error404(request, 'Paste is expired.')

    try:
        pasteobj.view_count += 1
        pasteobj.save()
    except Exception as ex:
        log.error('Unable to update view_count!\n{}'.format(ex))
    try:
        content = pasteobj.content
    except Exception as ex:
        log.error('paste.content is not available!\n{}'.format(ex))
        return responses.error404(request, 'Paste content not found.')

    # Valid paste, return the plain content.
    return responses.text_response(content)
示例#12
0
def view_results(request, args=None):
    """ Process number/word given by request args. """

    errors = None
    results = None
    total = None
    rawquery = args['query']
    if not rawquery:
        return responses.error404(request, msgs=('Invalid url args.', ))

    lookupfunc, query, method = get_lookup_func(rawquery)
    cache_used = False
    # Try cached results first (for numbers only)
    if method == 'number':
        cachedresult = pwtools.lookup_results(query)
        if cachedresult:
            cache_used = True
            log.debug('Using cached result: {}'.format(cachedresult))
            total = cachedresult.attempts
            results = pwtools.get_results(cachedresult)
            if results:
                # Cancel lookup, we have cached results.
                lookupfunc = None

        else:
            log.debug('No cached found for: {}'.format(query))

    if lookupfunc:
        # Get wp words file.
        wordfile = os.path.join(
            settings.BASE_DIR,
            'apps/phonewords/words'
        )
        if os.path.isfile(wordfile):
            # Get results.
            try:
                rawresults = lookupfunc(query, wordfile=wordfile)
            except ValueError as exval:
                errors = exval
            except Exception as ex:
                log.error('Error looking up number: {}\n{}'.format(query, ex))
                errors = ex
            else:
                # Good results, fix them.
                try:
                    results, total = fix_results(rawresults)
                except Exception as ex:
                    log.error('Error fixing results:\n{}'.format(ex))
                    errmsg = (
                        'Sorry, there was an error parsing the results.<br>{}'
                    )
                    errors = Exception(errmsg.format(ex))
                # Cache these results for later if its a number.
                if method == 'number' and (not cache_used):
                    pwtools.save_results(query, results, total)
        else:
            log.error('missing word file: {}'.format(wordfile))
            errors = Exception('Can\'t find word file!')

    # Return response.
    context = {
        'version': app_version,
        'hasargs': True,
        'query': args['query'],
        'results': results,
        'errors': errors,
        'total': total,
    }

    return responses.clean_response(
        'phonewords/index.html',
        context=context,
        request=request)
示例#13
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)
示例#14
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)