Пример #1
0
def view_picture(request):
    picture = request.ductus.resource
    mime_type = picture.blob.mime_type

    # figure out size to send
    thumbnail_str = ''
    if 'max_size' in request.GET:
        try:
            max_width, max_height = [
                int(n) for n in request.GET['max_size'].split(',')
            ]
        except ValueError:
            return query_string_not_found(request)

        try:
            thumbnail_size = six.next(
                iter(s for s in __allowed_thumbnail_sizes
                     if s <= (max_width, max_height)))
        except StopIteration:
            # refuse to make a thumbnail this small
            return query_string_not_found(request)

        thumbnail_str = '_'.join(str(s) for s in thumbnail_size)
        if picture.rotation:
            thumbnail_str += '_' + picture.rotation

    #TODO: we probably shouldn't convert png images to jpeg...
    return mediacache_redirect(request, picture.blob.href, 'image/jpeg',
                               thumbnail_str, picture)
Пример #2
0
def view_picture(request):
    picture = request.ductus.resource
    mime_type = picture.blob.mime_type

    # figure out size to send
    thumbnail_str = ''
    if 'max_size' in request.GET:
        try:
            max_width, max_height = [int(n) for n in
                                     request.GET['max_size'].split(',')]
        except ValueError:
            return query_string_not_found(request)

        try:
            thumbnail_size = six.next(iter(s for s in __allowed_thumbnail_sizes
                                           if s <= (max_width, max_height)))
        except StopIteration:
            # refuse to make a thumbnail this small
            return query_string_not_found(request)

        thumbnail_str = '_'.join(str(s) for s in thumbnail_size)
        if picture.rotation:
            thumbnail_str += '_' + picture.rotation

    #TODO: we probably shouldn't convert png images to jpeg...
    return mediacache_redirect(request, picture.blob.href, 'image/jpeg',
                               thumbnail_str, picture)
Пример #3
0
def main_document_view(request, urn=None, wiki_page=None, wiki_revision=None):
    """Dispatches the appropriate view for a resource/page
    """

    requested_view = request.GET.get('view', None)

    resource_database = get_resource_database()

    if requested_view == 'raw':
        etag = __handle_etag(request, ['raw', urn], weak=False)
        # fixme: we may also want to set last-modified, expires, max-age
        try:
            data_iterator = resource_database[urn]
        except KeyError:
            raise Http404
        response = HttpResponse(list(data_iterator), # see django #6527
                                content_type='application/octet-stream')
        response["ETag"] = etag
        return response

    if request.method == "GET":
        unvaried_etag = [urn, bool(wiki_page),
                         request.META.get("QUERY_STRING", "")]
        varied_etag = unvaried_etag + [request.LANGUAGE_CODE,
                                       bool(request.is_secure()),
                                       request.META.get("HTTP_COOKIE", "")]
        unvaried_etag = __handle_etag(request, unvaried_etag)
        varied_etag = __handle_etag(request, varied_etag)

    try:
        resource = resource_database.get_resource_object(urn)
    except KeyError:
        raise Http404("resource does not exist")
    except UnexpectedHeader as e:
        raise Http404(str(e))
    request.ductus = DuctusRequestInfo(resource, requested_view,
                                       wiki_page, wiki_revision)

    try:
        f = registered_views[resource.fqn][requested_view]
    except KeyError:
        try:
            f = registered_views[None][requested_view]
        except KeyError:
            return query_string_not_found(request)
    if not f.meets_requirements(request.ductus):
        return query_string_not_found(request)
    response = f(request)

    if request.method == "GET" and not response.has_header("ETag"):
        if getattr(response, "_unvarying", False):
            response["ETag"] = unvaried_etag
        else:
            vary_headers = set([h.strip().lower() for h in response.get("Vary", "").split(',') if h])
            if vary_headers.issubset(set(['cookie', 'accept-language'])):
                response["ETag"] = varied_etag
    return response
Пример #4
0
def view_flashcard_deck(request):
    interactions_array = request.ductus.resource.interactions.array

    if not interactions_array:
        return edit_flashcard_deck(request)

    try:
        interaction = interactions_array[int(request.GET.get('interaction', 0))]
    except (ValueError, IndexError):
        return query_string_not_found(request)
    else:
        interaction = interaction.get()
        interaction_view = registered_interaction_views[interaction.fqn]
        return interaction_view(request, interaction)
Пример #5
0
def view_flashcard_deck(request):
    interactions_array = request.ductus.resource.interactions.array

    if not interactions_array:
        return edit_flashcard_deck(request)

    try:
        interaction = interactions_array[int(request.GET.get('interaction',
                                                             0))]
    except (ValueError, IndexError):
        return query_string_not_found(request)
    else:
        interaction = interaction.get()
        interaction_view = registered_interaction_views[interaction.fqn]
        return interaction_view(request, interaction)
Пример #6
0
def view_diff(request):
    this = request.ductus.resource
    try:
        that = get_resource_database().get_resource_object(request.GET["diff"])
    except KeyError:
        # This could mean there is no "diff" in the query string, or that the
        # resource object doesn't exist.  Let's first try to diff against one
        # of the parents, and fail out if that doesn't work.
        if this.common.parents.array:
            that = this.common.parents.array[0].get()
        else:
            return query_string_not_found(request)

    return render_to_response("wiki/diff.html", {
        'diff': Diff(this, that),
    }, RequestContext(request))
Пример #7
0
def view_wikipage(request, prefix, pagename):
    """Used for pages represented by a WikiPage"""

    if not is_legal_wiki_pagename(prefix, pagename):
        raise Http404

    if request.method == 'POST' and not request.GET.get('view', None):
        return _fully_handle_blueprint_post(request, prefix, pagename)

    name = join_pagename(prefix, pagename)
    try:
        page = WikiPage.objects.get(name=name)
    except WikiPage.DoesNotExist:
        page = None

    if page:
        if "oldid" in request.GET:
            try:
                revision = WikiRevision.objects.get(id=request.GET["oldid"], page=page)
            except (ValueError, WikiRevision.DoesNotExist):
                return query_string_not_found(request)
            if not revision.urn:
                return query_string_not_found(request)
        else:
            revision = page.get_latest_revision()
    else:
        revision = None

    if revision is None and getattr(settings, "DUCTUS_WIKI_REMOTE", None):
        # See if DUCTUS_WIKI_REMOTE has the page
        try:
            remote_url = "%s%s?view=urn" % (settings.DUCTUS_WIKI_REMOTE, iri_to_uri(urlquote(u'%s/%s' % (prefix, pagename))))
            remote_urn = json.loads(urlopen(remote_url).read(1000))["urn"]
            # we never actually save this WikiPage or WikiRevision to the database
            if page is None:
                page, page_created = WikiPage.objects.get_or_create(name=name)
            revision = WikiRevision(page=page, urn=remote_urn[4:])
        except urllib2_HTTPError:
            pass

    if revision and revision.urn:
        urn = 'urn:' + revision.urn
    else:
        urn = None

    if request.GET.get('view', None) == 'urn':
        if revision:
            return render_json_response({"urn": urn})

    response = None

    if urn:
        response = main_document_view(request, urn, page, revision)
        if isinstance(response, SuccessfulEditRedirect):
            return _handle_successful_wikiedit(request, response, page)
        response["X-Ductus-URN"] = urn
    else:
        requested_view = request.GET.get("view", None)
        request.ductus = DuctusRequestInfo(None, requested_view, page, revision)
        if requested_view:
            f = registered_views[None].get(requested_view, None)
            if f and f.meets_requirements(request.ductus):
                response = f(request)

    if response is None:
        response = new_wikipage(request, prefix, pagename)

    # wikipage urls expire immediately since they can frequently be edited
    patch_response_headers(response, cache_timeout=0)
    patch_cache_control(response, must_revalidate=True)

    return response