コード例 #1
0
ファイル: views.py プロジェクト: tutalk/django_tutalk
def tutalk_login(request, noise=""):
    if logged_in_and_authenticated(request):
        if utils.password_required_to_login(request):
            return redirect('tutalk-users:password-login')
        return redirect("tutalk-users:profile")
    auth_with = None
    ctx = mk_tu_context(
        breadcrumbs=[dict(label="Login Page")],
        title="Login Page",
        login_failed=False,
        logged_out='logout' in noise)
    if "POST" == request.method:
        utils.LOGGER.debug("%s",
                           json.dumps(dict(request.POST.items()), indent=4))
    if OneAllAuthBackend.KEY in request.POST:
        auth_with = dict(request.POST.items())
    if auth_with:
        user = authenticate(**auth_with)
        if user:
            login(request, user)
            if utils.password_required_to_login(request):
                return redirect('tutalk-users:password-login')
            return redirect("tutalk-users:profile")
        else:
            ctx['login_failed'] = True
    return render(request, LOGIN_TMPL, context=ctx)
コード例 #2
0
ファイル: views.py プロジェクト: tutalk/django_tutalk
def tutalk_profile(request):
    if not logged_in_and_authenticated(request):
        return redirect("tutalk-users:login")
    if not utils.user_is_registered(request.user):
        return redirect("tutalk-users:registration")
    user = request.user
    tutalk_user = utils.get_tutalk_user(user)
    # site = tutalk_user.promo_code.site
    sites = []
    site_parts = {}
    part = None
    if is_staff_or_superuser(user):
        parts = tutalk_user.participants\
                           .order_by("site__experiment__name",
                                     "site__name",
                                     "uid")\
                           .all()
        # couldn't get template tag 'regroup' working
        # site_parts = {}
        for part in parts:
            site = part.site
            site_name = part.site.fqname
            if site_name not in site_parts:
                site_parts[site_name] = []
            site_parts[site_name].append(part)
        for site_name in site_parts:
            sites.append({
                "site_name": site_name,
                "participants": site_parts[site_name]
            })
    else:
        # student user
        part = tutalk_user.participants.first()
        # kwargs = dict(
        #     part_id=int(part.id),
        #     app_id=int(part.site.experiment.app.id))
        # return redirect('tutalk-users:launch_app', **kwargs)
    ctx = mk_tu_context(
        breadcrumbs=create_tu_breadcrumbs(),
        title="Profile Page",
        user=user,
        tutalk_user=tutalk_user,
        participant=part,
        sites=sites)
    return render(request,
                  "django_tutalk/tutalk_users/profile.html",
                  ctx)
コード例 #3
0
ファイル: views.py プロジェクト: tutalk/django_tutalk
def tutalk_password_login(request):
    if not logged_in_and_authenticated(request):
        return redirect("tutalk-users:login")
    if not utils.user_is_registered(request.user):
        return redirect("tutalk-users:registration")
    tutalk_user = utils.get_tutalk_user(request)
    ctx = mk_tu_context(
        breadcrumbs=create_tu_breadcrumbs(),
        promo_code=tutalk_user.promo_code.name,
        error=False,
        error_message="")
    if "POST" == request.method:
        password = request.POST.get("password", None)
        if password is None:
            ctx["error"] = True
            ctx["error_message"] = "Please enter a password"
        elif not tutalk_user.promo_code.login_password_is_valid(password):
            ctx["error"] = True
            ctx["error_message"] = "Invalid password"
        else:
            return redirect("tutalk-users:profile")
    return render(request, PASSWD_LOGIN_TMPL, ctx)
コード例 #4
0
ファイル: views.py プロジェクト: tutalk/django_tutalk
def tutalk_index(request):
    if logged_in_and_authenticated(request):
        return redirect("tutalk-users:profile")
    ctx = mk_tu_context()
    return render(request, INDEX_TMPL, ctx)
コード例 #5
0
ファイル: views.py プロジェクト: tutalk/django_tutalk
def tutalk_registration(request):
    if not logged_in_and_authenticated(request):
        return redirect("tutalk-users:login")
    user = request.user
    if utils.user_is_registered(user):
        return redirect("tutalk-users:profile")
    # initialize values
    (promo_code, passwd, email, part, form) = (None, None, None, None, None)
    (promo_ok, password_ok, email_ok, stud_num_ok) = (True, True, True, True)
    registered = False
    title = "Registration Page"
    ctx = mk_tu_context(
        breadcrumbs=[dict(label=title)],
        title=title,
        user=user,
        errors=[])
    if "GET" == request.method:
        form = forms.RegistrationForm()
        ctx["form"] = form
        return render(request, REGISTER_TMPL, ctx)
    else:
        form = forms.RegistrationForm(request.POST)
        ctx["form"] = form

        if form.is_valid():
            utils.LOGGER.debug("%s",
                               json.dumps(dict(form.cleaned_data.items()),
                                          indent=True))
            promo = form.cleaned_data["promo_code"]
            promo_code = utils.get_promo_code(promo)
            if promo_code is None:
                promo_ok = False
                form.set_field_required("password", False)
                form.set_field_required("email", False)
                form.set_field_required("student_number", False)
                form.add_error("promo_code",
                               ValidationError("Invalid Value",
                                               code="invalid"))
            else:
                first_post = form.cleaned_data["first_post"]
                utils.LOGGER.debug("first_post: %s", first_post)
                if first_post:
                    # setup the constraints for this promo code and
                    # then set first_post to false so we enforce those
                    # constraints the next time around
                    initial_data = copy.copy(form.cleaned_data)
                    initial_data["first_post"] = False
                    new_form = forms.RegistrationForm(initial=initial_data)
                    new_form.set_field_required(
                        "password",
                        promo_code.registration_requires_password)
                    new_form.set_field_required(
                        "email",
                        promo_code.registration_requires_email)
                    new_form.set_field_required(
                        "student_number",
                        promo_code.registration_requires_uid)
                    del form
                    form = new_form
                    # prevent registration
                    promo_ok = False
                else:
                    form.set_field_required(
                        "password",
                        promo_code.registration_requires_password)
                    form.set_field_required(
                        "email",
                        promo_code.registration_requires_email)
                    form.set_field_required(
                        "student_number",
                        promo_code.registration_requires_uid)

                    passwd = form.cleaned_data["password"]
                    email = form.cleaned_data["email"]
                    stud_num = form.cleaned_data["student_number"]
                    # deal with registration passwords
                    passwd_err = utils.check_registration_password(promo_code,
                                                                   passwd)
                    if passwd_err is not None:
                        password_ok = False
                        form.add_error("password",
                                       ValidationError(passwd_err,
                                                       code="invalid"))
                    # deal with registration emails
                    if promo_code.registration_requires_email:
                        utils.LOGGER.debug("promo code requires email")
                        # we're not doing any checking beyond what Django
                        # does for EmailFields. if the form is valid
                        # and the an email is required this is good enough
                        # for us (so far)

                    # deal with registration participant uids
                    uid_err = utils.check_registration_stud_num(promo_code,
                                                                stud_num)
                    if uid_err is not None:
                        form.add_error("student_number",
                                       ValidationError(uid_err,
                                                       code="invalid"))
                        stud_num_ok = False
                    else:
                        part = utils.get_participant_by_promo_uid(promo_code,
                                                                  stud_num)
        else:
            # form not valid
            utils.LOGGER.debug("form not valid")
            utils.LOGGER.debug("%s", request.POST)
            promo = request.POST.get("promo_code", None)
            promo_code = utils.get_promo_code(promo)
            if promo_code is not None:
                form.set_field_required(
                    "password",
                    promo_code.registration_requires_password)
                form.set_field_required(
                    "email",
                    promo_code.registration_requires_email)
                form.set_field_required(
                    "student_number",
                    promo_code.registration_requires_uid)
            else:
                # nothing to work with. simply create a new form
                new_form = forms.RegistrationForm()
                del form
                form = new_form
            # prevent registration
            promo_ok = False

    if promo_ok and password_ok and email_ok and stud_num_ok:
        utils.LOGGER.debug("attempting to register user")
        registered = \
            utils.register_user(user,
                                promo_code,
                                participant=part,
                                email=email)
        if not registered:
            ctx["errors"].append("Registration Failed")
        else:
            return redirect("tutalk-users:profile")
    else:
        utils.LOGGER.debug("""
            skipping registration:
            promo_ok: %s
            password_ok: %s
            email_ok: %s
            stud_num_ok: %s""", promo_ok, password_ok, email_ok, stud_num_ok)
    # just in case we need to update it
    ctx["form"] = form
    return render(request, REGISTER_TMPL, context=ctx)