kopia lustrzana https://gitlab.com/marnanel/chapeau
one-to-one links from ActivityObject
rodzic
8a8359c88b
commit
1c09eb1d5d
|
@ -30,3 +30,12 @@ ATSIGN_CONTEXT = [
|
|||
"Emoji": "toot:Emoji"
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
object_type_registry = {
|
||||
'Object': None,
|
||||
}
|
||||
|
||||
def register_type(a_typename, a_typeclass):
|
||||
object_type_registry[a_typename] = a_typeclass
|
||||
|
||||
|
|
|
@ -14,5 +14,21 @@
|
|||
"a_type": "Object",
|
||||
"verified": false
|
||||
}
|
||||
},
|
||||
{
|
||||
"model": "django_kepi.ActivityObject",
|
||||
"pk": 3,
|
||||
"fields": {
|
||||
"a_type": "Person",
|
||||
"verified": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"model": "things_for_testing.ThingUser",
|
||||
"pk": 1,
|
||||
"fields": {
|
||||
"name": "Alice Test",
|
||||
"activity_object": 3
|
||||
}
|
||||
}
|
||||
]
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
# Generated by Django 2.0.6 on 2018-08-12 07:48
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('django_kepi', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Activity',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
],
|
||||
),
|
||||
]
|
|
@ -1,17 +1,16 @@
|
|||
from django.db import models
|
||||
|
||||
object_type_registry = {
|
||||
'Object': None,
|
||||
}
|
||||
|
||||
def register_type(a_typename, a_typeclass):
|
||||
object_type_registry[a_typename] = a_typeclass
|
||||
from django_kepi import object_type_registry
|
||||
|
||||
class ActivityObject(models.Model):
|
||||
a_type = models.CharField(max_length=255,
|
||||
default='Object')
|
||||
verified = models.BooleanField(default=False)
|
||||
|
||||
# attachment | attributedTo | audience | content | context |
|
||||
# name | endTime | generator | icon | image | inReplyTo |
|
||||
# location | preview | published | replies | startTime |
|
||||
# summary | tag | updated | url | to | bto | cc | bcc | mediaType | duration
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
|
||||
if self.a_type not in object_type_registry:
|
||||
|
@ -34,7 +33,7 @@ class ActivityObject(models.Model):
|
|||
))
|
||||
|
||||
if a_typeclass is not None:
|
||||
instance = a_typeclass.objects.find(activity_object__pk=self.pk)
|
||||
instance = a_typeclass.objects.get(activity_object__pk=self.pk)
|
||||
|
||||
if instance is None:
|
||||
raise ValueError("ActivityObject {} has no corresponding instance in type {}".format(
|
||||
|
@ -52,3 +51,8 @@ class ActivityObject(models.Model):
|
|||
})
|
||||
|
||||
return result
|
||||
|
||||
class Activity(models.Model):
|
||||
# actor | object | target | result | origin | instrument
|
||||
pass
|
||||
|
||||
|
|
|
@ -9,10 +9,8 @@ INSTALLED_APPS = (
|
|||
'django.contrib.messages',
|
||||
'django.contrib.staticfiles',
|
||||
|
||||
'rest_framework',
|
||||
'django_fields',
|
||||
|
||||
'django_kepi',
|
||||
'things_for_testing',
|
||||
|
||||
)
|
||||
|
||||
|
|
|
@ -34,3 +34,13 @@ class UserTests(TestCase):
|
|||
|
||||
with self.assertRaises(ActivityObject.DoesNotExist):
|
||||
self._get_json('/obj/0')
|
||||
|
||||
def test_one_to_one(self):
|
||||
|
||||
activity = self._get_json('/obj/3')
|
||||
|
||||
self.assertEqual(activity['id'], 3)
|
||||
self.assertEqual(activity['type'], 'Person')
|
||||
self.assertEqual(activity['name'], 'Alice Test')
|
||||
|
||||
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
|
@ -0,0 +1,5 @@
|
|||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class ThingsForTestingConfig(AppConfig):
|
||||
name = 'things_for_testing'
|
|
@ -0,0 +1,24 @@
|
|||
# Generated by Django 2.0.6 on 2018-08-12 07:57
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
('django_kepi', '0002_activity'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='ThingUser',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('name', models.CharField(max_length=256)),
|
||||
('activity_object', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to='django_kepi.ActivityObject')),
|
||||
],
|
||||
),
|
||||
]
|
|
@ -0,0 +1,19 @@
|
|||
from django.db import models
|
||||
from django_kepi import register_type
|
||||
from django_kepi.models import ActivityObject
|
||||
|
||||
class ThingUser(models.Model):
|
||||
|
||||
name = models.CharField(max_length=256)
|
||||
activity_object = models.OneToOneField(
|
||||
ActivityObject,
|
||||
on_delete=models.CASCADE,
|
||||
)
|
||||
|
||||
def activity_fields(self):
|
||||
return {
|
||||
'name': self.name,
|
||||
}
|
||||
|
||||
register_type('Person', ThingUser)
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
|
@ -0,0 +1,3 @@
|
|||
from django.shortcuts import render
|
||||
|
||||
# Create your views here.
|
Ładowanie…
Reference in New Issue