diff --git a/README.md b/README.md index 7ef05ec..21b512a 100644 --- a/README.md +++ b/README.md @@ -10,13 +10,21 @@ docker compose build ``` -## запуск +### Сборка документации + +``` +make --directory docs html +``` + +Открыть в браузере docs/build/html/index.html + +## Запуск ``` docker compose up - d ``` -## Утилиты +## API Добавление пользователя diff --git a/src/myapp/construct/__init__.py b/src/myapp/construct/__init__.py new file mode 100644 index 0000000..18c55bb --- /dev/null +++ b/src/myapp/construct/__init__.py @@ -0,0 +1,2 @@ +__author__ = 'RemiZOffAlex' +__email__ = 'remizoffalex@mail.ru' diff --git a/src/myapp/construct/mutation.py b/src/myapp/construct/mutation.py new file mode 100644 index 0000000..eb935fe --- /dev/null +++ b/src/myapp/construct/mutation.py @@ -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 diff --git a/src/myapp/mutations/__init__.py b/src/myapp/mutations/__init__.py index 18c55bb..bcb1083 100644 --- a/src/myapp/mutations/__init__.py +++ b/src/myapp/mutations/__init__.py @@ -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) diff --git a/src/myapp/mutations/note.py b/src/myapp/mutations/note.py index 059c2b7..312bc2c 100644 --- a/src/myapp/mutations/note.py +++ b/src/myapp/mutations/note.py @@ -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 field in funcs: - func = funcs[field] - result[field] = func(note) + 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() - log.info(result) return result diff --git a/src/myapp/mutations/page.py b/src/myapp/mutations/page.py index 74c6cc5..4daceda 100644 --- a/src/myapp/mutations/page.py +++ b/src/myapp/mutations/page.py @@ -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 field in funcs: - func = funcs[field] - result[field] = func(page) + 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() - log.info(result) return result diff --git a/src/myapp/mutations/tag.py b/src/myapp/mutations/tag.py index 1fcab7e..85677ca 100644 --- a/src/myapp/mutations/tag.py +++ b/src/myapp/mutations/tag.py @@ -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 field in funcs: - func = funcs[field] - result[field] = func(tag) + 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() return result diff --git a/src/myapp/mutations/user.py b/src/myapp/mutations/user.py index 0580f4a..7064325 100644 --- a/src/myapp/mutations/user.py +++ b/src/myapp/mutations/user.py @@ -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 field in funcs: - func = funcs[field] - result[field] = func(user) + 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() - log.info(result) return result diff --git a/src/myapp/ns_api/note.py b/src/myapp/ns_api/note.py index 553d0d1..879f69d 100644 --- a/src/myapp/ns_api/note.py +++ b/src/myapp/ns_api/note.py @@ -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') diff --git a/src/myapp/ns_api/page/common.py b/src/myapp/ns_api/page/common.py index 7d37171..02de521 100644 --- a/src/myapp/ns_api/page/common.py +++ b/src/myapp/ns_api/page/common.py @@ -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') diff --git a/src/myapp/ns_api/user.py b/src/myapp/ns_api/user.py index 43a8e8a..c5f7449 100644 --- a/src/myapp/ns_api/user.py +++ b/src/myapp/ns_api/user.py @@ -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')