diff --git a/django_kepi/__init__.py b/django_kepi/__init__.py index 109885c..e7c1492 100644 --- a/django_kepi/__init__.py +++ b/django_kepi/__init__.py @@ -1,3 +1,5 @@ +from collections import Iterable + __title__ = 'django_kepi' __version__ = '0.0.16' VERSION = __version__ @@ -31,13 +33,30 @@ ATSIGN_CONTEXT = [ } ] - object_type_registry = { - 'Object': None, } -def register_type(a_typename, a_typeclass): - object_type_registry[a_typename] = a_typeclass +def register_type(atype, handler): + object_type_registry[atype] = handler + +def resolve(identifier, atype=None): + + if atype is None: + atype = object_type_registry.keys() + elif not isinstance(atype, Iterable): + atype = [atype] + + for t in atype: + + if t not in object_type_registry: + continue + + result = object_type_registry[t].find_activity(url=identifier) + + if result is not None: + return result + + return None class TombstoneException(Exception): diff --git a/django_kepi/migrations/0010_auto_20180906_1657.py b/django_kepi/migrations/0010_auto_20180906_1657.py new file mode 100644 index 0000000..3486c7b --- /dev/null +++ b/django_kepi/migrations/0010_auto_20180906_1657.py @@ -0,0 +1,20 @@ +# Generated by Django 2.1 on 2018-09-06 16:57 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('django_kepi', '0009_namedobject'), + ] + + operations = [ + migrations.RemoveField( + model_name='namedobject', + name='content_type', + ), + migrations.DeleteModel( + name='NamedObject', + ), + ] diff --git a/django_kepi/models.py b/django_kepi/models.py index d42106d..b3de1aa 100644 --- a/django_kepi/models.py +++ b/django_kepi/models.py @@ -373,48 +373,4 @@ class RequestingAccess(UserRelationship): self.following.name, ) -class UnexpiredNamedObjectsManager(models.Manager): - def get_queryset(self): - return super().get_queryset().exclude( - #expiry==datetime.datetime.now(), - ) -class NamedObject(models.Model): - """ - This table maps URL identifiers to Django objects, using the - contenttypes mechanism. It also keeps track of the object's - ActivityObject type. - - Any object we know about on a remote server must be listed - here. Objects on this server *may* be listed here; if they're not, - django_kepi.resolve() can also find them - through following the URL path. - """ - - url = models.URLField( - max_length=255, - primary_key=True, - ) - - activity_type = models.CharField( - max_length=255, - ) - - expiry = models.DateTimeField(default=None) - - content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE) - object_id = models.PositiveIntegerField() - content_object = GenericForeignKey('content_type', 'object_id') - - objects = UnexpiredNamedObjectsManager() - all_objects = models.Manager() - -def resolve(identifier): - - try: - result = NamedObject.objects.get(url=identifier) - return result - except NamedObject.DoesNotExist: - pass - - return None diff --git a/tests/tests_resolve.py b/tests/tests_resolve.py index 4c72b15..57eb1c4 100644 --- a/tests/tests_resolve.py +++ b/tests/tests_resolve.py @@ -1,5 +1,5 @@ from django.test import TestCase -from django_kepi.models import resolve +from django_kepi import resolve class ResolveTests(TestCase): diff --git a/things_for_testing/models.py b/things_for_testing/models.py index 8a87571..574ff0f 100644 --- a/things_for_testing/models.py +++ b/things_for_testing/models.py @@ -51,6 +51,10 @@ class ThingUser(models.Model): self.name, ) + @classmethod + def find_activity(self, url): + return None # XXX stub + register_type('Person', ThingUser) class ThingArticle(models.Model): @@ -86,5 +90,10 @@ class ThingArticle(models.Model): def activity_like(cls, type_name, actor, fobject): pass + @classmethod + def find_activity(self, url): + return None # XXX stub + + register_type('Article', ThingArticle)