def add_object(self, **kwargs): """ Creates a new policy object. """ try: if kwargs['object_id'] is None or not kwargs['object_id'].isdigit(): raise TypeError('Policy ID is either None or ID is not int') if kwargs ['object_content'] is None: raise TypeError('Policy content None') except KeyError as e: return 'Key %s does not exist' % e except TypeError as e: return '<p>An error occured</p><p><b>Errorinfo:</b><br/>%s' % e policy_id = kwargs['object_id'] try: policy_object = Session.query(PolicyObject).filter(PolicyObject.id == policy_id).one() policy_object.contents = escape_string(kwargs['object_content']) Session.merge(policy_object) Session.flush() except NoResultFound: policy_object = PolicyObject(contents = escape_string(kwargs['object_content'])) Session.add(policy_object) Session.flush() finally: raise cherrypy.HTTPRedirect("/policy/")
def save_sensor_data(self, **kwargs): """ Get a sensors data provided bu a form, and either change it or add new. """ try: """ Retrieves information from web request. """ addName = escape_string(kwargs['sensor_name']) addIp = escape_string(kwargs['sensor_ip']) addLocation = kwargs['sensor_location'] addDescription = escape_string(kwargs['sensor_description']) sensor_id = kwargs['sensor_id'] except KeyError as e: print 'There was a key error %s' % (e) try: """ In case sensor does not excist. """ if sensor_id == 0: raise NoResultFound sensor = Session.query(Sensor).filter(Sensor.id == sensor_id).one() sensor.name = addName sensor.ip = addIp sensor.description = addDescription Session.merge(sensor) except NoResultFound: # Sensor does not exsist, add new. new_sensor = Sensor(addName, addIp, addLocation, addDescription) Session.add(new_sensor) finally: Session.flush() Session.flush()
def add_policy(self, name='', description=''): """ Creates a new policy based on user input. Known bug: Doesn't check exsitence of policy. """ try: self.check_status() if len(name) > 0 and len(description) > 0: new_policy = PolicyChainMeta(escape_string(name.capitalize()), escape_string(description)) Session.add(new_policy) Session.flush() except SystemLockedException: pass finally: raise cherrypy.HTTPRedirect("/policy/") # Return to index
def save_object(self, **kwargs): """ Add the contents of a policy object to the database. """ print '\n\n Save object \n\n' try: content = escape_string(kwargs['object-content']) object_id = int(kwargs['object-id']) type = kwargs['object-type'] item_id = kwargs['policy-id'] if object_id == 0: policy_item = Session.query(PolicyChain).filter( PolicyChain.id == item_id).one() policy_object = PolicyObject(type, content) Session.add(policy_object) Session.flush() policy_item.policyobject_id = policy_object.id else: policy_object= Session.query(PolicyObject).filter(PolicyObject.id == object_id).one() policy_object.contents = content policy_object.type = type Session.merge(policy_object) Session.flush() except KeyError as e: print e except NoResultFound as e: #policy_object = PolicyObject(type, content) #Session.add(policy_object) #Session.flush() print 'no result found' pass
def add_location(self, **kwargs): """ Adds a new location to the database """ try: name = escape_string(kwargs['location_name']) if len(name) == 0: raise TypeError('The location name is empty!') new_location = SensorLocation(name) Session.add(new_location) Session.flush() print new_location return simplejson.dumps(new_location.id) except KeyError as e: print 'KEYERROR; this key does not excist: %s' % s except TypeError as e: print e