コード例 #1
0
def registerAPI(request):
    if request.method == 'POST':
        data = {}

        email = request.data.get('email', '0').lower()
        if check_email(email) != None:
            data['error_message'] = ''
            data['response'] = 'Error'
            return Response({"error":"That email is already in use."}, status=status.HTTP_400_BAD_REQUEST)


    serializer = RegisterSerializer(data=request.data)

    if serializer.is_valid():
        account = serializer.save()
        # profile = Profile.objects.get(account=account)

        data['response'] = f'Account created successfully for {account.email}, you can now login'
        # data['email'] = account.email
        # data['first_name'] = account.first_name
        # data['last_name'] = account.last_name
        # data['pk'] = account.pk
        # token = Token.objects.get(user=account).key
        # data['token'] = token
        # data['slug'] = profile.slug
        # data['profile_pic'] = profile.profile_pic
    else:
        data = serializer.errors
    return Response(data)
コード例 #2
0
    def test_correct_validation(self):
        self.RegisterAtt2 = copy.copy(self.RegisterAtt)
        self.RegisterAtt2['password_repeat'] = 'psw2'
        self.serializer2 = RegisterSerializer(data=self.RegisterAtt2)

        self.serializer.is_valid()
        self.serializer2.is_valid()

        self.assertEqual(set(self.serializer2.errors),
                         set(['non_field_errors']))
        self.assertEqual(set(self.serializer.errors), set([]))
コード例 #3
0
class PasswordUpdateSerializerTest(TestCase):
    def setUp(self):
        self.RegisterAtt = {
            'username': '******',
            'email': '*****@*****.**',
            'first_name': 'jan',
            'last_name': 'kowalski',
            'password': '******',
            'password_repeat': 'psw'
        }

        self.NewPasswordAtt = {
            'password': '******',
            'new_password': '******',
            'new_password_repeat': 'pws'
        }

        self.RegSerializer = RegisterSerializer(data=self.RegisterAtt)
        self.RegSerializer.is_valid()
        self.created_user = self.RegSerializer.save()
        self.serializer = PasswordUpdateSerializer(data=self.NewPasswordAtt,
                                                   instance=self.created_user)

    def test_validation_password(self):
        self.NewPasswordAtt2 = copy.copy(self.NewPasswordAtt)
        self.NewPasswordAtt2['password'] = '******'
        self.serializer2 = PasswordUpdateSerializer(data=self.NewPasswordAtt2,
                                                    instance=self.created_user)

        self.serializer.is_valid()
        self.serializer2.is_valid()

        self.assertEqual(set(self.serializer.errors), set([]))
        self.assertEqual(set(self.serializer2.errors), set(['password']))

    def test_validation(self):
        self.NewPasswordAtt2 = copy.copy(self.NewPasswordAtt)
        self.NewPasswordAtt3 = copy.copy(self.NewPasswordAtt)
        self.NewPasswordAtt2['new_password'] = '******'
        self.NewPasswordAtt3['new_password'] = '******'
        self.NewPasswordAtt3['new_password_repeat'] = 'psw'
        self.serializer2 = PasswordUpdateSerializer(data=self.NewPasswordAtt2,
                                                    instance=self.created_user)
        self.serializer3 = PasswordUpdateSerializer(data=self.NewPasswordAtt3,
                                                    instance=self.created_user)

        self.serializer.is_valid()
        self.serializer2.is_valid()
        self.serializer3.is_valid()

        self.assertEqual(set(self.serializer.errors), set([]))
        self.assertEqual(set(self.serializer2.errors),
                         set(['non_field_errors']))
        self.assertEqual(set(self.serializer3.errors), set([]))
コード例 #4
0
    def setUp(self):
        self.RegisterAtt = {
            'username': '******',
            'email': '*****@*****.**',
            'first_name': 'jan',
            'last_name': 'kowalski',
            'password': '******',
            'password_repeat': 'psw'
        }

        self.serializer = RegisterSerializer(data=self.RegisterAtt)
コード例 #5
0
def registration(request):
	serializer = RegisterSerializer(data=request.data)
	if not serializer.is_valid():
		return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
	user = serializer.save()
	refresh = RefreshToken.for_user(user)
	res = {
		"refresh": str(refresh),
		"access": str(refresh.access_token),
	}
	return Response(res, status=status.HTTP_201_CREATED)
コード例 #6
0
    def post(self, request, format='json', *args, **kwargs):
        serializer = RegisterSerializer(data=request.data)
        if serializer.is_valid():
            user = serializer.save()
            data = serializer.data
            if user.user_type == 'S':
                data['student_id'] = user.student.student_id

            if user:
                return Response(data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
コード例 #7
0
def register_view(request):

    if request.method == "Post":
        serializer = RegisterSerializer(data=request.data)
        data = {}
        if serializer.is_valid():
            account = serializer.save()
            data['response'] = "success"
            data['email'] = account.email
            data['username'] = account.username
        else:
            data = serializer.errors
        return Response(data)
コード例 #8
0
def api_register_view(request):

    if request.method == 'POST':
        serializer = RegisterSerializer(data=request.data)
        data = {}
        if serializer.is_valid():
            account = serializer.save(
            )  #call the  save method we have overwritten
            data['response'] = 'successfully registered a new user'
            data['email'] = account.email
            data['username'] = account.username
        else:
            data = serializer.errors
        return Response(data)
コード例 #9
0
def registration_view(request):

    if request.method == 'POST':
        serializer = RegisterSerializer(data=request.data)
        data = {}
        if serializer.is_valid():
            account = serializer.save()
            data['response'] = 'successfully registered new user'
            data['email'] = account.email
            data['username'] = account.username
            token = Token.objects.get(user=account).key
            data['token'] = token
        else:
            data = serializer.errors
        return Response(data)
コード例 #10
0
class LoginSerializerTest(TestCase):
    def setUp(self):
        self.RegisterAtt = {
            'username': '******',
            'email': '*****@*****.**',
            'first_name': 'jan',
            'last_name': 'kowalski',
            'password': '******',
            'password_repeat': 'psw'
        }

        self.LoginAtt = {
            'username': '******',
            'password': '******',
        }

        self.serializer = LoginSerializer(data=self.LoginAtt)
        self.RegSerializer = RegisterSerializer(data=self.RegisterAtt)
        self.RegSerializer.is_valid()
        self.created_user = self.RegSerializer.save()

    def test_contains_expected_fields(self):
        expected_fields = ['username', 'password']

        self.serializer.is_valid()
        self.assertEqual(set(self.serializer.data.keys()),
                         set(expected_fields))

    def test_validation(self):
        self.LoginAtt2 = copy.copy(self.LoginAtt)
        self.LoginAtt3 = copy.copy(self.LoginAtt)
        self.LoginAtt2['password'] = '******'
        self.LoginAtt3['username'] = '******'
        self.serializer2 = LoginSerializer(data=self.LoginAtt2)
        self.serializer3 = LoginSerializer(data=self.LoginAtt3)

        self.serializer.is_valid()
        self.serializer2.is_valid()
        self.serializer3.is_valid()

        self.assertEqual(set(self.serializer.errors), set([]))
        self.assertEqual(set(self.serializer2.errors),
                         set(['non_field_errors']))
        self.assertEqual(set(self.serializer3.errors),
                         set(['non_field_errors']))
コード例 #11
0
    def setUp(self):
        self.RegisterAtt = {
            'username': '******',
            'email': '*****@*****.**',
            'first_name': 'jan',
            'last_name': 'kowalski',
            'password': '******',
            'password_repeat': 'psw'
        }

        self.LoginAtt = {
            'username': '******',
            'password': '******',
        }

        self.serializer = LoginSerializer(data=self.LoginAtt)
        self.RegSerializer = RegisterSerializer(data=self.RegisterAtt)
        self.RegSerializer.is_valid()
        self.created_user = self.RegSerializer.save()
コード例 #12
0
    def setUp(self):
        self.RegisterAtt = {
            'username': '******',
            'email': '*****@*****.**',
            'first_name': 'jan',
            'last_name': 'kowalski',
            'password': '******',
            'password_repeat': 'psw'
        }

        self.NewPasswordAtt = {
            'password': '******',
            'new_password': '******',
            'new_password_repeat': 'pws'
        }

        self.RegSerializer = RegisterSerializer(data=self.RegisterAtt)
        self.RegSerializer.is_valid()
        self.created_user = self.RegSerializer.save()
        self.serializer = PasswordUpdateSerializer(data=self.NewPasswordAtt,
                                                   instance=self.created_user)
コード例 #13
0
class RegisterSerializerTests(TestCase):
    def setUp(self):
        self.RegisterAtt = {
            'username': '******',
            'email': '*****@*****.**',
            'first_name': 'jan',
            'last_name': 'kowalski',
            'password': '******',
            'password_repeat': 'psw'
        }

        self.serializer = RegisterSerializer(data=self.RegisterAtt)

    def test_contains_expected_fields(self):
        expected_fields = ['username', 'email', 'first_name', 'last_name']

        self.serializer.is_valid()
        self.assertEqual(set(self.serializer.data.keys()),
                         set(expected_fields))

    def test_correct_validation(self):
        self.RegisterAtt2 = copy.copy(self.RegisterAtt)
        self.RegisterAtt2['password_repeat'] = 'psw2'
        self.serializer2 = RegisterSerializer(data=self.RegisterAtt2)

        self.serializer.is_valid()
        self.serializer2.is_valid()

        self.assertEqual(set(self.serializer2.errors),
                         set(['non_field_errors']))
        self.assertEqual(set(self.serializer.errors), set([]))

    def test_field_content(self):
        self.serializer.is_valid()
        data = self.serializer.data

        self.assertEqual(data['username'], self.RegisterAtt['username'])
        self.assertEqual(data['email'], self.RegisterAtt['email'])
        self.assertEqual(data['first_name'], self.RegisterAtt['first_name'])
        self.assertEqual(data['last_name'], self.RegisterAtt['last_name'])