mirror of
https://github.com/MCV-Software/TWBlue.git
synced 2025-07-17 21:56:07 -04:00
Core: Display variables within templates. Closes #515
This commit is contained in:
@@ -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
|
||||
|
@@ -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
|
Reference in New Issue
Block a user