Commit a294649e authored by Michael Fladischer's avatar Michael Fladischer

Import djoser_0.6.0.orig.tar.gz

parent 92303734
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
......@@ -12,7 +12,6 @@ djoser/views.py
djoser.egg-info/PKG-INFO
djoser.egg-info/SOURCES.txt
djoser.egg-info/dependency_links.txt
djoser.egg-info/requires.txt
djoser.egg-info/top_level.txt
djoser/templates/activation_email_body.txt
djoser/templates/activation_email_subject.txt
......
Django>=1.7
djangorestframework>=3.0.0
......@@ -9,3 +9,4 @@ PASSWORD_MISMATCH_ERROR = _('The two password fields didn\'t match.')
USERNAME_MISMATCH_ERROR = _('The two {0} fields didn\'t match.')
INVALID_PASSWORD_ERROR = _('Invalid password.')
EMAIL_NOT_FOUND = _('User with given email does not exist.')
CANNOT_CREATE_USER_ERROR = _('Unable to create account.')
from django.contrib.auth import authenticate, get_user_model
from django.db import transaction
from django.db import IntegrityError, transaction
from django.utils import six
from django.utils.module_loading import import_string
......@@ -8,12 +8,10 @@ from rest_framework.authtoken.models import Token
from . import constants, utils, settings
User = get_user_model()
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = tuple(User.REQUIRED_FIELDS) + (
......@@ -26,31 +24,45 @@ class UserSerializer(serializers.ModelSerializer):
class UserRegistrationSerializer(serializers.ModelSerializer):
password = serializers.CharField(style={'input_type': 'password'},
write_only=True,
validators=settings.get('PASSWORD_VALIDATORS'))
password = serializers.CharField(
style={'input_type': 'password'},
write_only=True,
validators=settings.get('PASSWORD_VALIDATORS')
)
default_error_messages = {
'cannot_create_user': constants.CANNOT_CREATE_USER_ERROR,
}
class Meta:
model = User
fields = tuple(User.REQUIRED_FIELDS) + (
User.USERNAME_FIELD,
User._meta.pk.name,
'password',
User.USERNAME_FIELD, User._meta.pk.name, 'password',
)
def create(self, validated_data):
if settings.get('SEND_ACTIVATION_EMAIL'):
with transaction.atomic():
user = User.objects.create_user(**validated_data)
try:
user = self.perform_create(validated_data)
except IntegrityError:
raise serializers.ValidationError(
self.error_messages['cannot_create_user']
)
return user
def perform_create(self, validated_data):
with transaction.atomic():
user = User.objects.create_user(**validated_data)
if settings.get('SEND_ACTIVATION_EMAIL'):
user.is_active = False
user.save(update_fields=['is_active'])
else:
user = User.objects.create_user(**validated_data)
return user
class LoginSerializer(serializers.Serializer):
password = serializers.CharField(required=False, style={'input_type': 'password'})
password = serializers.CharField(
required=False, style={'input_type': 'password'}
)
default_error_messages = {
'inactive_account': constants.INACTIVE_ACCOUNT_ERROR,
......@@ -63,13 +75,20 @@ class LoginSerializer(serializers.Serializer):
self.fields[User.USERNAME_FIELD] = serializers.CharField(required=False)
def validate(self, attrs):
self.user = authenticate(username=attrs.get(User.USERNAME_FIELD), password=attrs.get('password'))
self.user = authenticate(
username=attrs.get(User.USERNAME_FIELD),
password=attrs.get('password')
)
if self.user:
if not self.user.is_active:
raise serializers.ValidationError(self.error_messages['inactive_account'])
raise serializers.ValidationError(
self.error_messages['inactive_account']
)
return attrs
else:
raise serializers.ValidationError(self.error_messages['invalid_credentials'])
raise serializers.ValidationError(
self.error_messages['invalid_credentials']
)
class PasswordResetSerializer(serializers.Serializer):
......
......@@ -28,11 +28,12 @@ def login_user(request, user):
def logout_user(request):
authtoken.models.Token.objects.filter(user=request.user).delete()
user_logged_out.send(sender=request.user.__class__, request=request, user=request.user)
user_logged_out.send(
sender=request.user.__class__, request=request, user=request.user
)
class ActionViewMixin(object):
def post(self, request):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
......@@ -45,44 +46,60 @@ class UserEmailFactoryBase(object):
plain_body_template_name = None
html_body_template_name = None
def __init__(self, from_email, user, protocol, domain, site_name):
def __init__(self, from_email, user, protocol, domain, site_name, **context):
self.from_email = from_email
self.user = user
self.domain = domain
self.site_name = site_name
self.protocol = protocol
self.context_data = context
@classmethod
def from_request(cls, request, user=None, from_email=None):
def from_request(cls, request, user=None, from_email=None, **context):
site = get_current_site(request)
from_email = from_email or getattr(
django_settings, 'DEFAULT_FROM_EMAIL', ''
)
return cls(
from_email=getattr(django_settings, 'DEFAULT_FROM_EMAIL', from_email),
from_email=from_email,
user=user or request.user,
domain=django_settings.DJOSER.get('DOMAIN') or site.domain,
site_name=django_settings.DJOSER.get('SITE_NAME') or site.name,
protocol='https' if request.is_secure() else 'http',
**context
)
def create(self,):
def create(self):
assert self.plain_body_template_name or self.html_body_template_name
context = self.get_context()
subject = loader.render_to_string(self.subject_template_name, context)
subject = ''.join(subject.splitlines())
if self.plain_body_template_name:
plain_body = loader.render_to_string(self.plain_body_template_name, context)
email_message = EmailMultiAlternatives(subject, plain_body, self.from_email, [self.user.email])
plain_body = loader.render_to_string(
self.plain_body_template_name, context
)
email_message = EmailMultiAlternatives(
subject, plain_body, self.from_email, [self.user.email]
)
if self.html_body_template_name:
html_body = loader.render_to_string(self.html_body_template_name, context)
html_body = loader.render_to_string(
self.html_body_template_name, context
)
email_message.attach_alternative(html_body, 'text/html')
else:
html_body = loader.render_to_string(self.html_body_template_name, context)
email_message = EmailMessage(subject, html_body, self.from_email, [self.user.email])
html_body = loader.render_to_string(
self.html_body_template_name, context
)
email_message = EmailMessage(
subject, html_body, self.from_email, [self.user.email]
)
email_message.content_subtype = 'html'
return email_message
def get_context(self):
return {
context = {
'user': self.user,
'domain': self.domain,
'site_name': self.site_name,
......@@ -90,6 +107,8 @@ class UserEmailFactoryBase(object):
'token': self.token_generator.make_token(self.user),
'protocol': self.protocol,
}
context.update(self.context_data)
return context
class UserActivationEmailFactory(UserEmailFactoryBase):
......@@ -108,7 +127,9 @@ class UserPasswordResetEmailFactory(UserEmailFactoryBase):
def get_context(self):
context = super(UserPasswordResetEmailFactory, self).get_context()
context['url'] = settings.get('PASSWORD_RESET_CONFIRM_URL').format(**context)
context['url'] = settings.get('PASSWORD_RESET_CONFIRM_URL').format(
**context
)
return context
......
from django.contrib.auth.tokens import default_token_generator
from django.contrib.auth import get_user_model
from rest_framework import generics, permissions, status, response, views
from rest_framework import generics, permissions, status, views
from rest_framework.response import Response
from rest_framework.reverse import reverse
from . import serializers, settings, utils, signals
User = get_user_model()
......@@ -55,27 +54,28 @@ class RegistrationView(generics.CreateAPIView):
def perform_create(self, serializer):
user = serializer.save()
signals.user_registered.send(sender=self.__class__, user=user, request=self.request)
signals.user_registered.send(
sender=self.__class__, user=user, request=self.request
)
if settings.get('SEND_ACTIVATION_EMAIL'):
self.send_activation_email(user)
elif settings.get('SEND_CONFIRMATION_EMAIL'):
self.send_confirmation_email(user)
def send_activation_email(self, user):
email_factory = utils.UserActivationEmailFactory.from_request(self.request, user=user)
email_factory = utils.UserActivationEmailFactory.from_request(
self.request, user=user
)
email = email_factory.create()
email.send()
def send_confirmation_email(self, user):
email_factory = utils.UserConfirmationEmailFactory.from_request(self.request, user=user)
email_factory = utils.UserConfirmationEmailFactory.from_request(
self.request, user=user
)
email = email_factory.create()
email.send()
def get_email_context(self, user):
context = super(RegistrationView, self).get_email_context(user)
context['url'] = settings.get('ACTIVATION_URL').format(**context)
return context
class LoginView(utils.ActionViewMixin, generics.GenericAPIView):
"""
......@@ -105,7 +105,7 @@ class LogoutView(views.APIView):
def post(self, request):
utils.logout_user(request)
return response.Response(status=status.HTTP_204_NO_CONTENT)
return Response(status=status.HTTP_204_NO_CONTENT)
class PasswordResetView(utils.ActionViewMixin, generics.GenericAPIView):
......@@ -122,7 +122,7 @@ class PasswordResetView(utils.ActionViewMixin, generics.GenericAPIView):
def _action(self, serializer):
for user in self.get_users(serializer.data['email']):
self.send_password_reset_email(user)
return response.Response(status=status.HTTP_204_NO_CONTENT)
return Response(status=status.HTTP_204_NO_CONTENT)
def get_users(self, email):
if self._users is None:
......@@ -159,7 +159,7 @@ class SetPasswordView(utils.ActionViewMixin, generics.GenericAPIView):
if settings.get('LOGOUT_ON_PASSWORD_CHANGE'):
utils.logout_user(self.request)
return response.Response(status=status.HTTP_204_NO_CONTENT)
return Response(status=status.HTTP_204_NO_CONTENT)
class PasswordResetConfirmView(utils.ActionViewMixin, generics.GenericAPIView):
......@@ -179,7 +179,7 @@ class PasswordResetConfirmView(utils.ActionViewMixin, generics.GenericAPIView):
def _action(self, serializer):
serializer.user.set_password(serializer.data['new_password'])
serializer.user.save()
return response.Response(status=status.HTTP_204_NO_CONTENT)
return Response(status=status.HTTP_204_NO_CONTENT)
class ActivationView(utils.ActionViewMixin, generics.GenericAPIView):
......@@ -221,7 +221,7 @@ class SetUsernameView(utils.ActionViewMixin, generics.GenericAPIView):
def _action(self, serializer):
setattr(self.request.user, User.USERNAME_FIELD, serializer.data['new_' + User.USERNAME_FIELD])
self.request.user.save()
return response.Response(status=status.HTTP_204_NO_CONTENT)
return Response(status=status.HTTP_204_NO_CONTENT)
class UserView(generics.RetrieveUpdateAPIView):
......
[egg_info]
tag_build =
tag_date = 0
tag_svn_revision = 0
#!/usr/bin/env python
import io
import os
from io import open
from setuptools import setup
......@@ -9,7 +9,7 @@ try:
import pypandoc
description = pypandoc.convert('README.md', 'rst')
except (IOError, ImportError):
description = io.open('README.md', encoding='utf-8').read()
description = open('README.md', encoding='utf-8').read()
def get_packages(package):
......@@ -18,20 +18,16 @@ def get_packages(package):
if os.path.exists(os.path.join(dirpath, '__init__.py'))]
def get_requirements(file_name):
return [i.strip() for i in open(file_name).readlines()]
setup(
name='djoser',
version='0.5.2',
version='0.6.0',
packages=get_packages('djoser'),
license='MIT',
author='SUNSCRAPERS',
description='REST version of Django authentication system.',
author_email='info@sunscrapers.com',
long_description=description,
install_requires=get_requirements('requirements.txt'),
install_requires=[],
include_package_data=True,
url='https://github.com/sunscrapers/djoser',
classifiers=[
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment