Ignore:
Timestamp:
19 Oct 2012, 22:59:41 (12 years ago)
Author:
uli
Message:

Update tools to allow new ids in imports. Support for new import col 'old_id'.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.kofa/trunk/tools/fix_import_file.py

    r9270 r9362  
    3636import csv
    3737import datetime
     38import os
    3839import re
    3940import sys
     
    159160    'session_id': 'p_session',
    160161    'type': 'r_company',
     162    'old_id': 'old_id',
    161163    }
    162164
     
    177179    }
    178180
     181# Mapping of special cases, where new id is not deductible from old id
     182# Set to `None`, if no such special cases should be considered.
     183ID_MAP_CSV = None
     184ID_MAP_CSV = "id_mapping.csv"
     185
    179186##
    180187## END OF CONFIG
     
    183190# Look for the first sequence of numbers
    184191RE_PHONE = re.compile('[^\d]*(\d*)[^\d]*')
     192
     193def get_id_mapping():
     194    """Returns a dict mapping from old (SRP) ids to new ids.
     195
     196    The dict is read from ID_MAP_CSV file. If this var is set to
     197    ``None`` an empty dict is returned. The ID_MAP_CSV contains only
     198    the student ids of those students, for which the standard method
     199    (new_id=CHAR+old_id) does not work.
     200    """
     201    if ID_MAP_CSV is None:
     202        return {}
     203    if not os.path.isfile(ID_MAP_CSV):
     204        raise IOError(
     205            "No such file for mapping old to new ids: %s" % ID_MAP_CSV)
     206    result = dict()
     207    reader = csv.DictReader(open(ID_MAP_CSV, 'rb'))
     208    for row in reader:
     209        result[row['student_id']] = row['new_id']
     210    return result
     211
    185212
    186213def convert_fieldnames(fieldnames):
     
    201228    """
    202229
    203     @classmethod
    204     def student_id(self, value, row):
     230    old_new_id_map = get_id_mapping()
     231
     232    @classmethod
     233    def student_id(cls, value, row):
    205234        """ 'A123456' --> 'EA123456'
    206235        """
     236        value = cls.old_new_id_map.get(value, value)
    207237        if len(value) == 7:
    208238            return 'M' + value
Note: See TracChangeset for help on using the changeset viewer.