Index: /main/waeup.kofa/trunk/src/waeup/kofa/students/student.py
===================================================================
--- /main/waeup.kofa/trunk/src/waeup/kofa/students/student.py	(revision 8451)
+++ /main/waeup.kofa/trunk/src/waeup/kofa/students/student.py	(revision 8452)
@@ -189,13 +189,31 @@
     Returns the name of folder in which files for a particular student
     should be stored. This is a relative path, relative to any general
-    students folder.
-
-    For instance ``K1000000`` will give ``01000/K1000000`` and
-    ``KM123456`` will result in ``00123/KM123456``.
+    students folder with 5 zero-padded digits (except when student_id
+    is overlong).
+
+    We normally map 1,000 different student ids into one single
+    path. For instance ``K1000000`` will give ``01000/K1000000``,
+    ``K1234567`` will give ``0123/K1234567`` and ``K12345678`` will
+    result in ``1234/K12345678``.
+
+    For lower numbers < 10**6 we return the same path for up to 10,000
+    student_ids. So for instance ``KM123456`` will result in
+    ``00120/KM123456`` (there will be no path starting with
+    ``00123``).
+
+    Works also with overlong number: here the leading zeros will be
+    missing but ``K123456789`` will give reliably
+    ``12345/K123456789`` as expected.
     """
     # remove all non numeric characters and turn this into an int.
     num = int(RE_STUDID_NON_NUM.sub('', student_id))
-    # store max. of 1000 studs per folder
-    folder_name = u'%05d' % (num / 1000)
+    if num < 10**6:
+        # store max. of 10000 studs per folder and correct num for 5 digits
+        num = num / 10000 * 10
+    else:
+        # store max. of 1000 studs per folder
+        num = num / 1000
+    # format folder name to have 5 zero-padded digits
+    folder_name = u'%05d' % num
     folder_name = os.path.join(folder_name, student_id)
     return folder_name
Index: /main/waeup.kofa/trunk/src/waeup/kofa/students/tests/test_student.py
===================================================================
--- /main/waeup.kofa/trunk/src/waeup/kofa/students/tests/test_student.py	(revision 8451)
+++ /main/waeup.kofa/trunk/src/waeup/kofa/students/tests/test_student.py	(revision 8452)
@@ -49,5 +49,16 @@
             path_from_studid('K1000000'), u'01000/K1000000')
         self.assertEqual(
-            path_from_studid('KM123456'), u'00123/KM123456')
+            path_from_studid('K1234567'), u'01234/K1234567')
+        self.assertEqual(
+            path_from_studid('K12345678'), u'12345/K12345678')
+        # The algorithm works also for overlong numbers, just to be
+        # sure.
+        self.assertEqual(
+            path_from_studid('K123456789'), u'123456/K123456789')
+        # low numbers (< 10**6) are treated special: they get max. of
+        # 10,000 entries. That's mainly because of old students
+        # migrated into our portal.
+        self.assertEqual(
+            path_from_studid('KM123456'), u'00120/KM123456')
         return
 
