1
0
Fork 0

[Fixes #246] Obsversation list now class based view with pagination

merge-requests/274/head
Scott Bragg 2016-12-21 13:51:43 +11:00 committed by Nikos Roussos
parent d941cd0862
commit f54421f4c0
No known key found for this signature in database
GPG Key ID: BADFF1767BA7C8E1
4 changed files with 72 additions and 14 deletions

View File

@ -10,7 +10,7 @@ base_urlpatterns = ([
url(r'^settings_site/$', views.settings_site, name='settings_site'),
# Observations
url(r'^observations/$', views.observations_list, name='observations_list'),
url(r'^observations/$', views.ObservationListView.as_view(), name='observations_list'),
url(r'^observations/(?P<id>[0-9]+)/$', views.observation_view, name='observation_view'),
url(r'^observations/(?P<id>[0-9]+)/delete/$', views.observation_delete,
name='observation_delete'),

View File

@ -16,6 +16,8 @@ from django.http import JsonResponse, HttpResponseNotFound, HttpResponseServerEr
from django.contrib.auth.decorators import login_required
from django.core.management import call_command
from django.views.generic import ListView
from rest_framework import serializers, viewsets
from network.base.models import (Station, Transmitter, Observation,
@ -116,21 +118,36 @@ def settings_site(request):
return render(request, 'base/settings_site.html')
def observations_list(request):
"""View to render Observations page."""
observations = Observation.objects.order_by('-id')[:20]
satellites = Satellite.objects.all()
class ObservationListView(ListView):
"""
Displays a list of observations with pagination
"""
model = Observation
ordering = '-id'
context_object_name = "observations"
paginate_by = settings.ITEMS_PER_PAGE
template_name = 'base/observations.html'
if request.method == 'GET':
form = SatelliteFilterForm(request.GET)
if form.is_valid():
norad = form.cleaned_data['norad']
observations = Observation.objects.filter(satellite__norad_cat_id=norad)
return render(request, 'base/observations.html', {'observations': observations,
'satellites': satellites, 'norad': int(norad)})
def get_queryset(self):
"""
Optionally filter based on norad get argument
"""
norad_cat_id = self.request.GET.get('norad', None)
if norad_cat_id is None or norad_cat_id == '':
return Observation.objects.all()
else:
return Observation.objects.filter(satellite__norad_cat_id=norad_cat_id)
return render(request, 'base/observations.html',
{'observations': observations, 'satellites': satellites})
def get_context_data(self, **kwargs):
"""
Need to add a list of satellites to the context for the template
"""
context = super(ObservationListView, self).get_context_data(**kwargs)
context['satellites'] = Satellite.objects.all()
norad_cat_id = self.request.GET.get('norad', None)
if norad_cat_id is not None and norad_cat_id != '':
context['norad'] = int(norad_cat_id)
return context
@login_required

View File

@ -223,3 +223,6 @@ OBSERVATION_MAX_DELETION_RANGE = getenv('OBSERVATION_MAX_DELETION_RANGE', 10)
# DB API
DB_API_ENDPOINT = getenv('DB_API_ENDPOINT', 'https://db.satnogs.org/api/')
# ListView pagination
ITEMS_PER_PAGE = 25

View File

@ -106,6 +106,44 @@
{% endfor %}
</tbody>
</table>
{% if is_paginated %}
<nav>
<ul class="pagination">
{% if page_obj.has_previous %}
<li>
<a href="?page={{ page_obj.previous_page_number }}">
<span>&laquo;</span>
</a>
</li>
{% else %}
<li class="disabled">
<a href="#">
<span>&laquo;</span>
</a>
</li>
{% endif %}
{% for page in paginator.page_range %}
<li {% if page == page_obj.number %}class="active"{% endif %}>
<a href="?page={{ page }}">{{ page }}</a>
</li>
{% endfor %}
{% if page_obj.has_next %}
<li>
<a href="?page={{ page_obj.next_page_number }}">
<span>&raquo;</span>
</a>
</li>
{% else %}
<li {% if not page_obj.has_next %}class="disabled"{% endif %}>
<a href="#">
<span>&raquo;</span>
</a>
</li>
{% endif %}
</ul>
</nav>
{% endif %}
</div>
</div>
{% include 'includes/satellite.html' %}