|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
"""Functions for test_models""" |
|
3
|
|
|
|
|
4
|
|
|
from loglan_db import db |
|
5
|
|
|
from loglan_db.model import Word, Key, Event, Author, Type, Definition |
|
6
|
|
|
from loglan_db.model_base import t_connect_authors, t_connect_keys, t_connect_words |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
def print_data_by_word(name: str): |
|
10
|
|
|
|
|
11
|
|
|
# get cpx |
|
12
|
|
|
word = Word.by_name(name).first() |
|
13
|
|
|
sources = word.parents |
|
14
|
|
|
|
|
15
|
|
|
affixes = [] |
|
16
|
|
|
definitions = [] |
|
17
|
|
|
types = [] |
|
18
|
|
|
events = [] |
|
19
|
|
|
authors = [] |
|
20
|
|
|
keys = [] |
|
21
|
|
|
|
|
22
|
|
|
relationship_authors = [] |
|
23
|
|
|
relationship_keys = [] |
|
24
|
|
|
relationship_words = [] |
|
25
|
|
|
|
|
26
|
|
|
for s in sources: |
|
27
|
|
|
for a in s.affixes: |
|
28
|
|
|
relationship_words.append((s.id, a.id)) |
|
29
|
|
|
affixes.append(a) |
|
30
|
|
|
relationship_words.append((s.id, word.id)) |
|
31
|
|
|
words = [word, ] + sources + affixes |
|
32
|
|
|
|
|
33
|
|
|
for w in words: |
|
34
|
|
|
for d in w.definitions: |
|
35
|
|
|
definitions.append(d) |
|
36
|
|
|
|
|
37
|
|
|
for a in w.authors: |
|
38
|
|
|
relationship_authors.append((a.id, w.id)) |
|
39
|
|
|
if a not in authors: |
|
40
|
|
|
authors.append(a) |
|
41
|
|
|
|
|
42
|
|
|
if w.type not in types: |
|
43
|
|
|
types.append(w.type) |
|
44
|
|
|
|
|
45
|
|
|
if w.event_start not in events and w.event_start: |
|
46
|
|
|
events.append(w.event_start) |
|
47
|
|
|
|
|
48
|
|
|
if w.event_end not in events and w.event_end: |
|
49
|
|
|
events.append(w.event_end) |
|
50
|
|
|
|
|
51
|
|
|
for d in definitions: |
|
52
|
|
|
for k in d.keys: |
|
53
|
|
|
relationship_keys.append((k.id, d.id)) |
|
54
|
|
|
if k not in keys: |
|
55
|
|
|
keys.append(k) |
|
56
|
|
|
|
|
57
|
|
|
all_objects = [ |
|
58
|
|
|
(Key, keys), (Event, events), (Author, authors), |
|
59
|
|
|
(Type, types), (Word, words), (Definition, definitions), ] |
|
60
|
|
|
|
|
61
|
|
|
for type_object, objects in all_objects: |
|
62
|
|
|
type_object = str(type_object.__name__).lower() |
|
63
|
|
|
print(f"# {'='*5} {type_object.upper()}S {'='*(70-len(type_object))}") |
|
64
|
|
|
for i, o in enumerate(objects, 1): |
|
65
|
|
|
print(f"{type_object}_{i} = {o.__str__()} ") |
|
66
|
|
|
list_of_items = [type_object + '_' + str(i) for i in range(1, len(objects) + 1)] |
|
67
|
|
|
print(f"{type_object}s = {list_of_items}\n".replace("'", "")) |
|
68
|
|
|
|
|
69
|
|
|
types = str([(to.__name__, to.__name__.lower() + 's') for to, _ in all_objects]).replace("'", "") |
|
70
|
|
|
print(f"all_objects = {types}\n") |
|
71
|
|
|
print(f"# {'=' * 5} CONNECTIONS {'=' * (71 - len('CONNECTIONS'))}") |
|
72
|
|
|
print(f"connect_authors = {relationship_authors} # (AID, WID)") |
|
73
|
|
|
print(f"connect_keys = {relationship_keys} # (KID, DID)") |
|
74
|
|
|
print(f"connect_words = {relationship_words} # (parent_id, child_id)") |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
def print_elements(objects, is_all: bool = False): |
|
78
|
|
|
type_object = str(type(objects[0]).__name__.replace("Base", "")).lower() |
|
79
|
|
|
print(f"# {'=' * 5}{' ALL' if is_all else ''} {type_object.upper()}S {'=' * (70 - len(type_object))}") |
|
80
|
|
|
for i, o in enumerate(objects, 1): |
|
81
|
|
|
print(f"{type_object}_{i} = {o.__str__()} ") |
|
82
|
|
|
list_of_items = [type_object + '_' + str(i) for i in range(1, len(objects) + 1)] |
|
83
|
|
|
print(f"{'all_' if is_all else ''}{type_object}s = {list_of_items}\n".replace("'", "")) |
|
84
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
def db_add_object(obj_class, obj): |
|
87
|
|
|
obj = obj_class(**obj) |
|
88
|
|
|
obj.save() |
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
def db_add_objects(obj_class, objects): |
|
92
|
|
|
[db_add_object(obj_class, obj) for obj in objects] |
|
93
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
def db_connect_authors(pairs: list): |
|
96
|
|
|
for aid, wid in pairs: |
|
97
|
|
|
ins = t_connect_authors.insert().values(WID=wid, AID=aid) |
|
98
|
|
|
db.engine.execute(ins) |
|
99
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
def db_connect_keys(pairs: list): |
|
102
|
|
|
for kid, did in pairs: |
|
103
|
|
|
ins = t_connect_keys.insert().values(DID=did, KID=kid) |
|
104
|
|
|
db.engine.execute(ins) |
|
105
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
def db_connect_words(pairs: list): |
|
108
|
|
|
for parent_id, child_id in pairs: |
|
109
|
|
|
ins = t_connect_words.insert().values(parent_id=parent_id, child_id=child_id) |
|
110
|
|
|
db.engine.execute(ins) |
|
111
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
def db_add_and_return(obj_class, data): |
|
114
|
|
|
db_add_object(obj_class, data) |
|
115
|
|
|
return obj_class.get_by_id(data["id"]) |
|
116
|
|
|
|
|
117
|
|
|
|
|
118
|
|
|
dar = db_add_and_return |
|
119
|
|
|
|