Core: Display variables within templates. Closes #515

This commit is contained in:
Manuel Cortez 2023-02-10 00:46:08 -06:00
parent 68c5b9affe
commit e9dc02e868
No known key found for this signature in database
GPG Key ID: 9E0735CA15EFE790
3 changed files with 11 additions and 22 deletions

View File

@ -2,6 +2,12 @@ TWBlue Changelog
## changes in this version
We had to release a 2023.2.8 version of TWblue, which removes the API deprecation code as Twitter has changed again their plans, and set their free API shutdown to February 13. This new release will not enforce any date for stopping support to Twitter sessions, but will skip loading the sessions if there are errors when logging into Twitter. There are also some significant improvements to our Mastodon support, which you can read in the following list of changes:
* TWBlue should be able to display variables within templates (for example, now it is possible to send a template inside a post's text). Before, it was removing $variables so it was difficult to show how to edit templates from the client. ([#515](https://github.com/MCV-Software/TWBlue/issues/515))
## Changes on version 2023.2.6
This release focuses on fixing some important bugs that have been reported in the previous version. Particularly, TWBlue should be able to authorize on some instances that have blocked the Mastodon.py library, and should be able to avoid repeatedly calling some endpoints that cause excessive connections for some instances. Additionally, it is possible to disable Streaming from the account options in Mastodon. This can be especially useful if TWBlue keeps making a lot of API calls for some instances.
* Fixed the update system.

View File

@ -45,11 +45,6 @@ def process_image_descriptions(media_attachments):
idescriptions = idescriptions + _("Image description: {}").format(image) + "\n"
return idescriptions
def remove_unneeded_variables(template, variables):
for variable in variables:
template = re.sub("\$"+variable, "", template)
return template
def render_post(post, template, relative_times=False, offset_hours=0):
""" Renders any given post according to the passed template.
Available data for posts will be stored in the following variables:
@ -64,7 +59,7 @@ def render_post(post, template, relative_times=False, offset_hours=0):
$visibility: post's visibility: public, not listed, followers only or direct.
"""
global post_variables
available_data = dict()
available_data = dict(source="")
created_at = process_date(post.created_at, relative_times, offset_hours)
available_data.update(date=created_at)
# user.
@ -91,10 +86,8 @@ def render_post(post, template, relative_times=False, offset_hours=0):
image_descriptions = process_image_descriptions(post.reblog.media_attachments)
else:
image_descriptions = process_image_descriptions(post.media_attachments)
if image_descriptions != "":
available_data.update(image_descriptions=image_descriptions)
available_data.update(image_descriptions=image_descriptions)
result = Template(_(template)).safe_substitute(**available_data)
result = remove_unneeded_variables(result, post_variables)
return result
def render_user(user, template, relative_times=True, offset_hours=0):
@ -121,7 +114,6 @@ def render_user(user, template, relative_times=True, offset_hours=0):
created_at = process_date(user.created_at, relative_times=relative_times, offset_hours=offset_hours)
available_data.update(created_at=created_at)
result = Template(_(template)).safe_substitute(**available_data)
result = remove_unneeded_variables(result, person_variables)
return result
def render_conversation(conversation, template, post_template, relative_times=False, offset_hours=0):
@ -135,7 +127,6 @@ def render_conversation(conversation, template, post_template, relative_times=Fa
last_post = render_post(conversation.last_status, post_template, relative_times=relative_times, offset_hours=offset_hours)
available_data = dict(users=users, last_post=last_post)
result = Template(_(template)).safe_substitute(**available_data)
result = remove_unneeded_variables(result, conversation_variables)
return result
def render_notification(notification, template, post_template, relative_times=False, offset_hours=0):
@ -178,6 +169,5 @@ def render_notification(notification, template, post_template, relative_times=Fa
text = _("wants to follow you.")
available_data.update(text=text)
result = Template(_(template)).safe_substitute(**available_data)
result = remove_unneeded_variables(result, post_variables)
result = result.replace(" . ", "")
return result

View File

@ -54,11 +54,6 @@ def process_image_descriptions(entities):
idescriptions += _("Image description: {}.").format(image)
return idescriptions
def remove_unneeded_variables(template, variables):
for variable in variables:
template = re.sub("\$"+variable, "", template)
return template
def render_tweet(tweet, template, session, relative_times=False, offset_seconds=0):
""" Renders any given Tweet according to the passed template.
Available data for tweets will be stored in the following variables:
@ -96,10 +91,8 @@ def render_tweet(tweet, template, session, relative_times=False, offset_seconds=
image_descriptions = process_image_descriptions(tweet.retweeted_status.quoted_status.extended_entities)
elif hasattr(tweet, "extended_entities"):
image_descriptions = process_image_descriptions(tweet.extended_entities)
if image_descriptions != "":
available_data.update(image_descriptions=image_descriptions)
available_data.update(image_descriptions=image_descriptions)
result = Template(_(template)).safe_substitute(**available_data)
result = remove_unneeded_variables(result, tweet_variables)
return result
def render_dm(dm, template, session, relative_times=False, offset_seconds=0):
@ -127,7 +120,6 @@ def render_dm(dm, template, session, relative_times=False, offset_seconds=0):
recipient = session.get_user(dm.message_create["target"]["recipient_id"])
available_data.update(sender_display_name=sender.name, sender_screen_name=sender.screen_name, recipient_display_name=recipient.name, recipient_screen_name=recipient.screen_name)
result = Template(_(template)).safe_substitute(**available_data)
result = remove_unneeded_variables(result, dm_variables)
return result
# Sesion object is not used in this function but we keep compatibility across all rendering functions.
@ -152,8 +144,9 @@ def render_person(user, template, session=None, relative_times=True, offset_seco
for nullable in nullables:
if hasattr(user, nullable) and getattr(user, nullable) != None:
available_data[nullable] = getattr(user, nullable)
else:
available_data[nullable] = ""
created_at = process_date(user.created_at, relative_times=relative_times, offset_seconds=offset_seconds)
available_data.update(created_at=created_at)
result = Template(_(template)).safe_substitute(**available_data)
result = remove_unneeded_variables(result, person_variables)
return result