nus-mtp/cs-modify

View on GitHub
components/handlers/add_module_handler.py

Summary

Maintainability
A
0 mins
Test Coverage
'''
    This module contains the handlers for add module page
'''

from app import RENDER, set_template
import web
from components import model, session
from components.handlers.outcome import Outcome


class AddModule(object):
    '''
        This class handles displaying the add module form and it's posts
    '''
    def GET(self):
        '''
            render page with add module form
        '''
        web.header('X-Frame-Options', 'SAMEORIGIN')
        web.header('X-Content-Type-Options', 'nosniff')
        web.header('X-XSS-Protection', '1')
        if not session.validate_session():
            raise web.seeother('/login')

        return RENDER.addModules()


    def POST(self):
        '''
            Handles the submitting of the add module form
        '''
        web.header('X-Frame-Options', 'SAMEORIGIN')
        web.header('X-Content-Type-Options', 'nosniff')
        web.header('X-XSS-Protection', '1')
        #get module values from form
        try:
            data = web.input()
            module_code = data.code.upper()
            module_name = data.name
            module_desc = data.description
            module_mc = data.mc
            #string is valid
            if not (model.check_code(module_code) and model.check_name(module_name)
                    and model.check_mcs(module_mc)):
                return model.outcome_invalid()

            outcome = model.add_module(module_code, module_name, module_desc, module_mc, 'New')
            if outcome is True:
                set_template()   # Refresh list of modules in module search

            return Outcome().POST("add_module", outcome, module_code)

        except AttributeError:
            return Outcome().POST("add_module", False, None)