summaryrefslogtreecommitdiff
path: root/app/utils
diff options
context:
space:
mode:
authorluxagraf <sng@luxagraf.net>2018-02-05 13:38:59 -0600
committerluxagraf <sng@luxagraf.net>2018-02-05 13:38:59 -0600
commit3398e7349e2bafddaa491c01f4c16c6dae14cd00 (patch)
tree0ae25201ce6fc50146dff819b03819ddce148cfe /app/utils
parentad112641f01a0fd36d0b738b6903dea3eadbf000 (diff)
abstracted the next prev links into utils so now they work for every
model that implements a get_%S_admin_link method and loads the JS
Diffstat (limited to 'app/utils')
-rw-r--r--app/utils/static/next-prev-links.js80
-rw-r--r--app/utils/urls.py12
-rw-r--r--app/utils/views.py13
3 files changed, 78 insertions, 27 deletions
diff --git a/app/utils/static/next-prev-links.js b/app/utils/static/next-prev-links.js
index 7af1b66..82a1dd8 100644
--- a/app/utils/static/next-prev-links.js
+++ b/app/utils/static/next-prev-links.js
@@ -1,33 +1,59 @@
function build_next_prev() {
+
var url = window.location.href
- var style = document.createElement('style');
- style.type = 'text/css';
- style.innerHTML = '.np-container {padding-left: 0;} .prev, .next {display: inline-block; margin-right: .5em;} .prev:after { content: "|"; margin-left:.5em;} .prev a:before {content: "\u00AB"; margin-right: 3px;} .next a:after{content: "\u00BB"; margin-left: 3px;}';
- document.getElementsByTagName('head')[0].appendChild(style);
var cur = Number(url.split('/')[6]);
- var base_url = url.split(cur)[0];
- var next = cur+1;
- var prev = cur-1;
- var container = document.createElement("ul");
- var next_li = document.createElement("li");
- var next_link = document.createElement("a");
- var prev_li = document.createElement("li");
- var prev_link = document.createElement("a");
- prev_li.className = "prev";
- next_li.className = "next";
- container.className = "np-container";
- next_link.href = base_url + next + "/change/";
- next_link.textContent = "Next";
- prev_link.href = base_url + prev + "/change/";
- prev_link.textContent = "Prev";
- next_li.appendChild(next_link);
- prev_li.appendChild(prev_link);
- container.appendChild(prev_li);
- container.appendChild(next_li);
- console.log(container);
- Array.from(document.getElementsByClassName('object-tools')).forEach(function(item) {
- item.parentNode.insertBefore(container, item.nextSibling);
- })
+ var app = url.split('/')[4];
+ var model = url.split('/')[5];
+ if (cur) {
+ var style = document.createElement('style');
+ style.type = 'text/css';
+ style.innerHTML = '.np-container {padding-left: 0;} .prev, .next {display: inline-block; margin-right: .5em;} .prev:after { content: "|"; margin-left:.5em;} .prev a:before {content: "\u00AB"; margin-right: 3px;} .next a:after{content: "\u00BB"; margin-left: 3px;}';
+ document.getElementsByTagName('head')[0].appendChild(style);
+
+ json_url = '/admin/data/'+app+'/'+model+'/'+cur+'/';
+ console.log(json_url);
+
+ var container = document.createElement("ul");
+ var next_li = document.createElement("li");
+ var next_link = document.createElement("a");
+ var prev_li = document.createElement("li");
+ var prev_link = document.createElement("a");
+ prev_li.className = "prev";
+ next_li.className = "next";
+ container.className = "np-container";
+ next_link.textContent = "Next";
+ prev_link.textContent = "Prev";
+
+ var request = new XMLHttpRequest();
+ request.open('GET', json_url, true);
+ request.onload = function() {
+ if (request.status >= 200 && request.status < 400) {
+ var data = JSON.parse(request.responseText);
+ next_link.href = data['next'];
+ prev_link.href = data['prev'];
+ if (data['next'] != '') {
+ next_li.appendChild(next_link);
+ }
+ if (data['prev']) {
+ prev_li.appendChild(prev_link);
+ }
+ } else {
+ console.log("server error");
+ }
+ };
+ request.onerror = function() {
+ console.log("error on request");
+ };
+ request.send();
+ container.appendChild(prev_li);
+ container.appendChild(next_li);
+ //console.log(container);
+ Array.from(document.getElementsByClassName('object-tools')).forEach(function(item) {
+ item.parentNode.insertBefore(container, item.nextSibling);
+ })
+ } else {
+ return;
+ }
};
document.addEventListener("DOMContentLoaded", function(event) {
build_next_prev();
diff --git a/app/utils/urls.py b/app/utils/urls.py
new file mode 100644
index 0000000..7c37c5d
--- /dev/null
+++ b/app/utils/urls.py
@@ -0,0 +1,12 @@
+from django.urls import path
+
+from . import views
+
+
+urlpatterns = [
+ path(
+ r'<str:app>/<str:model>/<int:pk>/',
+ views.nav_json,
+ name="admin_links"
+ ),
+]
diff --git a/app/utils/views.py b/app/utils/views.py
index 0b96111..6ebf44d 100644
--- a/app/utils/views.py
+++ b/app/utils/views.py
@@ -1,4 +1,6 @@
from itertools import chain
+import json
+from django.http import Http404, HttpResponse
from django.views.generic import ListView
from photos.models import LuxImage, LuxVideo
from django.shortcuts import render_to_response
@@ -53,3 +55,14 @@ class TagAutocomplete(autocomplete.Select2QuerySetView):
qs = qs.filter(name__istartswith=self.q)
return qs
+
+from django.apps import apps
+
+def nav_json(request, app, model, pk):
+ model = apps.get_model(app_label=app, model_name=model)
+ p = model.objects.get(pk=pk)
+ data = {}
+ data['prev'] = p.get_previous_admin_url
+ data['next'] = p.get_next_admin_url
+ data = json.dumps(data)
+ return HttpResponse(data)