Permalink
Browse files

A new BaseHandler.

  • Loading branch information...
1 parent bd08929 commit 8f13eb98c5f8182119ec9bd1d6056eebcb0aa7ff @livid committed Jan 25, 2011
Showing with 377 additions and 45 deletions.
  1. +4 −1 app.yaml.example
  2. +3 −1 config.py.example
  3. +45 −6 feed.py
  4. +64 −2 images.py
  5. +12 −0 index.yaml
  6. +98 −13 main.py
  7. +2 −0 member.py
  8. +10 −19 topic.py
  9. +7 −0 v2ex/babel/__init__.py
  10. +132 −1 v2ex/babel/handlers/__init__.py
  11. +0 −2 v2ex/babel/l10n/__init__.py
View
@@ -68,7 +68,7 @@ handlers:
- url: /feed/v2ex.rss
script: feed.py
-- url: /feed/.*
+- url: /feed/(.+).xml
script: feed.py
- url: /about
@@ -111,6 +111,9 @@ handlers:
- url: /images(/.*)?
script: images.py
+- url: /d/([0-9]+)?
+ script: images.py
+
- url: /place/([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})
script: place.py
View
@@ -18,4 +18,6 @@ fts_password = ''
# change this for deploy is you can registration from http://www.google.com/recaptcha
recaptcha_public_key = 'your recaptcha_public_key'
-recaptcha_private_key = 'your recaptcha_private_key'
+recaptcha_private_key = 'your recaptcha_private_key'
+
+daydream_secret = ''
View
51 feed.py
@@ -19,35 +19,74 @@
from v2ex.babel.da import *
+from v2ex.babel.handlers import BaseHandler
+
template.register_template_library('v2ex.templatetags.filters')
-class FeedHomeHandler(webapp.RequestHandler):
+class FeedHomeHandler(BaseHandler):
def head(self):
self.response.out.write('')
def get(self):
- site = GetSite()
output = memcache.get('feed_index')
if output is None:
+ self.values['site_domain'] = self.site.domain
+ self.values['site_name'] = self.site.title
+ self.values['site_slogan'] = self.site.slogan
+ self.values['feed_url'] = 'http://' + self.values['site_domain'] + '/index.xml'
+ self.values['site_updated'] = datetime.datetime.now()
+ q = db.GqlQuery("SELECT * FROM Topic ORDER BY created DESC LIMIT 10")
+ topics = []
+ IGNORED = ['pointless', '528491']
+ for topic in q:
+ if topic.node.name not in IGNORED:
+ topics.append(topic)
+ self.values['topics'] = topics
+ self.values['feed_title'] = self.site.title
+ path = os.path.join(os.path.dirname(__file__), 'tpl', 'feed', 'index.xml')
+ output = template.render(path, self.values)
+ memcache.set('feed_index', output, 600)
+ self.response.headers['Content-type'] = 'application/xml;charset=UTF-8'
+ self.response.out.write(output)
+
+class FeedNodeHandler(webapp.RequestHandler):
+ def head(self):
+ self.response.out.write('')
+
+ def get(self, node_name):
+ node_name = node_name.lower()
+ site = GetSite()
+ node = GetKindByName('Node', node_name)
+ if node is False:
+ return self.response.out.write('node not found')
+ output = memcache.get('feed_node_' + node_name)
+ if output is None:
template_values = {}
template_values['site'] = site
template_values['site_domain'] = site.domain
template_values['site_name'] = site.title
template_values['site_slogan'] = site.slogan
template_values['feed_url'] = 'http://' + template_values['site_domain'] + '/index.xml'
template_values['site_updated'] = datetime.datetime.now()
- q = db.GqlQuery("SELECT * FROM Topic ORDER BY created DESC LIMIT 10")
- template_values['topics'] = q
+ q = db.GqlQuery("SELECT * FROM Topic WHERE node = :1 ORDER BY created DESC LIMIT 10", node)
+ topics = []
+ IGNORED = ['pointless', 'flamewar', 'in', 'newbie']
+ for topic in q:
+ if topic.node.name not in IGNORED:
+ topics.append(topic)
+ template_values['topics'] = topics
+ template_values['feed_title'] = site.title + u'' + node.title
path = os.path.join(os.path.dirname(__file__), 'tpl', 'feed', 'index.xml')
output = template.render(path, template_values)
- memcache.set('feed_index', output, 600)
+ memcache.set('feed_node_' + node.name, output, 600)
self.response.headers['Content-type'] = 'application/xml;charset=UTF-8'
self.response.out.write(output)
def main():
application = webapp.WSGIApplication([
('/index.xml', FeedHomeHandler),
- ('/feed/v2ex.rss', FeedHomeHandler)
+ ('/feed/v2ex.rss', FeedHomeHandler),
+ ('/feed/([0-9a-zA-Z\-\_]+).xml', FeedNodeHandler)
],
debug=True)
util.run_wsgi_app(application)
View
@@ -8,9 +8,12 @@
import hashlib
import string
import random
+import urllib
+import urllib2
from google.appengine.ext import webapp
from google.appengine.api import memcache
+from google.appengine.api import urlfetch
from google.appengine.ext import db
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template
@@ -28,25 +31,84 @@
from v2ex.babel.security import *
from v2ex.babel.ua import *
from v2ex.babel.da import *
+from v2ex.babel.l10n import *
from v2ex.babel.ext.cookies import Cookies
+from v2ex.babel.ext.sessions import Session
+
+from django.utils import simplejson as json
template.register_template_library('v2ex.templatetags.filters')
class ImagesHomeHandler(webapp.RequestHandler):
def get(self):
site = GetSite()
browser = detect(self.request)
+ member = CheckAuth(self)
+ l10n = GetMessages(self, member, site)
+ self.session = Session()
+ if member:
+ source = 'http://daydream/stream/' + str(member.num)
+ result = urlfetch.fetch(source)
+ images = json.loads(result.content)
+ template_values = {}
+ template_values['images'] = images
+ template_values['site'] = site
+ template_values['member'] = member
+ template_values['page_title'] = site.title + u' › 图片上传'
+ template_values['l10n'] = l10n
+ template_values['system_version'] = SYSTEM_VERSION
+ if 'message' in self.session:
+ template_values['message'] = self.session['message']
+ del self.session['message']
+ path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'images_home.html')
+ output = template.render(path, template_values)
+ self.response.out.write(output)
+ else:
+ self.redirect('/signin')
+
+class ImagesUploadHandler(webapp.RequestHandler):
+ def post(self):
+ site = GetSite()
+ browser = detect(self.request)
+ member = CheckAuth(self)
+ l10n = GetMessages(self, member, site)
+ if member:
+ image = self.request.get('image')
+ if image is not None:
+ import urllib, urllib2
+ parameters = urllib.urlencode(dict(member_id=member.num, image=image))
+ try:
+ f = urllib2.urlopen('http://daydream/upload', parameters)
+ data = f.read()
+ f.close()
+ except:
+ self.session = Session()
+ self.session['message'] = '图片不能超过 1M'
+ self.redirect('/images')
+ else:
+ self.redirect('/signin')
+
+class ImagesUploadRulesHandler(webapp.RequestHandler):
+ def get(self):
+ site = GetSite()
+ browser = detect(self.request)
+ member = CheckAuth(self)
+ l10n = GetMessages(self, member, site)
template_values = {}
template_values['site'] = site
- template_values['page_title'] = site.title + u' › 图片上传'
+ template_values['member'] = member
+ template_values['page_title'] = site.title + u' › 图片上传规则'
+ template_values['l10n'] = l10n
template_values['system_version'] = SYSTEM_VERSION
- path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'images_home.html')
+ path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'images_rules.html')
output = template.render(path, template_values)
self.response.out.write(output)
def main():
application = webapp.WSGIApplication([
+ ('/images/upload', ImagesUploadHandler),
+ ('/images/upload/rules', ImagesUploadRulesHandler),
('/images/?', ImagesHomeHandler)
],
debug=True)
View
@@ -145,6 +145,18 @@ indexes:
- kind: Topic
properties:
+ - name: node
+ - name: created
+ direction: desc
+
+- kind: Topic
+ properties:
+ - name: node_name
+ - name: created
+ direction: desc
+
+- kind: Topic
+ properties:
- name: node_num
- name: last_touched
direction: desc
Oops, something went wrong.

0 comments on commit 8f13eb9

Please sign in to comment.