Browse Source

Add location templates

Colin Powell 1 year ago
parent
commit
85a91b340a

+ 3 - 0
vrobbler/apps/locations/admin.py

@@ -18,6 +18,9 @@ class GeoLocationAdmin(admin.ModelAdmin):
         "lat",
         "lon",
     )
+    inlines = [
+        ScrobbleInline,
+    ]
 
 
 @admin.register(RawGeoLocation)

+ 18 - 0
vrobbler/apps/locations/urls.py

@@ -0,0 +1,18 @@
+from django.urls import path
+from locations import views
+
+app_name = "locations"
+
+
+urlpatterns = [
+    path(
+        "locations/",
+        views.GeoLocationListView.as_view(),
+        name="geo_locations_list",
+    ),
+    path(
+        "locations/<slug:slug>/",
+        views.GeoLocationDetailView.as_view(),
+        name="geo_location_detail",
+    ),
+]

+ 22 - 1
vrobbler/apps/locations/views.py

@@ -1 +1,22 @@
-#!/usr/bin/env python3
+from django.db.models import Count
+from django.views import generic
+from locations.models import GeoLocation
+from scrobbles.stats import get_scrobble_count_qs
+
+
+class GeoLocationListView(generic.ListView):
+    model = GeoLocation
+    paginate_by = 200
+
+    def get_queryset(self):
+        return super().get_queryset().order_by("-created")
+
+    def get_context_data(self, **kwargs):
+        context_data = super().get_context_data(**kwargs)
+        context_data["latest"] = self.get_queryset().first()
+        return context_data
+
+
+class GeoLocationDetailView(generic.DetailView):
+    model = GeoLocation
+    slug_field = "uuid"

+ 4 - 0
vrobbler/apps/scrobbles/stats.py

@@ -47,6 +47,10 @@ def get_scrobble_count_qs(
         data_model = apps.get_model(
             app_label="sports", model_name="SportEvent"
         )
+    if model_str == "GeoLocation":
+        data_model = apps.get_model(
+            app_label="locations", model_name="GeoLocation"
+        )
 
     if model_str == "Artist":
         base_qs = data_model.objects.filter(

+ 80 - 0
vrobbler/templates/locations/geolocation_detail.html

@@ -0,0 +1,80 @@
+{% extends "base_list.html" %}
+{% load mathfilters %}
+{% load static %}
+{% load naturalduration %}
+
+{% block title %}{{object.title}}{% endblock %}
+
+{% block head_extra %}
+<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css"
+     integrity="sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY="
+     crossorigin=""/>
+
+ <style>
+    #map { height: 280px; }
+</style>
+
+ {% endblock %}
+
+ {% block extra_js %}
+ <script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"
+     integrity="sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo="
+     crossorigin=""></script>
+
+ <script>
+   var map = L.map('map').setView([{{object.lat}}, {{object.lon}}], 13);
+    L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
+        maxZoom: 19,
+        attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
+    }).addTo(map);
+   var marker = L.marker([{{object.lat}}, {{object.lon}}]).addTo(map);
+</script>
+{% endblock %}
+
+{% block lists %}
+
+<div class="row">
+        <div id="map"></div>
+
+    <p style="float:left; width:402px; padding:0; border: 1px solid #ccc">
+    </p>
+    <div style="float:left; width:600px; margin-left:10px; ">
+      {{object.lat}}
+        {% if object.summary %}
+        <p>{{object.summary|safe|linebreaks|truncatewords:160}}</p>
+        <hr />
+        {% endif %}
+        <p style="float:right;">
+        </p>
+    </div>
+</div>
+<div class="row">
+    <p>{{object.scrobble_set.count}} scrobbles</p>
+<div class="row">
+    <div class="col-md">
+        <h3>Last scrobbles</h3>
+        <div class="table-responsive">
+            <table class="table table-striped table-sm">
+                <thead>
+                    <tr>
+                        <th scope="col">Date</th>
+                        <th scope="col">Completed</th>
+                        <th scope="col">Pages read</th>
+                        <th scope="col">Authors</th>
+                    </tr>
+                </thead>
+                <tbody>
+                    {% for scrobble in object.scrobble_set.all|dictsortreversed:"timestamp" %}
+                    <tr>
+                        <td>{{scrobble.timestamp}}</td>
+                        <td>{% if scrobble.long_play_complete == True %}Yes{% endif %}</td>
+                        <td>{% if scrobble.in_progress %}Now reading{% else %}{{scrobble.session_pages_read}}{% endif %}</td>
+                        <td>{% for author in scrobble.book.authors.all %}<a href="{{author.get_absolute_url}}">{{author}}</a>{% if not forloop.last %}, {% endif %}{% endfor %}</td>
+                    </tr>
+                    {% endfor %}
+                </tbody>
+            </table>
+        </div>
+    </div>
+</div>
+{% endblock %}

+ 94 - 0
vrobbler/templates/locations/geolocation_list.html

@@ -0,0 +1,94 @@
+{% extends "base_list.html" %}
+{% load urlreplace %}
+
+{% block title %}Locations{% endblock %}
+
+{% block head_extra %}
+<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css"
+     integrity="sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY="
+     crossorigin=""/>
+
+ <style>
+    #map { height: 580px; }
+</style>
+
+ {% endblock %}
+
+ {% block extra_js %}
+ <script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"
+     integrity="sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo="
+     crossorigin=""></script>
+
+ <script>
+   var map = L.map('map').setView([{{latest.lat}}, {{latest.lon}}], 13);
+    L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
+        maxZoom: 19,
+        attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
+    }).addTo(map);
+   var marker = L.marker([{{latest.lat}}, {{latest.lon}}]).addTo(map);
+   var latest_path = [
+       {% for l in object_list %}
+       [{{l.lat}}, {{l.lon}}]{% if not forloop.last %},{% endif %}
+       {% endfor %}
+   ]
+   var path = L.polyline(latest_path, {color: 'red'}).addTo(map);
+</script>
+{% endblock %}
+
+{% block lists %}
+
+<div class="row">
+        <div id="map"></div>
+</div>
+<div class="row">
+    <p class="pagination">
+        <span class="page-links">
+            {% if page_obj.has_previous %}
+            <a href="?{% urlreplace page=page_obj.previous_page_number %}">prev</a>
+            {% endif %}
+            <span class="page-current">
+                Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}
+            </span>
+            {% if page_obj.has_next %}
+            <a href="?{% urlreplace page=page_obj.next_page_number %}">next</a>
+            {% endif %}
+        </span>
+    </p>
+    <hr />
+
+    <div class="col-md">
+        <div class="table-responsive">
+            <table class="table table-striped table-sm">
+                <thead>
+                    <tr>
+                        <th scope="col">Scrobbles</th>
+                        <th scope="col">Point</th>
+                    </tr>
+                </thead>
+                <tbody>
+                    {% for location in object_list %}
+                    <tr>
+                        <td>{{location.scrobbles.count}}</td>
+                        <td><a href="{{location.get_absolute_url}}">{{location.lat}}x{{location.lon}}</a></td>
+                    </tr>
+                    {% endfor %}
+                </tbody>
+            </table>
+        </div>
+    </div>
+
+    <div class="pagination" style="margin-bottom:50px;">
+        <span class="page-links">
+            {% if page_obj.has_previous %}
+            <a href="?{% urlreplace page=page_obj.previous_page_number %}">prev</a>
+            {% endif %}
+            <span class="page-current">
+                Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}
+            </span>
+            {% if page_obj.has_next %}
+            <a href="?{% urlreplace page=page_obj.next_page_number %}">next</a>
+            {% endif %}
+        </span>
+    </div>
+</div>
+{% endblock %}

+ 2 - 0
vrobbler/urls.py

@@ -10,6 +10,7 @@ from vrobbler.apps.sports import urls as sports_urls
 from vrobbler.apps.podcasts import urls as podcast_urls
 from vrobbler.apps.videogames import urls as videogame_urls
 from vrobbler.apps.boardgames import urls as boardgame_urls
+from vrobbler.apps.locations import urls as locations_urls
 from vrobbler.apps.music.api.views import (
     AlbumViewSet,
     ArtistViewSet,
@@ -66,6 +67,7 @@ urlpatterns = [
     path("", include(videogame_urls, namespace="videogames")),
     path("", include(boardgame_urls, namespace="boardgames")),
     path("", include(sports_urls, namespace="sports")),
+    path("", include(locations_urls, namespace="locations")),
     path("", include(podcast_urls, namespace="podcasts")),
     path("", include(scrobble_urls, namespace="scrobbles")),
     path(