-
Notifications
You must be signed in to change notification settings - Fork 1
/
kfields.py
170 lines (142 loc) · 5.46 KB
/
kfields.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import inspect
import json
import os
from datetime import date
from django.conf import settings
from django.core import exceptions
from django.db import models
from django.utils import timezone
from jsonschema import exceptions as jsonschema_exceptions
from jsonschema import validate
from shortuuid import ShortUUID
KIDX_MODEL_MAP = getattr(settings, "KIDX_MODEL_MAP", {})
class KIdxField(models.CharField):
description = "A short UUID field."
alphabet = "23456789ABCDEFGHJKMNPQRSTUVWXYZ"
def __init__(self, *args, **kwargs):
self.length = kwargs.pop("length", 8)
if "max_length" not in kwargs:
kwargs["max_length"] = 15
kwargs.update({"unique": True, "editable": False, "blank": True})
super().__init__(*args, **kwargs)
def _generate_uuid(self, _prefix):
"""Generate a short random string."""
_year = str(date.today().year)[2:]
_uuid = ShortUUID(alphabet=self.alphabet).random(length=self.length)
return f"{_prefix}{_year}{_uuid}".upper()
def pre_save(self, instance, add):
"""
This is used to ensure that we auto-set values if required.
See CharField.pre_save
"""
value = super().pre_save(instance, add)
if not value:
_table_name = instance._meta.db_table
prefix = getattr(KIDX_MODEL_MAP, _table_name, "ID")
value = self._generate_uuid(prefix)
return value
def deconstruct(self):
name, path, args, kwargs = super().deconstruct()
kwargs["length"] = self.length
kwargs.pop("default", None)
return name, path, args, kwargs
class CreatedDateField(models.DateField):
def __init__(self, *args, **kwargs):
kwargs.update({"editable": False, "db_index": True})
super().__init__(*args, **kwargs)
def pre_save(self, instance, add):
value = super().pre_save(instance, add)
if not value:
created_on = getattr(instance, "created_on", None) or timezone.now()
value = timezone.localdate(created_on)
return value
class InforcedKeyJSONField(models.JSONField):
def __init__(
self,
verbose_name=None,
name=None,
encoder=None,
decoder=None,
full=None,
partial=None,
schema=None,
read_time_validate=False,
allowed_keys: set = set(),
**kwargs,
):
super().__init__(verbose_name, name, **kwargs)
if full and not schema:
raise exceptions.ValidationError(
f": For full inforce, Schema Params is required",
code="No Schema Provided",
params={"value": "Schema Missing"},
)
elif partial and not allowed_keys:
raise exceptions.ValidationError(
f": For partial inforce, allowed_keys Params is required",
code="No allow key Provided",
params={"value": "No Keys Provided"},
)
self.full = full
self.partial = partial
self.schema = schema
self.allowed_keys = allowed_keys
self.read_time_validate = read_time_validate
@property
def _schema_data(self):
model_file = inspect.getfile(self.model)
dirname = os.path.dirname(model_file)
# schema file related to model.py path
p = os.path.join(dirname, self.schema)
with open(p, "r") as file:
return json.loads(file.read())
def _validate_schema(self, value):
# Disable validation when migrations are faked
if self.model.__module__ == "__fake__":
return True
try:
status = validate(value, self._schema_data)
except jsonschema_exceptions.ValidationError as e:
raise exceptions.ValidationError(e.message, code="invalid")
return status
def _partial_validate(self, value):
if value and not (set(self.allowed_keys) == set(value.keys())):
raise exceptions.ValidationError(
f": Must have {self.allowed_keys} keys ",
code="Invalid Values",
params={"value": value},
)
def validate(self, value, model_instance):
super().validate(value, model_instance)
if self.full:
self._validate_schema(value)
elif self.partial:
if not isinstance(self.value, dict):
raise exceptions.ValidationError(
f": Must be a clear json/dict ",
code="Invalid Values",
params={"value": value},
)
self._partial_validate(value)
def pre_save(self, model_instance, add):
value = super().pre_save(model_instance, add)
if value:
if self.full:
self._validate_schema(value)
elif self.partial:
self._partial_validate(value)
return value
def from_db_value(self, value, expression, connection):
value = super().from_db_value(value, expression, connection)
if self.read_time_validate:
if self.full:
self._validate_schema(value)
elif self.partial:
self._partial_validate(value)
return value
# full -- schima restriction (choice for validation during readtime)
# partial -- key only
# none -- none
# def from_db_value(self, value, expression, connection):
# value = super().from_db_value(value, expression, connection)
# return pd.DataFrame(value)