Update mutation
This commit is contained in:
12
README.md
12
README.md
@@ -10,13 +10,21 @@
|
||||
docker compose build
|
||||
```
|
||||
|
||||
## запуск
|
||||
### Сборка документации
|
||||
|
||||
```
|
||||
make --directory docs html
|
||||
```
|
||||
|
||||
Открыть в браузере docs/build/html/index.html
|
||||
|
||||
## Запуск
|
||||
|
||||
```
|
||||
docker compose up - d
|
||||
```
|
||||
|
||||
## Утилиты
|
||||
## API
|
||||
|
||||
Добавление пользователя
|
||||
|
||||
|
||||
2
src/myapp/construct/__init__.py
Normal file
2
src/myapp/construct/__init__.py
Normal file
@@ -0,0 +1,2 @@
|
||||
__author__ = 'RemiZOffAlex'
|
||||
__email__ = 'remizoffalex@mail.ru'
|
||||
35
src/myapp/construct/mutation.py
Normal file
35
src/myapp/construct/mutation.py
Normal file
@@ -0,0 +1,35 @@
|
||||
__author__ = 'RemiZOffAlex'
|
||||
__email__ = 'remizoffalex@mail.ru'
|
||||
|
||||
class Wrapper:
|
||||
def __init__(self, context, name):
|
||||
self.__context = context
|
||||
self.__name = name
|
||||
|
||||
def __call__(self, *args, **kwargs):
|
||||
self.__function = self.__context.deferred(self.__name)
|
||||
print(self.__function, args, kwargs)
|
||||
response = self.__function(
|
||||
# self.__context,
|
||||
*args,
|
||||
**kwargs
|
||||
)
|
||||
return response
|
||||
|
||||
|
||||
class Mutation:
|
||||
def __init__(self):
|
||||
self.__methods = {}
|
||||
|
||||
def __call__(self, name):
|
||||
function = Wrapper(self, name)
|
||||
return function
|
||||
|
||||
def register(self, name, function):
|
||||
self.__methods[name] = function
|
||||
|
||||
def __getitem__(self, name):
|
||||
return self.__methods[name]
|
||||
|
||||
def __setitem__(self, name, function):
|
||||
self.__methods[name] = function
|
||||
@@ -1,2 +1,16 @@
|
||||
__author__ = 'RemiZOffAlex'
|
||||
__email__ = 'remizoffalex@mail.ru'
|
||||
|
||||
|
||||
from ..core import mutation
|
||||
|
||||
from .note import note_as_dict
|
||||
from .page import page_as_dict
|
||||
from .tag import tag_as_dict
|
||||
from .user import user_as_dict
|
||||
|
||||
|
||||
mutation.register('note', note_as_dict)
|
||||
mutation.register('page', page_as_dict)
|
||||
mutation.register('tag', tag_as_dict)
|
||||
mutation.register('user', user_as_dict)
|
||||
|
||||
@@ -1,21 +1,17 @@
|
||||
__author__ = 'RemiZOffAlex'
|
||||
__email__ = 'remizoffalex@mail.ru'
|
||||
|
||||
import logging
|
||||
|
||||
from . import mutation
|
||||
from .. import lib, models
|
||||
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def note_as_dict(
|
||||
note: models.Note,
|
||||
fields: list = ['id', 'title']
|
||||
):
|
||||
"""Статью как словарь (в JSON)
|
||||
"""
|
||||
def field_favorite(note):
|
||||
def field_favorite():
|
||||
# Избранное
|
||||
assert lib.get_user(), 'favorite only authorized users'
|
||||
result = False
|
||||
@@ -29,33 +25,37 @@ def note_as_dict(
|
||||
result = True
|
||||
return result
|
||||
|
||||
def field_tags(note):
|
||||
def field_tags(fields: list=['id', 'name']):
|
||||
# Теги
|
||||
tag_as_dict = mutation['tag']
|
||||
result = []
|
||||
for tagLink in note.tags:
|
||||
newTag = tagLink.tag.as_dict()
|
||||
result.append(newTag)
|
||||
for relation in note.tags:
|
||||
response = tag_as_dict(relation.tag, fields)
|
||||
result.append(response)
|
||||
return result
|
||||
|
||||
def field_user(note):
|
||||
def field_user(fields: list=['id', 'name']):
|
||||
# Пользователь
|
||||
return note.user.as_dict()
|
||||
user_as_dict = mutation['user']
|
||||
return user_as_dict(note.user, fields)
|
||||
|
||||
def field_parents(note):
|
||||
def field_parents(fields: list = ['id', 'title']):
|
||||
# Родители
|
||||
result = []
|
||||
parent = note.parent
|
||||
while parent:
|
||||
result.append(parent.as_dict())
|
||||
response = note_as_dict(parent, fields)
|
||||
result.append(response)
|
||||
parent = parent.parent
|
||||
result.reverse()
|
||||
return result
|
||||
|
||||
def field_nodes(note):
|
||||
def field_nodes(fields: list = ['id', 'title']):
|
||||
# Дети
|
||||
result = []
|
||||
for item in note.nodes:
|
||||
result.append(note_as_dict(item))
|
||||
response = note_as_dict(item, fields)
|
||||
result.append(response)
|
||||
return result
|
||||
|
||||
funcs = {
|
||||
@@ -72,9 +72,14 @@ def note_as_dict(
|
||||
result[column.name] = getattr(note, column.name)
|
||||
|
||||
for field in fields:
|
||||
if isinstance(field, dict):
|
||||
for key in field:
|
||||
if key in funcs:
|
||||
func = funcs[key]
|
||||
result[key] = func(field[key])
|
||||
else:
|
||||
if field in funcs:
|
||||
func = funcs[field]
|
||||
result[field] = func(note)
|
||||
result[field] = func()
|
||||
|
||||
log.info(result)
|
||||
return result
|
||||
|
||||
@@ -1,21 +1,17 @@
|
||||
__author__ = 'RemiZOffAlex'
|
||||
__email__ = 'remizoffalex@mail.ru'
|
||||
|
||||
import logging
|
||||
|
||||
from . import mutation
|
||||
from .. import lib, models
|
||||
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def page_as_dict(
|
||||
page: models.Page,
|
||||
fields: list = ['id', 'title']
|
||||
):
|
||||
"""Статью как словарь (в JSON)
|
||||
"""
|
||||
def field_favorite(page):
|
||||
def field_favorite():
|
||||
# Избранное
|
||||
assert lib.get_user(), 'favorite only authorized users'
|
||||
result = False
|
||||
@@ -29,33 +25,37 @@ def page_as_dict(
|
||||
result = True
|
||||
return result
|
||||
|
||||
def field_tags(page):
|
||||
def field_tags(fields: list=['id', 'name']):
|
||||
# Теги
|
||||
tag_as_dict = mutation['tag']
|
||||
result = []
|
||||
for tagLink in page.tags:
|
||||
newTag = tagLink.tag.as_dict()
|
||||
result.append(newTag)
|
||||
for relation in page.tags:
|
||||
response = tag_as_dict(relation.tag, fields)
|
||||
result.append(response)
|
||||
return result
|
||||
|
||||
def field_user(page):
|
||||
def field_user(fields: list=['id', 'name']):
|
||||
# Пользователь
|
||||
return page.user.as_dict()
|
||||
user_as_dict = mutation['user']
|
||||
return user_as_dict(page.user, fields)
|
||||
|
||||
def field_parents(page):
|
||||
def field_parents(fields: list = ['id', 'title']):
|
||||
# Родители
|
||||
result = []
|
||||
parent = page.parent
|
||||
while parent:
|
||||
result.append(parent.as_dict())
|
||||
response = page_as_dict(parent, fields)
|
||||
result.append(response)
|
||||
parent = parent.parent
|
||||
result.reverse()
|
||||
return result
|
||||
|
||||
def field_nodes(page):
|
||||
def field_nodes(fields: list = ['id', 'title']):
|
||||
# Дети
|
||||
result = []
|
||||
for item in page.nodes:
|
||||
result.append(page_as_dict(item))
|
||||
response = page_as_dict(item, fields)
|
||||
result.append(response)
|
||||
return result
|
||||
|
||||
funcs = {
|
||||
@@ -72,9 +72,14 @@ def page_as_dict(
|
||||
result[column.name] = getattr(page, column.name)
|
||||
|
||||
for field in fields:
|
||||
if isinstance(field, dict):
|
||||
for key in field:
|
||||
if key in funcs:
|
||||
func = funcs[key]
|
||||
result[key] = func(field[key])
|
||||
else:
|
||||
if field in funcs:
|
||||
func = funcs[field]
|
||||
result[field] = func(page)
|
||||
result[field] = func()
|
||||
|
||||
log.info(result)
|
||||
return result
|
||||
|
||||
@@ -1,26 +1,23 @@
|
||||
__author__ = 'RemiZOffAlex'
|
||||
__email__ = 'remizoffalex@mail.ru'
|
||||
|
||||
import logging
|
||||
|
||||
from . import mutation
|
||||
from .. import models
|
||||
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def tag_as_dict(
|
||||
tag: models.Tag,
|
||||
fields: list = ['id', 'name']
|
||||
):
|
||||
"""Тег в словарь
|
||||
"""
|
||||
def field_pages(tag):
|
||||
def field_pages(fields: list = ['id', 'title']):
|
||||
# Теги
|
||||
page_as_dict = mutation['page']
|
||||
result = []
|
||||
for tagLink in tag.tags:
|
||||
newTag = tagLink.tag.as_dict()
|
||||
result.append(newTag)
|
||||
for page in tag.pages:
|
||||
response = page_as_dict(page, fields)
|
||||
result.append(response)
|
||||
return result
|
||||
|
||||
def field_user(tag):
|
||||
@@ -38,8 +35,14 @@ def tag_as_dict(
|
||||
result[column.name] = getattr(tag, column.name)
|
||||
|
||||
for field in fields:
|
||||
if isinstance(field, dict):
|
||||
for key in field:
|
||||
if key in funcs:
|
||||
func = funcs[key]
|
||||
result[key] = func(field[key])
|
||||
else:
|
||||
if field in funcs:
|
||||
func = funcs[field]
|
||||
result[field] = func(tag)
|
||||
result[field] = func()
|
||||
|
||||
return result
|
||||
|
||||
@@ -1,21 +1,17 @@
|
||||
__author__ = 'RemiZOffAlex'
|
||||
__email__ = 'remizoffalex@mail.ru'
|
||||
|
||||
import logging
|
||||
|
||||
from . import mutation
|
||||
from .. import lib, models
|
||||
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def user_as_dict(
|
||||
user: models.User,
|
||||
fields: list = ['id', 'name']
|
||||
):
|
||||
"""Пользователя как словарь (в JSON)
|
||||
"""
|
||||
def field_favorite(user):
|
||||
def field_favorite():
|
||||
# Избранное
|
||||
assert lib.get_user(), 'favorite only authorized users'
|
||||
result = False
|
||||
@@ -29,12 +25,12 @@ def user_as_dict(
|
||||
result = True
|
||||
return result
|
||||
|
||||
def field_tags(user):
|
||||
def field_tags(fields: list=['id', 'name']):
|
||||
# Теги
|
||||
result = []
|
||||
for tagLink in user.tags:
|
||||
newTag = tagLink.tag.as_dict()
|
||||
result.append(newTag)
|
||||
for relation in user.tags:
|
||||
response = tag_as_dict(relation.tag, fields)
|
||||
result.append(response)
|
||||
return result
|
||||
|
||||
funcs = {
|
||||
@@ -49,9 +45,14 @@ def user_as_dict(
|
||||
result[column.name] = getattr(user, column.name)
|
||||
|
||||
for field in fields:
|
||||
if isinstance(field, dict):
|
||||
for key in field:
|
||||
if key in funcs:
|
||||
func = funcs[key]
|
||||
result[key] = func(field[key])
|
||||
else:
|
||||
if field in funcs:
|
||||
func = funcs[field]
|
||||
result[field] = func(user)
|
||||
result[field] = func()
|
||||
|
||||
log.info(result)
|
||||
return result
|
||||
|
||||
@@ -3,7 +3,10 @@ __email__ = 'remizoffalex@mail.ru'
|
||||
|
||||
from . import jsonrpc, login_required
|
||||
from .. import app, lib, models
|
||||
from ..mutations.note import note_as_dict
|
||||
from ..mutations import mutation
|
||||
|
||||
|
||||
note_as_dict = mutation['note']
|
||||
|
||||
|
||||
@jsonrpc.method('note')
|
||||
|
||||
@@ -3,7 +3,10 @@ __email__ = 'remizoffalex@mail.ru'
|
||||
|
||||
from .. import jsonrpc, login_required
|
||||
from ... import app, lib, models
|
||||
from ...mutations.page import page_as_dict
|
||||
from ...mutations import mutation
|
||||
|
||||
|
||||
page_as_dict = mutation['page']
|
||||
|
||||
|
||||
@jsonrpc.method('page')
|
||||
|
||||
@@ -4,7 +4,10 @@ __email__ = 'remizoffalex@mail.ru'
|
||||
|
||||
from . import jsonrpc
|
||||
from .. import app, lib, models
|
||||
from ..mutations.user import user_as_dict
|
||||
from ..mutations import mutation
|
||||
|
||||
|
||||
user_as_dict = mutation['user']
|
||||
|
||||
|
||||
@jsonrpc.method('user')
|
||||
|
||||
Reference in New Issue
Block a user