Author: ArcRiley
Date: 2009-03-07 13:23:49 -0500 (Sat, 07 Mar 2009)
New Revision: 1555

Modified:
   trunk/concordance/examples/register.py
Log:
cleaned up register.py a bit

Modified: trunk/concordance/examples/register.py
===================================================================
--- trunk/concordance/examples/register.py      2009-03-05 19:30:40 UTC (rev 
1554)
+++ trunk/concordance/examples/register.py      2009-03-07 18:23:49 UTC (rev 
1555)
@@ -28,35 +28,19 @@
 import xml.etree.cElementTree as ElementTree
 
 host = 'selket.apogean.org'
+UserDB = {}
 
-#All the Users are stored in a Dictionary/Hash(in-memory) in a session
-#they are NOT (de)serialized
-
 class User(object):
   '''
     Class to store the User information
   '''
-  def __init__(self, username, email, fname, lname, pwd):
+  def __init__(self, username='', email='', password=''):
     self.username = username
     self.email = email
-    self.fname = fname
-    self.lname = lname
-    self.pwd = pwd
-    #Mothers Maiden Name can also be added
+    self.password = password
 
-UserDB = {'foo': User('foo', 'f...@bar.com', 'Foo', 'Bar', 'test')}
 
 class InBandRegistration(concordance.services.Service) :
-  def createNode(self, parent_node,tag_name, tag_val):
-    '''
-      Method that creates a node given its name and value;
-      It is attached to its parent
-      TO-DO : Make this as th standard way of creating a 'simple'node; 
-              can this be moved to a better OO paradigm?
-     '''
-    temp_node = ElementTree.SubElement(parent_node, tag_name)
-    temp_node.text = tag_val
-
   def failedError(self, err_code, err_type, err_desc, input):
     ''' 
       Error Information to be added when something fails or errors out
@@ -71,18 +55,17 @@
     '''
       Host Returns Registration Fields to Entity
     '''
-    if query.tag == '{jabber:iq:register}query' :
-      out_query = ElementTree.SubElement(out_root, 'query')
-      out_query.attrib['xmlns'] = 'jabber:iq:register'
-      ElementTree.SubElement(out_query, 'instructions')
-      out_query[0].text = '''Welcome to the example server!
+    out_query = ElementTree.SubElement(out_root, 'query')
+    out_query.attrib['xmlns'] = 'jabber:iq:register'
+    ElementTree.SubElement(out_query, 'instructions')
+    out_query[0].text = '''Welcome to the example server!
 
 You'll just need to complete this registration form with your \
 chosen name, password, and an email address we can reach you at.
 
 There isn't much more to this server than a simple registration, so enjoy!'''
-      for field in ['username', 'password', 'first', 'last', 'email'] :
-        ElementTree.SubElement(out_query, field)
+    for field in ['username', 'password', 'email'] :
+      ElementTree.SubElement(out_query, field)
 
   def validateRegistration(self, input, out_root):
     '''
@@ -98,37 +81,46 @@
     #Checks for non-null Email, Username and Password fields
     i= ""
     error_code = 0
-    for qnode in input:
-      for r in qnode:
+    newuser = User()
+    
+    for qnode in input :
+      for r in qnode :
         tag = r.tag
         if tag == '{jabber:iq:register}username':
-          if r.text:
+          if r.text :
             #Checks whether the user has already registered or not 
             #this is done by a lookup for username in the UserDB dict
-            if r.text in UserDB:
-              self.createNode(out_query, 'username', r.text)
+            stanza = ElementTree.SubElement(out_query, 'username')
+            stanza.text = r.text
+            if r.text in UserDB :
               error_code = 1 #set to 1 for USER_CONFLICT
               print("User already Registered")
+            else :
+              newuser.username = r.text
           else:
             error_code = 2
             break
         elif tag == '{jabber:iq:register}password':
           if r.text:
-            self.createNode(out_query,'password',r.text)
+            stanza = ElementTree.SubElement(out_query, 'password')
+            stanza.text = r.text
+            newuser.password = r.text
           else:
             error_code = 2
             break
         elif tag == '{jabber:iq:register}email':
           if r.text:
-            self.createNode(out_query,'email',r.text)
+            stanza = ElementTree.SubElement(out_query, 'email')
+            stanza.text = r.text
+            newuser.email = r.text
           else:
             error_code = 2
             break
 
     if error_code == 0:
-      UserDB[r.text]=r.text # Save usernames alone
+      UserDB[newuser.username]=newuser
       #TO-DO : change this to include the record as the value
-      self.successReg( input, out_root)
+      self.successReg(input, out_root)
     elif error_code == 1:
       #User already exists with the same email Id - Conflict message 
       #Q:Do we need to check the correspondance of all fields for duplicate 
user      #presently only username is checked
@@ -207,7 +199,7 @@
     '''
     out_root.attrib['type'] = 'result'
 
-  def successPwdChange(self, input, out_root):
+  def successpasswordChange(self, input, out_root):
     '''
       Host Informs Client of Successful Password Change
       Note: 'input' is being passed to accomodate any future enhancements, 
@@ -215,7 +207,7 @@
     '''
     out_root.attrib['type']='result'
 
-  def pwdChangeFormError(self, input, out_root):
+  def passwordChangeFormError(self, input, out_root):
     '''Server Returns Password Change Form With Error
     '''
     #construct 'query' node; 'x' will be its child
@@ -240,7 +232,7 @@
     #construct the 'error' node
     self.failedError('401', 'modify', 'not-authorized', out_root)
 
-  def savePwdChange(self, input, out_root):
+  def savepasswordChange(self, input, out_root):
     '''
       Method to save the password provided by the user
       Method also checks whether the password saved in UserDB and 
@@ -252,10 +244,10 @@
 
 
   def xml(self, in_root) :
+    '''xml handler
+
+    This method is called for every stream-level stanza directed at the service
     '''
-       this method keeps waiting for a message and
-       dispatches it to the suitabe method after filtering
-    '''
     # this example only replies to <iq/> requests
     if in_root.tag != '{jabber:client}iq' :
       return ""
@@ -265,8 +257,10 @@
 
     if in_root.attrib['type'] == 'get' :
       out_root.attrib['type'] = 'result'
-      for query in in_root :
-        self.regScreen(query, out_root)
+      for stanza in in_root :
+        if stanza.tag == '{jabber:iq:register}query' :
+          self.regScreen(stanza, out_root)
+
     elif in_root.attrib['type'] == 'set':
        self.validateRegistration(in_root, out_root)        
     else :
@@ -278,8 +272,8 @@
 
   def sasl_password(self, authid, authzid) :
     if authid in UserDB :
-      print("%s found, password: %s" % (authid, UserDB[authid].pwd))
-      return UserDB[authid].pwd
+      print("%s found, password: %s" % (authid, UserDB[authid].password))
+      return UserDB[authid].password
     print("%s not found" % authid)
     return ''
 

_______________________________________________
PySoy-SVN mailing list
PySoy-SVN@pysoy.org
http://www.pysoy.org/mailman/listinfo/pysoy-svn

Reply via email to