Runs black

This commit is contained in:
Mouse Reeve
2021-03-08 08:49:10 -08:00
parent a07f955781
commit 70296e760b
198 changed files with 10239 additions and 8572 deletions

View File

@ -1,4 +1,4 @@
''' basics for an activitypub serializer '''
""" basics for an activitypub serializer """
from dataclasses import dataclass, fields, MISSING
from json import JSONEncoder
@ -8,46 +8,52 @@ from django.db import IntegrityError, transaction
from bookwyrm.connectors import ConnectorException, get_data
from bookwyrm.tasks import app
class ActivitySerializerError(ValueError):
''' routine problems serializing activitypub json '''
""" routine problems serializing activitypub json """
class ActivityEncoder(JSONEncoder):
''' used to convert an Activity object into json '''
""" used to convert an Activity object into json """
def default(self, o):
return o.__dict__
@dataclass
class Link:
''' for tagging a book in a status '''
""" for tagging a book in a status """
href: str
name: str
type: str = 'Link'
type: str = "Link"
@dataclass
class Mention(Link):
''' a subtype of Link for mentioning an actor '''
type: str = 'Mention'
""" a subtype of Link for mentioning an actor """
type: str = "Mention"
@dataclass
class Signature:
''' public key block '''
""" public key block """
creator: str
created: str
signatureValue: str
type: str = 'RsaSignature2017'
type: str = "RsaSignature2017"
def naive_parse(activity_objects, activity_json, serializer=None):
''' this navigates circular import issues '''
""" this navigates circular import issues """
if not serializer:
if activity_json.get('publicKeyPem'):
if activity_json.get("publicKeyPem"):
# ugh
activity_json['type'] = 'PublicKey'
activity_json["type"] = "PublicKey"
try:
activity_type = activity_json['type']
activity_type = activity_json["type"]
serializer = activity_objects[activity_type]
except KeyError as e:
raise ActivitySerializerError(e)
@ -57,14 +63,15 @@ def naive_parse(activity_objects, activity_json, serializer=None):
@dataclass(init=False)
class ActivityObject:
''' actor activitypub json '''
""" actor activitypub json """
id: str
type: str
def __init__(self, activity_objects=None, **kwargs):
''' this lets you pass in an object with fields that aren't in the
"""this lets you pass in an object with fields that aren't in the
dataclass, which it ignores. Any field in the dataclass is required or
has a default value '''
has a default value"""
for field in fields(self):
try:
value = kwargs[field.name]
@ -75,7 +82,7 @@ class ActivityObject:
except TypeError:
is_subclass = False
# serialize a model obj
if hasattr(value, 'to_activity'):
if hasattr(value, "to_activity"):
value = value.to_activity()
# parse a dict into the appropriate activity
elif is_subclass and isinstance(value, dict):
@ -83,25 +90,27 @@ class ActivityObject:
value = naive_parse(activity_objects, value)
else:
value = naive_parse(
activity_objects, value, serializer=field.type)
activity_objects, value, serializer=field.type
)
except KeyError:
if field.default == MISSING and \
field.default_factory == MISSING:
raise ActivitySerializerError(\
'Missing required field: %s' % field.name)
if field.default == MISSING and field.default_factory == MISSING:
raise ActivitySerializerError(
"Missing required field: %s" % field.name
)
value = field.default
setattr(self, field.name, value)
def to_model(self, model=None, instance=None, allow_create=True, save=True):
''' convert from an activity to a model instance '''
""" convert from an activity to a model instance """
model = model or get_model_from_type(self.type)
# only reject statuses if we're potentially creating them
if allow_create and \
hasattr(model, 'ignore_activity') and \
model.ignore_activity(self):
if (
allow_create
and hasattr(model, "ignore_activity")
and model.ignore_activity(self)
):
raise ActivitySerializerError()
# check for an existing instance
@ -142,8 +151,10 @@ class ActivityObject:
field.set_field_from_activity(instance, self)
# reversed relationships in the models
for (model_field_name, activity_field_name) in \
instance.deserialize_reverse_fields:
for (
model_field_name,
activity_field_name,
) in instance.deserialize_reverse_fields:
# attachments on Status, for example
values = getattr(self, activity_field_name)
if values is None or values is MISSING:
@ -161,13 +172,12 @@ class ActivityObject:
instance.__class__.__name__,
related_field_name,
instance.remote_id,
item
item,
)
return instance
def serialize(self):
''' convert to dictionary with context attr '''
""" convert to dictionary with context attr """
data = self.__dict__.copy()
# recursively serialize
for (k, v) in data.items():
@ -176,22 +186,19 @@ class ActivityObject:
data[k] = v.serialize()
except TypeError:
pass
data = {k:v for (k, v) in data.items() if v is not None}
data['@context'] = 'https://www.w3.org/ns/activitystreams'
data = {k: v for (k, v) in data.items() if v is not None}
data["@context"] = "https://www.w3.org/ns/activitystreams"
return data
@app.task
@transaction.atomic
def set_related_field(
model_name, origin_model_name, related_field_name,
related_remote_id, data):
''' load reverse related fields (editions, attachments) without blocking '''
model = apps.get_model('bookwyrm.%s' % model_name, require_ready=True)
origin_model = apps.get_model(
'bookwyrm.%s' % origin_model_name,
require_ready=True
)
model_name, origin_model_name, related_field_name, related_remote_id, data
):
""" load reverse related fields (editions, attachments) without blocking """
model = apps.get_model("bookwyrm.%s" % model_name, require_ready=True)
origin_model = apps.get_model("bookwyrm.%s" % origin_model_name, require_ready=True)
with transaction.atomic():
if isinstance(data, str):
@ -205,43 +212,45 @@ def set_related_field(
# this must exist because it's the object that triggered this function
instance = origin_model.find_existing_by_remote_id(related_remote_id)
if not instance:
raise ValueError(
'Invalid related remote id: %s' % related_remote_id)
raise ValueError("Invalid related remote id: %s" % related_remote_id)
# set the origin's remote id on the activity so it will be there when
# the model instance is created
# edition.parentWork = instance, for example
model_field = getattr(model, related_field_name)
if hasattr(model_field, 'activitypub_field'):
if hasattr(model_field, "activitypub_field"):
setattr(
activity,
getattr(model_field, 'activitypub_field'),
instance.remote_id
activity, getattr(model_field, "activitypub_field"), instance.remote_id
)
item = activity.to_model()
# if the related field isn't serialized (attachments on Status), then
# we have to set it post-creation
if not hasattr(model_field, 'activitypub_field'):
if not hasattr(model_field, "activitypub_field"):
setattr(item, related_field_name, instance)
item.save()
def get_model_from_type(activity_type):
''' given the activity, what type of model '''
""" given the activity, what type of model """
models = apps.get_models()
model = [m for m in models if hasattr(m, 'activity_serializer') and \
hasattr(m.activity_serializer, 'type') and \
m.activity_serializer.type == activity_type]
model = [
m
for m in models
if hasattr(m, "activity_serializer")
and hasattr(m.activity_serializer, "type")
and m.activity_serializer.type == activity_type
]
if not model:
raise ActivitySerializerError(
'No model found for activity type "%s"' % activity_type)
'No model found for activity type "%s"' % activity_type
)
return model[0]
def resolve_remote_id(remote_id, model=None, refresh=False, save=True):
''' take a remote_id and return an instance, creating if necessary '''
if model:# a bonus check we can do if we already know the model
""" take a remote_id and return an instance, creating if necessary """
if model: # a bonus check we can do if we already know the model
result = model.find_existing_by_remote_id(remote_id)
if result and not refresh:
return result
@ -251,11 +260,12 @@ def resolve_remote_id(remote_id, model=None, refresh=False, save=True):
data = get_data(remote_id)
except (ConnectorException, ConnectionError):
raise ActivitySerializerError(
'Could not connect to host for remote_id in %s model: %s' % \
(model.__name__, remote_id))
"Could not connect to host for remote_id in %s model: %s"
% (model.__name__, remote_id)
)
# determine the model implicitly, if not provided
if not model:
model = get_model_from_type(data.get('type'))
model = get_model_from_type(data.get("type"))
# check for existing items with shared unique identifiers
result = model.find_existing(data)