def check_action(self): '''Verify that the action_id is a valid id of the ir.actions.actions model or a valid xml_id. Validates that the action_dict is correctly evaluated as a dictionary in python code. ''' if not (self.action_id or self.action_dict): raise ValidationError(_('Action id or Action dict must be define.')) if self.action_id: try: action = self.env['ir.actions.actions'].browse(int(self.action_id)) if not action.exists(): raise ValidationError('There is no action created with this action_id') except ValueError: try: # Return the record corresponding to the given ``xml_id`` # in this case the xml_id is ``action_id`` action = self.env.ref(self.action_id) action._name.startswith('ir.actions.') except Exception as e: raise ValidationError( 'There is no action created with ' 'this xml_id, remember the structure to invoke ' 'it is addons.xml_id' ) if self.action_dict: try: evaluate(self.action_dict or 'result={}', mode='exec', env=self.env) except Exception as e: raise ValidationError(e.message)
def _validate_margins(self): for record in self: required_margin = record.required_margin max_margin = record.max_margin if bool(required_margin) != bool(max_margin): # Either both are set or both are unset raise ValidationError('You must set both required_margin ' 'and max_margin or none.') if required_margin < 0 or required_margin > 100: raise_validation_error('required minimum margin') if max_margin < 0 or max_margin > 100: raise_validation_error('maximum allowed margin') if required_margin and max_margin and \ required_margin >= max_margin: raise ValidationError('required margin must be less that ' 'max margin.') parent = record.parent_id if parent and parent.id: if parent.required_margin and required_margin and \ required_margin < parent.required_margin: raise ValidationError( 'You cannot lower the required margin') if parent.max_margin and max_margin and \ max_margin > parent.max_margin: raise ValidationError( 'You cannot raise the maximum margin')
def _validate_commission_margins(self): for record in self: min_comm = record.min_commission_margin max_comm = record.max_commission_margin if bool(min_comm) != bool(max_comm): # Either both are set or both are unset raise ValidationError('You must set both min commission ' 'and max commission or none.') if min_comm < 0 or min_comm > 100: raise_validation_error('minimum commission margin') if max_comm < 0 or max_comm > 100: raise_validation_error('maximum commission margin') if min_comm and max_comm and min_comm >= max_comm: raise ValidationError('min commission must be less that ' 'max commission.')
def _do_post_with_celery(self): # Do the real work inside an iterator so that we can use the # until_timeout. def _validate(): report_progress( _("Posting the entries. This may take several " "minutes depending on the length of the entries."), valuemin=0, valuemax=len(self) ) records = self.sorted(lambda invoice: len(get_lines(invoice))) for progress, record in enumerate(records): report_progress(progress=progress) with self.env.cr.savepoint(): # Since the soft time-limit may occur after the first # invoice was validate, but before the second finishes, we # must ensure all DB changes for each invoice is # 'atomically' done or not done at all. perform_post(record) yield record def count(iterable): result = 0 for _x in iterable: # noqa: F402 result += 1 return result res = count(until_timeout(_validate())) if not res: raise ValidationError(_("No entry could be posted")) else: return CLOSE_PROGRESS_BAR
def _check_signals(self): '''Check that there is at least some signal. ''' if not all(h.event_raise or h.continue_raising or h.stop_raising for h in self): raise ValidationError(_('At least one signal must be checked.'))
def check_name(self): '''Check name of a CDRIdentifier ''' if not check_identifier(self.name): raise ValidationError( _("Name must be a available python identifier"))
def _check_recipients(self): '''Restrict the use of at least one recipient.These recipients are the result of the defined domain. ''' if not all(handler.recipients for handler in self): raise ValidationError( _('At least one recipient is necessary.'))
def send_mail(self, **kwargs): # Odoo 10 introduced auto_commit '''Validate no readonly token was modified and remove added style. ''' self.ensure_one() if self._validate_template_restrictions(): return super(MailComposeMessage, self).send_mail(**kwargs) else: raise ValidationError(_('Non-editable items were modified.'))
def _check_strategy_ids(self): '''Validate that when no group field is defined, only one strategy can be selected. ''' for model in self: if not model.group_field and len(model.strategy_ids) > 1: raise ValidationError( _('When no group field is defined only one strategy ' 'must be selected.')) return True
def _check_other_fields(self): ''' Validates that the field names defined in the 'other_field' dictionary are among the names of the fields defined for the selected strategy. And that the defined fields are in some existing model. ''' for model in self: fields_name = model.strategy_ids.get_fields_name() if fields_name: other_fields = safe_eval(model.other_fields or '{}') if not other_fields or not all( other_fields.get(f, False) for f in fields_name): raise ValidationError( _('Other fields must define all fields used on ' 'selected strategies.')) obj = self.env[model.model.model] for field in other_fields.itervalues(): if not obj or not obj._fields.get(field, False): raise ValidationError( _('Some Other fields defined not exist on ' 'destination model')) return True
def str2dict(maybedict, field_name=None, default=Unset): from collections import Mapping from xoutil.eight import string_types try: if isinstance(maybedict, Mapping): return dict(maybedict) elif isinstance(maybedict, string_types): return dict(safe_eval(maybedict)) elif default is not Unset: return default else: raise TypeError except (ValueError, TypeError): raise ValidationError( _("The format's field '%s' is incorrect because not have " "structure dictionary. " "Example: {'field1': value1, 'field2': value2}") % field_name)
def raise_validation_error(*fields): raise ValidationError('Invalid value for field(s) %r', fields)
def _check_for_one_occurrency_change(self): 'Ensure only real ids.' real_ids = {self._virtual_id2real(x) for x in self.ids} if real_ids != set(self.ids): raise ValidationError('Expected only real ids')
def _required_field(field='A field'): raise ValidationError(field + _(' is required.'))
def _check_byeaster_no_longer_than_year(self): for record in self: if record.is_easterly: if not (-365 <= record.byeaster <= 365): raise ValidationError( 'By Easter must not extend longer than a year')
def _check_monthly_day_no_longer_than_month(self): for record in self: if not (-31 <= record.monthly_day <= 31): raise ValidationError( 'You must provide a valid day of the month')