-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor logic for injecting/creating RSS routes
This commit removes the duplicated code used to inject the logic for templating RSS feeds into existing routes.
- Loading branch information
Showing
6 changed files
with
69 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import datetime | ||
from typing import Optional, Dict, Any | ||
|
||
import sanic | ||
import sanic_ext | ||
|
||
# Wrapper around sanic_ext.render | ||
def render_template( | ||
template: str = "", | ||
context: Optional[Dict[str, Any]] = None, | ||
**kwargs | ||
): | ||
jinja_context = context or {} | ||
|
||
request = sanic.Request.get_current() | ||
|
||
if hasattr(request.route.ctx, "rss"): | ||
template = getattr(request.route.ctx, "template", None) or template | ||
template = f"rss/{template}.xml" | ||
kwargs["content_type"] = "application/rss+xml" | ||
|
||
# Remove /rss suffix | ||
base_path = request.app.url_for(request.endpoint[:-4], **request.match_info) | ||
|
||
page_url = f"{request.app.ctx.PRIVIBLUR_CONFIG.deployment.domain or ''}{base_path}" | ||
if request.query_string: | ||
page_url += f"?{request.query_string}" | ||
|
||
jinja_context["page_url"] = page_url | ||
|
||
if (elements := jinja_context.get("blog")) and elements.posts: | ||
jinja_context["updated"] = elements.posts[-1].date | ||
elif (elements := jinja_context.get("timeline")) and elements.elements: | ||
jinja_context["updated"] = elements.elements[-1].date | ||
else: | ||
jinja_context["updated"] = datetime.datetime.now(tz=datetime.timezone.utc) | ||
|
||
template = f"{template}.jinja" | ||
|
||
return sanic_ext.render( | ||
template, | ||
context=jinja_context, | ||
app=request.app, | ||
**kwargs | ||
) | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters