Ignore:
Timestamp:
3 Jul 2012, 07:13:59 (12 years ago)
Author:
Henrik Bettermann
Message:

Import payment tickets without p_id and generate p_id in checkConversion.

Increase resolution of timestamp.

Location:
main/waeup.kofa/trunk/src/waeup/kofa/students
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.kofa/trunk/src/waeup/kofa/students/batching.py

    r8736 r8884  
    2626import grok
    2727import csv
     28from time import time
    2829from zope.interface import Interface
    2930from zope.schema import getFields
     
    291292    grok.baseclass()
    292293
    293     #: required fields beside 'student_id', 'reg_number' and 'matric_number'
     294    # additional available  fields
     295    # beside 'student_id', 'reg_number' and 'matric_number'
    294296    additional_fields = []
    295297
    296     #: header fields additional required
     298    #: header fields additionally required
    297299    additional_headers = []
    298300
     
    558560    location_fields = []
    559561    additional_fields = ['p_id']
    560     additional_headers = ['p_id']
     562    additional_headers = []
     563
     564    def checkHeaders(self, headerfields, mode='ignore'):
     565        super(StudentOnlinePaymentProcessor, self).checkHeaders(headerfields)
     566        if mode == 'update' and not 'p_id' in headerfields:
     567            raise FatalCSVError(
     568                "Need p_id for import in update mode!")
     569        return True
    561570
    562571    def parentsExist(self, row, site):
     
    573582        if payments is None:
    574583            return None
     584        p_id = row.get('p_id', None)
     585        if p_id is None:
     586            return None
    575587        # We can use the hash symbol at the end of p_id in import files
    576588        # to avoid annoying automatic number transformation
    577589        # by Excel or Calc
    578         p_id = row['p_id'].strip('#')
    579         if p_id.startswith('p'):
    580             entry = payments.get(p_id)
    581         else:
    582             # For data migration from old SRP
    583             entry = payments.get('p' + p_id[6:])
     590        p_id = p_id.strip('#')
     591        if not p_id.startswith('p'):
     592            # For data migration from old SRP only
     593            p_id = 'p' + p_id[7:] + '0'
     594        entry = payments.get(p_id)
    584595        return entry
    585596
     
    600611        if not p_id.startswith('p'):
    601612            # For data migration from old SRP
    602             obj.p_id = 'p' + p_id[6:]
     613            obj.p_id = 'p' + p_id[7:] + '0'
    603614            parent[obj.p_id] = obj
    604615        else:
     
    613624
    614625        # We have to check p_id.
    615         p_id = row['p_id'].strip('#')
     626        p_id = row.get('p_id', None)
     627        if p_id is None:
     628            timestamp = ("%d" % int(time()*10000))[1:]
     629            p_id = "p%s" % timestamp
     630            conv_dict['p_id'] = p_id
     631            return errs, inv_errs, conv_dict
     632        else:
     633            p_id = p_id.strip('#')
    616634        if p_id.startswith('p'):
    617635            if not len(p_id) == 14:
  • main/waeup.kofa/trunk/src/waeup/kofa/students/tests/test_batching.py

    r8626 r8884  
    124124
    125125PAYMENT_HEADER_FIELDS = PAYMENT_SAMPLE_DATA.split(
     126    '\n')[0].split(',')
     127
     128PAYMENT_CREATE_SAMPLE_DATA = open(
     129    os.path.join(os.path.dirname(__file__), 'sample_create_payment_data.csv'),
     130    'rb').read()
     131
     132PAYMENT_CREATE_HEADER_FIELDS = PAYMENT_CREATE_SAMPLE_DATA.split(
    126133    '\n')[0].split(',')
    127134
     
    784791        self.student = self.app['students'][student.student_id]
    785792        payment = createObject(u'waeup.StudentOnlinePayment')
    786         payment.p_id = 'p123'
     793        payment.p_id = 'p120'
    787794        self.student['payments'][payment.p_id] = payment
    788795
     
    798805            self.workdir, 'sample_payment_data.csv')
    799806        open(self.csv_file, 'wb').write(PAYMENT_SAMPLE_DATA)
     807        self.csv_file2 = os.path.join(
     808            self.workdir, 'sample_create_payment_data.csv')
     809        open(self.csv_file2, 'wb').write(PAYMENT_CREATE_SAMPLE_DATA)
    800810
    801811    def test_interface(self):
     
    809819            dict(student_id='ID_NONE', p_id='nonsense'), self.app) is None
    810820        assert self.processor.getEntry(
    811             dict(student_id=self.student.student_id, p_id='p123'),
    812             self.app) is self.student['payments']['p123']
     821            dict(student_id=self.student.student_id, p_id='p120'),
     822            self.app) is self.student['payments']['p120']
    813823        assert self.processor.getEntry(
    814             dict(student_id=self.student.student_id, p_id='XXXXXX123'),
    815             self.app) is self.student['payments']['p123']
     824            dict(student_id=self.student.student_id, p_id='XXXXXX112'),
     825            self.app) is self.student['payments']['p120']
    816826
    817827    def test_addEntry(self):
     
    831841            self.app)
    832842        self.assertEqual(len(self.student['payments'].keys()),3)
    833         self.assertEqual(self.student['payments']['p456'].p_id, 'p456')
     843        self.assertEqual(self.student['payments']['p560'].p_id, 'p560')
    834844
    835845    def test_checkConversion(self):
    836846        errs, inv_errs, conv_dict = self.processor.checkConversion(
    837             dict(reg_number='1', p_id='3816951266236341955'))
     847            dict(p_id='3816951266236341955'))
    838848        self.assertEqual(len(errs),0)
    839849        errs, inv_errs, conv_dict = self.processor.checkConversion(
    840             dict(reg_number='1', p_id='p1266236341955'))
     850            dict(p_id='p1266236341955'))
    841851        self.assertEqual(len(errs),0)
    842852        errs, inv_errs, conv_dict = self.processor.checkConversion(
    843             dict(reg_number='1', p_id='nonsense'))
     853            dict(p_id='nonsense'))
    844854        self.assertEqual(len(errs),1)
    845         timestamp = "%d" % int(time()*1000)
     855        timestamp = ("%d" % int(time()*10000))[1:]
    846856        p_id = "p%s" % timestamp
    847857        errs, inv_errs, conv_dict = self.processor.checkConversion(
    848             dict(reg_number='1', p_id=p_id))
     858            dict(p_id=p_id))
    849859        self.assertEqual(len(errs),0)
    850860
     
    854864        self.assertEqual(num_warns,0)
    855865        payment = self.processor.getEntry(dict(reg_number='1',
    856             p_id='p1290797973744'), self.app)
    857         self.assertEqual(payment.p_id, 'p1290797973744')
     866            p_id='p2907979737440'), self.app)
     867        self.assertEqual(payment.p_id, 'p2907979737440')
    858868        cdate = payment.creation_date.strftime("%Y-%m-%d %H:%M:%S")
    859869        self.assertEqual(cdate, "2010-11-26 18:59:33")
     
    881891        self.assertEqual(num_warns,0)
    882892        shutil.rmtree(os.path.dirname(fin_file))
     893
     894    def test_import_wo_pid(self):
     895        num, num_warns, fin_file, fail_file = self.processor.doImport(
     896            self.csv_file2, PAYMENT_CREATE_HEADER_FIELDS,'create')
     897        self.assertEqual(num_warns,0)
     898        shutil.rmtree(os.path.dirname(fin_file))
     899        self.assertEqual(len(self.app['students']['X666666']['payments']), 50)
     900
    883901
    884902def test_suite():
Note: See TracChangeset for help on using the changeset viewer.