Changeset 9362


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'.

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

Legend:

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

    r8924 r9362  
    2828## The Ids of new students to search for in old portal.
    2929NEW_IDS_CSV = "StudentIds.csv"
     30
     31## The ids of students that will will get a complete new number, not
     32## an updated one. Must contain a mapping of old ids to new ids.
     33## If no such update should be performed, please set the constant to None.
     34ID_MAP_CSV = None
     35ID_MAP_CSV = "id_mapping.csv"
    3036
    3137## Permissions to be set on new files/dirs. Set OWNER and/or GROUP to
     
    7985
    8086def copy_file(file_src, file_dst):
    81     create_path(os.path.dirname(dst))
     87    create_path(os.path.dirname(file_dst))
    8288    print "COPY FILE: %s -> %s" % (file_src, file_dst)
    8389    shutil.copyfile(file_src, file_dst)
     
    8591    return
    8692
     93def get_new_old_id_mapping():
     94    """Returns a dict mapping from _new_ ids to old (SRP) ids.
     95
     96    The dict is read from ID_MAP_CSV file. If this var is set to
     97    ``None`` an empty dict is returned. The ID_MAP_CSV contains only
     98    the student ids of those students, for which the standard method
     99    (new_id=CHAR+old_id) does not work.
     100    """
     101    if ID_MAP_CSV is None:
     102        return {}
     103    if not os.path.isfile(ID_MAP_CSV):
     104        raise IOError(
     105            "No such file for mapping new to old ids: %s" % ID_MAP_CSV)
     106    result = dict()
     107    reader = csv.DictReader(open(ID_MAP_CSV, 'rb'))
     108    for row in reader:
     109        result[row['new_id']] = row['student_id']
     110    return result
     111
     112# special ids not handled in common way
     113new_to_old_map = get_new_old_id_mapping()
    87114removable_dirs = []
    88115reader = csv.DictReader(open(NEW_IDS_CSV, 'rb'))
    89116for row in reader:
    90117    new_stud_id = row['student_id']
    91     stud_id = new_stud_id[1:]
     118    stud_id = new_to_old_map.get(new_stud_id[1:], new_stud_id[1:])
    92119    src_folder = os.path.join(SRC_DIR, stud_id[0], stud_id)
    93120    dst_folder = os.path.join(DST_DIR, new_folder_name(stud_id), new_stud_id)
  • 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.