We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Paginate display of all sites
The display of sites should be broken into pages, and each page should have navigation links to jump to other pages.


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `workshops/views.py`
Content:
```
1 import yaml
2 
3 from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
4 from django.core.urlresolvers import reverse
5 from django.http import HttpResponseRedirect
6 from django.shortcuts import render, get_object_or_404
7 from django.views.generic.edit import CreateView, UpdateView
8 from django.db.models import Count
9 
10 from workshops.models import Site, Airport, Event, Person, Task, Cohort, Skill, Trainee, Badge
11 from workshops.forms import InstructorMatchForm
12 from workshops.util import earth_distance
13 
14 #------------------------------------------------------------
15 
16 ITEMS_PER_PAGE = 25
17 
18 #------------------------------------------------------------
19 
20 def index(request):
21     '''Home page.'''
22     upcoming_events = Event.objects.upcoming_events()
23     context = {'title' : 'Home Page',
24                'upcoming_events' : upcoming_events}
25     return render(request, 'workshops/index.html', context)
26 
27 #------------------------------------------------------------
28 
29 SITE_FIELDS = ['domain', 'fullname', 'country', 'notes']
30 
31 def all_sites(request):
32     '''List all sites.'''
33     all_sites = Site.objects.order_by('domain')
34     user_can_add = request.user.has_perm('edit')
35     context = {'title' : 'All Sites',
36                'all_sites' : all_sites,
37                'user_can_add' : user_can_add}
38     return render(request, 'workshops/all_sites.html', context)
39 
40 def site_details(request, site_domain):
41     '''List details of a particular site.'''
42     site = Site.objects.get(domain=site_domain)
43     events = Event.objects.filter(site=site)
44     context = {'title' : 'Site {0}'.format(site),
45                'site' : site,
46                'events' : events}
47     return render(request, 'workshops/site.html', context)
48 
49 class SiteCreate(CreateView):
50     model = Site
51     fields = SITE_FIELDS
52 
53 class SiteUpdate(UpdateView):
54     model = Site
55     fields = SITE_FIELDS
56     slug_field = 'domain'
57     slug_url_kwarg = 'site_domain'
58 
59 #------------------------------------------------------------
60 
61 AIRPORT_FIELDS = ['iata', 'fullname', 'country', 'latitude', 'longitude']
62 
63 def all_airports(request):
64     '''List all airports.'''
65     all_airports = Airport.objects.order_by('iata')
66     user_can_add = request.user.has_perm('edit')
67     context = {'title' : 'All Airports',
68                'all_airports' : all_airports,
69                'user_can_add' : user_can_add}
70     return render(request, 'workshops/all_airports.html', context)
71 
72 def airport_details(request, airport_iata):
73     '''List details of a particular airport.'''
74     airport = Airport.objects.get(iata=airport_iata)
75     context = {'title' : 'Airport {0}'.format(airport),
76                'airport' : airport}
77     return render(request, 'workshops/airport.html', context)
78 
79 class AirportCreate(CreateView):
80     model = Airport
81     fields = AIRPORT_FIELDS
82 
83 class AirportUpdate(UpdateView):
84     model = Airport
85     fields = AIRPORT_FIELDS
86     slug_field = 'iata'
87     slug_url_kwarg = 'airport_iata'
88 
89 #------------------------------------------------------------
90 
91 def all_persons(request):
92     '''List all persons.'''
93     all_persons = Person.objects.order_by('family', 'personal')
94     context = {'title' : 'All Persons',
95                'all_persons' : all_persons}
96     return render(request, 'workshops/all_persons.html', context)
97 
98 def person_details(request, person_id):
99     '''List details of a particular person.'''
100     person = Person.objects.get(id=person_id)
101     context = {'title' : 'Person {0}'.format(person),
102                'person' : person}
103     return render(request, 'workshops/person.html', context)
104 
105 #------------------------------------------------------------
106 
107 def all_events(request):
108     '''List all events.'''
109 
110     all_events = Event.objects.order_by('slug')
111 
112     # Get the number of items requested per page, default to 25
113     # This is important for unit testing, we need to be
114     # able to specify how many items to expect
115     items = request.GET.get('items_per_page', ITEMS_PER_PAGE)
116 
117     # Only paginate if the number of items is not 'all'
118     if not items == 'all':
119 
120         # If items is not an integer, set it to the default
121         try:
122             items = int(items)
123         except ValueError:
124             items = ITEMS_PER_PAGE
125 
126         events_paginator = Paginator(all_events, items)
127 
128         # Get the page number requested, if any
129         page = request.GET.get('page')
130 
131         try:
132             events = events_paginator.page(page)
133         except PageNotAnInteger:
134             # If page is not an integer, deliver first page.
135             events = events_paginator.page(1)
136         except EmptyPage:
137             # If page is out of range (e.g. 9999), deliver last page of results.
138             events = events_paginator.page(events_paginator.num_pages)
139     else:
140         events = all_events
141 
142     context = {'title' : 'All Events',
143                'all_events' : events}
144 
145     return render(request, 'workshops/all_events.html', context)
146 
147 def event_details(request, event_slug):
148     '''List details of a particular event.'''
149     event = Event.objects.get(slug=event_slug)
150     context = {'title' : 'Event {0}'.format(event),
151                'event' : event}
152     return render(request, 'workshops/event.html', context)
153 
154 #------------------------------------------------------------
155 
156 TASK_FIELDS = ['event', 'person', 'role']
157 
158 def all_tasks(request):
159     '''List all tasks.'''
160     all_tasks = Task.objects.order_by('event', 'person', 'role')
161     user_can_add = request.user.has_perm('edit')
162     context = {'title' : 'All Tasks',
163                'all_tasks' : all_tasks,
164                'user_can_add' : user_can_add}
165     return render(request, 'workshops/all_tasks.html', context)
166 
167 def task_details(request, event_slug, person_id, role_name):
168     '''List details of a particular task.'''
169     task = Task.objects.get(event__slug=event_slug, person__id=person_id, role__name=role_name)
170     context = {'title' : 'Task {0}'.format(task),
171                'task' : task}
172     return render(request, 'workshops/task.html', context)
173 
174 class TaskCreate(CreateView):
175     model = Task
176     fields = TASK_FIELDS
177 
178 class TaskUpdate(UpdateView):
179     model = Task
180     fields = TASK_FIELDS
181     pk_url_kwarg = 'task_id'
182 
183     def get_object(self):
184         """
185         Returns the object the view is displaying.
186         """
187 
188         event_slug = self.kwargs.get('event_slug', None)
189         person_id = self.kwargs.get('person_id', None)
190         role_name = self.kwargs.get('role_name', None)
191 
192         return get_object_or_404(Task, event__slug=event_slug, person__id=person_id, role__name=role_name)
193 
194 #------------------------------------------------------------
195 
196 COHORT_FIELDS = ['name', 'start', 'active', 'venue', 'qualifies']
197 
198 def all_cohorts(request):
199     '''List all cohorts.'''
200     all_cohorts = Cohort.objects.order_by('start')
201     user_can_add = request.user.has_perm('edit')
202     context = {'title' : 'All Cohorts',
203                'all_cohorts' : all_cohorts,
204                'user_can_add' : user_can_add}
205     return render(request, 'workshops/all_cohorts.html', context)
206 
207 def cohort_details(request, cohort_name):
208     '''List details of a particular cohort.'''
209     cohort = Cohort.objects.get(name=cohort_name)
210     trainees = Trainee.objects.filter(cohort_id=cohort.id)
211     context = {'title' : 'Cohort {0}'.format(cohort),
212                'cohort' : cohort,
213                'trainees' : trainees}
214     return render(request, 'workshops/cohort.html', context)
215 
216 class CohortCreate(CreateView):
217     model = Cohort
218     fields = COHORT_FIELDS
219 
220 class CohortUpdate(UpdateView):
221     model = Cohort
222     fields = COHORT_FIELDS
223     slug_field = 'name'
224     slug_url_kwarg = 'cohort_name'
225 
226 #------------------------------------------------------------
227 
228 def match(request):
229     persons = None
230 
231     if request.method == 'POST':
232         form = InstructorMatchForm(request.POST)
233         if form.is_valid():
234 
235             # Filter by skills.
236             persons = Person.objects.filter(airport__isnull=False)
237             skills = []
238             for s in Skill.objects.all():
239                 if form.cleaned_data[s.name]:
240                     skills.append(s)
241             persons = persons.have_skills(skills)
242 
243             # Sort by location.
244             loc = (float(form.cleaned_data['latitude']),
245                    float(form.cleaned_data['longitude']))
246             persons = [(earth_distance(loc, (p.airport.latitude, p.airport.longitude)), p)
247                        for p in persons]
248             persons.sort()
249             persons = [x[1] for x in persons[:10]]
250 
251         else:
252             pass # FIXME: error message
253 
254     # if a GET (or any other method) we'll create a blank form
255     else:
256         form = InstructorMatchForm()
257 
258     return render(request, 'workshops/match.html', {'form': form, 'persons' : persons})
259 
260 #------------------------------------------------------------
261 
262 def _export_badges():
263     '''Collect badge data as YAML.'''
264     result = {}
265     for badge in Badge.objects.all():
266         persons = Person.objects.filter(award__badge_id=badge.id)
267         result[badge.name] = [{"user" : p.slug, "name" : p.fullname()} for p in persons]
268     return result
269 
270 def _export_instructors():
271     '''Collect instructor airport locations as YAML.'''
272     # Exclude airports with no instructors, and add the number of instructors per airport
273     airports = Airport.objects.exclude(person=None).annotate(num_persons=Count('person'))
274     return [{'airport' : str(a.fullname),
275              'latlng' : '{0},{1}'.format(a.latitude, a.longitude),
276              'count'  : a.num_persons}
277             for a in airports]
278 
279 def export(request, name):
280     '''Export data as YAML for inclusion in main web site.'''
281     data = None
282     if name == 'badges':
283         title, data = 'Badges', _export_badges()
284     elif name == 'instructors':
285         title, data = 'Instructor Locations', _export_instructors()
286     else:
287         title, data = 'Error', None # FIXME - need an error message
288     context = {'title' : title,
289                'data' : data}
290     return render(request, 'workshops/export.html', context)
291
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/workshops/views.py b/workshops/views.py
--- a/workshops/views.py
+++ b/workshops/views.py
@@ -30,10 +30,33 @@
 
 def all_sites(request):
     '''List all sites.'''
+
     all_sites = Site.objects.order_by('domain')
+    items, page = _get_pagination_items(request)
+
+    # Show everything.
+    if items == 'all':
+        sites = all_sites
+
+    # Show selected items.
+    else:
+        sites_paginator = Paginator(all_sites, items)
+
+        # Select the sites.
+        try:
+            sites = sites_paginator.page(page)
+
+        # If page is not an integer, deliver first page.
+        except PageNotAnInteger:
+            sites = sites_paginator.page(1)
+
+        # If page is out of range, deliver last page of results.
+        except EmptyPage:
+            sites = sites_paginator.page(sites_paginator.num_pages)
+
     user_can_add = request.user.has_perm('edit')
     context = {'title' : 'All Sites',
-               'all_sites' : all_sites,
+               'all_sites' : sites,
                'user_can_add' : user_can_add}
     return render(request, 'workshops/all_sites.html', context)
 
@@ -108,36 +131,27 @@
     '''List all events.'''
 
     all_events = Event.objects.order_by('slug')
+    items, page = _get_pagination_items(request)
 
-    # Get the number of items requested per page, default to 25
-    # This is important for unit testing, we need to be
-    # able to specify how many items to expect
-    items = request.GET.get('items_per_page', ITEMS_PER_PAGE)
-
-    # Only paginate if the number of items is not 'all'
-    if not items == 'all':
-
-        # If items is not an integer, set it to the default
-        try:
-            items = int(items)
-        except ValueError:
-            items = ITEMS_PER_PAGE
+    # Show everything.
+    if items == 'all':
+        events = all_events
 
+    # Show selected items.
+    else:
         events_paginator = Paginator(all_events, items)
 
-        # Get the page number requested, if any
-        page = request.GET.get('page')
-
+        # Select the events.
         try:
             events = events_paginator.page(page)
+
+        # If page is not an integer, deliver first page.
         except PageNotAnInteger:
-            # If page is not an integer, deliver first page.
             events = events_paginator.page(1)
+
+        # If page is out of range, deliver last page of results.
         except EmptyPage:
-            # If page is out of range (e.g. 9999), deliver last page of results.
             events = events_paginator.page(events_paginator.num_pages)
-    else:
-        events = all_events
 
     context = {'title' : 'All Events',
                'all_events' : events}
@@ -288,3 +302,20 @@
     context = {'title' : title,
                'data' : data}
     return render(request, 'workshops/export.html', context)
+
+#------------------------------------------------------------
+
+def _get_pagination_items(request):
+    '''Determine how much pagination to do.'''
+
+    items = request.GET.get('items_per_page', ITEMS_PER_PAGE)
+
+    if items != 'all':
+        try:
+            items = int(items)
+        except ValueError:
+            items = ITEMS_PER_PAGE
+
+    page = request.GET.get('page')
+
+    return items, page

{"golden_diff": "diff --git a/workshops/views.py b/workshops/views.py\n--- a/workshops/views.py\n+++ b/workshops/views.py\n@@ -30,10 +30,33 @@\n \n def all_sites(request):\n     '''List all sites.'''\n+\n     all_sites = Site.objects.order_by('domain')\n+    items, page = _get_pagination_items(request)\n+\n+    # Show everything.\n+    if items == 'all':\n+        sites = all_sites\n+\n+    # Show selected items.\n+    else:\n+        sites_paginator = Paginator(all_sites, items)\n+\n+        # Select the sites.\n+        try:\n+            sites = sites_paginator.page(page)\n+\n+        # If page is not an integer, deliver first page.\n+        except PageNotAnInteger:\n+            sites = sites_paginator.page(1)\n+\n+        # If page is out of range, deliver last page of results.\n+        except EmptyPage:\n+            sites = sites_paginator.page(sites_paginator.num_pages)\n+\n     user_can_add = request.user.has_perm('edit')\n     context = {'title' : 'All Sites',\n-               'all_sites' : all_sites,\n+               'all_sites' : sites,\n                'user_can_add' : user_can_add}\n     return render(request, 'workshops/all_sites.html', context)\n \n@@ -108,36 +131,27 @@\n     '''List all events.'''\n \n     all_events = Event.objects.order_by('slug')\n+    items, page = _get_pagination_items(request)\n \n-    # Get the number of items requested per page, default to 25\n-    # This is important for unit testing, we need to be\n-    # able to specify how many items to expect\n-    items = request.GET.get('items_per_page', ITEMS_PER_PAGE)\n-\n-    # Only paginate if the number of items is not 'all'\n-    if not items == 'all':\n-\n-        # If items is not an integer, set it to the default\n-        try:\n-            items = int(items)\n-        except ValueError:\n-            items = ITEMS_PER_PAGE\n+    # Show everything.\n+    if items == 'all':\n+        events = all_events\n \n+    # Show selected items.\n+    else:\n         events_paginator = Paginator(all_events, items)\n \n-        # Get the page number requested, if any\n-        page = request.GET.get('page')\n-\n+        # Select the events.\n         try:\n             events = events_paginator.page(page)\n+\n+        # If page is not an integer, deliver first page.\n         except PageNotAnInteger:\n-            # If page is not an integer, deliver first page.\n             events = events_paginator.page(1)\n+\n+        # If page is out of range, deliver last page of results.\n         except EmptyPage:\n-            # If page is out of range (e.g. 9999), deliver last page of results.\n             events = events_paginator.page(events_paginator.num_pages)\n-    else:\n-        events = all_events\n \n     context = {'title' : 'All Events',\n                'all_events' : events}\n@@ -288,3 +302,20 @@\n     context = {'title' : title,\n                'data' : data}\n     return render(request, 'workshops/export.html', context)\n+\n+#------------------------------------------------------------\n+\n+def _get_pagination_items(request):\n+    '''Determine how much pagination to do.'''\n+\n+    items = request.GET.get('items_per_page', ITEMS_PER_PAGE)\n+\n+    if items != 'all':\n+        try:\n+            items = int(items)\n+        except ValueError:\n+            items = ITEMS_PER_PAGE\n+\n+    page = request.GET.get('page')\n+\n+    return items, page\n", "issue": "Paginate display of all sites\nThe display of sites should be broken into pages, and each page should have navigation links to jump to other pages.\n\n", "before_files": [{"content": "import yaml\n\nfrom django.core.paginator import Paginator, EmptyPage, PageNotAnInteger\nfrom django.core.urlresolvers import reverse\nfrom django.http import HttpResponseRedirect\nfrom django.shortcuts import render, get_object_or_404\nfrom django.views.generic.edit import CreateView, UpdateView\nfrom django.db.models import Count\n\nfrom workshops.models import Site, Airport, Event, Person, Task, Cohort, Skill, Trainee, Badge\nfrom workshops.forms import InstructorMatchForm\nfrom workshops.util import earth_distance\n\n#------------------------------------------------------------\n\nITEMS_PER_PAGE = 25\n\n#------------------------------------------------------------\n\ndef index(request):\n    '''Home page.'''\n    upcoming_events = Event.objects.upcoming_events()\n    context = {'title' : 'Home Page',\n               'upcoming_events' : upcoming_events}\n    return render(request, 'workshops/index.html', context)\n\n#------------------------------------------------------------\n\nSITE_FIELDS = ['domain', 'fullname', 'country', 'notes']\n\ndef all_sites(request):\n    '''List all sites.'''\n    all_sites = Site.objects.order_by('domain')\n    user_can_add = request.user.has_perm('edit')\n    context = {'title' : 'All Sites',\n               'all_sites' : all_sites,\n               'user_can_add' : user_can_add}\n    return render(request, 'workshops/all_sites.html', context)\n\ndef site_details(request, site_domain):\n    '''List details of a particular site.'''\n    site = Site.objects.get(domain=site_domain)\n    events = Event.objects.filter(site=site)\n    context = {'title' : 'Site {0}'.format(site),\n               'site' : site,\n               'events' : events}\n    return render(request, 'workshops/site.html', context)\n\nclass SiteCreate(CreateView):\n    model = Site\n    fields = SITE_FIELDS\n\nclass SiteUpdate(UpdateView):\n    model = Site\n    fields = SITE_FIELDS\n    slug_field = 'domain'\n    slug_url_kwarg = 'site_domain'\n\n#------------------------------------------------------------\n\nAIRPORT_FIELDS = ['iata', 'fullname', 'country', 'latitude', 'longitude']\n\ndef all_airports(request):\n    '''List all airports.'''\n    all_airports = Airport.objects.order_by('iata')\n    user_can_add = request.user.has_perm('edit')\n    context = {'title' : 'All Airports',\n               'all_airports' : all_airports,\n               'user_can_add' : user_can_add}\n    return render(request, 'workshops/all_airports.html', context)\n\ndef airport_details(request, airport_iata):\n    '''List details of a particular airport.'''\n    airport = Airport.objects.get(iata=airport_iata)\n    context = {'title' : 'Airport {0}'.format(airport),\n               'airport' : airport}\n    return render(request, 'workshops/airport.html', context)\n\nclass AirportCreate(CreateView):\n    model = Airport\n    fields = AIRPORT_FIELDS\n\nclass AirportUpdate(UpdateView):\n    model = Airport\n    fields = AIRPORT_FIELDS\n    slug_field = 'iata'\n    slug_url_kwarg = 'airport_iata'\n\n#------------------------------------------------------------\n\ndef all_persons(request):\n    '''List all persons.'''\n    all_persons = Person.objects.order_by('family', 'personal')\n    context = {'title' : 'All Persons',\n               'all_persons' : all_persons}\n    return render(request, 'workshops/all_persons.html', context)\n\ndef person_details(request, person_id):\n    '''List details of a particular person.'''\n    person = Person.objects.get(id=person_id)\n    context = {'title' : 'Person {0}'.format(person),\n               'person' : person}\n    return render(request, 'workshops/person.html', context)\n\n#------------------------------------------------------------\n\ndef all_events(request):\n    '''List all events.'''\n\n    all_events = Event.objects.order_by('slug')\n\n    # Get the number of items requested per page, default to 25\n    # This is important for unit testing, we need to be\n    # able to specify how many items to expect\n    items = request.GET.get('items_per_page', ITEMS_PER_PAGE)\n\n    # Only paginate if the number of items is not 'all'\n    if not items == 'all':\n\n        # If items is not an integer, set it to the default\n        try:\n            items = int(items)\n        except ValueError:\n            items = ITEMS_PER_PAGE\n\n        events_paginator = Paginator(all_events, items)\n\n        # Get the page number requested, if any\n        page = request.GET.get('page')\n\n        try:\n            events = events_paginator.page(page)\n        except PageNotAnInteger:\n            # If page is not an integer, deliver first page.\n            events = events_paginator.page(1)\n        except EmptyPage:\n            # If page is out of range (e.g. 9999), deliver last page of results.\n            events = events_paginator.page(events_paginator.num_pages)\n    else:\n        events = all_events\n\n    context = {'title' : 'All Events',\n               'all_events' : events}\n\n    return render(request, 'workshops/all_events.html', context)\n\ndef event_details(request, event_slug):\n    '''List details of a particular event.'''\n    event = Event.objects.get(slug=event_slug)\n    context = {'title' : 'Event {0}'.format(event),\n               'event' : event}\n    return render(request, 'workshops/event.html', context)\n\n#------------------------------------------------------------\n\nTASK_FIELDS = ['event', 'person', 'role']\n\ndef all_tasks(request):\n    '''List all tasks.'''\n    all_tasks = Task.objects.order_by('event', 'person', 'role')\n    user_can_add = request.user.has_perm('edit')\n    context = {'title' : 'All Tasks',\n               'all_tasks' : all_tasks,\n               'user_can_add' : user_can_add}\n    return render(request, 'workshops/all_tasks.html', context)\n\ndef task_details(request, event_slug, person_id, role_name):\n    '''List details of a particular task.'''\n    task = Task.objects.get(event__slug=event_slug, person__id=person_id, role__name=role_name)\n    context = {'title' : 'Task {0}'.format(task),\n               'task' : task}\n    return render(request, 'workshops/task.html', context)\n\nclass TaskCreate(CreateView):\n    model = Task\n    fields = TASK_FIELDS\n\nclass TaskUpdate(UpdateView):\n    model = Task\n    fields = TASK_FIELDS\n    pk_url_kwarg = 'task_id'\n\n    def get_object(self):\n        \"\"\"\n        Returns the object the view is displaying.\n        \"\"\"\n\n        event_slug = self.kwargs.get('event_slug', None)\n        person_id = self.kwargs.get('person_id', None)\n        role_name = self.kwargs.get('role_name', None)\n\n        return get_object_or_404(Task, event__slug=event_slug, person__id=person_id, role__name=role_name)\n\n#------------------------------------------------------------\n\nCOHORT_FIELDS = ['name', 'start', 'active', 'venue', 'qualifies']\n\ndef all_cohorts(request):\n    '''List all cohorts.'''\n    all_cohorts = Cohort.objects.order_by('start')\n    user_can_add = request.user.has_perm('edit')\n    context = {'title' : 'All Cohorts',\n               'all_cohorts' : all_cohorts,\n               'user_can_add' : user_can_add}\n    return render(request, 'workshops/all_cohorts.html', context)\n\ndef cohort_details(request, cohort_name):\n    '''List details of a particular cohort.'''\n    cohort = Cohort.objects.get(name=cohort_name)\n    trainees = Trainee.objects.filter(cohort_id=cohort.id)\n    context = {'title' : 'Cohort {0}'.format(cohort),\n               'cohort' : cohort,\n               'trainees' : trainees}\n    return render(request, 'workshops/cohort.html', context)\n\nclass CohortCreate(CreateView):\n    model = Cohort\n    fields = COHORT_FIELDS\n\nclass CohortUpdate(UpdateView):\n    model = Cohort\n    fields = COHORT_FIELDS\n    slug_field = 'name'\n    slug_url_kwarg = 'cohort_name'\n\n#------------------------------------------------------------\n\ndef match(request):\n    persons = None\n\n    if request.method == 'POST':\n        form = InstructorMatchForm(request.POST)\n        if form.is_valid():\n\n            # Filter by skills.\n            persons = Person.objects.filter(airport__isnull=False)\n            skills = []\n            for s in Skill.objects.all():\n                if form.cleaned_data[s.name]:\n                    skills.append(s)\n            persons = persons.have_skills(skills)\n\n            # Sort by location.\n            loc = (float(form.cleaned_data['latitude']),\n                   float(form.cleaned_data['longitude']))\n            persons = [(earth_distance(loc, (p.airport.latitude, p.airport.longitude)), p)\n                       for p in persons]\n            persons.sort()\n            persons = [x[1] for x in persons[:10]]\n\n        else:\n            pass # FIXME: error message\n\n    # if a GET (or any other method) we'll create a blank form\n    else:\n        form = InstructorMatchForm()\n\n    return render(request, 'workshops/match.html', {'form': form, 'persons' : persons})\n\n#------------------------------------------------------------\n\ndef _export_badges():\n    '''Collect badge data as YAML.'''\n    result = {}\n    for badge in Badge.objects.all():\n        persons = Person.objects.filter(award__badge_id=badge.id)\n        result[badge.name] = [{\"user\" : p.slug, \"name\" : p.fullname()} for p in persons]\n    return result\n\ndef _export_instructors():\n    '''Collect instructor airport locations as YAML.'''\n    # Exclude airports with no instructors, and add the number of instructors per airport\n    airports = Airport.objects.exclude(person=None).annotate(num_persons=Count('person'))\n    return [{'airport' : str(a.fullname),\n             'latlng' : '{0},{1}'.format(a.latitude, a.longitude),\n             'count'  : a.num_persons}\n            for a in airports]\n\ndef export(request, name):\n    '''Export data as YAML for inclusion in main web site.'''\n    data = None\n    if name == 'badges':\n        title, data = 'Badges', _export_badges()\n    elif name == 'instructors':\n        title, data = 'Instructor Locations', _export_instructors()\n    else:\n        title, data = 'Error', None # FIXME - need an error message\n    context = {'title' : title,\n               'data' : data}\n    return render(request, 'workshops/export.html', context)\n", "path": "workshops/views.py"}], "after_files": [{"content": "import yaml\n\nfrom django.core.paginator import Paginator, EmptyPage, PageNotAnInteger\nfrom django.core.urlresolvers import reverse\nfrom django.http import HttpResponseRedirect\nfrom django.shortcuts import render, get_object_or_404\nfrom django.views.generic.edit import CreateView, UpdateView\nfrom django.db.models import Count\n\nfrom workshops.models import Site, Airport, Event, Person, Task, Cohort, Skill, Trainee, Badge\nfrom workshops.forms import InstructorMatchForm\nfrom workshops.util import earth_distance\n\n#------------------------------------------------------------\n\nITEMS_PER_PAGE = 25\n\n#------------------------------------------------------------\n\ndef index(request):\n    '''Home page.'''\n    upcoming_events = Event.objects.upcoming_events()\n    context = {'title' : 'Home Page',\n               'upcoming_events' : upcoming_events}\n    return render(request, 'workshops/index.html', context)\n\n#------------------------------------------------------------\n\nSITE_FIELDS = ['domain', 'fullname', 'country', 'notes']\n\ndef all_sites(request):\n    '''List all sites.'''\n\n    all_sites = Site.objects.order_by('domain')\n    items, page = _get_pagination_items(request)\n\n    # Show everything.\n    if items == 'all':\n        sites = all_sites\n\n    # Show selected items.\n    else:\n        sites_paginator = Paginator(all_sites, items)\n\n        # Select the sites.\n        try:\n            sites = sites_paginator.page(page)\n\n        # If page is not an integer, deliver first page.\n        except PageNotAnInteger:\n            sites = sites_paginator.page(1)\n\n        # If page is out of range, deliver last page of results.\n        except EmptyPage:\n            sites = sites_paginator.page(sites_paginator.num_pages)\n\n    user_can_add = request.user.has_perm('edit')\n    context = {'title' : 'All Sites',\n               'all_sites' : sites,\n               'user_can_add' : user_can_add}\n    return render(request, 'workshops/all_sites.html', context)\n\ndef site_details(request, site_domain):\n    '''List details of a particular site.'''\n    site = Site.objects.get(domain=site_domain)\n    events = Event.objects.filter(site=site)\n    context = {'title' : 'Site {0}'.format(site),\n               'site' : site,\n               'events' : events}\n    return render(request, 'workshops/site.html', context)\n\nclass SiteCreate(CreateView):\n    model = Site\n    fields = SITE_FIELDS\n\nclass SiteUpdate(UpdateView):\n    model = Site\n    fields = SITE_FIELDS\n    slug_field = 'domain'\n    slug_url_kwarg = 'site_domain'\n\n#------------------------------------------------------------\n\nAIRPORT_FIELDS = ['iata', 'fullname', 'country', 'latitude', 'longitude']\n\ndef all_airports(request):\n    '''List all airports.'''\n    all_airports = Airport.objects.order_by('iata')\n    user_can_add = request.user.has_perm('edit')\n    context = {'title' : 'All Airports',\n               'all_airports' : all_airports,\n               'user_can_add' : user_can_add}\n    return render(request, 'workshops/all_airports.html', context)\n\ndef airport_details(request, airport_iata):\n    '''List details of a particular airport.'''\n    airport = Airport.objects.get(iata=airport_iata)\n    context = {'title' : 'Airport {0}'.format(airport),\n               'airport' : airport}\n    return render(request, 'workshops/airport.html', context)\n\nclass AirportCreate(CreateView):\n    model = Airport\n    fields = AIRPORT_FIELDS\n\nclass AirportUpdate(UpdateView):\n    model = Airport\n    fields = AIRPORT_FIELDS\n    slug_field = 'iata'\n    slug_url_kwarg = 'airport_iata'\n\n#------------------------------------------------------------\n\ndef all_persons(request):\n    '''List all persons.'''\n    all_persons = Person.objects.order_by('family', 'personal')\n    context = {'title' : 'All Persons',\n               'all_persons' : all_persons}\n    return render(request, 'workshops/all_persons.html', context)\n\ndef person_details(request, person_id):\n    '''List details of a particular person.'''\n    person = Person.objects.get(id=person_id)\n    context = {'title' : 'Person {0}'.format(person),\n               'person' : person}\n    return render(request, 'workshops/person.html', context)\n\n#------------------------------------------------------------\n\ndef all_events(request):\n    '''List all events.'''\n\n    all_events = Event.objects.order_by('slug')\n    items, page = _get_pagination_items(request)\n\n    # Show everything.\n    if items == 'all':\n        events = all_events\n\n    # Show selected items.\n    else:\n        events_paginator = Paginator(all_events, items)\n\n        # Select the events.\n        try:\n            events = events_paginator.page(page)\n\n        # If page is not an integer, deliver first page.\n        except PageNotAnInteger:\n            events = events_paginator.page(1)\n\n        # If page is out of range, deliver last page of results.\n        except EmptyPage:\n            events = events_paginator.page(events_paginator.num_pages)\n\n    context = {'title' : 'All Events',\n               'all_events' : events}\n\n    return render(request, 'workshops/all_events.html', context)\n\ndef event_details(request, event_slug):\n    '''List details of a particular event.'''\n    event = Event.objects.get(slug=event_slug)\n    context = {'title' : 'Event {0}'.format(event),\n               'event' : event}\n    return render(request, 'workshops/event.html', context)\n\n#------------------------------------------------------------\n\nTASK_FIELDS = ['event', 'person', 'role']\n\ndef all_tasks(request):\n    '''List all tasks.'''\n    all_tasks = Task.objects.order_by('event', 'person', 'role')\n    user_can_add = request.user.has_perm('edit')\n    context = {'title' : 'All Tasks',\n               'all_tasks' : all_tasks,\n               'user_can_add' : user_can_add}\n    return render(request, 'workshops/all_tasks.html', context)\n\ndef task_details(request, event_slug, person_id, role_name):\n    '''List details of a particular task.'''\n    task = Task.objects.get(event__slug=event_slug, person__id=person_id, role__name=role_name)\n    context = {'title' : 'Task {0}'.format(task),\n               'task' : task}\n    return render(request, 'workshops/task.html', context)\n\nclass TaskCreate(CreateView):\n    model = Task\n    fields = TASK_FIELDS\n\nclass TaskUpdate(UpdateView):\n    model = Task\n    fields = TASK_FIELDS\n    pk_url_kwarg = 'task_id'\n\n    def get_object(self):\n        \"\"\"\n        Returns the object the view is displaying.\n        \"\"\"\n\n        event_slug = self.kwargs.get('event_slug', None)\n        person_id = self.kwargs.get('person_id', None)\n        role_name = self.kwargs.get('role_name', None)\n\n        return get_object_or_404(Task, event__slug=event_slug, person__id=person_id, role__name=role_name)\n\n#------------------------------------------------------------\n\nCOHORT_FIELDS = ['name', 'start', 'active', 'venue', 'qualifies']\n\ndef all_cohorts(request):\n    '''List all cohorts.'''\n    all_cohorts = Cohort.objects.order_by('start')\n    user_can_add = request.user.has_perm('edit')\n    context = {'title' : 'All Cohorts',\n               'all_cohorts' : all_cohorts,\n               'user_can_add' : user_can_add}\n    return render(request, 'workshops/all_cohorts.html', context)\n\ndef cohort_details(request, cohort_name):\n    '''List details of a particular cohort.'''\n    cohort = Cohort.objects.get(name=cohort_name)\n    trainees = Trainee.objects.filter(cohort_id=cohort.id)\n    context = {'title' : 'Cohort {0}'.format(cohort),\n               'cohort' : cohort,\n               'trainees' : trainees}\n    return render(request, 'workshops/cohort.html', context)\n\nclass CohortCreate(CreateView):\n    model = Cohort\n    fields = COHORT_FIELDS\n\nclass CohortUpdate(UpdateView):\n    model = Cohort\n    fields = COHORT_FIELDS\n    slug_field = 'name'\n    slug_url_kwarg = 'cohort_name'\n\n#------------------------------------------------------------\n\ndef match(request):\n    persons = None\n\n    if request.method == 'POST':\n        form = InstructorMatchForm(request.POST)\n        if form.is_valid():\n\n            # Filter by skills.\n            persons = Person.objects.filter(airport__isnull=False)\n            skills = []\n            for s in Skill.objects.all():\n                if form.cleaned_data[s.name]:\n                    skills.append(s)\n            persons = persons.have_skills(skills)\n\n            # Sort by location.\n            loc = (float(form.cleaned_data['latitude']),\n                   float(form.cleaned_data['longitude']))\n            persons = [(earth_distance(loc, (p.airport.latitude, p.airport.longitude)), p)\n                       for p in persons]\n            persons.sort()\n            persons = [x[1] for x in persons[:10]]\n\n        else:\n            pass # FIXME: error message\n\n    # if a GET (or any other method) we'll create a blank form\n    else:\n        form = InstructorMatchForm()\n\n    return render(request, 'workshops/match.html', {'form': form, 'persons' : persons})\n\n#------------------------------------------------------------\n\ndef _export_badges():\n    '''Collect badge data as YAML.'''\n    result = {}\n    for badge in Badge.objects.all():\n        persons = Person.objects.filter(award__badge_id=badge.id)\n        result[badge.name] = [{\"user\" : p.slug, \"name\" : p.fullname()} for p in persons]\n    return result\n\ndef _export_instructors():\n    '''Collect instructor airport locations as YAML.'''\n    # Exclude airports with no instructors, and add the number of instructors per airport\n    airports = Airport.objects.exclude(person=None).annotate(num_persons=Count('person'))\n    return [{'airport' : str(a.fullname),\n             'latlng' : '{0},{1}'.format(a.latitude, a.longitude),\n             'count'  : a.num_persons}\n            for a in airports]\n\ndef export(request, name):\n    '''Export data as YAML for inclusion in main web site.'''\n    data = None\n    if name == 'badges':\n        title, data = 'Badges', _export_badges()\n    elif name == 'instructors':\n        title, data = 'Instructor Locations', _export_instructors()\n    else:\n        title, data = 'Error', None # FIXME - need an error message\n    context = {'title' : title,\n               'data' : data}\n    return render(request, 'workshops/export.html', context)\n\n#------------------------------------------------------------\n\ndef _get_pagination_items(request):\n    '''Determine how much pagination to do.'''\n\n    items = request.GET.get('items_per_page', ITEMS_PER_PAGE)\n\n    if items != 'all':\n        try:\n            items = int(items)\n        except ValueError:\n            items = ITEMS_PER_PAGE\n\n    page = request.GET.get('page')\n\n    return items, page\n", "path": "workshops/views.py"}]}