#!/usr/bin/env python """ This is the class configcontroller of the controller module of the Eventure Portal project. The interface is as specified in the SDD document http://wilma.vub.ac.be/~se4_2006/documents/SDD/sdd.pdf. @author: Eline Philips @since: 09-05-2006 @date: 21-05-2006 """ __version__ = "@version: 0.3 " # Import the CGI module. import cgi import cgitb; cgitb.enable() # Imports. import os import sys sys.path.insert(1, os.path.split(os.getcwd())[0]) # Import of other python files. from config import configuration from controllers import emailclass from controllers import controller from controllers import objectfiller from controllers import viewcontainer from database import database from database import dataclass from errorhandlers import exceptions from models import transformer from views import exception from wepy import * # This class is derived from the class Controller class ConfigController(controller.Controller): """ Class configcontroller: this class handles all the configuration requests. This class is derived from the class Controller. """ def __init__(self): """ This method initialises the datamembers. The action table is a dictionary which contains (operation_name, action) pairs. The other datamembers are also initialised. """ self.setActionTable({"add_keyword" : self.addKeyword, "add_keyword_form" : self.addKeywordForm, "delete_keyword" : self.deleteKeyword, "get_all_pending_permissions" : self.getAllPendingPermissions, "delete_pending_permission" : self.deletePendingPermission, "get_config" : self.getConfiguration, "change_permission_form" : self.changePermissionForm, "change_permission" : self.changePermission, "change_config_form" : self.getChangeConfigForm, "set_config" : self.setConfiguration}) self.setDatabase(database.Database()) self.setTransformer(transformer.Transformer()) self.setCgi(cgi.FieldStorage()) self.setObjectFiller(objectfiller.ObjectFiller()) self.setEMail(emailclass.EMail()) self.setConfig(self.getDatabase().getConfig()) self.__container_ = viewcontainer.ViewContainer() # Check if dict has all keys. def hasKeys(self, dict, *keys): """ This method is the "has_key()" operation of the dictionary for multiple keys. @param dict: the dictionary @type dict: python dictionary datastructure @param keys: number of keys @type key: string @return: boolean true if the dictionary contains all the keys, false otherwise """ for key in keys: if not key in dict: return 0 return 1 # Transform form (cgi) in dictionary. # It is impossible to add extra fields to the form # (retrieved from cgi). This is possible for # dictionaries. def formToDict(self, form): """ This method transforms a form into a python dictionary. @param form: the form which must be transformed @type form: a form datastructure @return: a dictionary """ dict = {} for entry in form: dict[entry] = cgi.escape(form[entry].value, True) return dict # Perform selects the right operation from the action_table # and runs that operation. def perform(self): """ This method forms the hart of the controller. It retrieves the right action from the action table and performs it. """ # Initialise variable. dict = self.formToDict(self.getCgi()) # Check whether the operation is legal. if(self.hasKeys(dict, "operation")) : self.addToContainer(self.getConfig()) self.getActionTable().get(dict\ ["operation"], self.error)() else: if SESSION['username']: self.setContainerLogin("yes") self.setContainerLevel(SESSION['level']) self.addToContainer(exception.Exception("HOME", "HOME")) self.generatePage() # Error. def error(self): """ This method is called whenever something goes wrong in the perform method. """ self.addToContainer(exception.Exception("Error")) self.generatePage() # The get all pending permissions operation. def getAllPendingPermissions(self): """ This method performs the get all pending permissions action. This method is calles by the perform method, whenever the operation is "get_all_pending_permissions". """ # Initialise variables. form = self.getCgi(); dict = self.formToDict(form); database = self.getDatabase(); # Check whether user is logged in. if SESSION['username']: # Check whether user has the right permission. if SESSION['level'] == "administrator": pendinglist = database.getAllPendingPermissions() self.setContainerLogin("yes") self.setContainerLevel(SESSION['level']) self.addToContainer(pendinglist) self.generatePage() else: excep = exceptions.GeneralException("You don't have the\ permission to pending permissions of a user.") self.setContainerLogin("yes") self.setContainerLevel(SESSION['level']) self.addToContainer(exception.Exception(excep.value)) self.generatePage() else: excep = exceptions.NotLoggedInException("You can't see \ the pending permissions because you aren't logged in.") self.addToContainer(exception.Exception(excep.value)) self.generatePage() # The deletePendingPermission operation. def deletePendingPermission(self): """ This method performs the delete pending permission operation. This method is called by the perform method, whenever the operation is "delete_pending_permission". """ # Initialise variables. form = self.getCgi() dict = self.formToDict(form) database = self.getDatabase() # Check whether user is logged in. if SESSION['username']: # Check whether user has the right permission. if SESSION['level'] == "administrator": if self.hasKeys(dict, "username"): database.deletePendingPermission(dict["username"]) self.setContainerLogin("yes") self.setContainerLevel(SESSION['level']) self.addToContainer(exception.Exception("The permissions \ are deleted from the database.")) self.generatePage() else: excep = exceptions.RequiredFieldsException("Not all the \ required fields are filled in.") self.setContainerLogin("yes") self.setContainerLevel(SESSION['level']) self.addToContainer(exception.Exception(excep.value)) self.generatePage() else: excep = exceptions.GeneralException("You don't have the\ permission to delete pending permissions of a user.") self.setContainerLogin("yes") self.setContainerLevel(SESSION['level']) self.addToContainer(exception.Exception(excep.value)) self.generatePage() else: excep = exceptions.NotLoggedInException("You can't delete \ a pending permission because you aren't logged in.") self.addToContainer(exception.Exception(excep.value)) self.generatePage() # The delete keyword operation. def deleteKeyword(self): """ This method performs the delete keyword operation. This method is called by the perform method, whenever the operation is "delete_keyword". """ # Initialise variables. form = self.getCgi() dict = self.formToDict(form) database = self.getDatabase() # Check whether user is logged in. if SESSION['username']: # Check whether user has the right permission: if SESSION['level'] == "administrator": if self.hasKeys(dict, "name", "parent"): keyword = keyword.Keyword(dict["name"], dict["parent"]) database.deleteKeyword(keyword) self.setContainerLogin("yes") self.setContainerLevel(SESSION['level']) self.addToContainer(exception.Exception("Your keyword is\ deleted from the database.")) self.generatePage() else: excep = exceptions.RequiredFieldsException("Not all the \ required fields are filled in.") self.setContainerLogin("yes") self.setContainerLevel(SESSION['level']) self.addToContainer(exception.Exception(excep.value)) self.generatePage() else: excep = exceptions.GeneralException("You don't have the\ permission to delete keywords.") self.setContainerLogin("yes") self.setContainerLevel(SESSION['level']) self.addToContainer(exception.Exception(excep.value)) self.generatePage() else: excep = exceptions.NotLoggedInException("You can't delete \ keywords because you aren't logged in.") self.addToContainer(exception.Exception(excep.value)) self.generatePage() # The add keyword operation. def addKeyword(self): """ This method performs the add keyword operation. This method is called by the perform method, whenever the operation is "add_keyword". """ # Initialise variables. form = self.getCgi() dict = self.formToDict(form) database = self.getDatabase() # Check whether user is logged in. if SESSION['username']: # Check whether user has the right permission: if SESSION['level'] == "administrator": if self.hasKeys(dict, "name", "parent"): keyword = keyword.Keyword(dict["name"], dict["parent"]) database.addKeyword(keyword) self.setContainerLogin("yes") self.setContainerLevel(SESSION['level']) self.addToContainer(exception.Exception("Your keyword is\ added to the database.")) self.generatePage() else: excep = exceptions.RequiredFieldsException("Not all the \ required fields are filled in.") self.setContainerLogin("yes") self.setContainerLevel(SESSION['level']) self.addToContainer(exception.Exception(excep.value)) self.generatePage() else: excep = exceptions.GeneralException("You don't have the\ permission to add keywords.") self.setContainerLogin("yes") self.setContainerLevel(SESSION['level']) self.addToContainer(exception.Exception(excep.value)) self.generatePage() else: excep = exceptions.NotLoggedInException("You can't add \ keywords because you aren't logged in.") self.addToContainer(exception.Exception(excep.value)) self.generatePage() # The add keyword form operation. def addKeywordForm(self): """ This method performs the add keyword form operation. This method is called by the perform method, whenever the operation is "add_keyword_form". """ # Check whether user is logged in. if SESSION['username']: # Check whether user has the right permission: if SESSION['level'] == "administrator": self.setContainerEdit("yes") self.setContainerLogin("yes") self.setContainerLevel(SESSION['level']) self.setContainerOperation("keyword_form") self.addToContainer(exception.Exception("", "")) self.generatePage() else: excep = exceptions.GeneralException("You don't have the\ permission to add keywords.") self.setContainerLogin("yes") self.setContainerLevel(SESSION['level']) self.addToContainer(exception.Exception(excep.value)) self.generatePage() else: excep = exceptions.NotLoggedInException("You can't add \ keywords because you aren't logged in.") self.addToContainer(exception.Exception(excep.value)) self.generatePage() # The change permission form operation. def changePermissionForm(self): """ This method performs the change permission form operation. This method is called by the perform method, whenever the operation is "change_permission_form". """ # Check whether user is logged in. if SESSION['username']: # Check whether user has the right permission: if SESSION['level'] == "administrator": self.setContainerEdit("yes") self.setContainerLogin("yes") self.setContainerLevel(SESSION['level']) self.setContainerOperation("permission_form") self.addToContainer(exception.Exception("", "")) self.generatePage() else: excep = exceptions.GeneralException("You don't have the\ permission to change the permission of a user.") self.setContainerLogin("yes") self.setContainerLevel(SESSION['level']) self.addToContainer(exception.Exception(excep.value)) self.generatePage() else: excep = exceptions.NotLoggedInException("You can't the \ permission of a user because you aren't logged in.") self.addToContainer(exception.Exception(excep.value)) self.generatePage() # The change permission operation. def changePermission(self): """ This method performs the change permission operation. This method is called by the perform method, whenever the operation is "change_permission". """ # Initialise variables. form = self.getCgi() dict = self.formToDict(form) database = self.getDatabase() # Check whether user is logged in. if SESSION['username']: # Check whether user has the right permission: if SESSION['level'] == "administrator": # Check whether all the required fields are filled in. if self.hasKeys(dict, "username", "permission"): database.setPermission(dict["username"], dict["permission"]) self.setContainerLogin("yes") self.setContainerLevel(SESSION['level']) self.addToContainer(exception.Exception("The permission of \ the user is changed.")) self.generatePage() else: excep = exceptions.RequiredFieldsException("Not all the \ required fields are filled in.") self.setContainerLogin("yes") self.setContainerLevel(SESSION['level']) self.addToContainer(exception.Exception(excep.value)) self.generatePage() else: excep = exceptions.GeneralException("You don't have the\ permission to change the permission of a user.") self.setContainerLogin("yes") self.setContainerLevel(SESSION['level']) self.addToContainer(exception.Exception(excep.value)) self.generatePage() else: excep = exceptions.NotLoggedInException("You can't the \ permission of a user because you aren't logged in.") self.addToContainer(exception.Exception(excep.value)) self.generatePage() # The get config operation. def getConfiguration(self): """ This method performs the get config operation. This method is called by the perform method, whenever the operation is "get_config". """ # Initialise variables. config = self.getConfig() # Check whether user is logged in. if SESSION['username']: # Check whether user has the right permission. if SESSION['level'] == "administrator": self.setContainerLogin("yes") self.setContainerLevel(SESSION['level']) self.addToContainer(config) self.generatePage() else: excep = exceptions.GeneralException("You don't have the\ permission to see the configuration.") self.setContainerLogin("yes") self.setContainerLevel(SESSION['level']) self.addToContainer(exception.Exception(excep.value)) self.generatePage() else: excep = exceptions.NotLoggedInException("You can't see the \ configuration because you aren't logged in.") self.addToContainer(exception.Exception(excep.value)) self.generatePage() # The get change config form operation. def getChangeConfigForm(self): """ This method performs the get change config form operation.. This method is called by the perform method, whenever the operation is "change_config_form". """ # Initialise variables. config = self.getConfig() # Check whether user is logged in. if SESSION['username']: # Check whether user has the right permission. if SESSION['level'] == "administrator": self.setContainerLogin("yes") self.setContainerLevel(SESSION['level']) self.setContainerEdit("yes") self.setContainerOperation("config_form") self.addToContainer(config) self.generatePage() else: excep = exceptions.GeneralException("You don't have the\ permission to change the configuration.") self.setContainerLogin("yes") self.setContainerLevel(SESSION['level']) self.addToContainer(exception.Exception(excep.value)) self.generatePage() else: excep = exceptions.NotLoggedInException("You can't change the \ configuration because you aren't logged in.") self.addToContainer(exception.Exception(excep.value)) self.generatePage() # The set config operation. def setConfiguration(self): """ This method performs the get config operation. This method is called by the perform method, whenever the operation is "get_config". @except: RequiredFieldsException when not all the required fields are filled in. """ # Initialise variables. form = self.getCgi() dict = self.formToDict(form) filler = self.getObjectFiller() # Check whether user is logged in. if SESSION['username']: # Check whether user has the right permission. if SESSION['level'] == "administrator": try: config = self.getConfig() new_config = filler.setConfig(dict, config) database.setConfig(new_config) self.setContainerLogin("yes") self.setContainerLevel(SESSION['level']) self.addToContainer(exception.Exception("The configuration \ 's changed.")) self.generatePage() except exceptions.RequiredFieldsException, e: self.setContainerLogin("yes") self.setContainerLevel(SESSION['level']) self.addToContainer(exception.Exception(e.value)) self.generatePage() else: excep = exceptions.GeneralException("You don't have the\ permission to change the configuration.") self.setContainerLogin("yes") self.setContainerLevel(SESSION['level']) self.addToContainer(exception.Exception(excep.value)) self.generatePage() else: excep = exceptions.NotLoggedInException("You can't change the \ configuration because you aren't logged in.") self.addToContainer(exception.Exception(excep.value)) self.generatePage() # Initialise new configcontroller object # and call the perform action. config_controller = ConfigController() config_controller.perform()