1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# coding=utf-8
import re
from gm_serializer import serializers
from utils.rpc import RPCMixin
from helios.rpc import RPCFaultException
class BlacklistValidator(RPCMixin):
def __call__(self, value):
try:
r = self.call_rpc('api/user/in_blacklist', user_id=value)
except:
return False
if r:
raise serializers.ValidationError("User in blacklist.")
class FilterWordValidator(RPCMixin):
def __init__(self, filter_type=None):
self.filter_type = filter_type
def __call__(self, value):
if re.findall('\d{7,}', value):
raise serializers.ValidationError("Content contains sensitive words.")
try:
r = self.call_rpc('api/filterWord/list', filter_type=self.filter_type)
except RPCFaultException:
return False
if value in r:
raise serializers.ValidationError("Content contains sensitive words.")
class RepeatedValidator(object):
def __init__(self, model_cls, field_name):
self.model_cls = model_cls
self.field_name = field_name
def __call__(self, value):
obj = self.model_cls.objects.last()
if getattr(obj, self.field_name, None) == value:
raise serializers.ValidationError("%s repeated." % self.model_cls.__class__)