view browser_side/register.py @ 275:a763b2ac5d41

bridge + browser_side: bridge signals for games and their callbacks: - added radiocolPlayers and tarotGamePlayers to get the list of players - added roomLeft to remove the muc from the host's room list - display symbols to identify the players in a muc - factorization of ChatPanel.startGame
author souliane <souliane@mailoo.org>
date Thu, 21 Nov 2013 16:13:14 +0100
parents c24715dcd2f6
children 2067d6241927
line wrap: on
line source

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

"""
Libervia: a Salut à Toi frontend
Copyright (C) 2011, 2012, 2013 Jérôme Poisson <goffi@goffi.org>
Copyright (C) 2011, 2012  Adrien Vigneron <adrienvigneron@mailoo.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.SimplePanel import SimplePanel
from pyjamas.ui.VerticalPanel import VerticalPanel
from pyjamas.ui.HorizontalPanel import HorizontalPanel
from pyjamas.ui.TabPanel import TabPanel
from pyjamas.ui.TabBar import TabBar
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.PopupPanel import PopupPanel
from pyjamas.ui.Image import Image
from pyjamas.ui.Hidden import Hidden
from pyjamas import Window
from pyjamas.ui.KeyboardListener import KEY_ESCAPE, KEY_ENTER
import re
from pyjamas.Timer import Timer


class RegisterPanel(FormPanel):

    def __init__(self, callback):
        """
        @param callback: method to call if login successful
        """
        FormPanel.__init__(self)
        self.setSize('600px', '350px')
        self.callback = callback
        self.setMethod(FormPanel.METHOD_POST)
        main_panel = HorizontalPanel()
        main_panel.setStyleName('registerPanel_main')
        left_side = Image("media/libervia/register_left.png")
        main_panel.add(left_side)

        ##TabPanel##
        tab_bar = TabBar()
        tab_bar.setStyleName('registerPanel_tabs')
        self.right_side = TabPanel(tab_bar)
        self.right_side.setStyleName('registerPanel_right_side')
        main_panel.add(self.right_side)
        main_panel.setCellWidth(self.right_side, '100%')

        ##Login tab##
        login_tab = SimplePanel()
        login_tab.setStyleName('registerPanel_content')
        login_vpanel = VerticalPanel()
        login_tab.setWidget(login_vpanel)

        self.login_warning_msg = Label('')
        self.login_warning_msg.setVisible(False)
        self.login_warning_msg.setStyleName('formWarning')
        login_vpanel.add(self.login_warning_msg)

        login_label = Label('Login:')
        self.login_box = TextBox()
        self.login_box.setName("login")
        self.login_box.addKeyboardListener(self)
        login_pass_label = Label('Password:')
        self.login_pass_box = PasswordTextBox()
        self.login_pass_box.setName("login_password")
        self.login_pass_box.addKeyboardListener(self)

        login_vpanel.add(login_label)
        login_vpanel.add(self.login_box)
        login_vpanel.add(login_pass_label)
        login_vpanel.add(self.login_pass_box)
        login_but = Button("Log in", getattr(self, "onLogin"))
        login_but.setStyleName('button')
        login_but.addStyleName('red')
        login_vpanel.add(login_but)

        #The hidden submit_type field
        self.submit_type = Hidden('submit_type')
        login_vpanel.add(self.submit_type)

        ##Register tab##
        register_tab = SimplePanel()
        register_tab.setStyleName('registerPanel_content')
        register_vpanel = VerticalPanel()
        register_tab.setWidget(register_vpanel)

        self.register_warning_msg = Label('')
        self.register_warning_msg.setVisible(False)
        self.register_warning_msg.setStyleName('formWarning')
        register_vpanel.add(self.register_warning_msg)

        register_login_label = Label('Login:')
        self.register_login_box = TextBox()
        self.register_login_box.setName("register_login")
        self.register_login_box.addKeyboardListener(self)
        email_label = Label('E-mail:')
        self.email_box = TextBox()
        self.email_box.setName("email")
        self.email_box.addKeyboardListener(self)
        register_pass_label = Label('Password:')
        self.register_pass_box = PasswordTextBox()
        self.register_pass_box.setName("register_password")
        self.register_pass_box.addKeyboardListener(self)
        register_vpanel.add(register_login_label)
        register_vpanel.add(self.register_login_box)
        register_vpanel.add(email_label)
        register_vpanel.add(self.email_box)
        register_vpanel.add(register_pass_label)
        register_vpanel.add(self.register_pass_box)

        register_but = Button("Register", getattr(self, "onRegister"))
        register_but.setStyleName('button')
        register_but.addStyleName('red')
        register_vpanel.add(register_but)

        self.right_side.add(login_tab, 'Login')
        self.right_side.add(register_tab, 'Register')
        self.right_side.addTabListener(self)
        self.right_side.selectTab(0)
        login_tab.setWidth(None)
        register_tab.setWidth(None)

        self.add(main_panel)
        self.addFormHandler(self)
        self.setAction('register_api/login')

    def onBeforeTabSelected(self, sender, tabIndex):
        return True

    def onTabSelected(self, sender, tabIndex):
        if tabIndex == 0:
            self.login_box.setFocus(True)
        elif tabIndex == 1:
            self.register_login_box.setFocus(True)

    def onKeyPress(self, sender, keycode, modifiers):
        if keycode == KEY_ENTER:
            # Browsers offer an auto-completion feature to any
            # text box, but the selected value is not set when
            # the widget looses the focus. Using a timer with
            # any delay value > 0 would do the trick.
            if sender == self.login_box:
                Timer(5, lambda: self.login_pass_box.setFocus(True))
            elif sender == self.login_pass_box:
                self.onLogin(None)
            elif sender == self.register_login_box:
                Timer(5, lambda: self.email_box.setFocus(True))
            elif sender == self.email_box:
                Timer(5, lambda: self.register_pass_box.setFocus(True))
            elif sender == self.register_pass_box:
                self.onRegister(None)

    def onKeyUp(self, sender, keycode, modifiers):
        pass

    def onKeyDown(self, sender, keycode, modifiers):
        pass

    def onLogin(self, button):
        if not re.match(r'^[a-z0-9_-]+$',self.login_box.getText(), re.IGNORECASE):
            self.login_warning_msg.setText('Invalid login, valid characters are a-z A-Z 0-9 _ -')
            self.login_warning_msg.setVisible(True)
        else:
            self.submit_type.setValue('login')
            self.submit()

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

    def onSubmit(self, event):
        pass

    def onSubmitComplete(self, event):
        result = event.getResults()
        if result == "AUTH ERROR":
            Window.alert('Your login and/or password is incorrect. Please try again')
        elif result == "LOGGED":
            self.callback()
        elif result == "SESSION_ACTIVE":
            Window.alert('Session already active, this should not happen, please contact the author to fix it')
        elif result == "ALREADY EXISTS":
            self.register_warning_msg.setText('This login already exists, please choose an other one')
            self.register_warning_msg.setVisible(True)
        elif result == "INTERNAL":
            self.register_warning_msg.setText('SERVER ERROR: something went wrong during registration process, please contact the server administrator')
            self.register_warning_msg.setVisible(True)
        elif result == "REGISTRATION":
            self.login_warning_msg.setVisible(False)
            self.register_warning_msg.setVisible(False)
            self.login_box.setText(self.register_login_box.getText())
            self.register_login_box.setText('')
            self.register_pass_box.setText('')
            self.email_box.setText('')
            self.right_side.selectTab(0)
            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(PopupPanel):

    def __init__(self, callback, *args, **kwargs):
        PopupPanel.__init__(self, *args, **kwargs)
        self._form = RegisterPanel(callback)
        self.setWidget(self._form)

    def onWindowResized(self, width, height):
        super(RegisterBox, self).onWindowResized(width, height)
        self.centerBox()

    def show(self):
        super(RegisterBox, self).show()
        self.centerBox()
        self._form.login_box.setFocus(True)