Ignore:
Timestamp:
18 Apr 2012, 05:12:32 (13 years ago)
Author:
Henrik Bettermann
Message:

Empty If no value is provided in import files, attributes must not be cleared. Clear attribute only if value == DELETIONMARKER.

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

Legend:

Unmodified
Added
Removed
  • main/waeup.kofa/trunk/src/waeup/kofa/applicants/tests/sample_applicant_data.csv

    r7984 r8202  
    1 container_code,reg_number,firstname,lastname,date_of_birth,email,course1,sex
    2 dp2011,1001,Aaren,Pieri,1990-01-02,xx@yy.zz,CERT1,m
    3 dp2011,1002,Aaren,Finau,1990-01-03,xx@yy.zz,CERT1,m
    4 dp2011,1003,Aaren,Berson,1990-01-04,xx@yy.zz,CERT1,m
     1container_code,reg_number,firstname,lastname,date_of_birth,email,course1,sex,middlename
     2dp2011,1001,Aaren,Pieri,1990-01-02,xx@yy.zz,CERT1,m,Peter
     3dp2011,1002,Aaren,Finau,1990-01-03,xx@yy.zz,CERT1,m,Claus
     4dp2011,1003,Aaren,Berson,1990-01-04,xx@yy.zz,CERT1,m,Alfons
  • main/waeup.kofa/trunk/src/waeup/kofa/applicants/tests/sample_applicant_data_update.csv

    r7270 r8202  
    1 container_code,reg_number,firstname
    2 dp2011,1001,Aaren
    3 dp2011,1002,Alfons
    4 dp2011,1003,Abraham
     1container_code,reg_number,firstname,middlename
     2dp2011,1001,Aaren,
     3dp2011,1002,Alfons,
     4dp2011,1003,Abraham,
  • main/waeup.kofa/trunk/src/waeup/kofa/applicants/tests/sample_faulty_applicant_data.csv

    r7984 r8202  
    1 container_code,reg_number,firstname,lastname,date_of_birth,email,course1,sex
    2 dp2011,1001,Aaren,Pieri,01/02/1990,xx@yy.zz,CERT1,m
     1container_code,reg_number,firstname,lastname,date_of_birth,email,course1,sex,middlename
     2dp2011,1001,Aaren,Pieri,01/02/1990,xx@yy.zz,CERT1,m,Claus
  • main/waeup.kofa/trunk/src/waeup/kofa/applicants/tests/test_batching.py

    r8200 r8202  
    4848    '\n')[0].split(',')
    4949
    50 # The same for students
     50# The same for applicants
    5151APPLICANT_SAMPLE_DATA = open(
    5252    os.path.join(os.path.dirname(__file__), 'sample_applicant_data.csv'),
     
    5656                 'sample_faulty_applicant_data.csv'), 'rb').read()
    5757
    58 
    5958APPLICANT_HEADER_FIELDS = APPLICANT_SAMPLE_DATA.split(
    6059    '\n')[0].split(',')
     
    6362    os.path.join(os.path.dirname(__file__),
    6463                 'sample_applicant_data_update.csv'), 'rb').read()
     64
     65APPLICANT_SAMPLE_DATA_UPDATE2 = open(
     66    os.path.join(os.path.dirname(__file__),
     67                 'sample_applicant_data_update2.csv'), 'rb').read()
    6568
    6669APPLICANT_HEADER_FIELDS_UPDATE = APPLICANT_SAMPLE_DATA_UPDATE.split(
     
    231234        self.csv_file_update = os.path.join(
    232235            self.workdir, 'sample_applicant_data_update.csv')
     236        self.csv_file_update2 = os.path.join(
     237            self.workdir, 'sample_applicant_data_update2.csv')
    233238        open(self.csv_file, 'wb').write(APPLICANT_SAMPLE_DATA)
    234239        open(self.csv_file_faulty, 'wb').write(FAULTY_APPLICANT_SAMPLE_DATA)
    235240        open(self.csv_file_update, 'wb').write(APPLICANT_SAMPLE_DATA_UPDATE)
     241        open(self.csv_file_update2, 'wb').write(APPLICANT_SAMPLE_DATA_UPDATE2)
    236242
    237243    def test_interface(self):
     
    306312            self.csv_file_update, APPLICANT_HEADER_FIELDS_UPDATE, 'update')
    307313        self.assertEqual(num_warns,0)
     314        # The middlename import value was None.
     315        # Confirm that middlename has not been deleted.
     316        container = self.app['applicants']['dp2011']
     317        for key in container.keys():
     318            if container[key].lastname == 'Pieri':
     319                applicant = container[key]
     320                break
     321        self.assertEqual(applicant.middlename, 'Peter')
     322        shutil.rmtree(os.path.dirname(fin_file))
     323        # Now we import another file which clears all middlename attributes.
     324        num, num_warns, fin_file, fail_file = self.processor.doImport(
     325            self.csv_file_update2, APPLICANT_HEADER_FIELDS_UPDATE, 'update')
     326        self.assertEqual(num_warns,0)
     327        assert applicant.middlename is None
    308328        shutil.rmtree(os.path.dirname(fin_file))
    309329
  • main/waeup.kofa/trunk/src/waeup/kofa/interfaces.py

    r8176 r8202  
    3737_ = MessageFactory = zope.i18nmessageid.MessageFactory('waeup.kofa')
    3838
     39DELETIONMARKER = 'XXX'
     40
    3941CREATED = 'created'
    4042ADMITTED = 'admitted'
  • main/waeup.kofa/trunk/src/waeup/kofa/students/batching.py

    r8176 r8202  
    3434from waeup.kofa.interfaces import (
    3535    IBatchProcessor, FatalCSVError, IObjectConverter, IUserAccount,
    36     IObjectHistory, VALIDATED)
     36    IObjectHistory, VALIDATED, DELETIONMARKER)
    3737from waeup.kofa.interfaces import MessageFactory as _
    3838from waeup.kofa.students.interfaces import (
     
    168168                history.addMessage(msg)
    169169            elif hasattr(obj, key):
    170                 setattr(obj, key, value)
     170                # Set attribute to None if value is marked for deletion
     171                if value == DELETIONMARKER:
     172                    setattr(obj, key, None)
     173                elif value is not None:
     174                    setattr(obj, key, value)
    171175            items_changed += '%s=%s, ' % (key,value)
    172176        parent = self.getParent(row, site)
     
    296300            # Skip fields not declared in interface.
    297301            if hasattr(obj, key):
    298                 setattr(obj, key, value)
    299                 if key == 'certificate':
    300                     value = value.code
     302                # Set attribute to None if value is marked for deletion
     303                if value == DELETIONMARKER:
     304                    setattr(obj, key, None)
     305                elif value is not None:
     306                    setattr(obj, key, value)
     307                    if key == 'certificate':
     308                        value = value.code
    301309            items_changed += '%s=%s, ' % (key,value)
    302310        parent = self.getParent(row, site)
     
    687695            if hasattr(obj, key) and not key in [
    688696                'current_session','current_level']:
    689                 setattr(obj, key, value)
     697                # Set attribute to None if value is marked for deletion
     698                if value == DELETIONMARKER:
     699                    setattr(obj, key, None)
     700                elif value is not None:
     701                    setattr(obj, key, value)
    690702            items_changed += '%s=%s, ' % (key,value)
    691703        parent = self.getParent(row, site)
  • main/waeup.kofa/trunk/src/waeup/kofa/utils/batching.py

    r7950 r8202  
    3232from waeup.kofa.interfaces import (
    3333    IBatchProcessor, FatalCSVError, IObjectConverter,
    34     ICSVExporter)
     34    ICSVExporter, DELETIONMARKER)
    3535
    3636class BatchProcessor(grok.GlobalUtility):
     
    204204            # Skip fields not declared in interface.
    205205            if hasattr(obj, key):
    206                 setattr(obj, key, value)
     206                # Set attribute to None if value is marked for deletion
     207                if value == DELETIONMARKER:
     208                    setattr(obj, key, None)
     209                elif value is not None:
     210                    setattr(obj, key, value)
    207211        return
    208212
  • main/waeup.kofa/trunk/src/waeup/kofa/utils/tests/test_batching.py

    r7858 r8202  
    104104
    105105    def updateEntry(self, obj, row, site):
    106         # This is not strictly necessary, as the default
    107         # updateEntry method does exactly the same
    108106        for key, value in row.items():
    109             setattr(obj, key, value)
     107            if value is not None:
     108                setattr(obj, key, value)
    110109
    111110class BatchProcessorTests(FunctionalTestCase):
Note: See TracChangeset for help on using the changeset viewer.