Aggregates (de)serializable model fields

This commit is contained in:
Mouse Reeve 2020-12-13 11:16:12 -08:00
parent 957f0889aa
commit b67aea22fc

View file

@ -10,6 +10,8 @@ from Crypto.Hash import SHA256
from django.core.paginator import Paginator
from django.db import models
from django.db.models import Q
from django.db.models.fields.files import ImageFileDescriptor
from django.db.models.fields.related_descriptors import ManyToManyDescriptor
from django.dispatch import receiver
from bookwyrm import activitypub
@ -68,6 +70,30 @@ class ActivitypubMixin:
activity_serializer = lambda: {}
reverse_unfurl = False
def __init__(self, *args, **kwargs):
''' collect some info on model fields '''
self.image_fields = []
self.many_to_many_fields = []
self.simple_fields = [] # "simple"
for field in self._meta.get_fields():
if not hasattr(field, 'field_to_activity'):
continue
if isinstance(field, ImageFileDescriptor):
self.image_fields.append(field)
elif isinstance(field, ManyToManyDescriptor):
self.many_to_many_fields.append(field)
else:
self.simple_fields.append(field)
self.deserialize_reverse_fields = self.deserialize_reverse_fields \
if hasattr(self, 'deserialize_reverse_fields') else []
self.serialize_reverse_fields = self.serialize_reverse_fields \
if hasattr(self, 'serialize_reverse_fields') else []
super().__init__(*args, **kwargs)
@classmethod
def find_existing_by_remote_id(cls, remote_id):
''' look up a remote id in the db '''