Esempio n. 1
0
    def post(self):
        user_model = UserModel()
        user = user_model.exists(
            username=self.get_argument('username'),
            password=self.get_argument('password'),
            is_admin=True
        )

        if not user:
            self.render('admin/login.html', error="Unknow user or wrong password")
        else:
            self.login(user)
            self.redirect('/admin/')
Esempio n. 2
0
    def post(self):
        model = UserModel()
        email = self.get_argument("email")
        password = self.get_argument("password")
        message = "Unknow user (bad email or wrong password). You should retry."
        message_level = 1

        if email and password:
            user = model.validate_user(email, password)
            if user:
                self.login(user)
                message = "You are now connected."
                message_level = 0

        self.add_flash_message(message_level, message)
        self.redirect("/")
Esempio n. 3
0
    def get_template_namespace(self):
        """
        Make some variables global for all templates
        """
        ns = super(RequestHandler, self).get_template_namespace()
        # pref = PreferenceModel().get_codes()
        cookie = self.get_secure_cookie("messages")
        user = self.get_current_user() or False
        if user:
            self.current_user = UserModel().find_by_username(user)

        config = Config()

        ns.update({
            'question': Question(),
            'analytics': config.get("google_analytics_id"),
            'msvalidate': config.get('msvalidate'),
            'messages': pickle.loads(cookie) if cookie else None,
            'connected': bool(self.current_user)
        })

        if self.current_user:
            ns.update({
                'username': self.current_user['username'],
                'email': self.current_user.get('email', '')
            })

        # Remove messages
        self.clear_cookie("messages")
        return ns
Esempio n. 4
0
 def get(self, hash):
     """
     Confirm the inscription by mail
     """
     model = UserModel()
     user = model.find_by_hash(hash)
     if user:
         model.set_user_registered(user)
         self.render(
             "registration_confirm.html",
             error=False
         )
     else:
         self.render(
             "registration_confirm.html",
             error=True
         )
Esempio n. 5
0
    def post(self, name):
        """
        Save the user modification
        """
        add_another = 'add_another' in self.request.arguments
        continue_edit = 'continue_edit' in self.request.arguments
        email = self.get_argument('email')
        creation_date = self.get_argument('creation_date')
        is_admin = self.get_argument('is_admin') == 'on'

        user_model = UserModel()
        user_model.edit(name, creation_date, email, is_admin)
        if add_another:
            self.redirect('/admin/users/add/')
        elif continue_edit:
            self.render('admin/users/edit.html', user=user_model.get_by_name(name))
        else:
            self.redirect('/admin/users/')
Esempio n. 6
0
class RequestHandler(tornado.web.RequestHandler):
    """
    Extend the tornado requesthandler with few useful
    methods, mainly for authentication
    """

    def __init__(self, *args, **kwargs):
        super(RequestHandler, self).__init__(*args, **kwargs)
        self.current_user = self.get_user_profile()

    def get_current_user(self):
        """
        Return the current user. Used for tornado for authentication
        """
        return self.get_secure_cookie('user')

    def add_flash_message(self, level, message):
        """
        Pass a message
        """
        cookie = self.get_secure_cookie("messages")
        messages = None

        if cookie:
            messages = pickle.loads(cookie)

        if not messages:
            messages = []

        messages.append({'level': level, 'message': message})
        self.set_secure_cookie("messages", pickle.dumps(messages))

    def get_template_namespace(self):
        """
        Make some variables global for all templates
        """
        ns = super(RequestHandler, self).get_template_namespace()
        # pref = PreferenceModel().get_codes()
        cookie = self.get_secure_cookie("messages")
        user = self.get_current_user() or False
        if user:
            self.current_user = UserModel().find_by_username(user)

        config = Config()

        ns.update({
            'question': Question(),
            'analytics': config.get("google_analytics_id"),
            'msvalidate': config.get('msvalidate'),
            'messages': pickle.loads(cookie) if cookie else None,
            'connected': bool(self.current_user)
        })

        if self.current_user:
            ns.update({
                'username': self.current_user['username'],
                'email': self.current_user.get('email', '')
            })

        # Remove messages
        self.clear_cookie("messages")
        return ns

    def get_user_profile(self):
        """
        Get the user if she/he's connected
        :return: The user db entry
        """
        user = self.get_current_user() or False
        return UserModel().find_by_username(user) if user else None

    def login(self, user):
        """
        Log the user. Convinient function
        :param user: the user db entry
        :return: None
        """
        self.set_secure_cookie('user', user['username'])
        self.current_user = user

    def logout(self):
        self.clear_cookie('user')
        self.current_user = None