Updating string format synatx part 2

This commit is contained in:
Mouse Reeve
2021-09-18 11:32:00 -07:00
parent be86fbcf4d
commit acfb1bb376
36 changed files with 96 additions and 110 deletions

View File

@ -266,7 +266,7 @@ class ObjectMixin(ActivitypubMixin):
signed_message = signer.sign(SHA256.new(content.encode("utf8")))
signature = activitypub.Signature(
creator="%s#main-key" % user.remote_id,
creator=f"{user.remote_id}#main-key",
created=activity_object.published,
signatureValue=b64encode(signed_message).decode("utf8"),
)
@ -285,16 +285,16 @@ class ObjectMixin(ActivitypubMixin):
return activitypub.Delete(
id=self.remote_id + "/activity",
actor=user.remote_id,
to=["%s/followers" % user.remote_id],
to=[f"{user.remote_id}/followers"],
cc=["https://www.w3.org/ns/activitystreams#Public"],
object=self,
).serialize()
def to_update_activity(self, user):
"""wrapper for Updates to an activity"""
activity_id = "%s#update/%s" % (self.remote_id, uuid4())
uuid = uuid4()
return activitypub.Update(
id=activity_id,
id=f"{self.remote_id}#update/{uuid}",
actor=user.remote_id,
to=["https://www.w3.org/ns/activitystreams#Public"],
object=self,
@ -337,8 +337,8 @@ class OrderedCollectionPageMixin(ObjectMixin):
paginated = Paginator(queryset, PAGE_LENGTH)
# add computed fields specific to orderd collections
activity["totalItems"] = paginated.count
activity["first"] = "%s?page=1" % remote_id
activity["last"] = "%s?page=%d" % (remote_id, paginated.num_pages)
activity["first"] = f"{remote_id}?page=1"
activity["last"] = f"{remote_id}?page={paginated.num_pages}"
return serializer(**activity)
@ -420,7 +420,7 @@ class CollectionItemMixin(ActivitypubMixin):
"""AP for shelving a book"""
collection_field = getattr(self, self.collection_field)
return activitypub.Add(
id="{:s}#add".format(collection_field.remote_id),
id=f"{collection_field.remote_id}#add",
actor=user.remote_id,
object=self.to_activity_dataclass(),
target=collection_field.remote_id,
@ -430,7 +430,7 @@ class CollectionItemMixin(ActivitypubMixin):
"""AP for un-shelving a book"""
collection_field = getattr(self, self.collection_field)
return activitypub.Remove(
id="{:s}#remove".format(collection_field.remote_id),
id=f"{collection_field.remote_id}#remove",
actor=user.remote_id,
object=self.to_activity_dataclass(),
target=collection_field.remote_id,
@ -458,7 +458,7 @@ class ActivityMixin(ActivitypubMixin):
"""undo an action"""
user = self.user if hasattr(self, "user") else self.user_subject
return activitypub.Undo(
id="%s#undo" % self.remote_id,
id=f"{self.remote_id}#undo",
actor=user.remote_id,
object=self,
).serialize()
@ -555,11 +555,11 @@ def to_ordered_collection_page(
prev_page = next_page = None
if activity_page.has_next():
next_page = "%s?page=%d" % (remote_id, activity_page.next_page_number())
next_page = f"{remote_id}?page={activity_page.next_page_number()}"
if activity_page.has_previous():
prev_page = "%s?page=%d" % (remote_id, activity_page.previous_page_number())
prev_page = f"{remote_id}?page=%d{activity_page.previous_page_number()}"
return activitypub.OrderedCollectionPage(
id="%s?page=%s" % (remote_id, page),
id=f"{remote_id}?page={page}",
partOf=remote_id,
orderedItems=items,
next=next_page,

View File

@ -35,7 +35,7 @@ class Author(BookDataModel):
def get_remote_id(self):
"""editions and works both use "book" instead of model_name"""
return "https://%s/author/%s" % (DOMAIN, self.id)
return f"https://{DOMAIN}/author/{self.id}"
activity_serializer = activitypub.Author

View File

@ -32,11 +32,11 @@ class BookWyrmModel(models.Model):
def get_remote_id(self):
"""generate a url that resolves to the local object"""
base_path = "https://%s" % DOMAIN
base_path = f"https://{DOMAIN}"
if hasattr(self, "user"):
base_path = "%s%s" % (base_path, self.user.local_path)
base_path = f"{base_path}{self.user.local_path}"
model_name = type(self).__name__.lower()
return "%s/%s/%d" % (base_path, model_name, self.id)
return f"{base_path}/{model_name}/{self.id}"
class Meta:
"""this is just here to provide default fields for other models"""
@ -46,7 +46,7 @@ class BookWyrmModel(models.Model):
@property
def local_path(self):
"""how to link to this object in the local app"""
return self.get_remote_id().replace("https://%s" % DOMAIN, "")
return self.get_remote_id().replace(f"https://{DOMAIN}", "")
def visible_to_user(self, viewer):
"""is a user authorized to view an object?"""

View File

@ -164,9 +164,9 @@ class Book(BookDataModel):
@property
def alt_text(self):
"""image alt test"""
text = "%s" % self.title
text = self.title
if self.edition_info:
text += " (%s)" % self.edition_info
text += f" ({self.edition_info})"
return text
def save(self, *args, **kwargs):
@ -177,9 +177,10 @@ class Book(BookDataModel):
def get_remote_id(self):
"""editions and works both use "book" instead of model_name"""
return "https://%s/book/%d" % (DOMAIN, self.id)
return f"https://{DOMAIN}/book/{self.id}"
def __repr__(self):
# pylint: disable=consider-using-f-string
return "<{} key={!r} title={!r}>".format(
self.__class__,
self.openlibrary_key,
@ -216,7 +217,7 @@ class Work(OrderedCollectionPageMixin, Book):
"""an ordered collection of editions"""
return self.to_ordered_collection(
self.editions.order_by("-edition_rank").all(),
remote_id="%s/editions" % self.remote_id,
remote_id=f"{self.remote_id}/editions",
**kwargs,
)

View File

@ -29,7 +29,4 @@ class Connector(BookWyrmModel):
isbn_search_url = models.CharField(max_length=255, null=True, blank=True)
def __str__(self):
return "{} ({})".format(
self.identifier,
self.id,
)
return f"{self.identifier} ({self.id})"

View File

@ -308,7 +308,7 @@ class ManyToManyField(ActivitypubFieldMixin, models.ManyToManyField):
def field_to_activity(self, value):
if self.link_only:
return "%s/%s" % (value.instance.remote_id, self.name)
return f"{value.instance.remote_id}/{self.name}"
return [i.remote_id for i in value.all()]
def field_from_activity(self, value):
@ -388,7 +388,7 @@ def image_serializer(value, alt):
else:
return None
if not url[:4] == "http":
url = "https://{:s}{:s}".format(DOMAIN, url)
url = f"https://{DOMAIN}{url}"
return activitypub.Document(url=url, name=alt)
@ -448,7 +448,7 @@ class ImageField(ActivitypubFieldMixin, models.ImageField):
image_content = ContentFile(response.content)
extension = imghdr.what(None, image_content.read()) or ""
image_name = "{:s}.{:s}".format(str(uuid4()), extension)
image_name = f"{uuid4()}.{extension}"
return [image_name, image_content]
def formfield(self, **kwargs):

View File

@ -198,7 +198,9 @@ class ImportItem(models.Model):
return []
def __repr__(self):
# pylint: disable=consider-using-f-string
return "<{!r}Item {!r}>".format(self.data["import_source"], self.data["Title"])
def __str__(self):
# pylint: disable=consider-using-f-string
return "{} by {}".format(self.data["Title"], self.data["Author"])

View File

@ -42,7 +42,7 @@ class List(OrderedCollectionMixin, BookWyrmModel):
def get_remote_id(self):
"""don't want the user to be in there in this case"""
return "https://%s/list/%d" % (DOMAIN, self.id)
return f"https://{DOMAIN}/list/{self.id}"
@property
def collection_queryset(self):

View File

@ -53,7 +53,7 @@ class UserRelationship(BookWyrmModel):
def get_remote_id(self):
"""use shelf identifier in remote_id"""
base_path = self.user_subject.remote_id
return "%s#follows/%d" % (base_path, self.id)
return f"{base_path}#follows/{self.id}"
class UserFollows(ActivityMixin, UserRelationship):
@ -144,7 +144,8 @@ class UserFollowRequest(ActivitypubMixin, UserRelationship):
"""get id for sending an accept or reject of a local user"""
base_path = self.user_object.remote_id
return "%s#%s/%d" % (base_path, status, self.id or 0)
status_id = self.id or 0
return f"{base_path}#{status}/{status_id}"
def accept(self, broadcast_only=False):
"""turn this request into the real deal"""

View File

@ -44,7 +44,7 @@ class Shelf(OrderedCollectionMixin, BookWyrmModel):
def get_identifier(self):
"""custom-shelf-123 for the url"""
slug = re.sub(r"[^\w]", "", self.name).lower()
return "{:s}-{:d}".format(slug, self.id)
return f"{slug}-{self.id}"
@property
def collection_queryset(self):
@ -55,7 +55,7 @@ class Shelf(OrderedCollectionMixin, BookWyrmModel):
"""shelf identifier instead of id"""
base_path = self.user.remote_id
identifier = self.identifier or self.get_identifier()
return "%s/books/%s" % (base_path, identifier)
return f"{base_path}/books/{identifier}"
class Meta:
"""user/shelf unqiueness"""

View File

@ -81,7 +81,7 @@ class SiteInvite(models.Model):
@property
def link(self):
"""formats the invite link"""
return "https://{}/invite/{}".format(DOMAIN, self.code)
return f"https://{DOMAIN}/invite/{self.code}"
class InviteRequest(BookWyrmModel):
@ -121,7 +121,7 @@ class PasswordReset(models.Model):
@property
def link(self):
"""formats the invite link"""
return "https://{}/password-reset/{}".format(DOMAIN, self.code)
return "https://{DOMAIN}/password-reset/{self.code}"
# pylint: disable=unused-argument

View File

@ -179,7 +179,7 @@ class Status(OrderedCollectionPageMixin, BookWyrmModel):
"""helper function for loading AP serialized replies to a status"""
return self.to_ordered_collection(
self.replies(self),
remote_id="%s/replies" % self.remote_id,
remote_id=f"{self.remote_id}/replies",
collection_only=True,
**kwargs
).serialize()
@ -226,10 +226,10 @@ class GeneratedNote(Status):
"""indicate the book in question for mastodon (or w/e) users"""
message = self.content
books = ", ".join(
'<a href="%s">"%s"</a>' % (book.remote_id, book.title)
f'<a href="{book.remote_id}">"{book.title}"</a>'
for book in self.mention_books.all()
)
return "%s %s %s" % (self.user.display_name, message, books)
return f"{self.user.display_name} {message} {books}"
activity_serializer = activitypub.GeneratedNote
pure_type = "Note"
@ -277,11 +277,7 @@ class Comment(BookStatus):
@property
def pure_content(self):
"""indicate the book in question for mastodon (or w/e) users"""
return '%s<p>(comment on <a href="%s">"%s"</a>)</p>' % (
self.content,
self.book.remote_id,
self.book.title,
)
return f'{self.content}<p>(comment on <a href="{self.book.remote_id}">"{self.book.title}"</a>)</p>'
activity_serializer = activitypub.Comment
@ -306,12 +302,7 @@ class Quotation(BookStatus):
"""indicate the book in question for mastodon (or w/e) users"""
quote = re.sub(r"^<p>", '<p>"', self.quote)
quote = re.sub(r"</p>$", '"</p>', quote)
return '%s <p>-- <a href="%s">"%s"</a></p>%s' % (
quote,
self.book.remote_id,
self.book.title,
self.content,
)
return f'{quote} <p>-- <a href="{self.book.remote_id}">"{self.book.title}"</a></p>{self.content}'
activity_serializer = activitypub.Quotation