Join a class

    POST parameters (JSON):
        code:
            code of the class

def join_class(request):
    """Join a class

    POST parameters (JSON):
        code:
            code of the class
    """

    if request.method == 'GET':
        return render(request, 'classes_join.html', {}, help_text=join_class.__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='classes_join.html', status=401)

        data = json_body(request.body.decode("utf-8"))

        if 'code' not in data or not data['code']:
            return render_json(request, {'error': _('Class code is missing.'), 'error_type': 'missing_class_code'},
                               template='classes_join.html', status=400)

        try:
            cls = Class.objects.get(code=data['code'])
        except Class.DoesNotExist:
            return render_json(request, {
                'error': _('Class with given code not found.'),
                'error_type': 'class_not_found',
            }, template='classes_join.html', status=404)

        cls.members.add(request.user.userprofile)
        return render_json(request, cls.to_json(), template='classes_join.html', status=200)
    else:
        return HttpResponseBadRequest("method %s is not allowed".format(request.method))