Frames

The API Views

0
1
2
3
1from django.core import serializers
2from django.http import JsonResponse, HttpResponse
3from django.db.models import Q
4from rest_framework import viewsets, generics
5from rest_framework.decorators import api_view
6from salesforce.models import Adopter, School, MapBoxDataset
7from errata.models import ERRATA_RESOURCES
8from global_settings.models import StickyNote, Footer
9from wagtail.images.models import Image
10from wagtail.documents.models import Document
11from .models import ProgressTracker
12from .serializers import AdopterSerializer, ImageSerializer, DocumentSerializer, ProgressSerializer
13from flags.sources import get_flags
14
15class AdopterViewSet(viewsets.ModelViewSet):
16 queryset = Adopter.objects.all()
17 serializer_class = AdopterSerializer
18
19
20class ImageViewSet(viewsets.ModelViewSet):
21 queryset = Image.objects.all()
22 serializer_class = ImageSerializer
23
24
25class DocumentViewSet(viewsets.ModelViewSet):
26 queryset = Document.objects.all()
27 serializer_class = DocumentSerializer
28
29 def get_queryset(self):
30 queryset = Document.objects.all()
31 search = self.request.query_params.get('search', None)
32 if search is not None:
33 queryset = queryset.filter(title__icontains=search)
34 return queryset
35
36
37class ProgressViewSet(viewsets.ModelViewSet):
38 queryset = ProgressTracker.objects.all()
39 serializer_class = ProgressSerializer
40
41 def get_queryset(self):
42 queryset = ProgressTracker.objects.all()
43 account_id = self.request.query_params.get('account_id', None)
44 if account_id is not None:
45 queryset = queryset.filter(account_id=account_id)
46 return queryset
47
48def sticky_note(request):
49 sticky_note = StickyNote.for_site(request.site)
50
51 return JsonResponse({
52 'show': sticky_note.show,
53 'expires': sticky_note.expires,
54 'content': sticky_note.content,
55 'emergency_expires': sticky_note.emergency_expires,
56 'emergency_content': sticky_note.emergency_content,
57 })
58
59
60def footer(request):
61 footer = Footer.for_site(request.site)
62
63 return JsonResponse({
64 'supporters': footer.supporters,
65 'copyright': footer.copyright,
66 'ap_statement': footer.ap_statement,
67 'facebook_link': footer.facebook_link,
68 'twitter_link': footer.twitter_link,
69 'linkedin_link': footer.linkedin_link,
70 })
71
72def mapbox(request):
73 mapbox = MapBoxDataset.objects.all()
74 response = []
75
76 for mapbox_instance in mapbox:
77 response.append({
78 'name': mapbox_instance.name,
79 'style': mapbox_instance.style_url
80 })
81
82 return JsonResponse(response, safe=False)
83
84def errata_fields(request):
85 response = []
86
87 if request.GET.get('field', None) == 'resources':
88 for field, verbose in ERRATA_RESOURCES:
89 response.append({'field': field, 'verbose': verbose})
90
91 return JsonResponse(response, safe=False)
92
93def schools(request):
94 format = request.GET.get('format', 'json')
95 q = request.GET.get('q', False)
96 name = request.GET.get('name', False)
97 id = request.GET.get('id', False)
98 type = request.GET.get('type', False)
99 physical_country = request.GET.get('physical_country', False)
100 physical_state_province = request.GET.get('physical_state_province', False)
101 physical_city = request.GET.get('physical_city', False)
102 key_institutional_partner = request.GET.get('key_institutional_partner', None)
103 achieving_the_dream_school = request.GET.get('achieving_the_dream_school', None)
104 saved_one_million = request.GET.get('saved_one_million', None)
105 testimonial = request.GET.get('testimonial', None)
106
107 schools = School.objects.filter(long__isnull=False, lat__isnull=False)
108
109 if format == 'geojson':
110 data = []
111 for school in schools:
112 if school.lat and school.long:
113 item = {
114 'type': 'Feature',
115 'geometry': {
116 'type': 'Point',
117 'coordinates': [float(school.long), float(school.lat)]
118 },
119 'properties': {
120 'name': school.name
121 }
122 }
123 data.append(item)
124 return JsonResponse(data, safe=False)
125 elif format == 'json':
126 if name:
127 schools = schools.filter(name__icontains=name)
128 if id:
129 schools = schools.filter(pk=id)
130 if type:
131 schools = schools.filter(type__icontains=type)
132 if physical_city:
133 schools = schools.filter(physical_city=physical_city)
134 if physical_state_province:
135 schools = schools.filter(physical_state_province=physical_state_province)
136 if physical_country:
137 schools = schools.filter(physical_country=physical_country)
138 if key_institutional_partner:
139 schools = schools.filter(key_institutional_partner=True)
140 if achieving_the_dream_school:
141 schools = schools.filter(achieving_the_dream_school=True)
142 if saved_one_million:
143 schools = schools.filter(all_time_savings__gte = 1000000)
144
145 if testimonial:
146 schools = schools.filter(testimonial__isnull=False)
147
148 if q:
149 schools = schools.filter((Q(name__icontains=q) | Q(physical_city__icontains=q)| Q(physical_state_province__icontains=q)| Q(physical_country__icontains=q)))
150
151 response = serializers.serialize("json", schools)
152 return HttpResponse(response, content_type='application/json')
153 else:
154 return JsonResponse({'error': 'Invalid format requested.'})
155
156def flags(request):
157 q_flag = request.GET.get('flag', False)
158
159 list_flags = get_flags(sources=None, ignore_errors=False)
160
161 if not q_flag:
162 # We return the list of all flags.
163 data = []
164 for (flag, fobject) in list_flags.items():
165 data.append({
166 'name': flag
167 })
168 return JsonResponse(data, safe=False)
169 else:
170 # We return the enabled settings for the set flag.
171 if q_flag in list_flags:
172 data = [{'name': q_flag, "conditions": []}]
173 for condition in list_flags[q_flag].conditions:
174 data[0]['conditions'].append({
175 'required': condition.__dict__['required'],
176 'type': condition.__dict__['condition'],
177 'value': condition.__dict__['value']
178 })
179 return JsonResponse(data, safe=False)
180 else:
181 return JsonResponse([{'error': 'The flag does not exist.'}], safe=False)
182
This is the view that controls the adopter list for use on the adoption page.