class TestJulyApp(object): def setUp(self): self.app = JulyApp('test', __name__) def test_first_register(self): assert self.app.first_register() == True assert self.app.first_register() == False def test_add_handler(self): self.app.add_handler(('/', 'TestJulyApp')) assert self.app.handlers == [('/', 'TestJulyApp')]
@asynchronous def post(self): content = self.get_argument('content', None) user_id = self.get_argument('user', None) if not (content and user_id): self.finish('deny') return network = Social.query.get_first(service='twitter', user_id=user_id) if not network: self.finish('deny') return token = escape.json_decode(network.token) status = escape.utf8(content) self.twitter_request('/statuses/update', post_args={'status': status}, access_token=token, callback=self._on_post) def _on_post(self, entry): if not entry: self.finish('fail') return self.finish('ok') handlers = [ ('/twitter', TwitterHandler), ] app = JulyApp('social', __name__, handlers=handlers)
class PostHandler(JulyHandler): def get(self, id): post = Post.query.filter_by(id=id).first() if not post: self.send_error(404) return self.render('post.html', post=post) handlers = [ ('/create', CreateHandler), url('/(\d+)', PostHandler, name='post'), ] post_app = JulyApp('post', __name__, handlers=handlers, template_folder="templates") if __name__ == '__main__': from tornado.options import options options.sqlalchemy_master = 'sqlite:////tmp/demo.sqlite' from july import run_server, JulyApplication run_server( JulyApplication(handlers=handlers, debug=True, template_path="templates"))
class MailHandler(JulyHandler): def check_xsrf_cookie(self): #: disable xsrf cookie check return def post(self): #: provide as a service #: only the server has access to this service if self.request.remote_ip != '127.0.0.1': self.send_error(403) return user = self.get_argument('user', None) subject = self.get_argument('subject', None) body = self.get_argument('body', '') subtype = self.get_argument('subtype', 'plain') if not (user and subject): self.send_error(403) logging.warn('Send mail without user or subject') return try: send_mail(user, subject, body, subtype=subtype) except Exception as e: logging.error(e) self.write('') app = JulyApp('mail', __name__, handlers=[url('/outbox', MailHandler, name='mail-outbox')])
def setUp(self): self.app = JulyApp('test', __name__)
def test_register_app2(self): #: app without handlers app = JulyApp('app2', 'app2') assert '__july_apps__' not in self.application.settings self.application.register_app(app, '/app')
@require_user def post(self): title = self.get_argument('title', None) content = self.get_argument('content', None) if not (title and content): self.flash_message('Please fill in required fields', 'error') self.render("create_feedback.html") return key = hashlib.md5(utf8(content)).hexdigest() url = cache.get(key) if url: self.redirect(url) return feedback = Feedback(title=title, content=content) feedback.sender = self.current_user.id db.session.add(feedback) db.session.commit() self.flash_message('Thank you for your feedback', 'info') self.redirect('/') handlers = [ ('/create', CreateFeedbackHandler), ] app = JulyApp('feedback', __name__, handlers=handlers)
def test_register_app1(self): app = JulyApp('app1', __name__) app.add_handler(('', 'joking')) self.application.register_app(app, '/app')
sidebar = Storage.get('sidebar') return sidebar or '' class SiteInfoModule(UIModule): def render(self): _ = self.handler.locale.translate member = Member.query.count() topic = Topic.query.count() node = Node.query.count() reply = Reply.query.count() html = '<ul>' html += '<li>%s: %s</li>' % (_('Member'), member) html += '<li>%s: %s</li>' % (_('Node'), node) html += '<li>%s: %s</li>' % (_('Topic'), topic) html += '<li>%s: %s</li>' % (_('Reply'), reply) html += '</ul>' return html modules = { 'Sidebar': SidebarMoudle, 'SiteInfo': SiteInfoModule, } app = JulyApp('dashboard', __name__, handlers=handlers, ui_modules=modules, template_folder='templates')
following = True else: following = False return self.render_string('module/node.html', node=node, following=following) class FollowingNodesModule(UIModule): def render(self, user_id): fs = FollowNode.query.filter_by(user_id=user_id).values('node_id') node_ids = (f[0] for f in fs) nodes = Node.query.filter_by(id__in=node_ids).all() return self.render_string('module/node_list.html', nodes=nodes) class RecentAddNodesModule(UIModule): def render(self): nodes = Node.query.order_by('-id')[:20] return self.render_string('module/node_list.html', nodes=nodes) app_modules = { 'Node': NodeModule, 'FollowingNodes': FollowingNodesModule, 'RecentAddNodes': RecentAddNodesModule, } app = JulyApp('node', __name__, handlers=app_handlers, ui_modules=app_modules)
('/password', PasswordHandler), ('/message', MessageHandler), ] class RecentMembersModule(UIModule): def render(self): users = Member.query.order_by('-id').limit(12) return self.render_string('module/member_cell.html', users=users) modules = { 'RecentMembers': RecentMembersModule, } app = JulyApp('account', __name__, handlers=handlers, ui_modules=modules) class MembersHandler(UserHandler): def head(self): pass def get(self): p = self.get_argument('p', 1) pagination = Member.query.order_by('-reputation').paginate(p, 80) self.render('member_list.html', pagination=pagination) class CityMembersHandler(UserHandler): def head(self): pass
backend = import_object(options.image_backend) backend.save(body, filename, self._on_post) def _on_post(self, result): if result: self.write('{"stat":"ok", "url":"%s"}' % result) else: self.write('{"stat":"fail", "msg": "server error"}') self.finish() handlers = [ ('/', LatestHandler), ('/following', FollowingHandler), ('/popular', PopularHandler), ('/preview', PreviewHandler), ('/feed', SiteFeedHandler), ('/search', SearchHandler), ('/upload', UploadHandler), ('/node/(\w+)', NodeHandler), ('/node/(\w+)/feed', NodeFeedHandler), ('/member/(\w+)', MemberHandler), ('/~(\w+)', RedirectMemberHandler), ('/doc/(\w+)', DocHandler), ] app = JulyApp('front', __name__, handlers=handlers, template_folder='_templates')
('/(\d+)/reply', CreateReplyHandler), ('/(\d+)/vote', VoteTopicHandler), ] class UserModule(UIModule): """UserModule This module contains topic and reply count, in this case, it is located in topic app """ def render(self, user_id): user = Member.query.get_first(id=user_id) if not user: return '' topic_count = Topic.query.filter_by(user_id=user_id).count() reply_count = Reply.query.filter_by(user_id=user_id).count() user.topic_count = topic_count user.reply_count = reply_count return self.render_string('module/user.html', user=user) app_modules = { 'User': UserModule, } app = JulyApp( 'topic', __name__, handlers=app_handlers, ui_modules=app_modules )