def validate(self, clean=True): """ Override mongoengine validate. We should validate crontab entry. Use crontab_parser for crontab expressions. The parser is a general purpose one, useful for parsing hours, minutes and day_of_week expressions. example for minutes: minutes = crontab_parser(60).parse('*/15') [0, 15, 30, 45] """ if isinstance(self.schedule_type, Crontab): cronj_entry = self.schedule_type.as_dict() try: for k, v in cronj_entry.items(): if k == 'minute': celery.schedules.crontab_parser(60).parse(v) elif k == 'hour': celery.schedules.crontab_parser(24).parse(v) elif k == 'day_of_week': celery.schedules.crontab_parser(7).parse(v) elif k == 'day_of_month': celery.schedules.crontab_parser(31, 1).parse(v) elif k == 'month_of_year': celery.schedules.crontab_parser(12, 1).parse(v) else: raise me.ValidationError( 'You should provide valid period of time') except celery.schedules.ParseException: raise me.ValidationError('Crontab entry is not valid') except Exception as exc: raise me.ValidationError('Crontab entry is not valid:%s' % exc.message) super(Schedule, self).validate(clean=True)
def proper_feelings(param): """Raises a ValidationError if doesn't meet format for feelings.""" if type(param) != list: raise mongo.ValidationError("feelings is a list.") for item in param: if item not in FEELS: raise mongo.ValidationError("Improper feeling.")
def valid_required_blocks(param): """Raises a ValidationError if doesn't meet format for required_blocks.""" if type(param) != list: raise mongo.ValidationError("required_blocks is a list") else: for r in param: if type(r) != dict: raise mongo.ValidationError( "required_blocks is a list of dicts")
def clean(self): """Overriding the default clean method to implement param checking""" super(AAAARecord, self).clean() try: ip_addr = self.rdata[0] ip.ip_address(ip_addr) except ValueError: raise me.ValidationError('IPv6 address provided is not valid') if not len(self.rdata) == 1: raise me.ValidationError('We cannot have more than one rdata' 'values for this type of record.')
def valid_required_text(param): """Raises a ValidationError if doesn't meet format for required_text.""" if type(param) != list: raise mongo.ValidationError("required_text is a list") else: for r in param: if type(r) != list: raise mongo.ValidationError("required_text is a list of lists") for item in r: if type(item) != str: raise mongo.ValidationError( "required_text is a list of lists of strings")
def clean(self): # make sure that each team's name is unique used = set() for team in self.teams: if team.name in used: raise me.ValidationError("Team name exists.") used.add(team.name) # make sure that all team members are also org members for team in self.teams: for i, member in enumerate(list(team.members)): if member not in self.members: team.members.pop(i) # make sure that owners team is present try: owners = self.teams.get(name='Owners') except me.DoesNotExist: raise me.ValidationError("Owners team can't be removed.") # make sure that owners team is not empty if not owners.members: raise me.ValidationError("Owners team can't be empty.") if config.HAS_RBAC: # make sure owners policy allows all permissions if owners.policy.operator != 'ALLOW': owners.policy.operator = 'ALLOW' log.warning("Owners policy must be set to ALLOW. Updating...") # make sure owners policy doesn't contain specific rules if owners.policy.rules: raise me.ValidationError("Can't set policy rules for Owners.") # make sure org name is unique - we can't use the unique keyword on the # field definition because both User and Organization subclass Owner # but only Organization has a name if self.name and Organization.objects(name=self.name, id__ne=self.id): raise me.ValidationError("Organization with name '%s' " "already exists." % self.name) self.members_count = len(self.members) self.teams_count = len(self.teams) # Add schedule for metering. try: from mist.api.poller.models import MeteringPollingSchedule MeteringPollingSchedule.add(self, run_immediately=False) except Exception as exc: log.error('Error adding metering schedule for %s: %r', self, exc) super(Organization, self).clean()
def validate(self, clean=True): if self.tags: regex = re.compile(r'^[a-z0-9_-]+$') for key, value in self.tags.iteritems(): if not key: raise me.ValidationError('You cannot add a tag ' 'without a key') elif not regex.match(key) or (value and not regex.match(value)): raise me.ValidationError('Tags must be in key=value ' 'format and only contain the ' 'characters a-z, 0-9, _, -') super(TaggingCondition, self).validate(clean=True)
def clean(self): """Ensures that answer exist in question options and types are equal""" options = self.question.attribute.options if options: option = next((o for o in options if o.text == self.answer), None) if option is None: raise mongoengine.ValidationError("Option not availible") else: self.answer = option.value if not self.question.attribute.is_valid_answer(self.answer): raise mongoengine.ValidationError("Not valid answer type")
def clean(self): """Ensure RBAC Mappings are properly initialized.""" if config.HAS_RBAC: mappings = RBACMapping.objects(org=self._instance.id, team=self.id).only('id') if not mappings: self.init_mappings() elif self.name == 'Owners': raise me.ValidationError('RBAC Mappings are not intended for ' 'Team Owners') elif len(mappings) is not 2: raise me.ValidationError('RBAC Mappings have not been properly' ' initialized for Team %s' % self)
def clean(self): if self.json: try: json.loads(self.json) except json.decoder.JSONDecodeError as e: raise me.ValidationError("Invalid JSON payload: %s" % e.args[0]) if self.headers: try: json.loads(self.headers) except json.decoder.JSONDecodeError as e: raise me.ValidationError( "HTTP Headers should be defined as a valid " "JSON dictionary: %s" % e.args[0])
def validate(self, clean=True): for field in ['include', 'exclude']: if getattr(self, field): regex = re.compile(r'^[a-z0-9_-]+$') for key, value in getattr(self, field).items(): if not key: raise me.ValidationError('You cannot add a tag ' 'without a key') elif not regex.match(key) or (value and not regex.match(value)): raise me.ValidationError('Tags must be in key=value ' 'format and only contain the ' 'characters a-z, 0-9, _, -') super(TaggingSelector, self).validate(clean=True)
def valid_comparison_basis(param): """Raises a ValidationError if doesn't meet format for comparison_basis.""" if type(param) != dict: raise mongo.ValidationError("comparison_basis is a dict") else: bases = [ "__none__", "required_text", "required_block_categories", "required_blocks" ] if not ("basis" in param and param["basis"] in bases): raise mongo.ValidationError( "comparison_basis basis key is invalid") elif not ("priority" in param): raise mongo.ValidationError("comparison_basis priority key is invalid") # yapf: disable
def clean(self): """Checks the CIDR to determine if it maps to a valid IPv4 network.""" try: self.cidr = str(netaddr.IPNetwork(self.cidr)) except (TypeError, netaddr.AddrFormatError) as err: raise me.ValidationError(err)
def clean(self): """Checks the CIDR to determine if it maps to a valid IPv4 network.""" if self.cidr: try: netaddr.cidr_to_glob(self.cidr) except (TypeError, netaddr.AddrFormatError) as err: raise me.ValidationError(err)
def createNotification(for_user, from_user=None, notification_type="O", text=None): if not for_user: return False notification = Notification() notification.notification_type = notification_type[:1].upper() notification.text = text if not isinstance(for_user, User): try: for_user = User.objects.get(username=for_user) except User.DoesNotExist: raise me.ValidationError( 'Notification cannot be created if user is unknown') notification.for_user = for_user notification.from_user = from_user try: notification.save() return notification except: return None
def clean(self): """Ensures that self is a valid RSA keypair.""" from Crypto import Random Random.atfork() if 'RSA' not in self.private: raise me.ValidationError("Private key is not a valid RSA key.") # Generate public key from private key file. try: key = RSA.importKey(self.private) self.public = key.publickey().exportKey('OpenSSH') except Exception: log.exception("Error while constructing public key " "from private.") raise me.ValidationError("Private key is not a valid RSA key.")
def clean(self): """Overriding the default clean method to implement param checking""" super(CNAMERecord, self).clean() if not self.rdata[0].endswith('.'): self.rdata[0] += '.' if not len(self.rdata) == 1: raise me.ValidationError('We cannot have more than one rdata' 'values for this type of record.')
def update(self, fail_on_error=True, **kwargs): for key, value in kwargs.iteritems(): if key not in type(self)._fields: if not fail_on_error: continue raise me.ValidationError('Field "%s" does not exist on %s', key, type(self)) setattr(self, key, value)
def clean(self): """Custom validation for GCE Networks. GCE enforces: - Regex constrains on network names. - CIDR assignment only if `legacy` mode has been selected. """ if self.mode == 'legacy': super(GoogleNetwork, self).clean() elif self.cidr is not None: raise me.ValidationError('CIDR cannot be set for modes other than ' '"legacy" - Current mode: %s' % self.mode) if not re.match('^(?:[a-z](?:[-a-z0-9]{0,61}[a-z0-9])?)$', self.name): raise me.ValidationError('A **lowercase** name must be specified')
def clean(self): if self.offset: super(TriggerOffset, self).clean() q, r = divmod(self.timedelta.total_seconds(), self._instance.frequency.timedelta.total_seconds()) if not q or r: raise me.ValidationError("The trigger offset must be a" " multiple of the rule's frequency")
def clean(self): # Make sure the script name does not contain any weird characters. if not re.match('^[\w]+$', self.name): raise me.ValidationError('Alphanumeric characters and underscores ' 'are only allowed in custom script names') # Custom scripts should be provided inline (for now). if not isinstance(self.location, InlineLocation): raise me.ValidationError('Only inline scripts supported for now') # Make sure shebang is present. if not self.location.source_code.startswith('#!'): raise me.ValidationError('Missing shebang') # Check metric type. if self.extra.get('value_type', 'gauge') not in ('gauge', 'derive'): raise me.ValidationError('value_type must be "gauge" or "derive"')
def clean(self): """Ensures that only published essays have a `pub_date` and automatically sets the pub_date if published and not set""" if self.status == "Draft" and self.pub_date is not None: msg = "Draft entries should not have a publication date." raise mongoengine.ValidationError(msg) # Set the pub_date for published items if not set. if self.status == "Published" and self.pub_date is None: self.pub_date = datetime.now()
def clean(self): # make sure user.email is unique - we can't use the unique keyword on # the field definition because both User and Organization subclass # Owner but only user has an email field if User.objects(email=self.email, id__ne=self.id): raise me.ValidationError("User with email '%s' already exists." % self.email) super(User, self).clean()
def clean(self): # FIXME This is needed in order to ensure rule name convention remains # backwards compatible with the old monitoring stack. However, it will # have to change in the future due to uniqueness constrains. if not self.title: self.title = 'rule%d' % self.owner.rule_counter # FIXME Ensure a single query condition is specified for backwards # compatibility with the existing monitoring/alert stack. if not len(self.queries) is 1: raise me.ValidationError()
def validate(self, value): if isinstance(value, datetime.timedelta): pass elif isinstance(value, int) or isinstance(value, float): pass elif isinstance(value, str): # Attempt to convert it from a string self.from_str(value) else: raise mongoengine.ValidationError( "type {} can't be converted to timedelta".format(type(value)))
def clean(self): if not (self.email or self.user_id): raise me.ValidationError('Neither a user ID nor email provided') # Get the user's id, if missing. Some notification policies may # belong to non-mist users (denoted by their e-mail). if not self.user_id: user = self.user self.user_id = user.id if user else None elif not self.email: self.email = self.user.email
def test_set_repo_scratchpad_non_dict(self, mock_repo_qs): """ Test setting an invalid scratchpad. """ scratchpad = 'not a dict' mock_repo = mock_repo_qs.get_repo_or_missing_resource.return_value mock_repo.save.side_effect = mongoengine.ValidationError() self.assertRaises(mixins.ImporterConduitException, self.mixin.set_repo_scratchpad, scratchpad) mock_repo_qs.get_repo_or_missing_resource.assert_called_once_with( self.repo_id) mock_repo.save.assert_called_once_with()
def clean(self): """Ensures that self is a valid RSA keypair.""" from Crypto import Random Random.atfork() # Generate public key from private key file. try: key = RSAKey.from_private_key(io.StringIO(self.private)) self.public = 'ssh-rsa ' + key.get_base64() except Exception: log.exception("Error while constructing public key " "from private.") raise me.ValidationError("Private key is not a valid RSA key.")
def clean(self): """ Invalidates insert of overlapping territory. Calculates derived attributes. """ if self.overlapping_territories(): raise me.ValidationError('Overlapping territory') self.area = self.calculate_area() self.painted_area = self.calculate_painted_area() if len(self.squares) == 0: self.squares = sq.Square.generate_squares(self.start, self.end)
def add_release(activity, release): # first release if not activity.latest_release and len(activity.previous_releases) == 0: activity.latest_release = release return if activity.latest_release.activity_version >= release.activity_version: Release.delete(release) raise me.ValidationError( 'New activity release version {} is less or equal than the ' 'current version {}'.format( release.activity_version, activity.latest_release.activity_version)) activity.previous_releases.append(activity.latest_release) activity.latest_release = release