12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- from django.http import JsonResponse, HttpResponseForbidden
- from django.views.generic import View
- from .models import Contrib
- class JSONContribView(View):
- def get(self, request):
- return JsonResponse({
- "id": self.ID,
- "license": self.LICENSE,
- "features": self.get_features(),
- })
- PLACE_PROPERTIES = [
- 'floor', 'angles', 'orientations', 'roof', 'floor', 'floor_total']
- class PublicJSON(JSONContribView):
- ID = 'public'
- LICENSE = {
- "type": "ODC-BY-1.0",
- "url": "http:\/\/opendatacommons.org\/licenses\/by\/1.0\/"
- }
- def get_features(self):
- contribs = Contrib.objects.all()
- data = []
- for i in contribs:
- if not i.is_public():
- continue
- data.append({
- "id": i.pk,
- "type": "Feature",
- "geometry": {
- "coordinates": [
- i.latitude,
- i.longitude,
- ],
- "type": "Point",
- },
- "properties": {
- "contrib_type": i.contrib_type,
- "name": i.get_public_field('name'),
- "place": {
- k: i.get_public_field(k) for k in self.PLACE_PROPERTIES
- },
- "comment": i.get_public_field('comment'),
- }
- })
- return data
- class PrivateJSON(JSONContribView):
- ID = 'private'
- LICENSE = {
- "type": "Copyright",
- }
- def dispatch(self, request, *args, **kwargs):
- if hasattr(request, 'user') and request.user.is_staff:
- return super().dispatch(request, *args, **kwargs)
- else:
- return HttpResponseForbidden('Need staff access')
- def get_features(self):
- contribs = Contrib.objects.all()
- data = []
- for i in contribs:
- if not i.is_public():
- continue
- data.append({
- "id": i.pk,
- "type": "Feature",
- "geometry": {
- "coordinates": [
- i.latitude,
- i.longitude,
- ],
- "type": "Point",
- },
- "properties": {
- "contrib_type": i.contrib_type,
- "name": i.name,
- "place": {
- k: getattr(i, k) for k in self.PLACE_PROPERTIES
- },
- "comment": i.comment,
- "phone": i.phone,
- "email": i.email
- }
- })
- return data
|