96 lines
2.5 KiB
Python
96 lines
2.5 KiB
Python
from django.http import HttpRequest, HttpResponse, HttpResponseNotFound, Http404, JsonResponse
|
|
from django.shortcuts import render, get_object_or_404
|
|
|
|
from arcology.models import Page, Feed, Site
|
|
from roam.models import Link
|
|
from localapi.auth import authenticated
|
|
|
|
from prometheus_client import Counter, Histogram
|
|
|
|
import logging
|
|
logger = logging.getLogger(__name__)
|
|
|
|
@authenticated
|
|
def index(request):
|
|
return JsonResponse(dict(state="ok :)"))
|
|
|
|
import roam.models
|
|
|
|
def _json_keywords(keywords):
|
|
return [
|
|
dict(path=kw.path.path, keyword=kw.keyword, value=kw.value)
|
|
for kw in keywords
|
|
]
|
|
|
|
@authenticated
|
|
def keyword_by_key(request, key):
|
|
keywords = roam.models.Keyword.objects.filter(keyword=key).all()
|
|
return JsonResponse(dict(
|
|
state="ok :)",
|
|
key=key,
|
|
keywords=_json_keywords(keywords),
|
|
))
|
|
|
|
@authenticated
|
|
def keyword_by_key_value(request, key, value):
|
|
keywords = roam.models.Keyword.objects.filter(keyword=key, value=value).all()
|
|
return JsonResponse(dict(
|
|
state="ok :)",
|
|
key=key,
|
|
value=value,
|
|
keywords=_json_keywords(keywords),
|
|
))
|
|
|
|
import arcology.models
|
|
|
|
def _json_page(page):
|
|
return dict(
|
|
title=page.title,
|
|
url=page.to_url(),
|
|
site=page.site.title,
|
|
)
|
|
|
|
@authenticated
|
|
def page_metadata(request, route_key):
|
|
page = arcology.models.Page.objects.get(route_key=route_key)
|
|
keywords = page.file.keyword_set.all()
|
|
return JsonResponse(dict(
|
|
state="ok :)",
|
|
route_key=route_key,
|
|
file=page.file.path,
|
|
page=_json_page(page),
|
|
keywords=_json_keywords(keywords)
|
|
))
|
|
|
|
from django.conf import settings
|
|
|
|
@authenticated
|
|
def file_metadata(request, file_path):
|
|
final_path = settings.ARCOLOGY_BASE_DIR.joinpath(file_path)
|
|
the_file = roam.models.File.objects.get(path=final_path)
|
|
page = the_file.page_set.first()
|
|
return JsonResponse(dict(
|
|
state="ok :)",
|
|
file_path=file_path,
|
|
file=dict(
|
|
path=the_file.path,
|
|
page=_json_page(page),
|
|
),
|
|
))
|
|
|
|
import tempfile
|
|
from django.core.management import call_command
|
|
|
|
@authenticated
|
|
def generate(request, module, role=None):
|
|
f = tempfile.NamedTemporaryFile()
|
|
fn = f.name
|
|
# i hate this!!!
|
|
if role:
|
|
call_command('generate', destination=fn, module=module, role=role)
|
|
else:
|
|
call_command('generate', destination=fn, module=module)
|
|
f.seek(0)
|
|
|
|
return HttpResponse(f)
|