chapeau/django_kepi/__init__.py

84 wiersze
2.4 KiB
Python

__title__ = 'django_kepi'
__version__ = '0.0.20'
VERSION = __version__
__author__ = 'Marnanel Thurman'
__license__ = 'GPL-2'
__copyright__ = 'Copyright (c) 2018 Marnanel Thurman'
import logging
from collections import defaultdict
logger = logging.getLogger(name='django_kepi')
# XXX this is mastodon-specific; it will be generalised later
ATSIGN_CONTEXT = [
"https://www.w3.org/ns/activitystreams",
"https://w3id.org/security/v1",
{
"schema": "http://schema.org#",
"inReplyToAtomUri": "ostatus:inReplyToAtomUri",
"movedTo": "as:movedTo",
"conversation": "ostatus:conversation",
"ostatus": "http://ostatus.org#",
"atomUri": "ostatus:atomUri",
"featured": "toot:featured",
"value": "schema:value",
"PropertyValue": "schema:PropertyValue",
"sensitive": "as:sensitive",
"toot": "http://joinmastodon.org/ns#",
"Hashtag": "as:Hashtag",
"manuallyApprovesFollowers": "as:manuallyApprovesFollowers",
"focalPoint": {
"@id": "toot:focalPoint",
"@container": "@list"
},
"Emoji": "toot:Emoji"
}
]
object_type_registry = defaultdict(list)
def register_type(f_type, handler):
logger.debug('Added handler for %s: %s',
f_type, handler)
object_type_registry[f_type].append(handler)
# Decorator
def implements_activity_type(f_type):
def register(cls):
register_type(f_type, cls)
return cls
return register
def create(fields):
if 'type' not in fields:
logger.debug('create: no "type" in %s', str(fields))
raise ValueError('objects must have a type')
t = fields['type']
if t not in object_type_registry:
logger.debug('create: unknown "type" in %s', str(fields))
raise ValueError('type {} is unknown'.format(t,))
logger.debug('create: for %s...', str(fields))
for cls in object_type_registry[t]:
result = cls.activity_create(fields)
logger.debug(' ...created %s', str(result))
return result
class TombstoneException(Exception):
def __init__(self, *args, **kwargs):
super().__init__()
self.activity_form = kwargs.copy()
self.activity_form['type'] = 'Tombstone'
def __str__(self):
return str(self.activity_form)