Ignore:
Timestamp:
14 May 2012, 14:29:02 (13 years ago)
Author:
uli
Message:

Distribute student media files over folders with chronological names, max. 1000 studs per folder.

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

Legend:

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

    r8411 r8448  
    2020"""
    2121import os
     22import re
    2223import shutil
    2324import grok
     
    4041from waeup.kofa.students.utils import generate_student_id
    4142from waeup.kofa.utils.helpers import attrs_to_fields, now, copy_filesystem_tree
     43
     44RE_STUDID_NON_NUM = re.compile('[^\d]+')
    4245
    4346class Student(grok.Container):
     
    179182    return
    180183
     184def path_from_studid(student_id):
     185    """Convert a student_id into a predictable relative folder path.
     186
     187    Used for storing files.
     188
     189    Returns the name of folder in which files for a particular student
     190    should be stored. This is a relative path, relative to any general
     191    students folder.
     192
     193    For instance ``K1000000`` will give ``01000/K1000000`` and
     194    ``KM123456`` will result in ``00123/KM123456``.
     195    """
     196    # remove all non numeric characters and turn this into an int.
     197    num = int(RE_STUDID_NON_NUM.sub('', student_id))
     198    # store max. of 1000 studs per folder
     199    folder_name = u'%05d' % (num / 1000)
     200    folder_name = os.path.join(folder_name, student_id)
     201    return folder_name
     202
    181203def move_student_files(student, del_dir):
    182204    """Move files belonging to `student` to `del_dir`.
     
    192214
    193215    src = getUtility(IExtFileStore).root
    194     src = os.path.join(src, 'students', stud_id[0], stud_id)
    195 
    196     dst = os.path.join(del_dir, 'media', 'students', stud_id[0], stud_id)
     216    src = os.path.join(src, 'students', path_from_studid(stud_id))
     217
     218    dst = os.path.join(
     219        del_dir, 'media', 'students', path_from_studid(stud_id))
    197220
    198221    if not os.path.isdir(src):
     
    336359        basename, ext = os.path.splitext(attr)
    337360        stud_id = self.context.student_id
    338         marked_filename = '__%s__%s/%s/%s_%s%s' % (
    339             STUDENT_FILE_STORE_NAME, stud_id[0], stud_id, basename,
     361        marked_filename = '__%s__%s/%s_%s%s' % (
     362            STUDENT_FILE_STORE_NAME, path_from_studid(stud_id), basename,
    340363            stud_id, ext)
    341364        return marked_filename
  • main/waeup.kofa/trunk/src/waeup/kofa/students/tests/test_student.py

    r8403 r8448  
    2020import os
    2121import re
     22import unittest
    2223from cStringIO import StringIO
    2324from datetime import tzinfo
     
    2829from waeup.kofa.students.export import EXPORTER_NAMES
    2930from waeup.kofa.students.student import (
    30     Student, StudentFactory, handle_student_removed)
     31    Student, StudentFactory, handle_student_removed, path_from_studid)
    3132from waeup.kofa.students.studycourse import StudentStudyCourse
    3233from waeup.kofa.students.studylevel import StudentStudyLevel, CourseTicket
     
    3940from waeup.kofa.testing import FunctionalLayer, FunctionalTestCase
    4041from waeup.kofa.university.department import Department
     42
     43class HelperTests(unittest.TestCase):
     44    # Tests for helper functions in student module.
     45
     46    def test_path_from_studid(self):
     47        # make sure we get predictable paths from student ids.
     48        self.assertEqual(
     49            path_from_studid('K1000000'), u'01000/K1000000')
     50        self.assertEqual(
     51            path_from_studid('KM123456'), u'00123/KM123456')
     52        return
    4153
    4254class StudentTest(FunctionalTestCase):
     
    121133        del_dir = self.app['datacenter'].deleted_path
    122134        del_img_path = os.path.join(
    123             del_dir, 'media', 'students', 'A', 'A111111',
     135            del_dir, 'media', 'students', '00111', 'A111111',
    124136            'passport_A111111.jpg')
    125137
Note: See TracChangeset for help on using the changeset viewer.