1679677: Tornado, Class UserRole and Role Wrapper

Added 5 years ago by Alejandro M. Bernardis

Love

4 Add your love!

Tags

Discussion

Example:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2012 The Octopus Apps Inc.
# Licensed under the Apache License, Version 2.0 (the "License")
#
# Author: Alejandro M. Bernardis
# Email: alejandro.m.bernardis at gmail.com
# Created: Feb 6, 2012, 2:44:12 PM
#
import os
import sys

#: -- fix ----------------------------------------------------------------------

p_root = os.path.abspath("../")
p_lib = p_root+"/libs"
p_src = p_root+"/src"

sys.path = sys.path + [p_lib, p_src]

# -- imports -------------------------------------------------------------------

import tornado.ioloop
import tornado.options
import tornado.web

from tornado.options import define, options

# -- meta-data -----------------------------------------------------------------

__project_name__ = u"XXXX"
__project_full_name__ = u"XXXX, XXXX"
__project_owner__ = u"XXXX"
__project_author__ = u"XXXX X. XXXX"
__project_version__ = u"0.0.1"
__project_created__ = u"Dec 00, 0000, 00:00:00 PM"

# -- config --------------------------------------------------------------------

define("debug", default=True)
define("port", default=8081, type=int)
define("cookie_user_session", default="user_session")

base_dir = os.path.dirname(__file__)

#: -- MainHandler --------------------------------------------------------------

class MainHandler(AuthBaseHandler):

    #: -------------------------------------------------------------------------
    #: @roles (wrapper)
    #: -------------------------------------------------------------------------
    @roles('guest', 'moderator')
    def get(self):
        self.finish(u"Main, GET!")

    #: -------------------------------------------------------------------------
    #: @authenticated_plus (wrapper)
    #: -------------------------------------------------------------------------
    @authenticated_plus('admin', 'moderator')   
    def post(self):
        self.finish(u"Main, POST!")

#: -- application --------------------------------------------------------------

class Application(tornado.web.Application):

    def __init__(self):

        #: ---------------------------------------------------------------------
        #: ROLES
        #: ---------------------------------------------------------------------
        UserRole('admin', admin=True)
        UserRole('guest')
        UserRole('moderator', write=True)
        #: ---------------------------------------------------------------------

        static_dir = os.path.join(base_dir, "static")
        static_dir_dict = dict(path=static_dir)

        handlers = [
            # handlers
            (r"/", MainHandler),

            # static files
            (r"/(crossdomain\.xml)", tornado.web.StaticFileHandler, static_dir_dict),
            (r"/(humans\.txt)", tornado.web.StaticFileHandler, static_dir_dict),
            (r"/(robots\.txt)", tornado.web.StaticFileHandler, static_dir_dict),
            (r"/(xd_receiver\.html)", tornado.web.StaticFileHandler, static_dir_dict),
        ]

        settings = dict(
            debug=options.debug,
            path=base_dir,
            static_path=static_dir,
            upload_path=os.path.join(base_dir, "upload"),
            template_path=os.path.join(base_dir, "templates"),
            backup_path=os.path.join(base_dir, "backup"),
            cookie_secret="43oETzKXQAGaYdkL5gEmGeJJFuYh7EQnp2XdTP1o/Vo=",
            xsrf_cookies=True,
            login_url="/auth/login",
            cookie_user_session=options.cookie_user_session,
        )

        tornado.web.Application.__init__(self, handlers, **settings)

# -- main ----------------------------------------------------------------------

def main():
    try:
        tornado.options.parse_command_line()
        app = Application()
        app.listen(options.port)
        io = tornado.ioloop.IOLoop.instance()
        io.start()

    except KeyboardInterrupt:
        print "::: Server Stop :::", __project_full_name__

if __name__ == "__main__":
    main()

File: users.py