2018-04-27 03:27:16 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
from django.core.validators import RegexValidator
|
2020-10-29 11:16:32 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2019-06-13 10:58:43 +00:00
|
|
|
from rest_framework.validators import (
|
|
|
|
UniqueTogetherValidator, ValidationError
|
|
|
|
)
|
2020-10-27 11:54:41 +00:00
|
|
|
from rest_framework import serializers
|
|
|
|
|
|
|
|
from common.utils.strings import no_special_chars
|
2018-04-27 03:27:16 +00:00
|
|
|
|
2019-06-13 10:58:43 +00:00
|
|
|
|
|
|
|
alphanumeric = RegexValidator(r'^[0-9a-zA-Z_@\-\.]*$', _('Special char not allowed'))
|
|
|
|
|
|
|
|
|
|
|
|
class ProjectUniqueValidator(UniqueTogetherValidator):
|
|
|
|
def __call__(self, attrs):
|
|
|
|
try:
|
|
|
|
super().__call__(attrs)
|
|
|
|
except ValidationError as e:
|
|
|
|
errors = {}
|
|
|
|
for field in self.fields:
|
|
|
|
if field == "org_id":
|
|
|
|
continue
|
|
|
|
errors[field] = _('This field must be unique.')
|
|
|
|
raise ValidationError(errors)
|
2020-10-27 11:54:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
class NoSpecialChars:
|
|
|
|
def __call__(self, value):
|
|
|
|
if not no_special_chars(value):
|
|
|
|
raise serializers.ValidationError(
|
|
|
|
_("Should not contains special characters")
|
|
|
|
)
|