From 0cf3b869af528b3b68a861b94ddb0aefc3819ecf Mon Sep 17 00:00:00 2001 From: Philip Sargent Date: Sun, 26 Jul 2020 20:48:25 +0100 Subject: [PATCH] First implementation of html API, both TSV and JSON --- core/views_logbooks.py | 27 +++++++--- core/views_statistics.py | 64 +++++++++++++---------- templates/baseapi.html | 1 + templates/core/expeditions_json_list.html | 4 ++ templates/core/expeditions_tsv_list.html | 3 ++ urls.py | 2 + 6 files changed, 66 insertions(+), 35 deletions(-) create mode 100644 templates/baseapi.html create mode 100644 templates/core/expeditions_json_list.html create mode 100644 templates/core/expeditions_tsv_list.html diff --git a/core/views_logbooks.py b/core/views_logbooks.py index 92be4cc..c3cea1e 100644 --- a/core/views_logbooks.py +++ b/core/views_logbooks.py @@ -41,7 +41,7 @@ def getNotablePersons(): for person in Person.objects.all(): if person.bisnotable(): notablepersons.append(person) - return notablepersons + return notablepersons def personindex(request): @@ -84,13 +84,28 @@ def expedition(request, expeditionname): def get_absolute_url(self): return ('expedition', (expedition.year)) -class ExpeditionListView(ListView): +class ExpeditionListView(ListView): # django thus expects a template called "expedition_list.html" +# from the name of the object not the name of the class. model = Expedition - def get_context_data(self, **kwargs): - context = super(ExpeditionListView, self).get_context_data(**kwargs) - context['now'] = timezone.now() - return context + +class Expeditions_tsvListView(ListView): + """This uses the Django built-in shortcut mechanism + It defaults to use a template with name /_list.html. + https://www.agiliq.com/blog/2017/12/when-and-how-use-django-listview/ + https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/Generic_views + Either a queryset variable or set_queryset() function is used, but not needed + if you want all the obejcts of a particaulr type in which case just set model = + """ + template_name = 'core/expeditions_tsv_list.html' # if not present then uses core/expedition_list.html + #queryset = Expedition.objects.all() + #context_object_name = 'expedition' + model = Expedition # equivalent to .objects.all() for a queryset + +class Expeditions_jsonListView(ListView): + template_name = 'core/expeditions_json_list.html' + model = Expedition + def person(request, first_name='', last_name='', ): this_person = Person.objects.get(first_name = first_name, last_name = last_name) diff --git a/core/views_statistics.py b/core/views_statistics.py index 204a24e..8fb968d 100644 --- a/core/views_statistics.py +++ b/core/views_statistics.py @@ -8,7 +8,7 @@ from django.shortcuts import render, render_to_response from django.template import Context, loader from django.template.defaultfilters import slugify from django.utils import timezone -from django.views.generic.list import ListView +#from django.views.generic.list import ListView from troggle.core.models import Expedition, Person, PersonExpedition from troggle.core.models_caves import Cave, LogbookEntry @@ -18,34 +18,40 @@ import troggle.settings as settings def pathsreport(request): - pathsdict={ - "CAVEDESCRIPTIONS" : settings.CAVEDESCRIPTIONS, - "DIR_ROOT" : settings.DIR_ROOT, - "ENTRANCEDESCRIPTIONS" : settings.ENTRANCEDESCRIPTIONS, - "EXPOUSER_EMAIL" : settings.EXPOUSER_EMAIL, - "EXPOUSERPASS" :"", - "EXPOUSER" : settings.EXPOUSER, - "EXPOWEB" : settings.EXPOWEB, - "EXPOWEB_URL" : settings.EXPOWEB_URL, - "FILES" : settings.FILES, - "JSLIB_URL" : settings.JSLIB_URL, - "LOGFILE" : settings.LOGFILE, - "LOGIN_REDIRECT_URL" : settings.LOGIN_REDIRECT_URL, - "MEDIA_ROOT" : settings.MEDIA_ROOT, - "MEDIA_URL" : settings.MEDIA_URL, - "PHOTOS_URL" : settings.PHOTOS_URL, - "PYTHON_PATH" : settings.PYTHON_PATH, - "REPOS_ROOT_PATH" : settings.REPOS_ROOT_PATH, - "ROOT_URLCONF" : settings.ROOT_URLCONF, - "STATIC_URL" : settings.STATIC_URL, - "SURVEX_DATA" : settings.SURVEX_DATA, - "SURVEY_SCANS" : settings.SURVEY_SCANS, - "SURVEYS" : settings.SURVEYS, - "SURVEYS_URL" : settings.SURVEYS_URL, - "THREEDCACHEDIR" : settings.THREEDCACHEDIR, - "TUNNEL_DATA" : settings.TUNNEL_DATA, - "URL_ROOT" : settings.URL_ROOT - } + pathsdict={} + try: + pathsdict={ +# "BOGUS" : settings.BOGUS, + "CAVEDESCRIPTIONS" : settings.CAVEDESCRIPTIONS, + "DIR_ROOT" : settings.DIR_ROOT, + "ENTRANCEDESCRIPTIONS" : settings.ENTRANCEDESCRIPTIONS, + "EXPOUSER_EMAIL" : settings.EXPOUSER_EMAIL, + "EXPOUSERPASS" :"", + "EXPOUSER" : settings.EXPOUSER, + "EXPOWEB" : settings.EXPOWEB, + "EXPOWEB_URL" : settings.EXPOWEB_URL, + "FILES" : settings.FILES, + "JSLIB_URL" : settings.JSLIB_URL, + "LOGFILE" : settings.LOGFILE, + "LOGIN_REDIRECT_URL" : settings.LOGIN_REDIRECT_URL, + "MEDIA_ROOT" : settings.MEDIA_ROOT, + "MEDIA_URL" : settings.MEDIA_URL, + "PHOTOS_URL" : settings.PHOTOS_URL, + "PYTHON_PATH" : settings.PYTHON_PATH, + "REPOS_ROOT_PATH" : settings.REPOS_ROOT_PATH, + "ROOT_URLCONF" : settings.ROOT_URLCONF, + "STATIC_URL" : settings.STATIC_URL, + "SURVEX_DATA" : settings.SURVEX_DATA, + "SURVEY_SCANS" : settings.SURVEY_SCANS, + "SURVEYS" : settings.SURVEYS, + "SURVEYS_URL" : settings.SURVEYS_URL, + "THREEDCACHEDIR" : settings.THREEDCACHEDIR, + "TUNNEL_DATA" : settings.TUNNEL_DATA, + "URL_ROOT" : settings.URL_ROOT + } + except: + pathsdict["! EXCEPTION !"] = "missing string constant in troggle/settings" + # settings are unique by paths are not ncodes = len(pathsdict) bycodeslist = sorted(pathsdict.items()) diff --git a/templates/baseapi.html b/templates/baseapi.html new file mode 100644 index 0000000..372c1bb --- /dev/null +++ b/templates/baseapi.html @@ -0,0 +1 @@ +{% block content %}{% endblock %} \ No newline at end of file diff --git a/templates/core/expeditions_json_list.html b/templates/core/expeditions_json_list.html new file mode 100644 index 0000000..ebd5834 --- /dev/null +++ b/templates/core/expeditions_json_list.html @@ -0,0 +1,4 @@ +{% extends "baseapi.html" %} +{% block content %}{ +{% for expedition in object_list %}"{{expedition.year}}": ["{{expedition.name}}","{{expedition.get_absolute_url}}"] +{% endfor %} }{% endblock %} \ No newline at end of file diff --git a/templates/core/expeditions_tsv_list.html b/templates/core/expeditions_tsv_list.html new file mode 100644 index 0000000..6aa2b15 --- /dev/null +++ b/templates/core/expeditions_tsv_list.html @@ -0,0 +1,3 @@ +{% extends "baseapi.html" %} +{% block content %}{% for expedition in object_list %}{{expedition.year}} "{{expedition.name}}" "{{expedition.get_absolute_url}}" +{% endfor %}{% endblock %} \ No newline at end of file diff --git a/urls.py b/urls.py index 74b0064..e64d7ce 100644 --- a/urls.py +++ b/urls.py @@ -45,6 +45,8 @@ actualurlpatterns = [ url(r'^expedition/(\d+)$', views_logbooks.expedition, name="expedition"), url(r'^expeditions/?$', views_logbooks.ExpeditionListView.as_view(), name="expeditions"), + url(r'^api/expeditions_tsv$', views_logbooks.Expeditions_tsvListView.as_view()), + url(r'^api/expeditions_json$', views_logbooks.Expeditions_jsonListView.as_view()), url(r'^personexpedition/(?P[A-Z]*[a-z&;]*)[^a-zA-Z]*(?P[A-Z]*[a-zA-Z&;]*)/(?P\d+)/?$', views_logbooks.personexpedition, name="personexpedition"), url(r'^logbookentry/(?P.*)/(?P.*)/?$', views_logbooks.logbookentry,name="logbookentry"), # url(r'^newlogbookentry/(?P.*)$', views_logbooks.newLogbookEntry, name="newLogBookEntry"), # Needed !