-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpiiwee.py
821 lines (650 loc) · 26.2 KB
/
piiwee.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
""" Adding cache, permissoin control, RESTful API to peewee
Peewee is a great ORM, and FastAPI is a great web framework. However, there
is a gap between them. Thousands of developers write their own caching,
permission control, and biuld RESTful API for frontend. This library is
trying to fill the gap. Refer to example folder for usage.
"""
import ast
import hashlib
import logging
import operator
import pickle
import random
from functools import partialmethod, reduce
from itertools import combinations
from typing import Dict, List, Union
from peewee import OP, Expression, Field, Model as PeeweeModel, ModelSelect
__author__ = "Jianshuo Wang"
__copyright__ = "Copyright 2023, Baixing.com"
__credits__ = ["Jianshuo Wang", "Chato Development Team"]
__license__ = "MIT"
__maintainer__ = "Jianshuo Wang"
__email__ = "[email protected]"
__status__ = "Production"
logger = logging.getLogger(__name__)
def operator_name(op: ast.operator) -> str:
"""Get the operator name from the ast.operator
Args:
op (ast.operator): The operator from ast
Returns:
str: Upper case operator name like "AND" or "OR"
>>> operator_name(ast.And())
'AND'
>>> operator_name(ast.Eq())
'='
"""
return OP.get(op.__class__.__name__.upper())
def expr(exp: Union[ast.AST, Expression, str], model: PeeweeModel) -> Expression:
"""Convert an ast expression to a peewee expression
I am surprised that no one actually created this function.
>>> from peewee import CharField, IntegerField
>>> class User(Model):
... name = CharField()
... age = IntegerField()
>>> expr("name == 'John' and age > 18", User) # doctest: +ELLIPSIS
<peewee.Expression object at 0x...>
Args:
exp (Union[ast.AST, Expression, str]): _description_
model (Model): _description_
Raises:
NotImplementedError: Not all ast expressions can map to
peewee expressions. Raise this error if the ast expression
is not supported yet.
Returns:
Expression: The peewee Expression
"""
if isinstance(exp, str):
return expr(ast.parse(exp, mode="eval").body, model)
if isinstance(exp, ast.Constant):
return exp.value
if isinstance(exp, ast.Name):
return getattr(model, exp.id)
if isinstance(exp, (ast.Tuple, ast.List)):
return [expr(e, model) for e in exp.elts]
if isinstance(exp, ast.UnaryOp):
return (
expr(exp.operand, model).desc()
if isinstance(exp.op, ast.USub)
else expr(exp.operand, model).asc()
)
if isinstance(exp, ast.BoolOp):
elements = [expr(e, model) for e in exp.values]
return (
reduce(operator.and_, elements)
if isinstance(exp.op, ast.And)
else reduce(operator.or_, elements)
)
if isinstance(exp, ast.Compare):
return Expression(
expr(exp.left, model),
operator_name(exp.ops[0]),
expr(exp.comparators[0], model),
)
raise NotImplementedError(f"Expression [{exp}] not supported yet")
def ensure_tuple(data) -> tuple:
"""Ensure the output is a tuple
>>> ensure_tuple(1)
(1,)
>>> ensure_tuple([1, 2])
(1, 2)
>>> ensure_tuple((1, 2))
(1, 2)
Args:
data (any): any data
Returns:
tuple: make sure the output is a tuple
"""
if isinstance(data, (list, tuple)):
return tuple(data)
return (data,)
def field_eq(
exp: Union[Expression, Field], field: Union[Field, str]
) -> Union[str, None]:
"""Get the value from the expression for the specified fields.
For example, expression reprsentation of "a = 1 AND b = 2",
and field = a will return "1".
If there is OR in the expression, or the expression is not
"=", like "a > 1", then None will be returned.
>>> from peewee import CharField, IntegerField
>>> class User(Model):
... name = CharField()
... age = IntegerField()
>>> field_eq(Expression(User.name, "=", "John"), "name")
'John'
>>> field_eq(Expression(Expression(User.name, "=", "John"),
... 'AND',
... Expression(User.age, "=", 18)), "age")
18
Args:
exp (Union[Expression, Field]): The expression to get the value from
field (Union[Field, str]): The name or Field representing the field
Returns:
str | None: the value of the field, or None if not found
"""
if isinstance(exp, Expression):
if exp.op == "AND":
return field_eq(exp.lhs, field) or field_eq(exp.rhs, field)
if exp.op == "=" and exp.lhs.name == field_names([field])[0]:
return exp.rhs
return None
def field_names(fields: List[Union[Field, str]]) -> List[str]:
"""Get the string names of the fields
>>> field_names(["a", "b"])
['a', 'b']
>>> from peewee import CharField, IntegerField
>>> class User(Model):
... name = CharField()
... age = IntegerField()
>>> field_names([User.name, User.age])
['name', 'age']
Args:
fields (List[Union[Field, str]]): The fields to get the names from
Returns:
List[str]: A list of string names of the fields
"""
return [f.name if isinstance(f, Field) else f.strip() for f in fields]
def all_combinations(names: List[str]) -> List[tuple]:
"""Generate all combinations of the names, from empty set, to two,
three or more elements of combinations, until the result is the same
length as the list.
>>> list(all_combinations(["a", "b"]))
[(), ('a',), ('b',), ('a', 'b')]
>>> list(all_combinations([]))
[()]
Args:
names (List[str]): The elements to generate combinations from
Yields:
List[tuple[str]]: The combinations of the names
"""
for i in range(len(names) + 1):
yield from combinations(names, i)
def md5(data: str) -> str:
"""Generate the md5 hash of the data
Args:
data (str): The data
Returns:
str: The md5 hash
"""
return hashlib.md5(data.encode("UTF-8")).hexdigest()
def flat(items: dict, sep: str = "=", join: str = ":") -> str:
"""Flatten a dictionary to a string. Key and value are separated by `sep`,
and each key-value pair is separated by `join`.
>>> flat({"a": 1, "b": 2})
'a=1:b=2'
>>> flat({"a": 1, "b": 2}, sep=":", join="=")
'a:1=b:2'
Args:
items (dict): the dictionary to flatten
sep (str, optional): the seperator. Defaults to "=".
join (_type_, optional): the joiner. Defaults to ":".
Returns:
str: the flattened string
"""
items = items or {}
return join.join([f"{key}{sep}{value}" for key, value in items.items()])
def getattrs(obj: Union[dict, object, Expression], names: List[str]) -> dict:
"""A helper to get the attributes from an object, a dict or
an peewee expression.
>>> getattrs({"a": 1, "b": 2, "c": 3}, ["a", "b"])
{'a': 1, 'b': 2}
>>> class User:
... def __init__(self, name, age):
... self.name = name
... self.age = age
>>> getattrs(User("John", 20), ["name", "age"])
{'name': 'John', 'age': 20}
Args:
obj (Union[dict, object, Expression]): the object to
get the attributes from
names (List[str]): the names to get attrabutes for
Returns:
dict: a dict with names as keys, and values as values
"""
if isinstance(obj, dict):
return {name: obj.get(name) for name in names if name in obj}
if isinstance(obj, Expression):
return {name: field_eq(obj, name) for name in names if field_eq(obj, name)}
return {name: getattr(obj, name) for name in names if hasattr(obj, name)}
class MemoryStore(dict):
"""A memory version of Redis store."""
def hget(self, key: str, tag: str):
"""Get the value of the key and tag from memory store.
>>> m = MemoryStore()
>>> m.hset("key", "tag", "This is the data")
>>> m.hget("key", "tag")
'This is the data'
Args:
key (str): the key
tag (str): a sub key
Returns:
str: the stored data
"""
return self.get(key, {}).get(tag)
def hset(self, key: str, tag: str, value: str):
"""Set the value of the key and tag in memory store.
Args:
key (str): key
tag (str): sub key
value (str): The value to be set into the memory store
"""
self.setdefault(key, {})[tag] = value
def delete(self, *keys) -> None:
"""Delete the specified keys from the memory store.
Clear the whole store once for every 1000 calls,
to avoid memory leak.
Args:
*keys: the keys to be cleared
"""
for key in keys:
self.pop(key, None)
if random.randint(1, 1000) == 1:
self.clear()
class Cache:
_store = MemoryStore()
@classmethod
def set_store(cls, store: object):
"""Set the store for the cache. It should be an instance of Redis
or MemoryStore that implement hget, hset, and delete functions.
Redis is preferred sicne MemoryStore can only work on a single
server. If you are using multiple servers, you should use Redis.
Args:
store (object): MemoryStore or Redis
"""
cls._store = store
@classmethod
def get_key(cls, key: str, sub_keys: dict = None) -> str:
"""Get the key for the cache. It is the class name, the key,
and the sub keys.
>>> Cache.get_key("key", {"a": 1, "b": 2})
'Cache:key:a=1:b=2'
>>> Cache.get_key("key")
'Cache:key:'
>>> Cache.get_key("key", {})
'Cache:key:'
>>> Cache.get_key("key", None)
'Cache:key:'
Args:
key (str): the key
sub_keys (dict, optional): the sub keys. Defaults to None.
Returns:
str: the key
"""
return f"{cls.__name__}:{key}:{flat(sub_keys)}"
@classmethod
def get_cache(
cls,
key: str,
func: callable,
*args,
tag: str = "-",
sub_keys: dict = None,
**kwargs,
) -> any:
"""Get the data from cache. If the data is not in cache,
call the function to get the data, and store it in cache.
The *args, and **kwargs will be passed to func().
>>> Cache.get_cache("key", lambda: "This is the data")
'This is the data'
>>> Cache.get_cache("key", operator.add, 1, 3)
'This is the data'
>>> Cache.clear_cache("key")
>>> Cache.get_cache("key", operator.add, 1, 3)
4
>>> Cache.clear_cache(Cache.get_key("key"), raw_key=True)
>>> Cache.get_cache("key", operator.add, 1, 4)
5
Args:
key (str): the key
func (callable): the function to get the data
tag (str, optional): the tag to use as hash key. Defaults to "-".
sub_keys (dict, optional): the sub keys. Defaults to None.
Returns:
any: the data
"""
key = cls.get_key(key, sub_keys)
if value := cls._store.hget(key, tag):
logger.debug(f"Cache HIT {key} {tag} {value[:10]}...")
return pickle.loads(value)
data = func(*args, **kwargs)
cls._store.hset(key, tag, pickle.dumps(data))
logger.debug(f"Cache MISS {key} {tag}")
return data
@classmethod
def clear_cache(cls, *keys, raw_key=False):
"""Clear the cache for the specified keys.
Args:
*keys: the keys to be cleared
raw_key (bool, optional): whether the key is raw key
or needs to be prefixed by get_key() . Defaults to False.
"""
raw_keys = [key if raw_key else cls.get_key(key) for key in keys]
for key in raw_keys:
logger.debug(f"Cache DELETED {key}")
cls._store.delete(*raw_keys)
class CachedModelSelect(ModelSelect, Cache):
def __iter__(self):
"""Iterate through the results with cache enabled.
The cache key is the model name, suffixed by the indexed fields
and values in the where clause. The cache tag is the md5 of the
whole SQL text.
For example, if the model name is "User", and the where clause
is "User.id == 1", the cache key is "Cache:User:id=1", and the cache
tag is the md5 of the whole SQL text.
Following is a sample Cache Key, and Tag:
Key: CachedModelSelect:ChatoDomain:creator=3
Tag: 65702969e839a655eeaea0e89243efe9
Yields:
list: the results of the SELECT query, served from cache
if possible
"""
assert len(self._from_list) == 1, "Only one table is allowed by cache"
yield from self.get_cache(
key=self._from_list[0].__name__,
sub_keys=getattrs(self._where, field_names(self.model.index_fields())),
tag=md5(str(self)),
func=lambda: list(super(ModelSelect, self).__iter__()),
)
def _call(self, func: str, *expressions):
"""Pass the arguments to the function, and return the result.
Only call the function if the first expression is not None.
Before calling the function, make sure the expression is a tuple.
"""
if expressions and expressions[0] is not None:
if isinstance(expressions[0], str):
expressions = ensure_tuple(expr(expressions[0], self.model))
return getattr(super(), func)(*expressions)
return self
where = partialmethod(_call, "where")
select = partialmethod(_call, "select")
order_by = partialmethod(_call, "order_by")
class CachedModel(PeeweeModel, Cache):
@classmethod
def get_by_id(cls, id: int):
"""Get the model by id with cache enabled."""
return cls.get_cache(id, super().get_by_id, id)
def save(self, *args, **kwargs):
"""Save the model with cache enabled.
Returns:
int: the number of row saved
"""
self.clear_cache(*self.cache_keys(), raw_key=True)
return super().save(*args, **kwargs)
@classmethod
def index_fields(cls) -> List[Field]:
"""Returns a list of fields that are marked as index in the model.
Returns:
List[Field]: the list of fields that are marked as index
"""
return [f for f in cls._meta.sorted_fields if f.index]
def cache_keys(self) -> List[str]:
"""Returns a list of cache keys for the model.
It caculate which content of the cache keys MAY be changed because
of the save. For example, if the model is "User", and the index
fields are "id" and "name", the following cache keys may contains
invalid data:
Cache:User: (The whole User table)
CachedModelSelect:User:id=1:name=John
CachedModelSelect:User:id=1
CachedModelSelect:User:name=John
Depending on whether id, or name, or both id and name appeared in
the SELECT query, the data may be stored in either of the keys. We
just cleared all the possible combination to be safe - the clear
operation is cheap in Redis anyway.
"""
yield self.get_key(self.get_id())
for keys in all_combinations(field_names(self.index_fields())):
yield CachedModelSelect.get_key(
self.__class__.__name__, getattrs(self, keys)
)
@classmethod
def select(cls, *fields) -> CachedModelSelect:
"""A small operation to create a CachedModelSelect (our Cached Version)
instead of the default ModelSelect.
I just copied the code from Model.select() and replaced the ModelSelect
Returns:
CachedModelSelect: the CachedModelSelect
"""
if not fields:
fields = cls._meta.sorted_fields
return CachedModelSelect(cls, fields)
class PermissionedModel(PeeweeModel):
"""A Model with permission control. It is used to control the
permission of the model, and the fields of the model.
The permission is defined following the Unix UGO permission model.
The permission is defined as a 3-digit octal number, where the
first digit is the permission for the owner, the second digit is
the permission for the group, and the third digit is the permission
for the other users. For each digit, the value is the sum of the
following values:
4: READ
2: WRITE
1: NOT DEFINED (Please keep it 0 all the time, and may be used
for future extension)
The model permission is defined as "permission" in Meta class.
class User(Model):
class Meta:
permission = 0o606
The field permission is defined as "_hidden" in Field class:
class User(Model):
name = CharField(max_length=100, _hidden=0o604)
mobile = CharField(max_length=100, _hidden=0o600)
role = CharField(max_length=100, _hidden=0o404)
The permission above spefieid everyone can read the name, role,
but only the owner (the user him/herself) can read the mobile.
The owner can also write the name, mobile, but not role.
The permission is defined in the following order:
1. The default permission for the model
2. The permission for the field
3. The permission for the role of the user
4. The permission for the operation
Raises:
PermissionError: if the user does not has the permission
for the model or the field, it will raise PermissionError.
"""
default_model_permission = 0o606 # READ and WRITE
default_field_permission = 0o604 # OWNER READ WRITE, OTHER READ
default_role = 0o007 # OTHER
def get_role(self, user_id: int) -> int:
"""The role of the user. It is used to determine the permission
of the user. Override this function to implement your own role
management in subclasses.
Args:
user_id (int): the user id
Returns:
int: the role of the user
"""
return self.default_role
@classmethod
def fields(cls, op_perm: int = 0, role: int = 0) -> List[Field]:
"""Returns a list of fields that the user has the permission
to read/write.
Args:
op_perm (int, optional): the required permission. Defaults to 0.
role (int, optional): the required role. Defaults to 0.
Returns:
List[Field]: the list of fields that the user has the permission
to read/write.
>>> from peewee import CharField
>>> class User(Model):
... name = CharField(max_length=100, _hidden=0o604)
... mobile = CharField(max_length=100, _hidden=0o600)
... role = CharField(max_length=100, _hidden=0o404)
... class Meta:
... permission = 0o600
Check what fields the user can write (0o200):
>>> User.fields(op_perm=0o200, role=0o700)
[<AutoField: User.id>, <CharField: User.name>, <CharField: User.mobile>]
"""
return [
field
for field, permission in cls.field_perms().items()
if permission & op_perm & role
]
@classmethod
def field_perms(cls) -> Dict[Field, int]:
"""Returns a dict of fields and their permission.
Returns:
Dict[Field, int]: a dict of fields and their permission
>>> from peewee import CharField
>>> class User(Model):
... name = CharField(max_length=100, _hidden=0o604)
... mobile = CharField(max_length=100, _hidden=0o600)
... role = CharField(max_length=100, _hidden=0o404)
... class Meta:
... permission = 0o600
>>> User.field_perms()
{<AutoField: User.id>: 384, <CharField: User.name>: 384, <CharField: User.mobile>: 384, <CharField: User.role>: 256}
"""
return {f: cls.field_perm(f) for f in cls._meta.sorted_fields}
@classmethod
def field_perm(cls, field: Field) -> int:
"""Returns the permission of the field.
Args:
field (Field): the field
Returns:
_type_: the permission of the field
>>> from peewee import CharField
>>> class User(Model):
... name = CharField(max_length=100, _hidden=0o604)
... mobile = CharField(max_length=100, _hidden=0o600)
... role = CharField(max_length=100, _hidden=0o404)
... class Meta:
... permission = 0o600
>>> oct(User.field_perm(User.name))
'0o600'
"""
perm = cls.default_field_permission if field._hidden is False else field._hidden
return perm & cls.model_perm()
@classmethod
def model_perm(cls) -> int:
"""Returns the permission of the model.
>>> from peewee import CharField
>>> class User(Model):
... class Meta:
... permission = 0o604
>>> oct(User().model_perm())
'0o604'
"""
return getattr(cls._meta, "permission", cls.default_model_permission)
def readable_fields(self, user_id: int = 0) -> List[Field]:
"""Get a list of readable fields for the current user
Args:
user_id (int, optional): User ID. Defaults to 0.
Returns:
List[Field]: a list of readable fields for the current user
>>> from peewee import CharField, IntegerField
>>> class User(Model):
... name = CharField(max_length=100, _hidden=0o604)
... age = IntegerField(_hidden=0o600)
>>> user = User(name="John", age=18)
>>> user.readable_fields(user_id=3)
[<AutoField: User.id>, <CharField: User.name>]
"""
return self.fields(0o444, self.get_role(user_id))
def writable_fields(self, user_id: int = 0) -> List[Field]:
"""Get a list of writable fields for the current user
Args:
user_id (int, optional): User ID. Defaults to 0.
Returns:
List[Fields]: a list of writable fields for the current user
>>> from peewee import CharField, IntegerField
>>> class User(Model):
... name = CharField(max_length=100, _hidden=0o606)
... age = IntegerField(_hidden=0o600)
>>> user = User(name="John", age=18)
>>> user.writable_fields(user_id=3)
[<CharField: User.name>]
"""
return self.fields(0o222, self.get_role(user_id))
def to_dict(
self,
user_id: int = 0,
only: List[Union[Field, str]] = None,
exclude: List[Field] = None,
) -> dict:
"""Returns a dict of the model. Only the fields that the user
has the permission to read will be included.
Args:
user_id (int, optional): the user id. Defaults to 0.
only (List[Union[Field, str]], optional): the list of fields
to be included. Defaults to None. None or empty list means
all fields.
exclude (List[Field], optional): the list of fields to be
excluded. Defaults to None.
Returns:
dict: a dict of the model
>>> from peewee import CharField
>>> class User(Model):
... name = CharField(max_length=100, _hidden=0o604)
... mobile = CharField(max_length=100, _hidden=0o600)
... role = CharField(max_length=100, _hidden=0o404)
... class Meta:
... permission = 0o604
>>> user = User()
>>> user.name = "John"
>>> user.mobile = "1234567890"
>>> user.role = "user"
OTHER user (default_role) can only read name and role:
>>> user.to_dict(user_id=0)
{'name': 'John', 'role': 'user'}
>>> user.to_dict(user_id=0, only=["name"])
{'name': 'John'}
The mobile is not available for OTHER user, and name is excluded:
>>> user.to_dict(user_id=0, exclude=["name"])
{'role': 'user'}
"""
readable_fields = [
field
for field in field_names(self.readable_fields(user_id))
if (only is None or not any(only) or field in field_names(only))
and (exclude is None or field not in field_names(exclude))
]
return getattrs(self.__data__, readable_fields)
def from_dict(self, items: dict, user_id: int = 0) -> "PermissionedModel":
"""Update the model from a dict. Only the fields that the user
has the permission to write will be updated. If the user does
not have the permission to write the field, it will raise
PermissionError.
Args:
items (dict): the dict to update the model
user_id (int, optional): the user id. Defaults to 0.
Raises:
PermissionError: if the user does not have the permission
to write the field, it will raise PermissionError.
Returns:
PermissionedModel: the updated model
>>> from peewee import CharField
>>> class User(Model):
... name = CharField(max_length=100, _hidden=0o604)
... mobile = CharField(max_length=100, _hidden=0o600)
... role = CharField(max_length=100, _hidden=0o404)
... class Meta:
... permission = 0o604
>>> user = User()
>>> props = {"name": "John", "mobile": "1234567890", "role": "admin"}
>>> user.from_dict(props, user_id=0)
Traceback (most recent call last):
...
PermissionError: Field name is not writable for user 0 (role 0o7)
>>> user.from_dict(props, user_id=1)
Traceback (most recent call last):
...
PermissionError: Field name is not writable for user 1 (role 0o7)
"""
for key, value in items.items():
if key in field_names(self.writable_fields(user_id)):
setattr(self, key, value)
else:
raise PermissionError(
f"Field {key} is not writable for user "
f"{user_id} (role {oct(self.get_role(user_id))})"
)
return self
class Model(PermissionedModel, CachedModel, PeeweeModel):
"""The final model that is used in the application. It is a
combination of PermissionedModel and CachedModel.
Any sub class Model will have the following features out of box.
1. Permission control
2. Cache control
"""
pass