1
0
Fork 0

New version of black, new whitespace

This commit is contained in:
Mouse Reeve 2021-04-26 09:15:42 -07:00
parent ef83eb33b0
commit 3ade2d3bb1
152 changed files with 1289 additions and 1289 deletions

View file

@ -31,18 +31,18 @@ PropertyField = namedtuple("PropertyField", ("set_activity_from_field"))
def set_activity_from_property_field(activity, obj, field):
""" assign a model property value to the activity json """
"""assign a model property value to the activity json"""
activity[field[1]] = getattr(obj, field[0])
class ActivitypubMixin:
""" add this mixin for models that are AP serializable """
"""add this mixin for models that are AP serializable"""
activity_serializer = lambda: {}
reverse_unfurl = False
def __init__(self, *args, **kwargs):
""" collect some info on model fields """
"""collect some info on model fields"""
self.image_fields = []
self.many_to_many_fields = []
self.simple_fields = [] # "simple"
@ -85,7 +85,7 @@ class ActivitypubMixin:
@classmethod
def find_existing_by_remote_id(cls, remote_id):
""" look up a remote id in the db """
"""look up a remote id in the db"""
return cls.find_existing({"id": remote_id})
@classmethod
@ -126,7 +126,7 @@ class ActivitypubMixin:
return match.first()
def broadcast(self, activity, sender, software=None):
""" send out an activity """
"""send out an activity"""
broadcast_task.delay(
sender.id,
json.dumps(activity, cls=activitypub.ActivityEncoder),
@ -134,7 +134,7 @@ class ActivitypubMixin:
)
def get_recipients(self, software=None):
""" figure out which inbox urls to post to """
"""figure out which inbox urls to post to"""
# first we have to figure out who should receive this activity
privacy = self.privacy if hasattr(self, "privacy") else "public"
# is this activity owned by a user (statuses, lists, shelves), or is it
@ -182,20 +182,20 @@ class ActivitypubMixin:
return list(set(recipients))
def to_activity_dataclass(self):
""" convert from a model to an activity """
"""convert from a model to an activity"""
activity = generate_activity(self)
return self.activity_serializer(**activity)
def to_activity(self, **kwargs): # pylint: disable=unused-argument
""" convert from a model to a json activity """
"""convert from a model to a json activity"""
return self.to_activity_dataclass().serialize()
class ObjectMixin(ActivitypubMixin):
""" add this mixin for object models that are AP serializable """
"""add this mixin for object models that are AP serializable"""
def save(self, *args, created=None, **kwargs):
""" broadcast created/updated/deleted objects as appropriate """
"""broadcast created/updated/deleted objects as appropriate"""
broadcast = kwargs.get("broadcast", True)
# this bonus kwarg would cause an error in the base save method
if "broadcast" in kwargs:
@ -254,7 +254,7 @@ class ObjectMixin(ActivitypubMixin):
self.broadcast(activity, user)
def to_create_activity(self, user, **kwargs):
""" returns the object wrapped in a Create activity """
"""returns the object wrapped in a Create activity"""
activity_object = self.to_activity_dataclass(**kwargs)
signature = None
@ -280,7 +280,7 @@ class ObjectMixin(ActivitypubMixin):
).serialize()
def to_delete_activity(self, user):
""" notice of deletion """
"""notice of deletion"""
return activitypub.Delete(
id=self.remote_id + "/activity",
actor=user.remote_id,
@ -290,7 +290,7 @@ class ObjectMixin(ActivitypubMixin):
).serialize()
def to_update_activity(self, user):
""" wrapper for Updates to an activity """
"""wrapper for Updates to an activity"""
activity_id = "%s#update/%s" % (self.remote_id, uuid4())
return activitypub.Update(
id=activity_id,
@ -306,13 +306,13 @@ class OrderedCollectionPageMixin(ObjectMixin):
@property
def collection_remote_id(self):
""" this can be overriden if there's a special remote id, ie outbox """
"""this can be overriden if there's a special remote id, ie outbox"""
return self.remote_id
def to_ordered_collection(
self, queryset, remote_id=None, page=False, collection_only=False, **kwargs
):
""" an ordered collection of whatevers """
"""an ordered collection of whatevers"""
if not queryset.ordered:
raise RuntimeError("queryset must be ordered")
@ -341,11 +341,11 @@ class OrderedCollectionPageMixin(ObjectMixin):
class OrderedCollectionMixin(OrderedCollectionPageMixin):
""" extends activitypub models to work as ordered collections """
"""extends activitypub models to work as ordered collections"""
@property
def collection_queryset(self):
""" usually an ordered collection model aggregates a different model """
"""usually an ordered collection model aggregates a different model"""
raise NotImplementedError("Model must define collection_queryset")
activity_serializer = activitypub.OrderedCollection
@ -354,24 +354,24 @@ class OrderedCollectionMixin(OrderedCollectionPageMixin):
return self.to_ordered_collection(self.collection_queryset, **kwargs)
def to_activity(self, **kwargs):
""" an ordered collection of the specified model queryset """
"""an ordered collection of the specified model queryset"""
return self.to_ordered_collection(
self.collection_queryset, **kwargs
).serialize()
class CollectionItemMixin(ActivitypubMixin):
""" for items that are part of an (Ordered)Collection """
"""for items that are part of an (Ordered)Collection"""
activity_serializer = activitypub.CollectionItem
def broadcast(self, activity, sender, software="bookwyrm"):
""" only send book collection updates to other bookwyrm instances """
"""only send book collection updates to other bookwyrm instances"""
super().broadcast(activity, sender, software=software)
@property
def privacy(self):
""" inherit the privacy of the list, or direct if pending """
"""inherit the privacy of the list, or direct if pending"""
collection_field = getattr(self, self.collection_field)
if self.approved:
return collection_field.privacy
@ -379,7 +379,7 @@ class CollectionItemMixin(ActivitypubMixin):
@property
def recipients(self):
""" the owner of the list is a direct recipient """
"""the owner of the list is a direct recipient"""
collection_field = getattr(self, self.collection_field)
if collection_field.user.local:
# don't broadcast to yourself
@ -387,7 +387,7 @@ class CollectionItemMixin(ActivitypubMixin):
return [collection_field.user]
def save(self, *args, broadcast=True, **kwargs):
""" broadcast updated """
"""broadcast updated"""
# first off, we want to save normally no matter what
super().save(*args, **kwargs)
@ -400,14 +400,14 @@ class CollectionItemMixin(ActivitypubMixin):
self.broadcast(activity, self.user)
def delete(self, *args, broadcast=True, **kwargs):
""" broadcast a remove activity """
"""broadcast a remove activity"""
activity = self.to_remove_activity(self.user)
super().delete(*args, **kwargs)
if self.user.local and broadcast:
self.broadcast(activity, self.user)
def to_add_activity(self, user):
""" AP for shelving a book"""
"""AP for shelving a book"""
collection_field = getattr(self, self.collection_field)
return activitypub.Add(
id="{:s}#add".format(collection_field.remote_id),
@ -417,7 +417,7 @@ class CollectionItemMixin(ActivitypubMixin):
).serialize()
def to_remove_activity(self, user):
""" AP for un-shelving a book"""
"""AP for un-shelving a book"""
collection_field = getattr(self, self.collection_field)
return activitypub.Remove(
id="{:s}#remove".format(collection_field.remote_id),
@ -428,24 +428,24 @@ class CollectionItemMixin(ActivitypubMixin):
class ActivityMixin(ActivitypubMixin):
""" add this mixin for models that are AP serializable """
"""add this mixin for models that are AP serializable"""
def save(self, *args, broadcast=True, **kwargs):
""" broadcast activity """
"""broadcast activity"""
super().save(*args, **kwargs)
user = self.user if hasattr(self, "user") else self.user_subject
if broadcast and user.local:
self.broadcast(self.to_activity(), user)
def delete(self, *args, broadcast=True, **kwargs):
""" nevermind, undo that activity """
"""nevermind, undo that activity"""
user = self.user if hasattr(self, "user") else self.user_subject
if broadcast and user.local:
self.broadcast(self.to_undo_activity(), user)
super().delete(*args, **kwargs)
def to_undo_activity(self):
""" undo an action """
"""undo an action"""
user = self.user if hasattr(self, "user") else self.user_subject
return activitypub.Undo(
id="%s#undo" % self.remote_id,
@ -455,7 +455,7 @@ class ActivityMixin(ActivitypubMixin):
def generate_activity(obj):
""" go through the fields on an object """
"""go through the fields on an object"""
activity = {}
for field in obj.activity_fields:
field.set_activity_from_field(activity, obj)
@ -478,7 +478,7 @@ def generate_activity(obj):
def unfurl_related_field(related_field, sort_field=None):
""" load reverse lookups (like public key owner or Status attachment """
"""load reverse lookups (like public key owner or Status attachment"""
if sort_field and hasattr(related_field, "all"):
return [
unfurl_related_field(i) for i in related_field.order_by(sort_field).all()
@ -494,7 +494,7 @@ def unfurl_related_field(related_field, sort_field=None):
@app.task
def broadcast_task(sender_id, activity, recipients):
""" the celery task for broadcast """
"""the celery task for broadcast"""
user_model = apps.get_model("bookwyrm.User", require_ready=True)
sender = user_model.objects.get(id=sender_id)
for recipient in recipients:
@ -505,7 +505,7 @@ def broadcast_task(sender_id, activity, recipients):
def sign_and_send(sender, data, destination):
""" crpyto whatever and http junk """
"""crpyto whatever and http junk"""
now = http_date()
if not sender.key_pair.private_key:
@ -534,7 +534,7 @@ def sign_and_send(sender, data, destination):
def to_ordered_collection_page(
queryset, remote_id, id_only=False, page=1, pure=False, **kwargs
):
""" serialize and pagiante a queryset """
"""serialize and pagiante a queryset"""
paginated = Paginator(queryset, PAGE_LENGTH)
activity_page = paginated.get_page(page)