From b100c15a8285c41b907d42fbed173d5ad2c73722 Mon Sep 17 00:00:00 2001 From: Alexander Werner Date: Mon, 20 Feb 2017 19:11:48 +0100 Subject: [PATCH] Ajaxify views --- billard/templates/billard/base.html | 4 +- billard/templates/billard/index.html | 35 +++-------- billard/templates/billard/index_ajax.html | 23 ++++++++ .../billard/locationdata_detail.html | 58 +++---------------- .../billard/locationdata_detail_ajax.html | 51 ++++++++++++++++ .../templates/billard/locationdata_list.html | 43 +++----------- .../billard/locationdata_list_ajax.html | 42 ++++++++++++++ billard/views.py | 16 ++++- 8 files changed, 157 insertions(+), 115 deletions(-) create mode 100644 billard/templates/billard/index_ajax.html create mode 100644 billard/templates/billard/locationdata_detail_ajax.html create mode 100644 billard/templates/billard/locationdata_list_ajax.html diff --git a/billard/templates/billard/base.html b/billard/templates/billard/base.html index 24d0aac..7a19c4b 100644 --- a/billard/templates/billard/base.html +++ b/billard/templates/billard/base.html @@ -42,12 +42,14 @@ -
+
{% block content %} {% endblock %}
+ {% block js %} + {% endblock %} diff --git a/billard/templates/billard/index.html b/billard/templates/billard/index.html index 4ddd858..234ee34 100644 --- a/billard/templates/billard/index.html +++ b/billard/templates/billard/index.html @@ -1,34 +1,15 @@ {% extends 'billard/base.html' %} {% load display_client %} -{% block header %} - -{% endblock %} - {% block title %}Location Data{% endblock %} {% block content %} -{% if not locations|length_is:"1" %} -
- {% csrf_token %} -
- -
-
-{% endif %} -{% if clients %} -{% for cli in clients %} -{% for i in range %} {{ cli|display_client:i }} {% endfor %} -{% endfor %} -{% else %} -
-
Keine Tische angelegt!
-
-{% endif %} +{% include 'billard/index_ajax.html' %} +{% endblock %} +{% block js %} + {% endblock %} - - diff --git a/billard/templates/billard/index_ajax.html b/billard/templates/billard/index_ajax.html new file mode 100644 index 0000000..2c72d20 --- /dev/null +++ b/billard/templates/billard/index_ajax.html @@ -0,0 +1,23 @@ +{% load display_client %} + +{% if not locations|length_is:"1" %} +
+ {% csrf_token %} +
+ +
+
+{% endif %} +{% if clients %} +{% for cli in clients %} +{% for i in range %} {{ cli|display_client:i }} {% endfor %} +{% endfor %} +{% else %} +
+
Keine Tische angelegt!
+
+{% endif %} \ No newline at end of file diff --git a/billard/templates/billard/locationdata_detail.html b/billard/templates/billard/locationdata_detail.html index de332c9..1ad71ff 100644 --- a/billard/templates/billard/locationdata_detail.html +++ b/billard/templates/billard/locationdata_detail.html @@ -3,55 +3,13 @@ {% block title %}Location Data{% endblock %} {% block content %} -

Locationdata: {{ locationdata.id }}

- - {% if error_message %}

{{ error_message }}

{% endif %} - -
- {% csrf_token %} -
- -
- -
-
- -
- -
-
- -
- -
-
-
- -
- -
-
-
- -
- -
-
-
- -
- -
-
- - Zurück - + {% include 'billard/locationdata_detail_ajax.html' %} {% endblock %} - +{% block js %} + +{% endblock %} \ No newline at end of file diff --git a/billard/templates/billard/locationdata_detail_ajax.html b/billard/templates/billard/locationdata_detail_ajax.html new file mode 100644 index 0000000..b64e59a --- /dev/null +++ b/billard/templates/billard/locationdata_detail_ajax.html @@ -0,0 +1,51 @@ +

Locationdata: {{ locationdata.id }}

+ +{% if error_message %}

{{ error_message }}

{% endif %} + +
+ {% csrf_token %} +
+ +
+ +
+
+ +
+ +
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+ + Zurück + + + diff --git a/billard/templates/billard/locationdata_list.html b/billard/templates/billard/locationdata_list.html index 1bad748..dbc0eee 100644 --- a/billard/templates/billard/locationdata_list.html +++ b/billard/templates/billard/locationdata_list.html @@ -3,40 +3,13 @@ {% block title %}Location Data{% endblock %} {% block content %} -
- -
- -{% if object_list %} -

Location Data

- - - - - - - - - - - {% for location_data in object_list %} - - - - - - - - - - {% endfor %} -
IDLocationTableTimestampOn_OffProcError
{{ location_data.id }}{{ location_data.location_id }}{{ location_data.table_no }}{{ location_data.tst }}{{ location_data.on_off }}{{ location_data.processed }}{{ location_data.error_msg }}
-{% else %} -

No data available.

-{% endif %} +{% include 'billard/locationdata_list_ajax.html' %} {% endblock %} - +{% block js %} + +{% endblock %} \ No newline at end of file diff --git a/billard/templates/billard/locationdata_list_ajax.html b/billard/templates/billard/locationdata_list_ajax.html new file mode 100644 index 0000000..1bad748 --- /dev/null +++ b/billard/templates/billard/locationdata_list_ajax.html @@ -0,0 +1,42 @@ +{% extends 'billard/base.html' %} + +{% block title %}Location Data{% endblock %} + +{% block content %} +
+ +
+ +{% if object_list %} +

Location Data

+ + + + + + + + + + + {% for location_data in object_list %} + + + + + + + + + + {% endfor %} +
IDLocationTableTimestampOn_OffProcError
{{ location_data.id }}{{ location_data.location_id }}{{ location_data.table_no }}{{ location_data.tst }}{{ location_data.on_off }}{{ location_data.processed }}{{ location_data.error_msg }}
+{% else %} +

No data available.

+{% endif %} +{% endblock %} + + diff --git a/billard/views.py b/billard/views.py index 839ba46..9601f70 100644 --- a/billard/views.py +++ b/billard/views.py @@ -17,14 +17,26 @@ class LocationDataViewSet(viewsets.ModelViewSet): class IndexView(generic.ListView): model = LocationData + def get_template_names(self): + if self.request.is_ajax(): + return ('billard/locationdata_list_ajax.html',) + return super().get_template_names() class LocationDataDetailView(DetailView): model = LocationData + def get_template_names(self): + if self.request.is_ajax(): + return ('billard/locationdata_detail_ajax.html',) + return super().get_template_names() + @login_required def index(request): if request.method == 'GET': + template = 'billard/index.html' + if request.is_ajax(): + template = 'billard/index_ajax.html' min_loc = Location.objects.filter(users__id=request.user.id).aggregate(Min('id'))['id__min'] loc = None if 'loc' in request.GET: @@ -35,7 +47,7 @@ def index(request): resp['Location'] += '?loc={}'.format(str(min_loc)) return resp else: - return render(request, 'billard/index.html') + return render(request, template) if loc is None: loc = min_loc locations = Location.objects.filter(users__id=request.user.id).order_by('code') @@ -46,7 +58,7 @@ def index(request): 'clients': clients, 'location_id': int(loc), } - return render(request, 'billard/index.html', context=context) + return render(request, template, context=context) if request.method == 'POST': loc = request.POST['location-selector'] resp = redirect('carom_index')