def create_student(request):
    if not get_config('proso_user', 'allow_create_students', default=False):
        return render_json(request, {
            'error': _('Creation of new users is not allowed.'),
            'error_type': 'student_creation_not_allowed'
        }, template='class_create_student.html', help_text=create_student.__doc__, status=403)
    if request.method == 'GET':
        return render(request, 'class_create_student.html', {}, help_text=create_student.__doc__)
    if request.method == 'POST':
        if not request.user.is_authenticated() or not hasattr(request.user, "userprofile"):
            return render_json(request, {
                'error': _('User is not logged in.'),
                'error_type': 'user_unauthorized'
            }, template='class_create_student.html', status=401)
        data = json_body(request.body.decode("utf-8"))
        try:
            cls = Class.objects.get(pk=data['class'], owner=request.user.userprofile)
        except (Class.DoesNotExist, KeyError):
            return render_json(request, {
                'error': _('Class with given id not found.'),
                'error_type': 'class_not_found',
            }, template='class_create_student.html', status=404)
        if 'first_name' not in data or not data['first_name']:
            return render_json(request, {
                'error': _('First name code is missing.'),
                'error_type': 'missing_first_name'
            }, template='class_create_student.html', status=400)
        user = User(first_name=data['first_name'])
        if data.get('last_name'):
            user.last_name = data['last_name']
        if data.get('email'):
            if User.objects.filter(email=data['email']).exists():
                return render_json(request, {
                    'error': _('There is already a user with the given e-mail.'),
                    'error_type': 'email_exists'
                }, template='class_create_student.html', status=400)
            user.email = data['email']
        if data.get('username'):
            if User.objects.filter(username=data['username']).exists():
                return render_json(request, {
                    'error': _('There is already a user with the given username.'),
                    'error_type': 'username_exists'
                }, template='class_create_student.html', status=400)
            user.username = data['username']
        else:
            user.username = get_unused_username(user)
        if data.get('password'):
            user.set_password(data['password'])
        user.save()
        cls.members.add(user.userprofile)
        return render_json(request, user.userprofile.to_json(nested=True), template='class_create_student.html', status=201)
    else:
        return HttpResponseBadRequest("method %s is not allowed".format(request.method))