def open_database(self, db_name, pathValues=[]): try: if (db_name in ['null', '']): raise ARMException('No database name defined') dbList = list(map(lambda db: db['database'], self.show_databases())) b = Borg() ses_settings = b.get_settings(self.session_id) dbUser = ses_settings['dbUser'] currentlyOpenDbName = ses_settings['dbName'] toOpenDbName = dbUser + '_' + canonicalDbName(db_name) if ((db_name not in dbList) and (toOpenDbName != currentlyOpenDbName)): raise ObjectNotFound(db_name + " does not exist") if (db_name == 'default'): b = Borg() ses_settings = b.get_settings(self.session_id) database_owner = ses_settings['dbUser'] else: database_owner = dbOwner(db_name) self.db_proxy.openDatabase(database_owner + '_' + db_name, self.session_id) return 'Database successfully opened' except ObjectNotFound as ex: self.close() raise ObjectNotFoundHTTPError(db_name + ' does not exist') except DatabaseProxyException as ex: raise ARMHTTPError(ex) except ARMException as ex: raise ARMHTTPError(ex)
def json_serialize(obj, pretty_printing=False, session_id=None): """ Serializes the Python object to a JSON serialized string. :param obj: The object to be serialized :type obj: object :param pretty_printing: Defines if the string needs to be pretty printed :type pretty_printing: bool :param session_id: The user's session ID :type session_id: int :return: Returns a JSON serialized string of the object """ b = Borg() if session_id is None: session_id = session.get('session_id', None) s = b.get_settings(session_id) if s is not None: pretty_printing = s.get('jsonPrettyPrint', False) if pretty_printing: json_string = dumps(loads(serialize(obj)), indent=4) else: json_string = serialize(obj) for key in conv_terms: json_string = json_string.replace(key, conv_terms[key]) return json_string
def get_fonts(session_id=None): """ Validates that the fonts to output the SVG models are properly set up :param session_id: The session ID provided by the user :return: The font name, font size and AP font name :rtype : str,str,str :raise CairisHTTPError: Raises a CairisHTTPError when the database could not be properly set up """ if session_id is not None: b = Borg() settings = b.get_settings(session_id) fontName = settings.get('fontName', None) fontSize = settings.get('fontSize', None) apFontName = settings.get('apFontSize', None) if fontName is None or fontSize is None or apFontName is None: raise CairisHTTPError( status_code=httplib.BAD_REQUEST, message='The method is not callable without setting up the project settings.' ) elif isinstance(fontName, str) and isinstance(fontSize, str) and isinstance(apFontName, str): return fontName, fontSize, apFontName else: raise CairisHTTPError( status_code=httplib.BAD_REQUEST, message='The database connection was not properly set up. Please try to reset the connection.' ) else: raise CairisHTTPError( status_code=httplib.BAD_REQUEST, message='The method is not callable without setting up the project settings.' )
def set_permission(self,db_name, user_id, permission, pathValues = []): if (existingAccount(user_id) == False): raise CairisHTTPError(status_code=http.client.NOT_FOUND,status="User not found",message=user_id + " was not found.") if (permission != 'grant' and permission != 'revoke'): raise CairisHTTPError(status_code=http.client.BAD_REQUEST,status="Invalid permission",message=permission + " is an invalid permission.") try: b = Borg() dbUser = b.get_settings(self.session_id)['dbUser'] if (isOwner(dbUser,db_name) == False): raise CairisHTTPError(status_code=http.client.BAD_REQUEST,status="Unauthorised permission",message="Cannot change permissions as you are not the database owner.") if (permission == 'grant'): grantDatabaseAccess(b.rPasswd, b.dbHost, b.dbPort, db_name, user_id) else: revokeDatabaseAccess(b.rPasswd, b.dbHost, b.dbPort, db_name, user_id) msg = 'Permission successfully ' if (permission == 'grant'): msg += 'granted' else: msg += 'revoked' return msg except ARMException as ex: self.close() raise ARMHTTPError(ex)
def get_fonts(session_id=None): if session_id is not None: b = Borg() settings = b.get_settings(session_id) fontName = settings.get('fontName', None) fontSize = settings.get('fontSize', None) apFontName = settings.get('apFontSize', None) if fontName is None or fontSize is None or apFontName is None: raise CairisHTTPError( status_code=BAD_REQUEST, message= 'The method is not callable without setting up the project settings.' ) elif isinstance(fontName, string_types) and isinstance( fontSize, string_types) and isinstance(apFontName, string_types): return fontName, fontSize, apFontName else: raise CairisHTTPError( status_code=BAD_REQUEST, message= 'The database connection was not properly set up. Please try to reset the connection.' ) else: raise CairisHTTPError( status_code=BAD_REQUEST, message= 'The method is not callable without setting up the project settings.' )
def json_serialize(obj, pretty_printing=False, session_id=None): """ Serializes the Python object to a JSON serialized string. :param obj: The object to be serialized :type obj: object :param pretty_printing: Defines if the string needs to be pretty printed :type pretty_printing: bool :param session_id: The user's session ID :type session_id: int :return: Returns a JSON serialized string of the object """ b = Borg() if session_id is None: session_id = session.get('session_id', None) s = b.get_settings(session_id) if s is not None: pretty_printing = s.get('jsonPrettyPrint', False) if pretty_printing: json_string = dumps(loads(serialize(obj)), indent=4) else: json_string = serialize(obj) for key in conv_terms: json_string = json_string.replace(key, conv_terms[key]) return json_string
def get_permissions(self,db_name, pathValues = []): try: b = Borg() dbUser = b.get_settings(self.session_id)['dbUser'] if (isOwner(dbUser,db_name) == False): raise CairisHTTPError(status_code=http.client.BAD_REQUEST,status="Unauthorised request",message="Not authorised to get permissions for " + db_name) return dbUsers(dbUser + '_' + canonicalDbName(db_name)) except ARMException as ex: self.close() raise ARMHTTPError(ex)
def clear_project(self): try: self.db_proxy.clearDatabase(session_id=self.session_id) b = Borg() ses_settings = b.get_settings(self.session_id) dbHost = ses_settings['dbHost'] dbPort = ses_settings['dbPort'] dbUser = ses_settings['dbUser'] dbPasswd = ses_settings['dbPasswd'] dbName = ses_settings['dbName'] createDefaults(b.cairisRoot,dbHost,dbPort,dbUser,dbPasswd,dbName) except DatabaseProxyException as ex: raise ARMHTTPError(ex) except ARMException as ex: raise ARMHTTPError(ex)
def clear_project(self, objt=None, pathValues=[]): try: self.db_proxy.clearDatabase(session_id=self.session_id) b = Borg() ses_settings = b.get_settings(self.session_id) dbHost = ses_settings['dbHost'] dbPort = ses_settings['dbPort'] dbUser = ses_settings['dbUser'] dbPasswd = ses_settings['dbPasswd'] dbName = ses_settings['dbName'] createDefaults(b.cairisRoot, dbHost, dbPort, dbUser, dbPasswd, dbName) return 'Project cleared successfully' except DatabaseProxyException as ex: raise ARMHTTPError(ex) except ARMException as ex: raise ARMHTTPError(ex)
def show_databases(self, pathValues=[]): try: b = Borg() ses_settings = b.get_settings(self.session_id) dbUser = ses_settings['dbUser'] rows = self.db_proxy.showDatabases(self.session_id) dbs = [] for db, owner in rows: permissioned = 'N' if (dbUser == owner): permissioned = 'Y' if (db == 'default'): permissioned = 'N' dbs.append({"database": db, "permissioned": permissioned}) return dbs except DatabaseProxyException as ex: raise ARMHTTPError(ex) except ARMException as ex: raise ARMHTTPError(ex)
def create_new_database(self,db_name): try: if (db_name in ['null','']): raise ARMException('No database name defined') if (db_name == 'default'): raise ARMException('Cannot create a new default database') b = Borg() ses_settings = b.get_settings(self.session_id) dbUser = ses_settings['dbUser'] dbName = dbUser + '_' + canonicalDbName(db_name) if (dbExists(dbName)): raise ARMException(db_name + " already exists") self.db_proxy.createDatabase(db_name,self.session_id) except DatabaseProxyException as ex: raise ARMHTTPError(ex) except ARMException as ex: raise ARMHTTPError(ex)
def get_fonts(session_id=None): """ Validates that the fonts to output the SVG models are properly set up :param session_id: The session ID provided by the user :return: The font name, font size and AP font name :rtype : str,str,str :raise CairisHTTPError: Raises a CairisHTTPError when the database could not be properly set up """ if session_id is not None: b = Borg() settings = b.get_settings(session_id) fontName = settings.get('fontName', None) fontSize = settings.get('fontSize', None) apFontName = settings.get('apFontSize', None) if fontName is None or fontSize is None or apFontName is None: raise CairisHTTPError( status_code=BAD_REQUEST, message= 'The method is not callable without setting up the project settings.' ) elif isinstance(fontName, string_types) and isinstance( fontSize, string_types) and isinstance(apFontName, string_types): return fontName, fontSize, apFontName else: raise CairisHTTPError( status_code=BAD_REQUEST, message= 'The database connection was not properly set up. Please try to reset the connection.' ) else: raise CairisHTTPError( status_code=BAD_REQUEST, message= 'The method is not callable without setting up the project settings.' )