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