Fix code formatting

This commit is contained in:
Bruno Alla 2020-01-23 15:04:30 +00:00
parent 2833600aec
commit fa9a8cfe7b
5 changed files with 16 additions and 17 deletions

View File

@ -7,8 +7,8 @@ if settings.DEBUG:
else: else:
router = SimpleRouter() router = SimpleRouter()
router.register('users', UserViewSet) router.register("users", UserViewSet)
app_name = 'api' app_name = "api"
urlpatterns = router.urls urlpatterns = router.urls

View File

@ -308,13 +308,11 @@ STATICFILES_FINDERS += ["compressor.finders.CompressorFinder"]
# ------------------------------------------------------------------------------- # -------------------------------------------------------------------------------
# django-rest-framework - https://www.django-rest-framework.org/api-guide/settings/ # django-rest-framework - https://www.django-rest-framework.org/api-guide/settings/
REST_FRAMEWORK = { REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': ( "DEFAULT_AUTHENTICATION_CLASSES": (
'rest_framework.authentication.SessionAuthentication', "rest_framework.authentication.SessionAuthentication",
'rest_framework.authentication.TokenAuthentication', "rest_framework.authentication.TokenAuthentication",
), ),
'DEFAULT_PERMISSION_CLASSES': ( "DEFAULT_PERMISSION_CLASSES": ("rest_framework.permissions.IsAuthenticated",),
'rest_framework.permissions.IsAuthenticated',
)
} }
{%- endif %} {%- endif %}
# Your stuff... # Your stuff...

View File

@ -24,9 +24,10 @@ urlpatterns = [
# API URLS # API URLS
urlpatterns += [ urlpatterns += [
# API base url # API base url
path("api/", include('config.api_router')), path("api/", include("config.api_router")),
# DRF auth token # DRF auth token
path("auth-token/", obtain_auth_token)] path("auth-token/", obtain_auth_token),
]
{%- endif %} {%- endif %}
if settings.DEBUG: if settings.DEBUG:

View File

@ -1,12 +1,12 @@
from rest_framework import serializers from rest_framework import serializers
from {{ cookiecutter.project_slug }}.users.models import User from {{ cookiecutter.project_slug }}.users.models import User
class UserSerializer(serializers.ModelSerializer): class UserSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = User model = User
fields = ['username', 'email', 'name', 'url'] fields = ["username", "email", "name", "url"]
extra_kwargs = { extra_kwargs = {
'url': {'view_name': 'api:user-detail', 'lookup_field': 'username'} "url": {"view_name": "api:user-detail", "lookup_field": "username"}
} }

View File

@ -5,20 +5,20 @@ from rest_framework.mixins import RetrieveModelMixin, ListModelMixin, UpdateMode
from rest_framework.response import Response from rest_framework.response import Response
from rest_framework.viewsets import GenericViewSet from rest_framework.viewsets import GenericViewSet
from .serializers import UserSerializer from .serializers import UserSerializer
User = get_user_model() User = get_user_model()
class UserViewSet(RetrieveModelMixin, ListModelMixin, UpdateModelMixin, GenericViewSet): class UserViewSet(RetrieveModelMixin, ListModelMixin, UpdateModelMixin, GenericViewSet):
serializer_class = UserSerializer serializer_class = UserSerializer
queryset = User.objects.all() queryset = User.objects.all()
lookup_field = 'username' lookup_field = "username"
def get_queryset(self, *args, **kwargs): def get_queryset(self, *args, **kwargs):
return self.queryset.filter(id=self.request.user.id) return self.queryset.filter(id=self.request.user.id)
@action(detail=False, methods=['GET']) @action(detail=False, methods=["GET"])
def me(self, request): def me(self, request):
serializer = UserSerializer(request.user, context={'request': request}) serializer = UserSerializer(request.user, context={"request": request})
return Response(status=status.HTTP_200_OK, data=serializer.data) return Response(status=status.HTTP_200_OK, data=serializer.data)