Skip to content

Commit

Permalink
Remove get_parser_context, change rest_channels decorator to django m…
Browse files Browse the repository at this point in the history
…ethod_decorator.
  • Loading branch information
khasanovbi committed May 8, 2016
1 parent 6e0a8fe commit 1c7ce88
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 50 deletions.
6 changes: 3 additions & 3 deletions docs/guide/view.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ For example:
route(
'websocket.receive',
CompositionSocketView.as_view(),
path=r'/(?P<composition_id>\d+)/$'
path=r'ws/composition/(?P<composition_id>\d+)/$'
),
...
]
Expand All @@ -26,8 +26,8 @@ For example:
class CompositionSocketView(SocketView):
COMPOSITION_GROUP_TEMPLATE = 'Composition-%s'
# wrap django-channels decorators to `rest_channels` decorator
@rest_channels(channel_session)
# wrap django-channels decorators to standard django decorator `method_decorator`
@method_decorator(channel_session)
def receive(self, request, *args, **kwargs):
# Get composition id from path
composition_id = kwargs.get('composition_id')
Expand Down
2 changes: 0 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@
* [View][view]
* [Parsers][parsers]
* [Renderers][renderers]
* [Decorators][decorators]
* [Settings][settings]

[request]: guide/request.md
[view]: guide/view.md
[parsers]: guide/parsers.md
[renderers]: guide/renderers.md
[decorators]: guide/decorators.md
[settings]: guide/settings.md

## Socket Routing
Expand Down
16 changes: 0 additions & 16 deletions rest_channels/decorators.py

This file was deleted.

4 changes: 2 additions & 2 deletions rest_channels/socket_routing/route_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
from __future__ import unicode_literals

from channels.sessions import channel_session
from django.utils.decorators import method_decorator
from rest_framework import status as rest_framework_status

from rest_channels.decorators import rest_channels
from rest_channels.socket_routing.serializers import RouteSerializer, RouteResponseSerializer
from rest_channels.views import SocketView


class SocketRouteView(SocketView):
@rest_channels(channel_session)
@method_decorator(channel_session)
def receive(self, request, *args, **kwargs):
serializer = RouteSerializer(data=request.data, context={'view': self})
serializer.is_valid(raise_exception=True)
Expand Down
34 changes: 7 additions & 27 deletions rest_channels/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@

def exception_handler(exc, context):
if isinstance(exc, exceptions.APIException):
# headers = {}
if isinstance(exc.detail, (list, dict)):
if isinstance(exc.detail, dict):
data = exc.detail
else:
data = {'detail': exc.detail}
data.update({'status': exc.status_code})
set_rollback()
return data
# Note: Unhandled exceptions will raise a 500 error.
Expand Down Expand Up @@ -64,7 +64,6 @@ def force_evaluation():
cls.queryset._fetch_all = force_evaluation
cls.queryset._result_iter = force_evaluation # Django <= 1.5

# ######################
for key in initkwargs:
if key in cls.socket_channel_names:
raise TypeError("You tried to pass in the %s method name as a "
Expand All @@ -85,13 +84,8 @@ def view(message, *args, **kwargs):
view.view_class = cls
view.view_initkwargs = initkwargs

# take name and docstring from class
update_wrapper(view, cls, updated=())

# and possible attributes set by decorators
# like csrf_exempt from dispatch
update_wrapper(view, cls.dispatch, assigned=())
##########################
view.cls = cls
return view

Expand All @@ -110,19 +104,6 @@ def not_allowed(self, request, *args, **kwargs):
"""
raise rest_exceptions.EventNotAllowed(request.channel.name)

def get_parser_context(self, http_request):
"""
Returns a dict that is passed through to Parser.parse(),
as the `parser_context` keyword argument.
"""
# Note: Additionally `request` and `encoding` will also be added
# to the context by the RESTMessage object.
return {
'view': self,
'args': getattr(self, 'args', ()),
'kwargs': getattr(self, 'kwargs', {})
}

def get_exception_handler_context(self):
"""
Returns a dict that is passed through to EXCEPTION_HANDLER,
Expand Down Expand Up @@ -154,14 +135,13 @@ def initialize_request(self, message, *args, **kwargs):
)

def get_renderer(self):
if self.message.content_type == ContentType.text:
if self.request.content_type == ContentType.text:
return self.text_renderer_class
else:
return self.bytes_renderer_class

def get_rendered_data(self, data):
renderer = self.get_renderer()
# TODO понять на каком моменте инициализация
rendered_data = renderer().render(data=data)
return rendered_data

Expand All @@ -174,13 +154,13 @@ def handle_exception(self, exc):
if response is None:
raise

self.send(self.message.reply_channel, response)
self.send(self.request.reply_channel, response)

def dispatch(self, request, *args, **kwargs):
def dispatch(self, message, *args, **kwargs):
self.args = args
self.kwargs = kwargs
request = self.initialize_request(request, *args, **kwargs)
self.message = request
request = self.initialize_request(message, *args, **kwargs)
self.request = request
try:
channel_name = request.channel.name
if channel_name in self.socket_channel_names:
Expand Down

0 comments on commit 1c7ce88

Please sign in to comment.