Ejemplo n.º 1
0
def sources_edit(request, source=None, template="cache/sources_edit.html"):

    if request.method == "POST":
        instance = TileSource.objects.get(name=source)
        source_form = TileSourceForm(request.POST,instance=instance)
        if source_form.is_valid():
            source_form.save()
            reloadTileSources(proxy=False)
            reloadTileSources(proxy=True)
            ###
            context_dict = {
                'source': instance,
                'source_form': TileSourceForm(instance=instance)
            }
            return HttpResponseRedirect(reverse('sources_list',args=()))
        else:
            return HttpResponse(
                'An unknown error has occured.',
                content_type="text/plain",
                status=401
            )
    else:
        instance = TileSource.objects.get(name=source)
        context_dict = {
            'source': instance,
            'source_form': TileSourceForm(instance=instance)
        }
        return render_to_response(
            template,
            RequestContext(request, context_dict))
Ejemplo n.º 2
0
def proxy_tms(request, origin=None, slug=None, z=None, x=None, y=None, u=None, ext=None):

    #starttime = time.clock()
    # Check Existing Tile Sourcesi
    match_tilesource = None
    tilesources = getTileSources(proxy=True)
    for tilesource in tilesources:
        if tilesource['name'] == slug:
            match_tilesource = tilesource
            break

    if match_tilesource:
        if match_tilesource['origin'] != origin:
            print "Origin is not correct.  Tilesource is unique, but origin need to match too."
            print tilesource['origin']
            return None
        else:
            tile = _requestTile(
                request,
                tileservice=None,
                tilesource=match_tilesource,
                tileorigin=match_tilesource['origin'],
                z=z,x=x,y=y,u=u,ext=ext)
            #print "Time Elapsed: "+str(time.clock()-starttime)
            return tile


    # Check Existing Tile Origins to see if we need to create a new tile source
    match_tileorigin = None
    if origin:
        tileorigins = getTileOrigins(proxy=True)
        for tileorigin in tileorigins:
            if tileorigin.name == origin:
                match_tileorigin = tileorigin
                break

    if match_tileorigin:
        to = match_tileorigin
        if to.multiple:
            ts_url = to.url.replace('{slug}', slug)
            if TileSource.objects.filter(url=ts_url).count() > 0:
                print "Error: This souldn't happen.  You should have matched the tilesource earlier so you don't duplicate"
                return None
            exts = string_to_list(to.extensions)
            ts_pattern = url_to_pattern(ts_url, extensions=exts)
            ts = TileSource(auto=True,url=ts_url,pattern=ts_pattern,name=slug,type=to.type,extensions=exts,origin=to)
            ts.save()
            reloadTileSources(proxy=False)
            reloadTileSources(proxy=True)
            return _requestTile(request,tileservice=None,tilesource=tilesource,z=z,x=x,y=y,u=u,ext=ext)
        else:
            ts = TileSource(auto=True,url=to.url,pattern=to.pattern,name=to.name,type=to.type,extensions=to.extensions)
            ts.save()
            reloadTileSources(proxy=False)
            reloadTileSources(proxy=True)
            return _requestTile(request,tileservice=None,tilesource=tilesource,z=z,x=x,y=y,u=u,ext=ext)
    else:
        return None
Ejemplo n.º 3
0
def sources_new(request, origin=None, template="cache/sources_edit.html"):

    if request.method == "POST":
        source_form = TileSourceForm(request.POST)
        if source_form.is_valid():
            source_form.save()
            reloadTileSources(proxy=False)
            reloadTileSources(proxy=True)
            ###
            context_dict = {
                'source_form': TileSourceForm()
            }
            return HttpResponseRedirect(reverse('sources_list',args=()))
        else:
            return HttpResponse(
                'An unknown error has occured.'+json.dumps(source_form.errors),
                content_type="text/plain",
                status=401
            )

    else:
        source_form = None
        if origin:
            origin_object = TileOrigin.objects.get(name=origin)
            if origin_object.multiple:
                source_form = TileSourceForm(initial={'origin': origin_object, 'auto': False, 'type': origin_object.type, 'url': origin_object.url, 'extensions': [u'png']})
            else:
                source_form = TileSourceForm(initial={'origin': origin_object, 'auto': False, 'type': origin_object.type, 'url': origin_object.url, 'extensions': [u'png']})
        else:
            source_form = TileSourceForm()
        context_dict = {
            'source_form': source_form
        }
        return render_to_response(
            template,
            RequestContext(request, context_dict))
Ejemplo n.º 4
0
def proxy(request):
    PROXY_ALLOWED_HOSTS = getattr(settings, 'PROXY_ALLOWED_HOSTS', ())

    host = None

    if 'url' not in request.GET:
        return HttpResponse("The proxy service requires a URL-encoded URL as a parameter.",
                            status=400,
                            content_type="text/plain"
                            )

    raw_url = request.GET['url']
    url = urlsplit(raw_url)
    locator = url.path
    if url.query != "":
        locator += '?' + url.query
    if url.fragment != "":
        locator += '#' + url.fragment

    if not settings.DEBUG:
        if not validate_host(url.hostname, PROXY_ALLOWED_HOSTS):
            return HttpResponse("DEBUG is set to False but the host of the path provided to the proxy service"
                                " is not in the PROXY_ALLOWED_HOSTS setting.",
                                status=403,
                                content_type="text/plain"
                                )
    headers = {}

    if settings.SESSION_COOKIE_NAME in request.COOKIES and is_safe_url(url=raw_url, host=host):
        headers["Cookie"] = request.META["HTTP_COOKIE"]

    if request.method in ("POST", "PUT") and "CONTENT_TYPE" in request.META:
        headers["Content-Type"] = request.META["CONTENT_TYPE"]

    print "Raw URL: "+ raw_url
    match_regex = None
    match_tilesource = None

    # Try to match against existing tile sources
    #tilesources = TileSource.objects.exclude(pattern__isnull=True).exclude(pattern__exact='')
    tilesources = getTileSources(proxy=True)
    for tilesource in tilesources:
        match = match_pattern_url(tilesource.pattern, raw_url)
        if match:
            match_regex = match
            match_tilesource = tilesource
            break

    if match_tilesource and match_regex:
        return proxy_tilesource(request, match_tilesource, match_regex)
    #else:
    #    return HttpResponse('No matching tilesource found.',RequestContext(request, {}), status=404)

    # Try to match against existing origins that can automatically create tile sources (auto=true)
    match_tileorigin = None
    #tileorigins = TileOrigin.objects.exclude(pattern__isnull=True).exclude(pattern__exact='').filter(auto=True)
    tileorigins = getTileOrigins(proxy=True)
    for tileorigin in tileorigins:
        match = match_pattern_url(tileorigin.pattern, raw_url)
        if match:
            match_regex = match
            match_tileorigin = tileorigin
            break

    if match_tileorigin and match_regex:
        to = match_tileorigin
        if to.multiple:
            slug = getRegexValue(match_regex, 'slug')
            ts_url = to.url.replace('{slug}', slug)
            #print "ts_url: "+ts_url
            if TileSource.objects.filter(url=ts_url).count() > 0:
                print "Error: This souldn't happen.  You should have matched the tilesource earlier so you don't duplicate"
                return None
            exts = string_to_list(to.extensions)
            ts_pattern = url_to_pattern(ts_url, extensions=exts)
            ts = TileSource(auto=True,url=ts_url,pattern=ts_pattern,name=slug,type=to.type,extensions=exts,origin=to)
            ts.save()
            reloadTileSources(proxy=False)
            reloadTileSources(proxy=True)
            return proxy_tilesource(request, ts, match_regex)
        else:
            ts = TileSource(auto=True,url=to.url,pattern=to.pattern,name=to.name,type=to.type,extensions=to.extensions)
            ts.save()
            reloadTileSources(proxy=False)
            reloadTileSources(proxy=True)
            return proxy_tilesource(request, ts, match_regex)
    else:
        return HttpResponse('No matching tile origin or tile source found.',RequestContext(request, {}), status=404)