Add bare JID as third argument to register form post functions

darcs-hash:20070606184618-86b55-d2a169b118adbec03202b7f9508d809d09bebb03.gz
This commit is contained in:
David Rousselie
2007-06-06 20:46:18 +02:00
parent db4d7fd2ac
commit 3a85b455cf
3 changed files with 179 additions and 143 deletions

View File

@@ -317,7 +317,7 @@ class JCLComponent(Component, object):
query.newTextChild(query.ns(), "jid", jid)
self.stream.send(info_query)
return 1
def disco_get_info(self, node, info_query):
"""Discovery get info handler
"""
@@ -677,8 +677,11 @@ class AccountManager(object):
"""Handle get_register for given account_class"""
info_query = info_query.make_result_response()
query = info_query.new_query("jabber:iq:register")
from_jid = info_query.get_from()
bare_from_jid = unicode(from_jid.bare())
self.get_reg_form(lang_class,
account_class).as_xml(query)
account_class,
bare_from_jid).as_xml(query)
return [info_query]
def account_type_get_register(self, info_query, account_type, lang_class):
@@ -726,6 +729,8 @@ class AccountManager(object):
"""Populate given account"""
field = None
result = []
from_jid = info_query.get_from()
bare_from_jid = unicode(from_jid.bare())
self.db_connect()
try:
for (field, field_type, field_options, field_post_func,
@@ -736,7 +741,8 @@ class AccountManager(object):
else:
value = None
setattr(_account, field,
field_post_func(value, field_default_func))
field_post_func(value, field_default_func,
bare_from_jid))
except FieldError, exception:
_account.destroySelf()
type, value, stack = sys.exc_info()
@@ -1037,7 +1043,7 @@ class AccountManager(object):
"""Delete connection associated to the current thread"""
del account.hub.threadConnection
def get_reg_form(self, lang_class, _account_class):
def get_reg_form(self, lang_class, _account_class, bare_from_jid):
"""Return register form based on language and account class
"""
reg_form = Form(title=lang_class.register_title,
@@ -1078,20 +1084,20 @@ class AccountManager(object):
field.add_option(label=label,
values=[option_value])
try:
post_func(None, default_func)
post_func(None, default_func, bare_from_jid)
except:
self.__logger.debug("Setting field " + field_name + " required")
field.required = True
## TODO : get default value if any
return reg_form
def get_reg_form_init(self, lang_class, _account):
"""Return register form for an existing account (update)
"""
reg_form = self.get_reg_form(lang_class, _account.__class__)
reg_form = self.get_reg_form(lang_class, _account.__class__,
_account.user_jid)
reg_form["name"].value = _account.name
reg_form["name"].type = "hidden"
for field in reg_form.fields: # TODO
for field in reg_form.fields:
if hasattr(_account, field.name):
field.value = getattr(_account, field.name)
return reg_form

View File

@@ -1,21 +1,21 @@
# -*- coding: UTF-8 -*-
# -*- coding: utf-8 -*-
##
## account.py
## Login : David Rousselie <dax@happycoders.org>
## Started on Wed Aug 9 21:04:42 2006 David Rousselie
## $Id$
##
##
## Copyright (C) 2006 David Rousselie
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 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 General Public License for more details.
##
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
@@ -37,13 +37,13 @@ OFFLINE = "offline"
ONLINE = "online"
def default_post_func(field_value, default_func):
def default_post_func(field_value, default_func, bare_from_jid):
"""Default post process function: do nothing"""
if field_value is None or str(field_value) == "":
return default_func()
return field_value
def int_post_func(field_value, default_func):
def int_post_func(field_value, default_func, bare_from_jid):
"""Return an integer from integer field value"""
if field_value is None or str(field_value) == "":
return int(default_func())
@@ -67,9 +67,9 @@ class Account(InheritableSQLObject):
name = StringCol()
jid = StringCol()
## Not yet used first_check = BoolCol(default = True)
__status = StringCol(default = OFFLINE, dbName = "status")
in_error = BoolCol(default = False)
__status = StringCol(default=OFFLINE, dbName="status")
in_error = BoolCol(default=False)
## Use these attributs to support volatile password
## login = StringCol(default = "")
## password = StringCol(default = None)
@@ -83,7 +83,7 @@ class Account(InheritableSQLObject):
return self.name
long_name = property(get_long_name)
def get_status_msg(self):
"""Return current status"""
return self.name
@@ -108,7 +108,7 @@ class Account(InheritableSQLObject):
# if previous status was OFFLINE
self.first_check = True
self.__status = status
status = property(get_status, set_status)
def _get_register_fields(cls, real_class = None):
@@ -117,12 +117,12 @@ class Account(InheritableSQLObject):
- field_name: might be the name of one of the class attribut
- field_type: 'text-single', 'hidden', 'text-private', 'boolean',
'list-single', ...
- field_options:
- field_options:
- field_post_func: function called to process received field
- field_default_func: function to return default value
"""
return [] # "name" field is mandatory
get_register_fields = classmethod(_get_register_fields)
def get_new_message_subject(self, lang_class):
@@ -178,14 +178,14 @@ class PresenceAccount(Account):
PresenceAccount.DO_NOTHING), \
'offline_action': (cls.possibles_actions, \
PresenceAccount.DO_NOTHING)}
get_presence_actions_fields = classmethod(_get_presence_actions_fields)
def _get_register_fields(cls, real_class = None):
""" See Account._get_register_fields """
def get_possibles_actions(presence_action_field):
return real_class.get_presence_actions_fields()[presence_action_field][0]
def is_action_possible(presence_action_field, action, default_func):
if int(action) in get_possibles_actions(presence_action_field):
return int(action)
@@ -197,44 +197,50 @@ class PresenceAccount(Account):
if real_class is None:
real_class = cls
return Account.get_register_fields(real_class) + \
[(None, None, None, None, None), \
("chat_action", "list-single", \
[str(action) for action in get_possibles_actions("chat_action")], \
lambda action, default_func: is_action_possible("chat_action", \
action, \
default_func), \
lambda : get_default_presence_action("chat_action")), \
("online_action", "list-single", \
[str(action) for action in get_possibles_actions("online_action")], \
lambda action, default_func: is_action_possible("online_action", \
action, \
default_func), \
lambda : get_default_presence_action("online_action")), \
("away_action", "list-single", \
[str(action) for action in get_possibles_actions("away_action")], \
lambda action, default_func: is_action_possible("away_action", \
action, \
default_func), \
lambda : get_default_presence_action("away_action")), \
("xa_action", "list-single", \
[str(action) for action in get_possibles_actions("xa_action")], \
lambda action, default_func: is_action_possible("xa_action", \
action, \
default_func), \
lambda : get_default_presence_action("xa_action")), \
("dnd_action", "list-single", \
[str(action) for action in get_possibles_actions("dnd_action")], \
lambda action, default_func: is_action_possible("dnd_action", \
action, \
default_func), \
lambda : get_default_presence_action("dnd_action")), \
("offline_action", "list-single", \
[str(action) for action in get_possibles_actions("offline_action")], \
lambda action, default_func: is_action_possible("offline_action", \
action, \
default_func), \
lambda : get_default_presence_action("offline_action"))]
[(None, None, None, None, None),
("chat_action", "list-single",
[str(action) for action in get_possibles_actions("chat_action")],
lambda action, default_func, bare_from_jid: \
is_action_possible("chat_action",
action,
default_func),
lambda : get_default_presence_action("chat_action")),
("online_action", "list-single",
[str(action) for action in get_possibles_actions("online_action")],
lambda action, default_func, bare_from_jid: \
is_action_possible("online_action",
action,
default_func),
lambda : get_default_presence_action("online_action")),
("away_action", "list-single",
[str(action) for action in get_possibles_actions("away_action")],
lambda action, default_func, bare_from_jid: \
is_action_possible("away_action",
action,
default_func),
lambda : get_default_presence_action("away_action")),
("xa_action", "list-single",
[str(action) for action in get_possibles_actions("xa_action")],
lambda action, default_func, bare_from_jid: \
is_action_possible("xa_action",
action,
default_func),
lambda : get_default_presence_action("xa_action")),
("dnd_action", "list-single",
[str(action) for action in get_possibles_actions("dnd_action")],
lambda action, default_func, bare_from_jid: \
is_action_possible("dnd_action",
action,
default_func),
lambda : get_default_presence_action("dnd_action")),
("offline_action", "list-single",
[str(action) for action in get_possibles_actions("offline_action")],
lambda action, default_func, bare_from_jid: \
is_action_possible("offline_action",
action,
default_func),
lambda : get_default_presence_action("offline_action"))]
get_register_fields = classmethod(_get_register_fields)
def get_action(self):
@@ -248,5 +254,5 @@ class PresenceAccount(Account):
if mapping.has_key(self.status):
return mapping[self.status]
return PresenceAccount.DO_NOTHING
action = property(get_action)

View File

@@ -3,18 +3,18 @@
## Login : David Rousselie <dax@happycoders.org>
## Started on Wed Nov 22 19:32:53 2006 David Rousselie
## $Id$
##
##
## Copyright (C) 2006 David Rousselie
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 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 General Public License for more details.
##
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
@@ -38,15 +38,15 @@ else:
DB_URL = DB_PATH# + "?debug=1&debugThreading=1"
class ExampleAccount(Account):
login = StringCol(default = "")
password = StringCol(default = None)
store_password = BoolCol(default = True)
waiting_password_reply = BoolCol(default = False)
login = StringCol(default="")
password = StringCol(default=None)
store_password = BoolCol(default=True)
waiting_password_reply = BoolCol(default=False)
test_enum = EnumCol(default = "choice1", enumValues = ["choice1", "choice2", "choice3"])
test_int = IntCol(default = 42)
def _get_register_fields(cls, real_class = None):
test_enum = EnumCol(default="choice1", enumValues=["choice1", "choice2", "choice3"])
test_int = IntCol(default=42)
def _get_register_fields(cls, real_class=None):
def password_post_func(password):
if password is None or password == "":
return None
@@ -55,107 +55,127 @@ class ExampleAccount(Account):
if real_class is None:
real_class = cls
return Account.get_register_fields(real_class) + \
[("login", "text-single", None, \
lambda field_value, default_func: account.mandatory_field(field_value), \
lambda : ""), \
("password", "text-private", None, \
lambda field_value, default_func: password_post_func(field_value), \
lambda : ""), \
("store_password", "boolean", None, account.default_post_func, \
lambda : True), \
("test_enum", "list-single", ["choice1", "choice2", "choice3"], \
account.default_post_func, \
lambda : "choice2"), \
("test_int", "text-single", None, account.int_post_func, \
lambda : 44)]
[("login", "text-single", None,
lambda field_value, default_func, bare_from_jid: \
account.mandatory_field(field_value),
lambda : ""),
("password", "text-private", None,
lambda field_value, default_func, bare_from_jid: \
password_post_func(field_value),
lambda : ""),
("store_password", "boolean", None, account.default_post_func,
lambda : True),
("test_enum", "list-single", ["choice1", "choice2", "choice3"],
account.default_post_func,
lambda : "choice2"),
("test_int", "text-single", None, account.int_post_func,
lambda : 44)]
get_register_fields = classmethod(_get_register_fields)
class Example2Account(Account):
test_new_int = IntCol(default = 42)
test_new_int = IntCol(default=42)
def _get_register_fields(cls, real_class = None):
def _get_register_fields(cls, real_class=None):
if real_class is None:
real_class = cls
return Account.get_register_fields(real_class) + \
[("test_new_int", "text-single", None, account.int_post_func, \
lambda : 43)]
[("test_new_int", "text-single", None, account.int_post_func,
lambda : 43)]
get_register_fields = classmethod(_get_register_fields)
class PresenceAccountExample(PresenceAccount):
DO_SOMETHING_ELSE = 2
possibles_actions = [PresenceAccount.DO_NOTHING, \
PresenceAccount.DO_SOMETHING, \
possibles_actions = [PresenceAccount.DO_NOTHING,
PresenceAccount.DO_SOMETHING,
DO_SOMETHING_ELSE]
def _get_presence_actions_fields(cls):
"""See PresenceAccount._get_presence_actions_fields
"""
return {'chat_action': (cls.possibles_actions, \
PresenceAccountExample.DO_SOMETHING_ELSE), \
'online_action': (cls.possibles_actions, \
PresenceAccountExample.DO_SOMETHING_ELSE), \
'away_action': (cls.possibles_actions, \
PresenceAccountExample.DO_SOMETHING_ELSE), \
'xa_action': (cls.possibles_actions, \
PresenceAccountExample.DO_SOMETHING_ELSE), \
'dnd_action': (cls.possibles_actions, \
PresenceAccountExample.DO_SOMETHING_ELSE), \
'offline_action': (cls.possibles_actions, \
return {'chat_action': (cls.possibles_actions,
PresenceAccountExample.DO_SOMETHING_ELSE),
'online_action': (cls.possibles_actions,
PresenceAccountExample.DO_SOMETHING_ELSE),
'away_action': (cls.possibles_actions,
PresenceAccountExample.DO_SOMETHING_ELSE),
'xa_action': (cls.possibles_actions,
PresenceAccountExample.DO_SOMETHING_ELSE),
'dnd_action': (cls.possibles_actions,
PresenceAccountExample.DO_SOMETHING_ELSE),
'offline_action': (cls.possibles_actions,
PresenceAccountExample.DO_SOMETHING_ELSE)}
get_presence_actions_fields = classmethod(_get_presence_actions_fields)
test_new_int = IntCol(default = 42)
test_new_int = IntCol(default=42)
def _get_register_fields(cls, real_class = None):
def _get_register_fields(cls, real_class=None):
if real_class is None:
real_class = cls
return PresenceAccount.get_register_fields(real_class) + \
[("test_new_int", "text-single", None, account.int_post_func, \
lambda : 43)]
[("test_new_int", "text-single", None, account.int_post_func,
lambda : 43)]
get_register_fields = classmethod(_get_register_fields)
class AccountModule_TestCase(unittest.TestCase):
def test_default_post_func(self):
result = account.default_post_func("test", None)
result = account.default_post_func("test", None, "user1@jcl.test.com")
self.assertEquals(result, "test")
def test_default_post_func_default_value(self):
result = account.default_post_func("", lambda : "test")
result = account.default_post_func("", lambda : "test", "user1@jcl.test.com")
self.assertEquals(result, "test")
def test_default_post_func_default_value2(self):
result = account.default_post_func(None, lambda : "test")
result = account.default_post_func(None, lambda : "test", "user1@jcl.test.com")
self.assertEquals(result, "test")
def test_int_post_func(self):
result = account.int_post_func("42", None)
result = account.int_post_func("42", None, "user1@jcl.test.com")
self.assertEquals(result, 42)
def test_int_post_func_default_value(self):
result = account.int_post_func("", lambda : 42)
result = account.int_post_func("", lambda : 42, "user1@jcl.test.com")
self.assertEquals(result, 42)
def test_int_post_func_default_value(self):
result = account.int_post_func(None, lambda : 42)
result = account.int_post_func(None, lambda : 42, "user1@jcl.test.com")
self.assertEquals(result, 42)
def test_mandatory_field_empty(self):
self.assertRaises(FieldError, \
account.mandatory_field, \
self.assertRaises(FieldError,
account.mandatory_field,
"")
def test_mandatory_field_none(self):
self.assertRaises(FieldError, \
account.mandatory_field, \
self.assertRaises(FieldError,
account.mandatory_field,
None)
def test_mandatory_field_empty(self):
self.assertEquals(account.mandatory_field("value"), \
self.assertEquals(account.mandatory_field("value"),
"value")
class Account_TestCase(unittest.TestCase):
class InheritableAccount_TestCase(unittest.TestCase):
def test_get_register_fields(self):
"""Check if post functions and default functions execute correctly.
To be validated this test only need to be executed without any
exception.
"""
for (field_name,
field_type,
field_options,
field_post_func,
field_default_func) in self.account_class.get_register_fields():
if field_name is not None:
try:
field_post_func(field_default_func(), field_default_func, "user1@jcl.test.com")
except FieldError, error:
# this type of error is OK
pass
class Account_TestCase(InheritableAccount_TestCase):
def setUp(self):
if os.path.exists(DB_PATH):
os.unlink(DB_PATH)
@@ -163,6 +183,7 @@ class Account_TestCase(unittest.TestCase):
Account.createTable(ifNotExists = True)
ExampleAccount.createTable(ifNotExists = True)
del account.hub.threadConnection
self.account_class = Account
def tearDown(self):
account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL)
@@ -173,12 +194,12 @@ class Account_TestCase(unittest.TestCase):
del account.hub.threadConnection
if os.path.exists(DB_PATH):
os.unlink(DB_PATH)
def test_set_status(self):
account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL)
account11 = Account(user_jid = "test1@test.com", \
name = "account11", \
jid = "account11@jcl.test.com")
account11 = Account(user_jid="test1@test.com",
name="account11",
jid="account11@jcl.test.com")
account11.status = account.OFFLINE
self.assertEquals(account11.status, account.OFFLINE)
# TODO : test first_check attribute
@@ -186,14 +207,14 @@ class Account_TestCase(unittest.TestCase):
def test_set_status_live_password(self):
account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL)
account11 = ExampleAccount(user_jid = "test1@test.com", \
name = "account11", \
jid = "account11@jcl.test.com", \
login = "mylogin", \
password = "mypassword", \
store_password = False, \
test_enum = "choice3", \
test_int = 21)
account11 = ExampleAccount(user_jid="test1@test.com",
name="account11",
jid="account11@jcl.test.com",
login="mylogin",
password="mypassword",
store_password=False,
test_enum="choice3",
test_int=21)
account11.waiting_password_reply = True
account11.status = account.OFFLINE
self.assertEquals(account11.status, account.OFFLINE)
@@ -201,7 +222,7 @@ class Account_TestCase(unittest.TestCase):
self.assertEquals(account11.password, None)
del account.hub.threadConnection
class PresenceAccount_TestCase(unittest.TestCase):
class PresenceAccount_TestCase(InheritableAccount_TestCase):
def setUp(self):
if os.path.exists(DB_PATH):
os.unlink(DB_PATH)
@@ -226,14 +247,16 @@ class PresenceAccount_TestCase(unittest.TestCase):
del account.hub.threadConnection
if os.path.exists(DB_PATH):
os.unlink(DB_PATH)
def test_get_presence_actions_fields(self):
fields = self.account_class.get_presence_actions_fields()
self.assertEquals(len(fields), 6)
(possibles_actions, chat_default_action) = fields["chat_action"]
self.assertEquals(possibles_actions, self.account_class.possibles_actions)
self.assertEquals(possibles_actions,
self.account_class.possibles_actions)
(possibles_actions, online_default_action) = fields["online_action"]
self.assertEquals(possibles_actions, self.account_class.possibles_actions)
self.assertEquals(possibles_actions,
self.account_class.possibles_actions)
def test_possibles_actions(self):
for (field_name, \
@@ -243,18 +266,19 @@ class PresenceAccount_TestCase(unittest.TestCase):
default_func) in self.account_class.get_register_fields()[1:]:
if possibles_actions is not None:
for possible_action in possibles_actions:
self.assertEquals(post_func(possible_action, default_func),
self.assertEquals(post_func(possible_action, default_func,
"user1@jcl.test.com"),
int(possible_action))
self.assertTrue(str(default_func()) in possibles_actions)
else:
try:
post_func("42", default_func)
post_func("42", default_func, "user1@jcl.test.com")
default_func()
except FieldError, error:
pass
except Exception, exception:
self.fail("Unexcepted exception: " + str(exception))
def suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(AccountModule_TestCase, 'test'))