Created
August 2, 2014 20:40
-
-
Save svetlyak40wt/411482dff45b28e75b09 to your computer and use it in GitHub Desktop.
Кусок адского мега-фреймворка, по типу БЭМ, но для питона и извращений типа bem-tools :)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ... | |
def block(**modifiers): | |
def decorator(func): | |
@wraps(func) | |
def wrapper(*args, **kwargs): | |
"""Этот декоратор реализует отложенный вызов функции, которую он декорирует. | |
По сути, он возвращает promice, который при вызове, уже зовет реальную функцию. | |
""" | |
def ret(): | |
return func(*args, **kwargs) | |
return ret | |
registry[(func.__name__, dict_to_key(modifiers))] = wrapper | |
return wrapper | |
return decorator | |
# ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# пример реализации блока с комментами | |
# для простоты, комменты хранятся в памяти, но тут может быть что угодно | |
from blocks import ImmediateResponse | |
from django.http import HttpResponseRedirect | |
_comments = [] | |
@block(method='GET') | |
def comments(request): | |
from blocks import blocks as b | |
form = b.form(fields=[b.text_field(name='comment_text')], | |
button=b.submit_button(), | |
action='.') | |
return render_to_string('blocks/comments.html', dict( | |
comments=_comments, | |
form=form)) | |
@block(method='POST') | |
def comments(request): | |
_comments.append(request.POST['comment_text']) | |
raise ImmediateResponse(HttpResponseRedirect('.')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# А вот так будет выглядеть вьюшка, рисующая страничку с контентом, комментами и формой добавления нового коммента: | |
@returns_blocks | |
def block_view(request): | |
from .blocks import blocks as b | |
return b.page(main_content=b.test_block(), | |
menu=b.menu(items=[b.menu_item(label='One'), | |
b.menu_item(label='two', selected=True)]), | |
comments=b.comments(request=request, method=request.method), | |
with_menu=True) | |
# вся логика обработки GET/POST инкапсулирована внутри блоков |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment