예제 #1
0
    def GET(self):
        '''
            Handles the loading of the 'Edit General Module Info' page
        '''
        input_data = model.validate_input(web.input(), ["code"])
        module_code = input_data.code

        module_info = model.get_module(module_code)
        if module_info is None:
            return RENDER.notfound("Module " + module_code + " does not exist in the system.")

        preclusions = model.get_preclusion_as_string(module_code)
        prerequisites = model.get_prerequisite_as_string(module_code)

        return RENDER.moduleEdit(module_info, preclusions, prerequisites)
    def POST(self):
        '''
            called from search with form
        '''
        # Input data will have valid inputs as function is called from submit button
        input_data = model.validate_input(web.input(), ["aysem"])
        try:
            ay_sem_of_interest = input_data.aysem
            lower_range_class_size = input_data.lowerClassSize
            higher_range_class_size = input_data.higherClassSize
        except AttributeError:
            error = RENDER.notfound(
                'Please do not tamper with our html forms. Thank you! ;)')
            raise web.notfound(error)

        webpage_to_redirect = "/moduleSpecificSize?aysem=" + ay_sem_of_interest + \
                              "&lowerClassSize=" + lower_range_class_size + \
                              "&higherClassSize=" + higher_range_class_size

        raise web.seeother(webpage_to_redirect)
예제 #3
0
def validate_input(input_data,
                   input_types,
                   is_future=False,
                   aysem_specific=True,
                   attr_required=True,
                   show_404=True):
    '''
        Validates that the GET input data (in the URL) is valid.

        input_types indicate the list of data types to validate.
        e.g. if GET input contains 'code' and 'aysem', then input_types = ["code", "aysem"]

        An input is considered valid if:
        1) The value was specified and
        2) The value exists in the system

        If any input is invalid, return 404 page (by default).

        Depending on the circumstances, optional arguments may be passed into the function:
        is_future:
            Set to True if want to ensure that AY-Sem is in a future AY
            (if AY-Sem is not required at all, then ignore this argument)
        aysem_specific:
            Set to True if the AY-Sem attribute is mandatory, False if it is optional
            (if AY-Sem is not required at all, then ignore this argument)
        attr_required:
            Set to True if at least one attribute is required
            Set to False if it is acceptable to have no input data
        show_404:
            Set to False if don't want to return 404 page (function will return False instead)
    '''
    from app import RENDER

    if attr_required is False and len(input_data) == 0:
        return input_data

    for input_type in input_types:
        if input_type == "code":
            try:
                module_code = input_data.code
            except AttributeError:
                if show_404:
                    error = RENDER.notfound('Module code is not specified')
                    raise web.notfound(error)
                else:
                    return False
            module_exist = database.is_existing_module(module_code.upper())
            if not module_exist:
                if show_404:
                    error = RENDER.notfound('Module code "' + module_code +\
                                            '" does not exist in our system')
                    raise web.notfound(error)
                else:
                    return False
            else:
                input_data.code = module_code.upper()

        elif input_type == "aysem":
            try:
                ay_sem = input_data.aysem
                if ay_sem == "":
                    raise AttributeError
            except AttributeError:
                if aysem_specific:
                    if show_404:
                        error = RENDER.notfound('AY-Semester is not specified')
                        raise web.notfound(error)
                    else:
                        return False
                else:
                    continue

            if is_future:
                valid_aysem = is_aysem_in_system_and_is_future(ay_sem)
            else:
                valid_aysem = is_aysem_in_system(ay_sem)

            if not valid_aysem:
                if is_future:
                    error = RENDER.notfound('AY-Semester "' + ay_sem +\
                                            '" does not exist in our system,' +\
                                            ' or is not in a future AY')
                else:
                    error = RENDER.notfound('AY-Semester "' + ay_sem +\
                                            '" does not exist in our system')
                if show_404:
                    raise web.notfound(error)
                else:
                    return False
            else:
                input_data.aysem = valid_aysem

        elif input_type == "modify_type" or input_type == "restore_type":
            try:
                if input_type == "modify_type":
                    info_type = input_data.modifyType
                else:
                    info_type = input_data.restoreType
            except AttributeError:
                if input_type == "modify_type":
                    error = RENDER.notfound('Modify type is not specified')
                else:
                    error = RENDER.notfound('Restore type is not specified')
                raise web.notfound(error)
            valid_info_type = info_type.upper() == "QUOTA" or \
                              info_type.upper() == "MOUNTING" or \
                              info_type.upper() == "MODULEDETAILS"
            if not valid_info_type:
                if input_type == "modify_type":
                    error = RENDER.notfound('Modify type "' + info_type +\
                                            '" is not recognised by the system')
                else:
                    error = RENDER.notfound('Restore type "' + info_type +\
                                            '" is not recognised by the system')
                raise web.notfound(error)

        elif input_type == "moduleAandB":
            try:
                moduleA_code = input_data.moduleA
                is_moduleA_specified = True
            except AttributeError:
                is_moduleA_specified = False
            try:
                moduleB_code = input_data.moduleB
                is_moduleB_specified = True
            except AttributeError:
                is_moduleB_specified = False

            if (is_moduleA_specified and not is_moduleB_specified) or \
               (not is_moduleA_specified and is_moduleB_specified):
                error = RENDER.notfound(
                    "1 out of 2 module codes is not specified")
                raise web.notfound(error)
            elif not is_moduleA_specified and not is_moduleB_specified:
                continue

            module_exist = database.is_existing_module(moduleA_code.upper())
            if not module_exist:
                error = RENDER.notfound('Module code "' + moduleA_code +\
                                        '" does not exist in our system')
                raise web.notfound(error)
            else:
                input_data.moduleA = moduleA_code.upper()
            module_exist = database.is_existing_module(moduleB_code.upper())
            if not module_exist:
                error = RENDER.notfound('Module code "' + moduleB_code +\
                                        '" does not exist in our system')
                raise web.notfound(error)
            else:
                input_data.moduleB = moduleB_code.upper()

    return input_data