Files
myapp-full/myapp/mutations/tag.py
2024-07-16 17:49:40 +03:00

46 lines
925 B
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
__author__ = 'RemiZOffAlex'
__email__ = 'remizoffalex@mail.ru'
import logging
from .. import lib, models
log = logging.getLogger(__name__)
def tag_as_dict(
tag: models.Tag,
fields: list = ['id', 'name']
):
"""Тег в словарь
"""
def field_pages(tag):
# Теги
result = []
for tagLink in tag.tags:
newTag = tagLink.tag.as_dict()
result.append(newTag)
return result
def field_user(tag):
# Пользователь
return tag.user.as_dict()
funcs = {
'pages': field_pages,
'user': field_user,
}
result = {}
for column in tag.__table__.columns:
if column.name in fields:
result[column.name] = getattr(tag, column.name)
for field in fields:
if field in funcs:
func = funcs[field]
result[field] = func(tag)
return result