47 lines
1.6 KiB
Python
47 lines
1.6 KiB
Python
# -*- coding: utf-8 -*-
|
|
#
|
|
# ITerativ GmbH
|
|
# http://www.iterativ.ch/
|
|
#
|
|
# Copyright (c) 2019 ITerativ GmbH. All rights reserved.
|
|
#
|
|
# Created on 2019-10-08
|
|
# @author: chrigu <christian.cueni@iterativ.ch>
|
|
from django.conf import settings
|
|
from django.contrib.auth import get_user_model
|
|
from rest_framework import serializers
|
|
from rest_framework.fields import CharField, EmailField
|
|
from django.utils.translation import ugettext_lazy as _
|
|
from registration.models import License, LicenseType
|
|
|
|
|
|
class RegistrationSerializer(serializers.Serializer):
|
|
first_name = CharField(allow_blank=False)
|
|
last_name = CharField(allow_blank=False)
|
|
email = EmailField(allow_blank=False)
|
|
license_key = CharField(allow_blank=False)
|
|
skillbox_license = None
|
|
|
|
def validate_email(self, value):
|
|
|
|
lower_email = value.lower()
|
|
|
|
if not settings.USE_LOCAL_REGISTRATION:
|
|
return lower_email
|
|
|
|
# the email is used as username
|
|
if len(get_user_model().objects.filter(username=lower_email)) > 0:
|
|
raise serializers.ValidationError(_(u'Diese E-Mail ist bereits registriert'))
|
|
elif len(get_user_model().objects.filter(email=lower_email)) > 0:
|
|
raise serializers.ValidationError(_(u'Dieser E-Mail ist bereits registriert'))
|
|
else:
|
|
return lower_email
|
|
|
|
def validate_license_key(self, value):
|
|
license_types = LicenseType.objects.filter(key=value, active=True)
|
|
if len(license_types) == 0:
|
|
raise serializers.ValidationError(_(u'Die Lizenznummer ist ungültig'))
|
|
|
|
self.context['license_type'] = license_types[0] # Assuming there is just ONE license per key
|
|
return value
|