Index: WAeUP_SRP/trunk/Students.py
===================================================================
--- WAeUP_SRP/trunk/Students.py	(revision 1598)
+++ WAeUP_SRP/trunk/Students.py	(revision 1599)
@@ -22,4 +22,6 @@
 i_home = Globals.INSTANCE_HOME
 MAX_TRANS = 1000
+from urllib import urlencode
+
 import DateTime
 # import PIL.Image
@@ -36,4 +38,13 @@
 
 ###)
+
+def response_write(response,s):
+    response.setHeader('Content-type','text/html; charset=ISO-8859-15')
+    while s.find('<') > -1:
+        s = s.replace('<','&lt;')
+    while s.find('>') > -1:
+        #from pdb import set_trace;set_trace()
+        s = s.replace('>','&gt;')
+    response.write("%s<br>\n" % s)
 
 def getInt(s): ###(
@@ -534,5 +545,6 @@
         wftool = self.portal_workflow
         current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S")
-        students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
+        #students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
+        students_folder = self.portal_url().getPortalObject().campus.students
         tr_count = 1
         total = 0
@@ -699,4 +711,124 @@
         logger.info(em)
         return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
+    ###)
+
+    security.declareProtected(ModifyPortalContent,"fixAllNames")###(
+    def fixAllNames(self): 
+        "fix all students names"
+        import transaction
+        response = self.REQUEST.RESPONSE
+        logger = logging.getLogger('fixAllNames')
+        logger.info('Start')
+        students = self.portal_catalog(portal_type='Student')
+        count = 0
+        total = 0
+        for student in students:
+            scat_res = self.students_catalog(id = student.getId)
+            if not scat_res:
+                self.students_catalog.addRecord(id = student.getId)
+                scat_res = self.students_catalog(id = student.getId)
+            student_entry = scat_res[0]
+            old_new = self.fixName(student,student_entry)
+            count += 1
+            response_write(response,"%d) %s: %s" % (count + total,student_entry.id,old_new))
+            if count > 2000:
+                transaction.commit()
+                logger.info("%d transactions commited" % count)
+                total += count
+                count = 0
+    ###)
+
+    security.declareProtected(ModifyPortalContent,"fixName")###(
+    def fixName(self,student_brain, student_entry): 
+        "fix the name of a student"
+        fix = "first"
+        if student_entry.get('name_fixed',None) == fix:
+            return "Name already fixed"
+        student_id = student_entry.id
+        new_student = student_entry.jamb_reg_no.startswith('6') 
+        student_obj = student_brain.getObject()
+        personal = getattr(student_obj,'personal',None)
+        invalid = ''
+        if personal is None:
+            return '"%s", "Returning: %s","%s"' % (invalid,student_entry.name,"not logged in")
+        per_doc = personal.getContent()
+        old_first = per_doc.firstname
+        old_middle = per_doc.middlename
+        old_last = per_doc.lastname
+        new_first = ''
+        new_middle = ''
+        new_last = ''
+        if new_student:
+            if not old_first and not old_middle and old_last:
+                new_names = [n.capitalize() for n in old_last.split()]
+                if len(new_names) > 3:
+                    old_first = new_names[0]
+                    old_last = new_names[1]
+                    old_middle = ' '.join(new_names[2:])
+                elif len(new_names) == 3:
+                    old_first = new_names[0]
+                    old_last = new_names[1]
+                    old_middle = new_names[2]
+                elif len(new_names) == 2:
+                    old_first = new_names[0]
+                    old_last = new_names[1]
+                    old_middle = ''
+                elif len(new_names) == 1:
+                    old_last = new_names[0]
+                    old_first = ''
+                    old_middle = ''
+            if old_last:
+                new_first = old_last
+            if old_middle:
+                new_middle = old_middle
+            if old_first:
+                new_last = old_first
+                    
+            if old_first.find('<') != -1 or\
+               old_first.find('>') != -1 or\
+               old_middle.find('<') != -1 or\
+               old_middle.find('>') != -1 or\
+               old_last.find('<') != -1 or\
+               old_last.find('>') != -1:
+                   invalid = "invalid characters"
+        else:
+            if old_first and old_middle and old_last:
+                new_first = old_first.capitalize()
+                new_middle = old_middle.capitalize()
+                new_last = old_last.capitalize()
+            elif old_first and not old_middle and old_last:
+                new_names = [n.capitalize() for n in old_first.split()]
+                if len(new_names) > 1:
+                    new_first = new_names[0]
+                    new_middle = ' '.join(new_names[1:])
+                else:
+                    new_first = old_first.capitalize()
+                    new_middle = ''
+                new_last = old_last.capitalize()
+            elif not old_first and not old_middle and old_last:
+                new_first = ''
+                new_middle = ''
+                new_last = old_last.capitalize()
+            else:
+                new_first = ''
+                new_middle = ''
+                new_last = ''
+        name = "%(new_last)s %(new_first)s %(new_middle)s" % vars()
+        if new_student:
+            text = "New"
+        else:
+            text = "Returning"
+        old_new = '"%s", "%s: %s","%s"' % (invalid,text,
+                                           student_entry.name,
+                                           name)
+        if not invalid:
+            self.students_catalog.modifyRecord(id = student_id,
+                                      name_fixed = fix,
+                                      name = name)
+            per_doc.edit(mapping = {'firstname' : new_first,
+                                'middlename' : new_middle,
+                                'lastname' : new_last,
+                                })
+        return old_new
     ###)
 
