remotes/origin/hasaccess
commit
aa5a820ce7
12 changed files with 166 additions and 28 deletions
@ -0,0 +1,45 @@ |
|||||||
|
from django.contrib.auth import get_user_model |
||||||
|
from django.utils.translation import ugettext_lazy as _ |
||||||
|
|
||||||
|
from rest_framework import serializers |
||||||
|
from rest_framework.authtoken.views import ObtainAuthToken |
||||||
|
from rest_framework.compat import authenticate |
||||||
|
|
||||||
|
User = get_user_model() |
||||||
|
|
||||||
|
|
||||||
|
class AuthTokenSerializer(serializers.Serializer): |
||||||
|
email = serializers.CharField(label=_("Email")) |
||||||
|
password = serializers.CharField( |
||||||
|
label=_("Password"), |
||||||
|
style={'input_type': 'password'}, |
||||||
|
trim_whitespace=False |
||||||
|
) |
||||||
|
|
||||||
|
def validate(self, attrs): |
||||||
|
email = attrs.get('email') |
||||||
|
password = attrs.get('password') |
||||||
|
|
||||||
|
if email and password: |
||||||
|
user = authenticate(request=self.context.get('request'), |
||||||
|
email=email, password=password) |
||||||
|
|
||||||
|
# The authenticate call simply returns None for is_active=False |
||||||
|
# users. (Assuming the default ModelBackend authentication |
||||||
|
# backend.) |
||||||
|
if not user: |
||||||
|
msg = _('Unable to log in with provided credentials.') |
||||||
|
raise serializers.ValidationError(msg, code='authorization') |
||||||
|
elif user.role != User.ADMIN_ROLE: |
||||||
|
msg = _('Only admin have permission to login admin page.') |
||||||
|
raise serializers.ValidationError(msg, code='authorization') |
||||||
|
else: |
||||||
|
msg = _('Must include "email" and "password".') |
||||||
|
raise serializers.ValidationError(msg, code='authorization') |
||||||
|
|
||||||
|
attrs['user'] = user |
||||||
|
return attrs |
||||||
|
|
||||||
|
|
||||||
|
class ObtainToken(ObtainAuthToken): |
||||||
|
serializer_class = AuthTokenSerializer |
||||||
@ -0,0 +1,17 @@ |
|||||||
|
# Generated by Django 2.0.2 on 2018-02-09 08:16 |
||||||
|
|
||||||
|
from django.db import migrations |
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration): |
||||||
|
|
||||||
|
dependencies = [ |
||||||
|
('course', '0026_auto_20180208_1053'), |
||||||
|
] |
||||||
|
|
||||||
|
operations = [ |
||||||
|
migrations.RemoveField( |
||||||
|
model_name='course', |
||||||
|
name='url', |
||||||
|
), |
||||||
|
] |
||||||
@ -0,0 +1,19 @@ |
|||||||
|
# Generated by Django 2.0.2 on 2018-02-09 08:59 |
||||||
|
from uuid import uuid4 |
||||||
|
from django.db import migrations, models |
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration): |
||||||
|
|
||||||
|
dependencies = [ |
||||||
|
('course', '0027_remove_course_url'), |
||||||
|
] |
||||||
|
|
||||||
|
operations = [ |
||||||
|
migrations.AddField( |
||||||
|
model_name='course', |
||||||
|
name='slug', |
||||||
|
field=models.SlugField(allow_unicode=True, default=str(uuid4()), max_length=100, unique=True), |
||||||
|
preserve_default=False, |
||||||
|
), |
||||||
|
] |
||||||
@ -0,0 +1,19 @@ |
|||||||
|
# Generated by Django 2.0.2 on 2018-02-09 09:11 |
||||||
|
|
||||||
|
import apps.course.models |
||||||
|
from django.db import migrations, models |
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration): |
||||||
|
|
||||||
|
dependencies = [ |
||||||
|
('course', '0028_course_slug'), |
||||||
|
] |
||||||
|
|
||||||
|
operations = [ |
||||||
|
migrations.AlterField( |
||||||
|
model_name='course', |
||||||
|
name='slug', |
||||||
|
field=models.SlugField(allow_unicode=True, default=apps.course.models.default_slug, max_length=100, unique=True), |
||||||
|
), |
||||||
|
] |
||||||
Loading…
Reference in new issue