Esempio n. 1
0
    def POST(self, key):
        
        if not key in ADMIN_COMPONENTS:
            raise web.notfound()

        http_input = web.input()
        
        # Scenario 1 : edit an existing element
        if http_input.event == Events.MODIFIED:
            
            # The grid should be bound to the form data
            component_to_sync = grid =  ADMIN_COMPONENTS.get(key).grid_component()
            grid.rebind(data=http_input)
            fieldset = ADMIN_COMPONENTS.get(key).fieldset_component()
        
        # Scenario 2 : create a new element
        elif http_input.event == Events.NEW:
            
            # The fieldset should be bound to the form data & the session
            grid =  ADMIN_COMPONENTS.get(key).grid_component()
            component_to_sync = fieldset = ADMIN_COMPONENTS.get(key).fieldset_component().bind(data=http_input, session=config.orm)
        
        # Scenario 3 : invalid action
        else:
            raise web.notfound()
        
        # Synchronizes the grid or the fieldset (depending on the action) & registers an email notification
        if component_to_sync.validate():
            component_to_sync.sync()
            http.register_hook(lambda: notify_via_email(component_to_sync.model, http_input.event))
            raise web.seeother("/")
        else:
            return config.views.layout(config.views.administration(grid, fieldset))
Esempio n. 2
0
 def POST(self):
     
     # Reads the email in the HTTP request parameters
     email = web.input(email=None).email
     
     # Check if the user exists and is active
     user = User.get_user(email)
           
     if user is None or not user.active:
         raise http.Forbidden("Utilisateur inconnu")
     
     # Checks if there is already an active password token matching this email
     current_password_token = PasswordToken.get_password_token(email)
     
     if current_password_token is not None:
         formatted_creation_dt = formatting.format_date(dates.change_timezone(current_password_token.creation_dt), "%d/%m/%y %H:%M")
         raise http.Forbidden(u"Demande similaire déjà effectuée le %s" % formatted_creation_dt)
     
     # Creates a new password token valid for 2 days
     password_token = PasswordToken(validity=2, user=user, token=PasswordToken.generate_random_token(16))
     config.orm.add(password_token)
     
     # Registers an email notification
     http.register_hook(lambda: notify_via_email(password_token, Events.NEW))
     
     return u"Instructions en cours d'envoi à %s" % email
Esempio n. 3
0
    def POST(self):

        # Reads the email in the HTTP request parameters
        email = web.input(email=None).email

        # Check if the user exists and is active
        user = User.get_user(email)

        if user is None or not user.active:
            raise http.Forbidden("Utilisateur inconnu")

        # Checks if there is already an active password token matching this email
        current_password_token = PasswordToken.get_password_token(email)

        if current_password_token is not None:
            formatted_creation_dt = formatting.format_date(
                dates.change_timezone(current_password_token.creation_dt),
                "%d/%m/%y %H:%M")
            raise http.Forbidden(u"Demande similaire déjà effectuée le %s" %
                                 formatted_creation_dt)

        # Creates a new password token valid for 2 days
        password_token = PasswordToken(
            validity=2,
            user=user,
            token=PasswordToken.generate_random_token(16))
        config.orm.add(password_token)

        # Registers an email notification
        http.register_hook(
            lambda: notify_via_email(password_token, Events.NEW))

        return u"Instructions en cours d'envoi à %s" % email
Esempio n. 4
0
    def POST(self):

        # Reads the HTTP request parameters
        http_input = web.input(poll_id=None, poll_user_choices=[])
        poll_id = http_input.poll_id
        poll_user_choices = http_input.poll_user_choices

        # Loads the poll
        if poll_id is None:
            raise web.notfound()

        poll = Poll.get(int(poll_id),
                        joined_attrs=["choices", "votes_by_user"])

        if poll is None:
            raise web.notfound()

        # Passes the user's choices to the model
        try:

            # Parses the choice numbers & makes sure they're valid
            poll_user_choices = map(int, poll_user_choices)
            if any(i not in range(len(poll.choices))
                   for i in poll_user_choices):
                raise ValueError(
                    u"Un des entiers passes a la methode /poll/vote n'est pas compris dans l'intervalle %s"
                    % range(len(poll.choices)))

            # Determines if it's the first vote ever in the poll
            someone_already_voted = poll.has_votes

            # Determines if it's the first vote for the user
            user_already_voted = config.session_manager.user in poll.choices_by_user

            # Actual vote action for the user
            poll_vote = poll.vote(config.session_manager.user,
                                  [poll.choices[i] for i in poll_user_choices])

            # Registers an email notification
            http.register_hook(lambda: notify_via_email(
                poll_vote, Events.MODIFIED
                if user_already_voted else Events.NEW))

            return dict(data=config.views.poll_votes(
                poll,
                highlight_user=config.session_manager.user
                if someone_already_voted else None),
                        partial=someone_already_voted)

        except ValueError as exception:
            raise http.Forbidden(exception)
Esempio n. 5
0
    def POST(self):
        
        # Reads the HTTP request parameters
        tournament_id = web.input().tournament_id
        comment = web.input().comment
        
        # Appends the comment
        # TODO: variables are ambiguous
        tournament = Tournament.get(int(tournament_id), joined_attrs=["comments"])
        added_comment = tournament.add_comment(config.session_manager.user, comment)
        
        # Registers an email notification
        http.register_hook(lambda: notify_via_email(added_comment, Events.NEW))

        # Returns the dictionary
        return config.views.comment(added_comment)
Esempio n. 6
0
    def POST(self):
        
        # Reads the HTTP request parameters
        http_input = web.input(poll_id=None, poll_user_choices=[])
        poll_id = http_input.poll_id
        poll_user_choices = http_input.poll_user_choices

        # Loads the poll
        if poll_id is None:
            raise web.notfound() 

        poll = Poll.get(int(poll_id), joined_attrs=["choices", "votes_by_user"])

        if poll is None:
            raise web.notfound()
        
        # Passes the user's choices to the model
        try:
            
            # Parses the choice numbers & makes sure they're valid
            poll_user_choices = map(int, poll_user_choices)
            if any(i not in range(len(poll.choices)) for i in poll_user_choices):
                raise ValueError(u"Un des entiers passes a la methode /poll/vote n'est pas compris dans l'intervalle %s" % range(len(poll.choices)))
            
            # Determines if it's the first vote ever in the poll
            someone_already_voted = poll.has_votes
            
            # Determines if it's the first vote for the user
            user_already_voted = config.session_manager.user in poll.choices_by_user
            
            # Actual vote action for the user
            poll_vote = poll.vote(config.session_manager.user, [poll.choices[i] for i in poll_user_choices])

            # Registers an email notification
            http.register_hook(lambda: notify_via_email(poll_vote, Events.MODIFIED if user_already_voted else Events.NEW))

            return dict(
                data=config.views.poll_votes(poll, highlight_user=config.session_manager.user if someone_already_voted else None),
                partial=someone_already_voted
            )
            
        except ValueError as exception:
            raise http.Forbidden(exception)
Esempio n. 7
0
    def POST(self):
        
        # Reads the token in the HTTP request parameters
        token = web.input(token=None).token
        
        # Checks if the token is valid
        password_token = PasswordToken.get_token(token)
        
        if password_token is None or password_token.expired:
            raise http.Forbidden()
        
        # The fieldset is bound to the form data & the user associated with the token : the token itself is passed because it should expire when successfully used
        password_fieldset = user_forms.NewPasswordFieldSet(password_token).bind(password_token.user, data=web.input())

        # Synchronizes the fieldset & registers a delayed login of the user (we could do it now but it's better to isolate the login process)
        if password_fieldset.validate():
            password_fieldset.sync()
            http.register_hook(lambda: session.login_workflow(password_fieldset.model))
            raise web.seeother("/")
        else:
            return config.views.layout(config.views.creation_form(password_fieldset))
Esempio n. 8
0
 def POST(self):
     
     # Reads the token in the HTTP request parameters
     token = web.input(token=None).token
     
     # Checks if the token is valid
     user_token = UserToken.get_token(token)
     
     if user_token is None or user_token.expired:
         raise http.Forbidden()
     
     # The fieldset is bound to the form data & the session : the token is passed because it contains the level
     user_fieldset = user_forms.NewUserFieldSet(user_token).bind(data=web.input(), session=config.orm)
     
     # Synchronizes the fieldset & registers a delayed login of the user (because the user id is not available yet)
     if user_fieldset.validate():
         user_fieldset.sync()
         http.register_hook(lambda: session.login_workflow(user_fieldset.model))
         raise web.seeother("/")
     else:
         return config.views.layout(config.views.creation_form(user_fieldset))
Esempio n. 9
0
    def POST(self, key):

        if not key in ADMIN_COMPONENTS:
            raise web.notfound()

        http_input = web.input()

        # Scenario 1 : edit an existing element
        if http_input.event == Events.MODIFIED:

            # The grid should be bound to the form data
            component_to_sync = grid = ADMIN_COMPONENTS.get(
                key).grid_component()
            grid.rebind(data=http_input)
            fieldset = ADMIN_COMPONENTS.get(key).fieldset_component()

        # Scenario 2 : create a new element
        elif http_input.event == Events.NEW:

            # The fieldset should be bound to the form data & the session
            grid = ADMIN_COMPONENTS.get(key).grid_component()
            component_to_sync = fieldset = ADMIN_COMPONENTS.get(
                key).fieldset_component().bind(data=http_input,
                                               session=config.orm)

        # Scenario 3 : invalid action
        else:
            raise web.notfound()

        # Synchronizes the grid or the fieldset (depending on the action) & registers an email notification
        if component_to_sync.validate():
            component_to_sync.sync()
            http.register_hook(lambda: notify_via_email(
                component_to_sync.model, http_input.event))
            raise web.seeother("/")
        else:
            return config.views.layout(
                config.views.administration(grid, fieldset))
Esempio n. 10
0
    def POST(self):
        
        # Reads the HTTP request parameters
        http_input = web.input()
        poll_id = http_input.poll_id
        comment = http_input.comment

        # Loads the poll
        if poll_id is None:
            raise web.notfound() 

        poll = Poll.get(int(poll_id), joined_attrs=["comments"])

        if poll is None:
            raise web.notfound()
        
        # Appends the comment
        poll_comment = poll.add_comment(config.session_manager.user, comment)
        
        # Registers an email notification
        http.register_hook(lambda: notify_via_email(poll_comment, Events.NEW))

        # Returns the dictionary
        return config.views.comment(poll_comment)
Esempio n. 11
0
    def POST(self):

        # Reads the token in the HTTP request parameters
        token = web.input(token=None).token

        # Checks if the token is valid
        user_token = UserToken.get_token(token)

        if user_token is None or user_token.expired:
            raise http.Forbidden()

        # The fieldset is bound to the form data & the session : the token is passed because it contains the level
        user_fieldset = user_forms.NewUserFieldSet(user_token).bind(
            data=web.input(), session=config.orm)

        # Synchronizes the fieldset & registers a delayed login of the user (because the user id is not available yet)
        if user_fieldset.validate():
            user_fieldset.sync()
            http.register_hook(
                lambda: session.login_workflow(user_fieldset.model))
            raise web.seeother("/")
        else:
            return config.views.layout(
                config.views.creation_form(user_fieldset))
Esempio n. 12
0
    def POST(self):

        # Reads the token in the HTTP request parameters
        token = web.input(token=None).token

        # Checks if the token is valid
        password_token = PasswordToken.get_token(token)

        if password_token is None or password_token.expired:
            raise http.Forbidden()

        # The fieldset is bound to the form data & the user associated with the token : the token itself is passed because it should expire when successfully used
        password_fieldset = user_forms.NewPasswordFieldSet(
            password_token).bind(password_token.user, data=web.input())

        # Synchronizes the fieldset & registers a delayed login of the user (we could do it now but it's better to isolate the login process)
        if password_fieldset.validate():
            password_fieldset.sync()
            http.register_hook(
                lambda: session.login_workflow(password_fieldset.model))
            raise web.seeother("/")
        else:
            return config.views.layout(
                config.views.creation_form(password_fieldset))
Esempio n. 13
0
    def POST(self):

        # Reads the HTTP request parameters
        http_input = web.input()
        poll_id = http_input.poll_id
        comment = http_input.comment

        # Loads the poll
        if poll_id is None:
            raise web.notfound()

        poll = Poll.get(int(poll_id), joined_attrs=["comments"])

        if poll is None:
            raise web.notfound()

        # Appends the comment
        poll_comment = poll.add_comment(config.session_manager.user, comment)

        # Registers an email notification
        http.register_hook(lambda: notify_via_email(poll_comment, Events.NEW))

        # Returns the dictionary
        return config.views.comment(poll_comment)