Skip to content

Commit

Permalink
port atom.py from django templates to jinja2
Browse files Browse the repository at this point in the history
...in order to (re)drop the app engine dependency.

cc @kylewm @kevinmarks @danlyke re our template discussion last night. not proud of this, but meh. at least the template itself didn't change much! :P
  • Loading branch information
snarfed committed Jul 2, 2015
1 parent 3c2162e commit 5a8d7ff
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 15 deletions.
37 changes: 26 additions & 11 deletions granary/atom.py
Expand Up @@ -3,18 +3,18 @@
Atom spec: http://atomenabled.org/developers/syndication/
"""

import collections
import os
import re
import urlparse

import webapp2
from google.appengine.ext.webapp import template
import jinja2
from oauth_dropins.webutil import util

import microformats2
import source

ATOM_TEMPLATE_FILE = os.path.join(os.path.dirname(__file__), 'templates', 'user_feed.atom')
ATOM_TEMPLATE_FILE = 'user_feed.atom'
# stolen from django.utils.html
UNENCODED_AMPERSANDS_RE = re.compile(r'&(?!(\w+|#\d+);)')

Expand Down Expand Up @@ -61,14 +61,29 @@ def activities_to_atom(activities, actor, title=None, request_url=None,
if image and not isinstance(image, list):
att['image'] = [image]

return template.render(ATOM_TEMPLATE_FILE, {
'items': activities,
'host_url': host_url,
'request_url': request_url,
'title': title or 'User feed for ' + source.Source.actor_name(actor),
'updated': activities[0]['object'].get('published') if activities else '',
'actor': actor,
})
# Emulate Django template behavior that returns a special default value that
# can continue to be referenced when an attribute or item lookup fails. Helps
# avoid conditionals in the template itself.
# https://docs.djangoproject.com/en/1.8/ref/templates/language/#variables
class Defaulter(collections.defaultdict):
def __init__(self, **kwargs):
super(Defaulter, self).__init__(Defaulter, **{
k: (Defaulter(**v) if isinstance(v, dict) else v)
for k, v in kwargs.items()})

def __unicode__(self):
return super(Defaulter, self).__unicode__() if self else u''

env = jinja2.Environment(loader=jinja2.PackageLoader(__package__, 'templates'),
autoescape=True)
return env.get_template(ATOM_TEMPLATE_FILE).render(
items=[Defaulter(**a) for a in activities],
host_url=host_url,
request_url=request_url,
title=title or 'User feed for ' + source.Source.actor_name(actor),
updated=activities[0]['object'].get('published', '') if activities else '',
actor=actor,
)


def _remove_query_params(url):
Expand Down
9 changes: 5 additions & 4 deletions granary/templates/user_feed.atom
Expand Up @@ -16,6 +16,7 @@
<logo>{{ actor.image.url }}</logo>
<updated>{{ updated }}</updated>
{% include "author.atom" %}

<link href="{{ actor.url }}" rel="alternate" type="text/html" />
<link rel="avatar" href="{{ actor.image.url }}" />
<link href="{{ request_url }}" rel="self" type="application/atom+xml" />
Expand All @@ -26,9 +27,9 @@
<!-- <link href="{{ salmon_uri }}" rel="http://salmon-protocol.org/ns/salmon-mention" /> -->
{% for activity in items %}
<entry>
{% with activity.actor as actor %}
{% set actor = activity.actor %}
{% include "author.atom" %}
{% endwith %}

<activity:object-type>
http://activitystrea.ms/schema/1.0/{{ activity.object.objectType }}
</activity:object-type>
Expand Down Expand Up @@ -65,14 +66,14 @@
{% endfor %}
<!-- <link rel="ostatus:conversation" href="" /> -->
<!-- http://www.georss.org/simple -->
{% with activity.object.location as location %}
{% set location = activity.object.location %}
{% if location.latitude and location.longitude %}
<georss:point>{{ location.latitude }} {{location.longitude }}</georss:point>
{% endif %}
{% if location.displayName %}
<georss:featureName>{{ location.displayName }}</georss:featureName>
{% endif %}
{% endwith %}

<link rel="self" type="application/atom+xml" href="{{ activity.url }}" />
</entry>
{% endfor %}
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
@@ -1,5 +1,6 @@
# Keep in sync with setup.py's install_requires!
beautifulsoup4
jinja2
mf2py>=0.2.6
oauth-dropins
requests
1 change: 1 addition & 0 deletions setup.py
Expand Up @@ -41,6 +41,7 @@ def __init__(self, *args, **kwargs):
install_requires=[
# Keep in sync with requirements.txt!
'beautifulsoup4',
'jinja2',
'mf2py>=0.2.6',
'oauth-dropins',
'requests',
Expand Down

0 comments on commit 5a8d7ff

Please sign in to comment.