Passed
Push — master ( 38bed0...e25529 )
by torrua
01:14
created

app.api.schemas.word   A

Complexity

Total Complexity 0

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 0
eloc 68
dl 0
loc 96
rs 10
c 0
b 0
f 0
1
# -*- coding:utf-8 -*-
2
"""
3
WordSchema module
4
"""
5
6
from loglan_core import Word as BaseWord, Definition, Type, Author, Event
7
from loglan_core.addons.word_getter import AddonWordGetter
8
from marshmallow import fields
9
from marshmallow_sqlalchemy.fields import Nested
10
11
from app.api.schemas import ma
12
from app.api.schemas.author import AuthorSchema
13
from app.api.schemas.definition import DefinitionSchema
14
from app.api.schemas.event import EventSchema
15
from app.api.schemas.type import TypeSchema
16
17
nested_exclude = {
18
    "notes",
19
    "tid_old",
20
}
21
full_include = {
22
    "event_end",
23
    "authors",
24
    "definitions",
25
    "type",
26
    "event_start",
27
    "derivatives",
28
    "parents",
29
}
30
31
32
class Word(BaseWord, AddonWordGetter):
33
    __tablename__ = BaseWord.__tablename__
34
35
36
class WordSchema(ma.SQLAlchemyAutoSchema):
37
    class Meta:
38
        model = Word
39
        include_fk = True
40
        exclude = (
41
            "created",
42
            "updated",
43
            "_event_end",
44
            "_authors",
45
            "_definitions",
46
            "_derivatives",
47
            "_type",
48
            "_event_start",
49
            "_parents",
50
        )
51
        load_instance = True
52
53
    year = fields.Function(lambda obj: int(obj.year.strftime("%Y")))
54
    _authors = Nested(AuthorSchema(only=Author.attributes_basic()), many=True)
55
    authors = _authors
56
57
    _type = Nested(TypeSchema(only=Type.attributes_basic()))
58
    type = _type
59
60
    _event_start = Nested(EventSchema(only=Event.attributes_basic()))
61
    event_start = _event_start
62
63
    _event_end = Nested(EventSchema(only=Event.attributes_basic()))
64
    event_end = _event_end
65
66
    _definitions = Nested(
67
        DefinitionSchema(only=Definition.attributes_all()),
68
        exclude={
69
            "source_word",
70
        },
71
        many=True,
72
    )
73
    definitions = _definitions
74
75
    _parents = Nested(
76
        lambda: WordSchema,
77
        only=Word.attributes_basic(),
78
        exclude=nested_exclude,
79
        many=True,
80
    )
81
    parents = _parents
82
83
    _derivatives = Nested(
84
        lambda: WordSchema,
85
        only=Word.attributes_basic(),
86
        exclude=nested_exclude,
87
        many=True,
88
    )
89
    derivatives = _derivatives
90
91
92
word_schema_nested = WordSchema(only=Word.attributes_basic(), exclude=nested_exclude)
93
word_schema_full = WordSchema(only=(*Word.attributes_extended(), *full_include))
94
95
blue_print_export = (Word, word_schema_nested, word_schema_full)
96