79 lines
2.4 KiB
Python
79 lines
2.4 KiB
Python
# [[file:../sitemap.org::*Django View Setup][Django View Setup:1]]
|
|
import logging
|
|
import json
|
|
import functools
|
|
|
|
from django.http import HttpResponse, HttpResponseNotFound, JsonResponse
|
|
from django.shortcuts import render, get_object_or_404
|
|
from django.db.models import Count
|
|
|
|
from arcology.models import Page, Feed, Site
|
|
from roam.models import Link, Tag, File
|
|
from sitemap.models import Node, Edge
|
|
|
|
# from prometheus_client import Counter, Histogram
|
|
|
|
logger = logging.getLogger(__name__)
|
|
# Django View Setup:1 ends here
|
|
|
|
# [[file:../sitemap.org::*Tag aggregation pages][Tag aggregation pages:1]]
|
|
from django.shortcuts import render, get_object_or_404
|
|
|
|
def tags_index(request):
|
|
site = Site.from_request(request)
|
|
tags = Tag.objects.all().values('tag').annotate(total=Count('tag')).order_by('-total')
|
|
return render(request, "sitemap/tags.html", dict(
|
|
tags=tags,
|
|
site=site,
|
|
feeds=site.feed_set.all()
|
|
))
|
|
# Tag aggregation pages:1 ends here
|
|
|
|
# [[file:../sitemap.org::*Individual Tag Pages (and list partial)][Individual Tag Pages (and list partial):1]]
|
|
def tag_page(request, tag: str):
|
|
site = Site.from_request(request)
|
|
pages = Tag.weighted_pages_by_name(tag)
|
|
|
|
if request.htmx:
|
|
base_template = "sitemap/tag_partial.html"
|
|
else:
|
|
base_template = "arcology/app.html"
|
|
|
|
return render(request, "sitemap/tag.html", dict(
|
|
base_template=base_template,
|
|
|
|
tag=tag,
|
|
pages=pages,
|
|
site=site,
|
|
))
|
|
# Individual Tag Pages (and list partial):1 ends here
|
|
|
|
# [[file:../sitemap.org::*JSON Handler/View][JSON Handler/View:1]]
|
|
import hashlib
|
|
from arcology.cache_decorator import cache
|
|
|
|
@cache(key_prefix="sitemap_resp", expire_secs=60*60*24)
|
|
def _cached(cache_key, hashes):
|
|
print(f"called w/ cache key {cache_key}")
|
|
ret = dict(
|
|
nodes=Node.get_sigmajs_nodes(),
|
|
edges=Edge.get_sigmajs_edges(),
|
|
)
|
|
print(f"finished call w/ cache key {cache_key}")
|
|
return ret
|
|
|
|
def sitemap_data(request):
|
|
hashes = [ file.digest for file in File.objects.order_by('path') ]
|
|
cache_key = hashlib.sha224(''.join(hashes).encode("utf-8")).hexdigest()
|
|
|
|
return JsonResponse(_cached(cache_key, cache_key))
|
|
# JSON Handler/View:1 ends here
|
|
|
|
# [[file:../sitemap.org::*Sitemap HTML Page][Sitemap HTML Page:1]]
|
|
def sitemap(request):
|
|
site = Site.from_request(request)
|
|
return render(request, "sitemap/map.html", dict(
|
|
site=site
|
|
))
|
|
# Sitemap HTML Page:1 ends here
|