mirror of
https://github.com/Tivix/django-rest-auth.git
synced 2024-11-25 18:43:44 +03:00
v0.2.2: support projects without user profiles and set defaul registration backend
This commit is contained in:
parent
217b413ba3
commit
0a1d239374
|
@ -5,6 +5,7 @@ from rest_framework import serializers
|
|||
from rest_framework.serializers import _resolve_model
|
||||
from rest_framework.authtoken.models import Token
|
||||
|
||||
profile_model_path = getattr(settings, 'REST_PROFILE_MODULE', None)
|
||||
|
||||
class LoginSerializer(serializers.Serializer):
|
||||
username = serializers.CharField(max_length=30)
|
||||
|
@ -22,28 +23,6 @@ class TokenSerializer(serializers.ModelSerializer):
|
|||
fields = ('key',)
|
||||
|
||||
|
||||
class UserRegistrationSerializer(serializers.ModelSerializer):
|
||||
|
||||
"""
|
||||
Serializer for Django User model and most of its fields.
|
||||
"""
|
||||
|
||||
class Meta:
|
||||
model = get_user_model()
|
||||
fields = ('username', 'password', 'email', 'first_name', 'last_name')
|
||||
|
||||
|
||||
class UserRegistrationProfileSerializer(serializers.ModelSerializer):
|
||||
|
||||
"""
|
||||
Serializer that includes all profile fields except for user fk / id.
|
||||
"""
|
||||
class Meta:
|
||||
model = _resolve_model(getattr(settings, 'REST_PROFILE_MODULE', None))
|
||||
fields = filter(lambda x: x != 'id' and x != 'user',
|
||||
map(lambda x: x.name, model._meta.fields))
|
||||
|
||||
|
||||
class UserDetailsSerializer(serializers.ModelSerializer):
|
||||
|
||||
"""
|
||||
|
@ -54,17 +33,15 @@ class UserDetailsSerializer(serializers.ModelSerializer):
|
|||
fields = ('username', 'email', 'first_name', 'last_name')
|
||||
|
||||
|
||||
class UserProfileSerializer(serializers.ModelSerializer):
|
||||
class UserRegistrationSerializer(serializers.ModelSerializer):
|
||||
|
||||
"""
|
||||
Serializer for UserProfile model.
|
||||
Serializer for Django User model and most of its fields.
|
||||
"""
|
||||
|
||||
user = UserDetailsSerializer()
|
||||
|
||||
class Meta:
|
||||
# http://stackoverflow.com/questions/4881607/django-get-model-from-string
|
||||
model = _resolve_model(getattr(settings, 'REST_PROFILE_MODULE', None))
|
||||
model = get_user_model()
|
||||
fields = ('username', 'password', 'email', 'first_name', 'last_name')
|
||||
|
||||
|
||||
class DynamicFieldsModelSerializer(serializers.ModelSerializer):
|
||||
|
@ -96,17 +73,50 @@ class UserUpdateSerializer(DynamicFieldsModelSerializer):
|
|||
fields = ('id', 'email', 'first_name', 'last_name')
|
||||
|
||||
|
||||
class UserProfileUpdateSerializer(serializers.ModelSerializer):
|
||||
|
||||
"""
|
||||
Serializer for updating User and UserProfile model.
|
||||
"""
|
||||
if profile_model_path:
|
||||
class UserRegistrationProfileSerializer(serializers.ModelSerializer):
|
||||
|
||||
user = UserUpdateSerializer()
|
||||
"""
|
||||
Serializer that includes all profile fields except for user fk / id.
|
||||
"""
|
||||
class Meta:
|
||||
|
||||
class Meta:
|
||||
# http://stackoverflow.com/questions/4881607/django-get-model-from-string
|
||||
model = _resolve_model(getattr(settings, 'REST_PROFILE_MODULE', None))
|
||||
model = _resolve_model(profile_model_path)
|
||||
fields = filter(lambda x: x != 'id' and x != 'user',
|
||||
map(lambda x: x.name, model._meta.fields))
|
||||
|
||||
class UserProfileSerializer(serializers.ModelSerializer):
|
||||
|
||||
"""
|
||||
Serializer for UserProfile model.
|
||||
"""
|
||||
|
||||
user = UserDetailsSerializer()
|
||||
|
||||
class Meta:
|
||||
# http://stackoverflow.com/questions/4881607/django-get-model-from-string
|
||||
model = _resolve_model(profile_model_path)
|
||||
|
||||
class UserProfileUpdateSerializer(serializers.ModelSerializer):
|
||||
|
||||
"""
|
||||
Serializer for updating User and UserProfile model.
|
||||
"""
|
||||
|
||||
user = UserUpdateSerializer()
|
||||
|
||||
class Meta:
|
||||
# http://stackoverflow.com/questions/4881607/django-get-model-from-string
|
||||
model = _resolve_model(profile_model_path)
|
||||
|
||||
else:
|
||||
class UserRegistrationProfileSerializer(serializers.Serializer):
|
||||
pass
|
||||
class UserProfileSerializer(serializers.Serializer):
|
||||
pass
|
||||
class UserProfileUpdateSerializer(serializers.Serializer):
|
||||
pass
|
||||
|
||||
|
||||
class SetPasswordSerializer(serializers.Serializer):
|
||||
|
|
|
@ -7,6 +7,7 @@ except:
|
|||
# make compatible with django 1.5
|
||||
from django.utils.http import base36_to_int as uid_decoder
|
||||
from django.conf import settings
|
||||
from django.core.exceptions import ObjectDoesNotExist
|
||||
|
||||
from rest_framework import status
|
||||
from rest_framework.views import APIView
|
||||
|
@ -32,11 +33,14 @@ from .serializers import TokenSerializer, UserDetailsSerializer, \
|
|||
|
||||
|
||||
# Get the UserProfile model from the setting value
|
||||
user_profile_model = _resolve_model(
|
||||
getattr(settings, 'REST_PROFILE_MODULE', None))
|
||||
user_profile_path = getattr(settings, 'REST_PROFILE_MODULE', None)
|
||||
user_profile_model = None
|
||||
if user_profile_path:
|
||||
user_profile_model = _resolve_model(user_profile_path)
|
||||
|
||||
# Get the REST Registration Backend for django-registration
|
||||
registration_backend = getattr(settings, 'REST_REGISTRATION_BACKEND', None)
|
||||
registration_backend = getattr(settings, 'REST_REGISTRATION_BACKEND',
|
||||
'registration.backends.default.views.RegistrationView')
|
||||
|
||||
if not registration_backend:
|
||||
raise Exception('Please configure a registration backend')
|
||||
|
@ -184,12 +188,16 @@ class UserDetails(LoggedInRESTAPIView, GenericAPIView):
|
|||
def get(self, request):
|
||||
# Create serializers with request.user and profile
|
||||
user_details = UserDetailsSerializer(request.user)
|
||||
serializer = self.serializer_class(request.user.get_profile())
|
||||
try:
|
||||
serializer = self.serializer_class(request.user.get_profile())
|
||||
profile_data = serializer.data
|
||||
except ObjectDoesNotExist:
|
||||
|
||||
profile_data = {}
|
||||
# Send the Return the User and its profile model with OK HTTP status
|
||||
return Response({
|
||||
'user': user_details.data,
|
||||
'profile': serializer.data},
|
||||
'profile': profile_data},
|
||||
status=status.HTTP_200_OK)
|
||||
|
||||
def post(self, request):
|
||||
|
|
Loading…
Reference in New Issue
Block a user