First implementation of html API, both TSV and JSON

This commit is contained in:
Philip Sargent 2020-07-26 20:48:25 +01:00
parent 69b843a824
commit 0cf3b869af
6 changed files with 66 additions and 35 deletions

View File

@ -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
class Expeditions_tsvListView(ListView):
"""This uses the Django built-in shortcut mechanism
It defaults to use a template with name <app-label>/<model-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 = <object>
"""
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 get_context_data(self, **kwargs):
context = super(ExpeditionListView, self).get_context_data(**kwargs)
context['now'] = timezone.now()
return context
def person(request, first_name='', last_name='', ):
this_person = Person.objects.get(first_name = first_name, last_name = last_name)

View File

@ -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" :"<redacted>",
"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" :"<redacted>",
"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())

1
templates/baseapi.html Normal file
View File

@ -0,0 +1 @@
{% block content %}{% endblock %}

View File

@ -0,0 +1,4 @@
{% extends "baseapi.html" %}
{% block content %}{
{% for expedition in object_list %}"{{expedition.year}}": ["{{expedition.name}}","{{expedition.get_absolute_url}}"]
{% endfor %} }{% endblock %}

View File

@ -0,0 +1,3 @@
{% extends "baseapi.html" %}
{% block content %}{% for expedition in object_list %}{{expedition.year}} "{{expedition.name}}" "{{expedition.get_absolute_url}}"
{% endfor %}{% endblock %}

View File

@ -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<first_name>[A-Z]*[a-z&;]*)[^a-zA-Z]*(?P<last_name>[A-Z]*[a-zA-Z&;]*)/(?P<year>\d+)/?$', views_logbooks.personexpedition, name="personexpedition"),
url(r'^logbookentry/(?P<date>.*)/(?P<slug>.*)/?$', views_logbooks.logbookentry,name="logbookentry"),
# url(r'^newlogbookentry/(?P<expeditionyear>.*)$', views_logbooks.newLogbookEntry, name="newLogBookEntry"), # Needed !