view browser_side/register.py @ 60:d9f4a3256da8

added README
author Goffi <goffi@goffi.org>
date Mon, 30 May 2011 16:13:07 +0200
parents c3ee630914ba
children 12e889a683ce
line wrap: on
line source

#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
Libervia: a Salut à Toi frontend
Copyright (C) 2011  Jérôme Poisson (goffi@goffi.org)

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""

#This page manage subscription and new account creation
import pyjd # this is dummy in pyjs

from pyjamas.ui.VerticalPanel import VerticalPanel
from pyjamas.ui.Grid import Grid
from pyjamas.ui.PasswordTextBox import PasswordTextBox
from pyjamas.ui.TextBox import TextBox
from pyjamas.ui.FormPanel import FormPanel
from pyjamas.ui.Button import Button
from pyjamas.ui.Label import Label
from pyjamas.ui.CheckBox import CheckBox
from pyjamas.ui.DialogBox import DialogBox
from pyjamas import Window
from pyjamas.ui import HasAlignment
import re




class RegisterPanel(FormPanel):

    def __init__(self, callback):
        """
        @param callback: method to call if login successful
        """
        FormPanel.__init__(self)
        self.callback = callback
        self.setMethod(FormPanel.METHOD_POST)
        vPanel = VerticalPanel()
        vPanel.setHorizontalAlignment(HasAlignment.ALIGN_CENTER)
        self.warning_msg = Label()
        self.warning_msg.setVisible(False)
        self.warning_msg.setStyleName('formWarning')
        vPanel.add(self.warning_msg)

        self.login_box = TextBox()
        self.login_box.setName("login")
        self.pass_box = PasswordTextBox()
        self.pass_box.setName("password")
        login_grid = Grid(2, 2)
        login_grid.setText(0,0,"Login:")
        login_grid.setWidget(0,1, self.login_box)
        login_grid.setText(1,0, "Password:")
        login_grid.setWidget(1,1, self.pass_box)
        vPanel.add(login_grid)
        
        self.new_account = CheckBox('Register new account')
        self.new_account.setName('new_account')
        self.new_account.addClickListener(self.onNewAccountChecked)
        vPanel.add(self.new_account)
        
        
        self.email_box = TextBox()
        self.email_box.setName("email")
        self.register_grid = Grid(1, 2)
        self.register_grid.setText(0,0,"email:")
        self.register_grid.setWidget(0,1, self.email_box)
        self.register_grid.setVisible(False)
        vPanel.add(self.register_grid)
        self.info_register_lb = Label('Your password will be sent to the given email')
        self.info_register_lb.setVisible(False)
        vPanel.add(self.register_grid)
        vPanel.add(self.info_register_lb)
        
        self.login_but = Button("Login", getattr(self, "onLogin"))
        vPanel.add(self.login_but)
        self.register_but = Button("Register", getattr(self, "onRegister"))
        self.register_but.setVisible(False)
        vPanel.add(self.register_but)
        self.add(vPanel)
        self.addFormHandler(self)
        self.setAction('register_api/login')

    def changeMode(self, mode):
        """Change the configuration of the dialog
        @param mode: "login" or "register"""
        if mode == "register":
            self.pass_box.setEnabled(False)
            self.register_grid.setVisible(True)
            self.info_register_lb.setVisible(True)
            self.login_but.setVisible(False)
            self.register_but.setVisible(True)
            self.new_account.setChecked(True)
        else:
            self.pass_box.setEnabled(True)
            self.register_grid.setVisible(False)
            self.info_register_lb.setVisible(False)
            self.login_but.setVisible(True)
            self.register_but.setVisible(False)
            self.new_account.setChecked(False)


    def onNewAccountChecked(self, sender):
        if sender.isChecked():
            self.changeMode("register")
        else:
            self.changeMode("login")

    def onLogin(self):
        self.submit()

    def onRegister(self):
        print self.login_box.getText()
        if not re.match(r'^[a-z0-9_-]+$',self.login_box.getText(), re.IGNORECASE):
            self.warning_msg.setText('Invaling login, valid characters are a-z A-Z 0-9 _ -')
            self.warning_msg.setVisible(True)
        elif not re.match(r'^.+@.+\..+', self.email_box.getText(), re.IGNORECASE):
            self.warning_msg.setText('Invaling email address')
            self.warning_msg.setVisible(True)
        else:
            self.warning_msg.setVisible(False)
            self.submit()

    
    def onSubmit(self, event):
        pass

    def onSubmitComplete(self, event):
        result = event.getResults()
        if result == "AUTH ERROR":
            Window.alert('You login and/or password is incorrect. Please try again')
        elif result == "LOGGED":
            self.callback()
        elif result == "ALREADY EXISTS":
            self.warning_msg.setText('This login already exists, please choose an other one')
            self.warning_msg.setVisible(True)
        elif result == "REGISTRATION":
            self.warning_msg.setVisible(False)
            self.changeMode('login')
            self.pass_box.setText('')
            Window.alert('An email has been sent to you with your login informations\nPlease remember that this is ONLY A TECHNICAL DEMO')
        else:
            Window.alert('Submit error: %s' % result)

class RegisterBox(DialogBox):

    def __init__(self, callback, *args,**kwargs):
        DialogBox.__init__(self,*args,**kwargs)
        _form = RegisterPanel(callback)
        self.setHTML('<b>You are not identified, please log in</b>')
        self.setWidget(_form)