-
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #150 from NilCoalescing/fix/#148_drf-permisions-cl…
…asses Wrap DRF permissions classes.
- Loading branch information
Showing
9 changed files
with
272 additions
and
43 deletions.
There are no files selected for viewing
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import asyncio | ||
from typing import Any, Dict, Callable | ||
|
||
from channels.db import database_sync_to_async | ||
from django.http import HttpRequest | ||
|
||
|
||
def ensure_async(method: Callable): | ||
""" | ||
Ensure method is async if not wrap it in database_sync_to_async. | ||
""" | ||
if asyncio.iscoroutinefunction(method): | ||
return method | ||
return database_sync_to_async(method) | ||
|
||
|
||
def request_from_scope(scope: Dict[str, Any]) -> HttpRequest: | ||
from django.contrib.auth.models import AnonymousUser | ||
|
||
request = HttpRequest() | ||
request.path = scope.get("path") | ||
request.session = scope.get("session", None) | ||
request.user = scope.get("user", AnonymousUser) | ||
|
||
request.META["HTTP_CONTENT_TYPE"] = "application/json" | ||
request.META["HTTP_ACCEPT"] = "application/json" | ||
|
||
for (header_name, value) in scope.get("headers", []): | ||
request.META[header_name.decode("utf-8")] = value.decode("utf-8") | ||
|
||
if scope.get("cookies"): | ||
request.COOKIES = scope.get("cookies") | ||
return request |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,65 @@ | ||
from django.conf import settings | ||
|
||
from rest_framework.settings import APISettings | ||
from django.core.signals import setting_changed | ||
from rest_framework.settings import perform_import | ||
|
||
DEFAULTS = { | ||
"DEFAULT_PAGE_SIZE": 25, | ||
"DEFAULT_PERMISSION_CLASSES": ("djangochannelsrestframework.permissions.AllowAny",), | ||
"DEFAULT_PAGINATION_CLASS": None, | ||
"PAGE_SIZE": None, | ||
} | ||
IMPORT_STRINGS = ("DEFAULT_PERMISSION_CLASSES",) | ||
IMPORT_STRINGS = ("DEFAULT_PERMISSION_CLASSES", "DEFAULT_PAGINATION_CLASS") | ||
|
||
|
||
class APISettings: | ||
def __init__(self, user_settings=None, defaults=None, import_strings=None): | ||
if user_settings: | ||
self._user_settings = user_settings | ||
self.defaults = defaults or DEFAULTS | ||
self.import_strings = import_strings or IMPORT_STRINGS | ||
self._cached_attrs = set() | ||
|
||
@property | ||
def user_settings(self): | ||
if not hasattr(self, "_user_settings"): | ||
self._user_settings = getattr(settings, "DJANGO_CHANNELS_REST_API", {}) | ||
return self._user_settings | ||
|
||
def __getattr__(self, attr): | ||
if attr not in self.defaults: | ||
raise AttributeError("Invalid API setting: '%s'" % attr) | ||
|
||
try: | ||
# Check if present in user settings | ||
val = self.user_settings[attr] | ||
except KeyError: | ||
# Fall back to defaults | ||
val = self.defaults[attr] | ||
|
||
# Coerce import strings into classes | ||
if attr in self.import_strings: | ||
val = perform_import(val, attr) | ||
|
||
# Cache the result | ||
self._cached_attrs.add(attr) | ||
setattr(self, attr, val) | ||
return val | ||
|
||
def reload(self): | ||
for attr in self._cached_attrs: | ||
delattr(self, attr) | ||
self._cached_attrs.clear() | ||
if hasattr(self, "_user_settings"): | ||
delattr(self, "_user_settings") | ||
|
||
|
||
api_settings = APISettings(None, DEFAULTS, IMPORT_STRINGS) | ||
|
||
|
||
def reload_api_settings(*args, **kwargs): | ||
setting = kwargs["setting"] | ||
if setting == "DJANGO_CHANNELS_REST_API": | ||
api_settings.reload() | ||
|
||
|
||
api_settings = APISettings( | ||
getattr(settings, "DJANGO_CHANNELS_REST_API", None), DEFAULTS, IMPORT_STRINGS | ||
) | ||
setting_changed.connect(reload_api_settings) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
[tool:pytest] | ||
addopts = tests/ |
Oops, something went wrong.