class OnlineScoreRequest(ApiRequest): first_name = CharField(required=False, nullable=True) last_name = CharField(required=False, nullable=True) email = EmailField(required=False, nullable=True) phone = PhoneField(required=False, nullable=True) birthday = BirthDayField(required=False, nullable=True) gender = GenderField(required=False, nullable=True) def validate(self, kwargs): super().validate(kwargs) validate_pairs = any([ self.phone and self.email, self.first_name and self.last_name, self.gender is not None and self.birthday ]) if not validate_pairs: logging.error( 'At least one pair expected from the (first_name - last_name, email - phone, birthday - gender)' ) raise ValidationError("Fields validate error")
class OnlineScoreRequest(Request): first_name = CharField(required=False, nullable=True) last_name = CharField(required=False, nullable=True) email = EmailField(required=False, nullable=True) phone = PhoneField(required=False, nullable=True) birthday = BirthDayField(required=False, nullable=True) gender = GenderField(required=False, nullable=True) def __init__(self, body): super(OnlineScoreRequest, self).__init__(body) valid = any( ((self.first_name is not None and self.last_name is not None), (self.email is not None and self.phone is not None), (self.birthday is not None and self.gender is not None))) if not valid: self._errors.append('invalid arguments set')
class OnlineScoreRequest(ApiRequest): first_name = CharField(required=False, nullable=True) last_name = CharField(required=False, nullable=True) email = EmailField(required=False, nullable=True) phone = PhoneField(required=False, nullable=True) birthday = BirthDayField(required=False, nullable=True) gender = GenderField(required=False, nullable=True) def validate(self): validating_pairs = [ ('first_name', 'last_name'), ('email', 'phone'), ('birthday', 'gender'), ] if ('first_name' in self.has and 'last_name' in self.has) or \ ('email' in self.has and 'phone' in self.has) or \ ('birthday' in self.has and 'gender' in self.has): return True else: raise AttributeError( "Required at least one of this fields pars: ('first_name', 'last_name'), " "('email', 'phone'), ('birthday', 'gender')")
class Person(object): name = NameField() birthday = BirthdayField() phone = PhoneField()
class MyUser(User): phone = PhoneField()
def test_phonefield_not_valid(required, nullable, value, ex_mes): field = PhoneField(required, nullable) with pytest.raises(ValidationError) as e: field.validate(value) assert str(e.value) == ex_mes
def test_phonesfield_valid(required, nullable, value): field = PhoneField(required, nullable) assert not field.validate(value)