1 | # -*- coding: utf-8 -*- |
---|
2 | |
---|
3 | """ |
---|
4 | | This file is part of the web2py Web Framework |
---|
5 | | Copyrighted by Massimo Di Pierro <mdipierro@cs.depaul.edu> |
---|
6 | | License: BSD |
---|
7 | |
---|
8 | Takes care of adapting pyDAL to web2py's needs |
---|
9 | ----------------------------------------------- |
---|
10 | """ |
---|
11 | |
---|
12 | from . import validators |
---|
13 | |
---|
14 | |
---|
15 | def default_validators(db, field): |
---|
16 | """ |
---|
17 | Field type validation, using web2py's validators mechanism. |
---|
18 | |
---|
19 | makes sure the content of a field is in line with the declared |
---|
20 | fieldtype |
---|
21 | """ |
---|
22 | |
---|
23 | field_type = field.type |
---|
24 | field_unique = field.unique |
---|
25 | field_notnull = field.notnull |
---|
26 | |
---|
27 | is_ref = field_type.startswith("reference") |
---|
28 | is_list = field_type.startswith("list:") |
---|
29 | if is_ref or field_type.startswith("list:reference"): |
---|
30 | table_field = field_type[10 if is_ref else 15 :].split(".", 1) |
---|
31 | table_name = table_field[0] |
---|
32 | field_name = table_field[-1] |
---|
33 | requires = None |
---|
34 | if table_name in db.tables: |
---|
35 | referenced = db[table_name] |
---|
36 | if len(table_field) == 1: |
---|
37 | requires = validators.IS_IN_DB( |
---|
38 | db, |
---|
39 | referenced._id, |
---|
40 | label=getattr(referenced, "_format", None), |
---|
41 | multiple=is_list, |
---|
42 | ) |
---|
43 | elif field_name in referenced.fields: |
---|
44 | requires = validators.IS_IN_DB( |
---|
45 | db, |
---|
46 | getattr(referenced, field_name), |
---|
47 | label=getattr(referenced, "_format", None), |
---|
48 | multiple=is_list, |
---|
49 | ) |
---|
50 | if requires: |
---|
51 | if field_unique: |
---|
52 | requires._and = validators.IS_NOT_IN_DB(db, field) |
---|
53 | if not field_notnull: |
---|
54 | requires = validators.IS_EMPTY_OR(requires) |
---|
55 | return requires |
---|
56 | |
---|
57 | if isinstance(field.options, (list, tuple)): |
---|
58 | requires = validators.IS_IN_SET(field.options, multiple=is_list) |
---|
59 | else: |
---|
60 | requires = [] |
---|
61 | if field_type in (("string", "text", "password")): |
---|
62 | requires.append(validators.IS_LENGTH(field.length)) |
---|
63 | elif field_type == "json": |
---|
64 | requires.append(validators.IS_EMPTY_OR(validators.IS_JSON())) |
---|
65 | elif field_type == "double" or field_type == "float": |
---|
66 | requires.append(validators.IS_FLOAT_IN_RANGE(-1e100, 1e100)) |
---|
67 | elif field_type == "integer": |
---|
68 | requires.append(validators.IS_INT_IN_RANGE(-(2 ** 31), 2 ** 31)) |
---|
69 | elif field_type == "bigint": |
---|
70 | requires.append(validators.IS_INT_IN_RANGE(-(2 ** 63), 2 ** 63)) |
---|
71 | elif field_type.startswith("decimal"): |
---|
72 | requires.append(validators.IS_DECIMAL_IN_RANGE(-(10 ** 10), 10 ** 10)) |
---|
73 | elif field_type == "date": |
---|
74 | requires.append(validators.IS_DATE()) |
---|
75 | elif field_type == "time": |
---|
76 | requires.append(validators.IS_TIME()) |
---|
77 | elif field_type == "datetime": |
---|
78 | requires.append(validators.IS_DATETIME()) |
---|
79 | |
---|
80 | if field_unique: |
---|
81 | requires.insert(0, validators.IS_NOT_IN_DB(db, field)) |
---|
82 | if (field_notnull or field_unique) and field_type not in ( |
---|
83 | "boolean", |
---|
84 | "password", |
---|
85 | "string", |
---|
86 | "text", |
---|
87 | "upload", |
---|
88 | ): |
---|
89 | requires.insert(0, validators.IS_NOT_EMPTY()) |
---|
90 | elif not field_notnull and not field_unique and requires: |
---|
91 | null = "" if field.type == "password" else None |
---|
92 | requires[0] = validators.IS_EMPTY_OR(requires[0], null=null) |
---|
93 | |
---|
94 | if len(requires) == 1: |
---|
95 | requires = requires[0] |
---|
96 | |
---|
97 | return requires or None |
---|