Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GeoJSONLayerView and LoginRequiredMixin #89

Closed
lpofredc opened this issue Sep 4, 2017 · 5 comments
Closed

GeoJSONLayerView and LoginRequiredMixin #89

lpofredc opened this issue Sep 4, 2017 · 5 comments

Comments

@lpofredc
Copy link

lpofredc commented Sep 4, 2017

Hi,

I'm using GeoJSONLayerView in my project. While I am using GeoJSONLayerView in my urls.py, it works.
My views.py:

from django.contrib.auth.mixins import LoginRequiredMixin
from djgeojson.views import GeoJSONLayerView, TiledGeoJSONLayerView

class GeoJSONData (LoginRequiredMixin, GeoJSONLayerView):
    pass

class TiledGeoJSONData (LoginRequiredMixin, TiledGeoJSONLayerView):
    pass

My urls.py

# -*- coding: utf-8 -*-
from django.conf.urls import url
from django.contrib.auth.decorators import login_required
from djgeojson.views import TiledGeoJSONLayerView, GeoJSONLayerView

from sights.models import Place, Territory, Municipality
from .views import GeoJSONData, TiledGeoJSONData

urlpatterns = [
    # url(r'^place.geojson$', GeoJSONLayerView.as_view(model=Place, properties=('name', 'pk', 'municipality')),
    #     name='geodata_place'),
    url(r'^place.geojson$', GeoJSONData.as_view(model=Place, properties=('name', 'pk', 'municipality')),
        name='geodata_place'),
    url(r'^place/(?P<z>\d+)/(?P<x>\d+)/(?P<y>\d+).geojson$',
        TiledGeoJSONData.as_view(model=Place, trim_to_boundary=True, properties=('name', 'pk', 'municipality')),
        name='geodata_place_tile'),
    url(r'^municipality.geojson$', login_required(
        GeoJSONLayerView.as_view(model=Municipality, precision=3, simplify=0.5, properties=('name', 'code'))),
        name='geodata_municipality'),
    url(r'^territory.geojson$', login_required(
        GeoJSONLayerView.as_view(model=Territory, precision=3, simplify=0.5, properties=('name', 'code'))),
        name='geodata_territory'),
]

My template:

{% extends 'base_leaflet.html' %}
{% load leaflet_tags %}
{% leaflet_js %}
{% leaflet_css %}
{% load geojson_tags %}
{% block content %}
    <h1><i class="fi-map"></i> Carte des localités</h1>
    {% leaflet_map "main" %}
    <br/>
    <script type="text/javascript">
                var dataurl = '{% url "geodata_place" %}';
                window.addEventListener("map:init", function (event) {
                    var markers = L.markerClusterGroup();
                    var map = event.detail.map;
                    // Download GeoJSON data with Ajax
                    fetch(dataurl)
                        .then(function (resp) {
                            return resp.json();
                        })
                        .then(function (data) {
                            markers.addLayer(L.geoJson(data, {
                                onEachFeature: function onEachFeature(feature, layer) {
                                    var props = feature.properties;
                                    var content = `<h4>${props.pk}${props.name}</h4>
        		                				<p>${props.municipality}</p>
        		                				<p><a class="button" align="center" style="color:white;" href="/place/${props.pk}/detail" target="_blank" style="font-color:white"><strong>Détails</strong></a></p>`;
                                    layer.bindPopup(content);
                                }
                            })).addTo(map);
                        });
                    map.fitBounds(markers.getBounds());
                    map.addControl(new L.Control.Fullscreen());
                });


    </script>
{% endblock %}

But when i'm trying to use LoginRequiredMixin with GeoJSONLayerView, I can't get data on the map where as i can read my geojson data directly in my browser.
Any explanation?
Thanks.

@Gagaro
Copy link
Member

Gagaro commented Sep 5, 2017

Hi,

Do you see the requests in your browser inspector ? Are the responses 403 or 200 ?

@lpofredc
Copy link
Author

lpofredc commented Sep 5, 2017

Hi,

responses are 302 with this parameters (layer is not loaded on map) whereas i can read this place.geojson from browser.

# views.py
class GeoJSONData (LoginRequiredMixin, GeoJSONLayerView):
    pass

# urls.py
    url(r'^place.geojson$', GeoJSONData.as_view(model=Place, properties=('name', 'pk', 'municipality')),
        name='geodata_place'),

I get this message in firefox web console
SyntaxError: JSON.parse: unexpected character at line 3 column 1 of the JSON data[En savoir plus] (source inconnue)
and this message in my terminal

[05/Sep/2017 21:55:19] "GET /api/place.geojson HTTP/1.1" 302 0
[05/Sep/2017 21:55:20] "GET /login/?next=/api/place.geojson HTTP/1.1" 200 16421

while i'm already logged in

response 200 with this config (layer loaded)

# urls.py
    url(r'^place.geojson$', GeoJSONLayerView.as_view(model=Place, properties=('name', 'pk', 'municipality')),
        name='geodata_place'),

@Gagaro
Copy link
Member

Gagaro commented Sep 6, 2017

fetch does not use cookie by defaut, you need to pass the cookies in order to be authenticated by Django.

From https://stackoverflow.com/q/34558264/2575355 :

fetch(url, {
  credentials: "include"
})

@lpofredc
Copy link
Author

lpofredc commented Sep 6, 2017

Thanks a lot, it solved my problem. I noticed than this error occur when using AJAX.

@Gagaro
Copy link
Member

Gagaro commented Sep 7, 2017

👍

@Gagaro Gagaro closed this as completed Sep 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants