Skip to content

Commit

Permalink
add feed
Browse files Browse the repository at this point in the history
  • Loading branch information
whtsky committed Oct 21, 2012
1 parent 05930fc commit 3c57d66
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 14 deletions.
12 changes: 7 additions & 5 deletions handlers/__init__.py
Expand Up @@ -43,8 +43,8 @@ def get_source(self):
for k, v in sources.items(): for k, v in sources.items():
if k in ua: if k in ua:
return v return v
if 'Windows Phone' in ua: if 'Windows Phone' in ua:
return 'Windows Phone' return 'Windows Phone'
if 'Nokia' in ua: if 'Nokia' in ua:
return _NOKIA_FINDER_.findall(ua)[0] return _NOKIA_FINDER_.findall(ua)[0]


Expand All @@ -53,12 +53,14 @@ def get_source(self):
@property @property
def db(self): def db(self):
return self.application.db return self.application.db

@property @property
def messages(self): def messages(self):
if not hasattr(self, '_messages'): if not hasattr(self, '_messages'):
messages = self.get_secure_cookie('flash_messages') messages = self.get_secure_cookie('flash_messages')
self._messages = messages and tornado.escape.json_decode(messages) or [] self._messages = []
if messages:
self._messages = tornado.escape.json_decode(messages)
return self._messages return self._messages


def get_member(self, name): def get_member(self, name):
Expand Down Expand Up @@ -87,7 +89,7 @@ def get_avatar(self, member, size=48):
avatar += '?s=%s' % size avatar += '?s=%s' % size
return '<a href="/member/%s" class="avatar">\ return '<a href="/member/%s" class="avatar">\
<img src="%s" /></a>' % (member['name'], avatar) <img src="%s" /></a>' % (member['name'], avatar)

def get_page_num(self, count, per_page): def get_page_num(self, count, per_page):
return int((count + per_page - 1) / per_page) return int((count + per_page - 1) / per_page)


Expand Down
23 changes: 15 additions & 8 deletions handlers/others.py
Expand Up @@ -4,13 +4,20 @@




class UserAgentHandler(BaseHandler): class UserAgentHandler(BaseHandler):
def get(self): def get(self):
ua = self.request.headers.get("User-Agent", "Unknow") ua = self.request.headers.get("User-Agent", "Unknow")
source = self.get_source() source = self.get_source()
if not source: if not source:
source = 'Desktop' source = 'Desktop'
self.render('others/ua.html', ua=ua, source=source) self.render('others/ua.html', ua=ua, source=source)



class FeedHandler(BaseHandler):
def get(self):
topics = self.db.topics.find(sort=[('modified', -1)])
self.render('feed.xml', topics=topics)

handlers = [ handlers = [
(r'/ua', UserAgentHandler), (r'/ua', UserAgentHandler),
(r'/feed', FeedHandler),
] ]
2 changes: 1 addition & 1 deletion main.py
Expand Up @@ -25,7 +25,7 @@ def __init__(self):
2: 'Admin', 2: 'Admin',
3: 'SuperAdmin'}} 3: 'SuperAdmin'}}
execfile(options.settings, {}, settings) execfile(options.settings, {}, settings)

settings['host'] = settings['forum_url'].split('/')[2] settings['host'] = settings['forum_url'].split('/')[2]


if 'static_path' not in settings: if 'static_path' not in settings:
Expand Down
1 change: 1 addition & 0 deletions templates/base.html
Expand Up @@ -7,6 +7,7 @@
<link rel="stylesheet" href="{{ static_url('css/style.css') }}" type="text/css" /> <link rel="stylesheet" href="{{ static_url('css/style.css') }}" type="text/css" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="canonical" href="{{ handler.settings['forum_url'] }}{% block canonical %}{% end %}" /> <link rel="canonical" href="{{ handler.settings['forum_url'] }}{% block canonical %}{% end %}" />
<link rel="alternate" type="application/atom+xml" href="/feed" />
{% block head %}{% end %} {% block head %}{% end %}
</head> </head>
<body> <body>
Expand Down
20 changes: 20 additions & 0 deletions templates/feed.xml
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>{{ handler.settings['forum_title'] }}</title>
<link href="{{ handler.settings['forum_url'] }}feed" rel="self" />
<link href="{{ handler.settings['forum_url'] }}" />
<updated>{{ topics and topics[0]['modified'] }}</updated>
<id>{{ handler.settings['forum_url'] }}</id>
{% for topic in topics[:15] %}
<entry>
<title><![CDATA[{{ topic['title'] }}]]></title>
<link href="{{ handler.settings['forum_url'] }}topic/{{ topic['_id'] }}"/>
<id>{{ handler.settings['forum_url'] }}topic/{{ topic['_id'] }}</id>
<content type="html">
<![CDATA[
{{ topic['content_html'] }}
]]>
</content>
</entry>
{% end %}
</feed>

0 comments on commit 3c57d66

Please sign in to comment.