server/ammicosrv/authentication/serializers.py
changeset 156 bf4ae7d9a517
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/server/ammicosrv/authentication/serializers.py	Wed Jun 10 15:19:07 2015 +0200
@@ -0,0 +1,35 @@
+from django.contrib.auth import authenticate, get_user_model
+from django.utils.translation import ugettext_lazy as _
+from rest_framework import exceptions, serializers
+
+
+class AuthTokenSerializer(serializers.Serializer):
+    email = serializers.CharField()
+    password = serializers.CharField(style={'input_type': 'password'})
+
+    def validate(self, attrs):
+        email = attrs.get('email')
+        password = attrs.get('password')
+
+        if email and password:
+            user = authenticate(email=email, password=password)
+
+            if user:
+                if not user.is_active:
+                    msg = _('User account is disabled.')
+                    raise exceptions.ValidationError(msg)
+            else:
+                msg = _('Unable to log in with provided credentials.')
+                raise exceptions.ValidationError(msg)
+        else:
+            msg = _('Must include "email" and "password".')
+            raise exceptions.ValidationError(msg)
+
+        attrs['user'] = user
+        return attrs
+
+class UserSerializer(serializers.ModelSerializer):
+    
+    class Meta:
+        model = get_user_model()
+        fields = ('id', 'email', 'last_login', 'id_user')
\ No newline at end of file