Index: WAeUP_SRP/trunk/WAeUPTables.py
===================================================================
--- WAeUP_SRP/trunk/WAeUPTables.py	(revision 5533)
+++ WAeUP_SRP/trunk/WAeUPTables.py	(revision 5559)
@@ -273,126 +273,4 @@
     ###)
 
-    security.declarePrivate("_import_old") ###(
-    def _import_old(self,filename,schema,layout, mode,logger):
-        "import data from csv"
-        import transaction
-        import random
-        pm = self.portal_membership
-        member = pm.getAuthenticatedMember()
-        current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S")
-        import_fn = "%s/import/%s.csv" % (i_home,filename)
-        imported_fn = "%s/import/%s_imported%s.csv" % (i_home,filename,current)
-        not_imported_fn = "%s/import/%s_not_imported%s.csv" % (i_home,filename,current)
-        start = True
-        tr_count = 1
-        total_imported = 0
-        total_not_imported = 0
-        total = 0
-        iname =  "%s" % filename
-        not_imported = []
-        imported = []
-        valid_records = []
-        invalid_records = []
-        d = {}
-        d['mode'] = mode
-        d['imported'] = total_imported
-        d['not_imported'] = total_not_imported
-        d['valid_records'] = valid_records
-        d['invalid_records'] = invalid_records
-        d['import_fn'] = import_fn
-        d['imported_fn'] = imported_fn
-        d['not_imported_fn'] = not_imported_fn
-        if schema is None:
-            em = 'No schema specified'
-            logger.error(em)
-            return d
-        if layout is None:
-            em = 'No layout specified'
-            logger.error(em)
-            return d
-        validators = {}
-        for widget in layout.keys():
-            try:
-                validators[widget] = layout[widget].validate
-            except AttributeError:
-                logger.info('%s has no validate attribute' % widget)
-                return d
-        # if mode == 'edit':
-        #     importer = self.importEdit
-        # elif mode == 'add':
-        #     importer = self.importAdd
-        # else:
-        #     importer = None
-        try:
-            items = csv.DictReader(open(import_fn,"rb"),
-                                   dialect="excel",
-                                   skipinitialspace=True)
-        except:
-            em = 'Error reading %s.csv' % filename
-            logger.error(em)
-            return d
-        #import pdb;pdb.set_trace()
-        for item in items:
-            if start:
-                start = False
-                logger.info('%s starts import from %s.csv' % (member,filename))
-                #import_keys = [k for k in item.keys() if not k.startswith('ignore')]
-                attrs = csv.reader(open("%s/import/%s.csv" % (i_home,filename),"rb"),
-                                   dialect="excel",
-                                   skipinitialspace=True).next()
-                import_keys = [k for k in attrs if not (k.startswith('ignore') or k.isupper())]
-                diff2schema = set(import_keys).difference(set(schema.keys()))
-                diff2layout = set(import_keys).difference(set(layout.keys()))
-                if diff2layout:
-                    em = "not ignorable key(s) %s found in heading" % diff2layout
-                    logger.info(em)
-                    return d
-                s = ','.join(['"%s"' % fn for fn in import_keys])
-                open(not_imported_fn,"a").write(s + ',"Error"'+ '\n')
-                #s = '"id",' + s
-                open(imported_fn,"a").write(s + '\n')
-                format = ','.join(['"%%(%s)s"' % fn for fn in import_keys])
-                format_error = format + ',"%(Error)s"'
-                #format = '"%(id)s",'+ format
-                adapters = [MappingStorageAdapter(schema, item)]
-            dm = DataModel(item, adapters,context=self)
-            ds = DataStructure(data=item,datamodel=dm)
-            error_string = ""
-            #import pdb;pdb.set_trace()
-            for k in import_keys:
-                if not validators[k](ds,mode=mode):
-                    error_string += " %s : %s" % (k,ds.getError(k))
-            # if not error_string and importer:
-            #     item.update(dm)
-            #     item['id'],error = importer(item)
-            #     if error:
-            #         error_string += error
-            if error_string:
-                item['Error'] = error_string
-                invalid_records.append(dm)
-                not_imported.append(format_error % item)
-                total_not_imported += 1
-            else:
-                em = format % item
-                valid_records.append(dm)
-                imported.append(em)
-                #logger.info("%(total_imported)d of %(total)d %(em)s" % vars())
-                tr_count += 1
-                total_imported += 1
-            total += 1
-        if len(imported) > 0:
-            open(imported_fn,"a").write('\n'.join(imported))
-        if len(not_imported) > 0:
-            open(not_imported_fn,"a").write('\n'.join(not_imported))
-        #em = "Imported: %d, not imported: %d of total %d" % (total_imported,total_not_imported,total)
-        d['imported'] = total_imported
-        d['not_imported'] = total_not_imported
-        d['valid_records'] = valid_records
-        d['invalid_records'] = invalid_records
-        d['imported_fn'] = imported_fn
-        d['not_imported_fn'] = not_imported_fn
-        #logger.info(em)
-        return d
-    ###)
 
     security.declarePrivate("_import") ###(
@@ -1991,4 +1869,48 @@
     ###)    
 
+    security.declareProtected(ModifyPortalContent,"dumpPayments")###(
+    def dumpPayments(self,session_id='09'):
+        """dump all valid payments and combine with student data """
+        member = self.portal_membership.getAuthenticatedMember()
+        logger = logging.getLogger('WAeUPTables.WAeUPTable.dumpPayments')
+        current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S")
+        export_file = "%s/export/valid_payments%s_%s.csv" % (i_home,session_id,current,)        
+        
+        pm_catalog = self.payments_catalog
+        query = Eq('status','paid') & Eq('type','online') & Eq('session_id',session_id)
+        payments = pm_catalog.evalAdvancedQuery(query)
+        payments_dic = []
+        s_catalog = self.students_catalog
+        fields_pm = pm_catalog.schema()
+        fields_s = s_catalog.schema()
+        fields =  fields_pm + fields_s 
+        
+        format = '"%(' + ')s","%('.join(fields) + ')s"'
+        #import pdb;pdb.set_trace()
+        for brain in payments:
+            d = {}
+            for field in fields_pm:
+                d[field] = getattr(brain,field,'')
+            
+            student_id = getattr(brain,'student_id','') 
+            query = Eq('id',student_id)    
+            student = s_catalog.evalAdvancedQuery(query)
+            if student:
+                for field in fields_s:
+                    d[field] = getattr(student[0],field,'')               
+            payments_dic.append(format % d)     
+            
+        if not os.path.exists(export_file):  
+            file_handler = open(export_file,"a")
+            headline = ','.join(fields)
+            file_handler.write(headline +'\n')
+        else:
+            file_handler = open(export_file,"a")
+        for line in payments_dic: 
+            file_handler.write(line +'\n')     
+        
+        return 'ready'       
+            
+
 InitializeClass(PaymentsCatalog)
 
