chapeau/django_kepi/views.py

472 wiersze
14 KiB
Python
Czysty Zwykły widok Historia

from django_kepi import ATSIGN_CONTEXT
import django_kepi.validation
from django_kepi.find import find
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, Http404
from django.contrib.auth.decorators import login_required
from django.core.exceptions import ValidationError
from django.conf import settings
from django_kepi.models import *
2019-07-18 11:16:44 +00:00
from django_kepi.validation import validate
from collections.abc import Iterable
import logging
2018-08-24 17:13:57 +00:00
import urllib.parse
import json
logger = logging.getLogger(name='django_kepi')
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
2019-04-28 20:17:10 +00:00
class KepiView(django.views.View):
2019-04-28 20:17:10 +00:00
def __init__(self):
super().__init__()
self.http_method_names.extend([
'activity',
'activity_store',
])
def activity_store(self, request, *args, **kwargs):
"""
An internal request to store request.activity
in whatever we happen to represent. No return
value is expected: instead, throw an exception if
there's a problem.
"""
raise NotImplementedError(
"I don't know how to store %s in %s." % (
request.activity,
request.path,
))
2019-04-28 20:17:10 +00:00
def activity(self, request, *args, **kwargs):
"""
Returns this view in a form suitable for ActivityPub.
It may return:
- a dict, suitable for turning into JSON
- an iterable, such as a list or QuerySet;
this will be turned into an OrderedCollection.
Every member will be passed through
_modify_list_item before rendering.
- anything with a property called "activity_form";
this value should be either an iterable or a dict,
as above.
This method is usually called by a KepiView's get() handler.
By default, its args and kwargs will be passed in unchanged.
It's also used to retrieve the ActivityPub form within
the rest of the program, rather than as a response to
a particular HTTP request. In that case, "request" will
not be a real HttpRequest. (XXX so, what *will* it be?)
Override this method in your subclass. In KepiView
it's abstract.
"""
2019-05-21 17:05:37 +00:00
raise NotImplementedError("implement activity() in a subclass: %s" % (
self.__class__,
))
2019-04-28 20:17:10 +00:00
def get(self, request, *args, **kwargs):
"""
Returns a rendered HttpResult for a GET request.
2019-04-28 20:17:10 +00:00
By default, KepiViews call self.activity() to get
the data to render.
"""
2019-04-28 20:17:10 +00:00
result = self.activity(request, *args, **kwargs)
2018-08-26 19:07:27 +00:00
if result is None:
raise Http404()
if isinstance(result, HttpResponse):
logger.info('self.activity() returned HttpResponse %s',
result)
return result
logger.debug('About to render object: %s',
result)
while True:
if isinstance(result, dict):
return self._render(result)
if isinstance(result, Iterable):
return self._collection_get(request, result)
try:
result = result.activity_form
logger.debug(' -- it has an activity_form, %s; recurring',
result)
except AttributeError:
2019-05-22 01:17:45 +00:00
logger.warn("I don't know how to render objects like %s.", result)
raise ValueError("I don't know how to render objects like %s." % (result,))
def _render(self, data):
result = JsonResponse(
data=data,
json_dumps_params={
'sort_keys': True,
'indent': 2,
}
)
2018-08-26 19:07:27 +00:00
result['Content-Type'] = 'application/activity+json'
2018-08-26 19:07:27 +00:00
2019-05-29 11:37:51 +00:00
if data['type']=='Tombstone':
result.reason = 'Entombed'
result.status_code = 410
return result
2018-08-26 19:07:27 +00:00
def _collection_get(self, request, items):
2018-08-24 17:13:57 +00:00
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()
index_url = self._make_query_page(request, None)
2018-08-24 17:13:57 +00:00
if PAGE_FIELD in request.GET:
2018-08-26 19:07:27 +00:00
2018-08-24 17:13:57 +00:00
page_number = int(request.GET[PAGE_FIELD])
start = (page_number-1) * PAGE_LENGTH
2018-08-26 19:07:27 +00:00
listed_items = items[start: start+PAGE_LENGTH]
2018-08-24 17:13:57 +00:00
result = {
"@context": ATSIGN_CONTEXT,
"type" : "OrderedCollectionPage",
"id" : our_url,
2018-08-24 23:04:08 +00:00
"totalItems" : items.count(),
"orderedItems" : [self._modify_list_item(x)
2018-08-26 20:03:00 +00:00
for x in listed_items],
2018-08-26 19:07:27 +00:00
"partOf": index_url,
2018-08-24 17:13:57 +00:00
}
if page_number > 1:
result["prev"] = self._make_query_page(request, page_number-1)
2018-08-24 17:13:57 +00:00
if start+PAGE_LENGTH < items.count():
result["next"] = self._make_query_page(request, page_number+1)
2018-08-24 17:13:57 +00:00
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-26 19:07:27 +00:00
"id": index_url,
2018-08-24 23:04:08 +00:00
"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 self._render(result)
2018-08-24 17:13:57 +00:00
def _make_query_page(
self,
request,
page_number,
):
fields = dict(request.GET)
if page_number is None:
if PAGE_FIELD in fields:
del fields[PAGE_FIELD]
else:
fields[PAGE_FIELD] = page_number
2018-08-24 17:13:57 +00:00
encoded = urllib.parse.urlencode(fields)
if encoded!='':
encoded = '?'+encoded
return '{}://{}{}{}'.format(
request.scheme,
request.get_host(),
request.path,
encoded,
)
def _modify_list_item(self, obj):
2018-08-26 20:03:00 +00:00
return str(obj)
2019-05-21 17:10:12 +00:00
class ThingView(KepiView):
def activity(self, request, *args, **kwargs):
try:
logger.debug('Looking up Thing by id==%s',
kwargs['id'])
activity_object = Thing.objects.get(
number=kwargs['id'],
)
except Thing.DoesNotExist:
logger.info(' -- unknown: %s', kwargs)
return None
except django.core.exceptions.ValidationError:
logger.info(' -- invalid: %s', kwargs)
return None
result = activity_object
logger.debug(' -- found object: %s', result)
return result
class ActorView(ThingView):
def activity(self, request, *args, **kwargs):
logger.debug('Looking up Actor by username==%s',
kwargs['username'])
try:
activity_object = Actor.objects.get(
f_preferredUsername=json.dumps(kwargs['username']),
)
except Actor.DoesNotExist:
logger.info(' -- unknown user: %s', kwargs)
return None
except django.core.exceptions.ValidationError:
logger.info(' -- invalid: %s', kwargs)
return None
result = activity_object
logger.debug(' -- found object: %s', result)
return result
def activity_store(self, request, *args, **kwargs):
from django_kepi.models.collection import Collection
inbox_name = Collection.build_name(
username = kwargs['username'],
collectionname = 'inbox',
)
inbox = Collection.get(
name = inbox_name,
create_if_missing = True,
)
logger.debug('%s: storing %s',
inbox_name, request.activity)
inbox.append(request.activity)
class FollowingView(KepiView):
2019-05-18 20:32:39 +00:00
def activity(self, request, *args, **kwargs):
logger.debug('Finding following of %s:', kwargs['name'])
person = Actor.objects.get(
f_preferredUsername=kwargs['name'],
)
logging.debug('Finding followers of %s: %s',
kwargs['name'], person)
return Following.objects.filter(follower=person.url,
pending=False)
def _modify_list_item(self, obj):
return obj.following
class FollowersView(KepiView):
2018-09-01 17:19:10 +00:00
2019-05-18 20:32:39 +00:00
def activity(self, request, *args, **kwargs):
2018-09-01 17:19:10 +00:00
logger.debug('Finding followers of %s:', kwargs['name'])
person = Actor.objects.get(
f_preferredUsername=kwargs['name'],
)
logging.debug('Finding followers of %s: %s',
kwargs['name'], person)
return Following.objects.filter(following=person.url,
pending=False)
def _modify_list_item(self, obj):
return obj.follower
2018-09-01 17:19:10 +00:00
2019-05-21 18:24:22 +00:00
class AllUsersView(KepiView):
def activity(self, request, *args, **kwargs):
logger.debug('Finding all users.')
return Thing.objects.filter(f_type='Person')
def _modify_list_item(self, obj):
return obj.activity_form
########################################
2018-09-01 17:19:10 +00:00
class UserCollectionView(KepiView):
_default_to_existing = False
def activity(self, request,
username,
listname,
*args, **kwargs):
from django_kepi.models.collection import Collection, CollectionMember
logger.debug('Finding user %s\'s %s collection',
username, listname)
try:
the_collection = Collection.objects.get(
owner__f_preferredUsername = json.dumps(username),
name = listname)
logger.debug(' -- found collection: %s',
the_collection)
return CollectionMember.objects.filter(
parent = the_collection,
)
except Collection.DoesNotExist:
if self._default_to_existing:
logger.debug(' -- does not exist; returning empty')
return CollectionMember.objects.none()
else:
logger.debug(' -- does not exist; 404')
raise Http404()
def _modify_list_item(self, obj):
return obj.member.activity_form
class InboxView(UserCollectionView):
_default_to_existing = True
# FIXME: Only externally visible to the owner
def activity(self, request, username=None, *args, **kwargs):
if username is None:
logger.info('Attempt to read from the shared inbox')
return HttpResponse(
status = 403,
reason = 'The shared inbox is write-only',
)
return super().activity(
request,
username = username,
listname = 'inbox',
)
def post(self, request, name=None, *args, **kwargs):
# name is None for the shared inbox.
if request.headers['Content-Type'] not in [
'application/activity+json',
'application/json',
]:
return HttpResponse(
status = 415, # unsupported media type
2019-05-29 09:31:26 +00:00
reason = 'Try application/activity+json',
)
try:
2019-07-25 16:17:40 +00:00
fields = json.loads(
2019-05-29 09:31:26 +00:00
str(request.body, encoding='UTF-8'))
except json.decoder.JSONDecodeError:
return HttpResponse(
status = 415, # unsupported media type
reason = 'Invalid JSON',
)
2019-05-29 09:36:37 +00:00
except UnicodeDecodeError:
return HttpResponse(
status = 400, # bad request
reason = 'Invalid UTF-8',
)
2019-07-25 16:17:40 +00:00
validate(
path = request.path,
headers = request.headers,
body = request.body,
# is_local_user is used by create() to
# determine whether to strip or require the
# "id" field.
# FIXME it probably shouldn't always be False here.
is_local_user = False,
)
return HttpResponse(
status = 200,
reason = 'Thank you',
content = '',
content_type = 'text/plain',
)
class OutboxView(UserCollectionView):
_default_to_existing = True
2019-07-18 11:16:44 +00:00
def post(self, request, *args, **kwargs):
logger.debug('Outbox: received %s',
str(request.body, 'UTF-8'))
logger.debug('Outbox: with headers %s',
request.headers)
2019-07-18 11:16:44 +00:00
try:
fields = json.loads(request.body)
except json.JSONDecoderError:
logger.info('Outbox: invalid JSON; dropping')
return HttpResponse(
status = 400,
reason = 'Invalid JSON',
content = 'Invalid JSON',
content_type = 'text/plain',
)
actor = fields.get('actor', None)
if actor is None:
actor = fields.get('attributedTo', None)
owner = settings.KEPI['USER_URL_FORMAT'] % (kwargs['username'],)
if actor != owner:
logger.info('Outbox: actor was %s but we needed %s',
actor, owner)
return HttpResponse(
status = 410,
reason = 'Not yours',
content = 'Sir, you are an interloper!',
content_type = 'text/plain',
)
2019-07-18 11:16:44 +00:00
validate(
path = request.path,
headers = request.headers,
body = request.body,
is_local_user = True,
2019-07-18 11:16:44 +00:00
)
return HttpResponse(
status = 200,
reason = 'Thank you',
content = '',
content_type = 'text/plain',
)