Ignore:
Timestamp:
6 Dec 2012, 11:23:03 (12 years ago)
Author:
uli
Message:

Lists of levels are computed at several places, everywhere following
the same algorithm. I think it's good to have this algorithm in
exactly one place.

File:
1 edited

Legend:

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

    r9324 r9776  
    3636nats_vocab = SimpleKofaVocabulary(*COUNTRIES)
    3737
     38def levels_from_range(start_level=None, end_level=None):
     39    """Get a list of valid levels for given start/end level.
     40
     41    Start and end level must be numbers (or ``None``).
     42
     43    If no start/end level is given, return the default list of levels.
     44
     45    If only one of start_level or end_level is given, the result is
     46    undefined.
     47    """
     48    if 999 in (start_level, end_level):
     49        return [999]
     50    levels = [10,] + [
     51        level for level in range(100,1000,10) if level % 100 < 30]
     52    if start_level is None and end_level is None:
     53        return levels + [999]
     54    if start_level == 10 == end_level:
     55        return [10,]
     56    start_level, end_level = (start_level or 10, end_level or 900)
     57    levels = [x for x in levels
     58              if x >= start_level and x <= (end_level + 120)]
     59    return levels
     60
    3861def study_levels(context):
    3962    certificate = getattr(context, 'certificate', None)
     63    start_level, end_level = (None, None)
    4064    if  certificate is not None:
    4165        start_level = int(certificate.start_level)
    4266        end_level = int(certificate.end_level)
    43         if start_level == 999 or end_level == 999:
    44             levels = [999]
    45         elif start_level == 10 and end_level == 10:
    46             levels = [10,]
    47         elif start_level == 10:
    48             levels = [10,] + [level for level in range(100,end_level+200,10)
    49                 if level % 100 < 30]
    50         else:
    51             levels = [level for level in range(start_level,end_level+200,10)
    52                 if level % 100 < 30]
    53     else:
    54         # default level range
    55         levels = [10,]
    56         levels += [level for level in range(100,1000,10) if level % 100 < 30]
    57         levels.append(999)
    58     return levels
    59 
     67    return levels_from_range(start_level, end_level)
    6068
    6169class StudyLevelSource(BasicContextualSourceFactory):
Note: See TracChangeset for help on using the changeset viewer.