forked from phith0n/Minos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.py
47 lines (37 loc) · 1.24 KB
/
base.py
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
__author__ = 'phithon'
import re
class BaseModel:
def __call__(self, *args, **kwargs):
data = args[0]
for (k, value) in data.items():
if k not in self.__invalid__:
continue
if (not value) and ("_need" not in self.__invalid__[k]):
continue
for (field, limit) in self.__invalid__[k].items():
if field[0] == "_": continue
func = "_check_%s" % field
if hasattr(self, func):
ret = getattr(self, func)(limit, value)
if not ret:
self.error_msg = self.__msg__[field] % self.__invalid__[k]["_name"]
return False
return True
def _check_type(self, valid, value):
return type(value) is valid
def _check_max_length(self, valid, value):
return len(value) <= valid
def _check_min_length(self, valid, value):
return len(value) >= valid
def _check_max(self, valid, value):
return value <= valid
def _check_min(self, valid, value):
return value >= valid
def _check_email(self, valid, value):
return re.match(r"^(\w)+(\.\w+)*@(\w)+((\.\w+)+)$", value)
def _check_number(self, valid, value):
return re.match(r"^\d+$", value)
def _check_url(self, valid, value):
return value.startswith("http://") or value.startswith("https://")
def _check_pattern(self, valid, value):
return re.match(valid, value)