Login

Custom DRF browsable API interface for InBBoxFilter of django-rest-framework-gis

Author:
iamtio
Posted:
March 2, 2017
Language:
Python
Version:
1.0
Score:
0 (after 0 ratings)

DRF browsable API interface for django-rest-framework-gis InBBoxFilter

Tested with

Django==1.10.5
django-filter==1.0.1
djangorestframework==3.5.4
djangorestframework-gis==0.11

Screenshot

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from rest_framework_gis.filters import InBBoxFilter
from django_filters.rest_framework import DjangoFilterBackend


class InBBoxHTMLMixin:
    template = """
    {% load i18n %}
    <style type="text/css">
    #geofilter input[type="text"]{
        width: 100px;
    }
    </style>
    <h2>{% trans "BBox filter" %}</h2>
    <form id="geofilter" action="" method="get">
        <p>
            <input type="text" id="gf-lat1" placeholder="min lat">
            <input type="text" id="gf-lon1" placeholder="min lon">
        </p>
        <p>
            <input type="text" id="gf-lat2" placeholder="max lat">
            <input type="text" id="gf-lon2" placeholder="max lon">
        </p>
        <input id="gf-result" type="hidden" name="{{bbox_param}}">
        <button type="submit" class="btn btn-primary">{% trans "Submit" %}
        </button>
    </form>
    <script language="JavaScript">
    (function() {
        document.getElementById("geofilter").onsubmit = function(){
            var result = document.getElementById("gf-result");
            var box = [
                document.getElementById("gf-lat1").value,
                document.getElementById("gf-lon1").value,
                document.getElementById("gf-lat2").value,
                document.getElementById("gf-lon2").value
            ];
            if(!box.every(function(i){ return i.length }))
                return false;
            result.value = box.join(",");
        }
    })();
    </script>
    """

    def to_html(self, request, queryset, view):
        return template_render(
            Template(self.template), {'bbox_param': self.bbox_param}, request
        )


class CustomBBoxFilter(InBBoxHTMLMixin, InBBoxFilter):
    bbox_param = 'position_bbox'

class CustomViewSet(ModelViewSet):
    filter_backends = (CustomBBoxFilter, DjangoFilterBackend,)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

Please login first before commenting.