chapeau/django_kepi/views.py

100 wiersze
2.7 KiB
Python
Czysty Zwykły widok Historia

2018-08-24 17:13:57 +00:00
from django_kepi import ATSIGN_CONTEXT
from django.shortcuts import render, get_object_or_404
2018-08-11 16:21:56 +00:00
import django.views
from django.http import HttpResponse, JsonResponse
from django.contrib.auth.decorators import login_required
2018-08-24 23:04:08 +00:00
from django_kepi.models import Following
2018-08-24 17:13:57 +00:00
import urllib.parse
import json
import re
2018-08-24 17:13:57 +00:00
PAGE_LENGTH = 50
2018-08-24 23:04:08 +00:00
PAGE_FIELD = 'page'
2018-08-24 17:13:57 +00:00
def render(data):
# XXX merge in
result = JsonResponse(
data=data,
json_dumps_params={
'sort_keys': True,
'indent': 2,
}
)
result['Content-Type'] = 'application/activity+json'
return result
2018-08-11 16:21:56 +00:00
class ActivityObjectView(django.views.View):
2018-08-11 16:21:56 +00:00
def get(self, request, *args, **kwargs):
#instance = ActivityObject.objects.get(pk=kwargs['id'])
instance = None # XXX temp
result = instance.serialize()
2018-08-06 13:44:35 +00:00
2018-08-11 16:21:56 +00:00
return render(result)
2018-08-24 17:13:57 +00:00
########################
class CollectionView(django.views.View):
class Meta:
abstract = True
def get(self, request, *args, **kwargs):
items = self.get_collection_items(*args, **kwargs)
2018-08-26 18:27:45 +00:00
# XXX assert that items.ordered
2018-08-24 17:13:57 +00:00
2018-08-24 23:04:08 +00:00
our_url = request.build_absolute_uri()
2018-08-24 23:05:18 +00:00
our_url, fragment = urllib.parse.urldefrag(our_url)
2018-08-24 17:13:57 +00:00
if PAGE_FIELD in request.GET:
page_number = int(request.GET[PAGE_FIELD])
start = (page_number-1) * PAGE_LENGTH
listed_items = all_items[start: start+PAGE_LENGTH]
result = {
"@context": ATSIGN_CONTEXT,
"type" : "OrderedCollectionPage",
"id" : our_url,
2018-08-24 23:04:08 +00:00
"totalItems" : items.count(),
2018-08-24 17:13:57 +00:00
"orderedItems" : listed_items,
"partOf": our_url,
}
if page_number > 1:
result["prev"] = "{}?page={}".format(our_url, page_number-1)
if items.count < (page_number*PAGE_LENGTH):
result["next"] = "{}?page={}".format(our_url, page_number+1)
else:
2018-08-24 23:04:08 +00:00
# Index page.
2018-08-24 17:13:57 +00:00
result = {
"@context": ATSIGN_CONTEXT,
"type": "OrderedCollection",
2018-08-24 23:04:08 +00:00
"id": our_url,
"totalItems" : items.count(),
2018-08-24 17:13:57 +00:00
}
2018-08-26 18:27:45 +00:00
if items.exists():
2018-08-24 17:13:57 +00:00
result["first"] = "{}?page=1".format(our_url,)
return render(result)
def get_collection_items(self, *args, **kwargs):
return RuntimeError("not in the superclass")
2018-08-24 18:29:35 +00:00
class FollowersView(CollectionView):
2018-08-24 17:13:57 +00:00
def get_collection_items(self, *args, **kwargs):
2018-08-24 23:04:08 +00:00
return Following.objects.filter(following__name=kwargs['username'])
2018-08-24 17:13:57 +00:00