Пример #1
0
def login(request, facility):
    facility_id = facility and facility.id or None
    facilities = list(Facility.objects.all())

    # Fix for #1211: refresh cached facility info when it's free and relevant
    refresh_session_facility_info(request, facility_count=len(facilities))

    if request.method == 'POST':
        # log out any Django user or facility user
        logout(request)

        username = request.POST.get("username", "")
        password = request.POST.get("password", "")

        # first try logging in as a Django user
        user = authenticate(username=username, password=password)
        if user:
            auth_login(request, user)
            return HttpResponseRedirect(request.next or reverse("easy_admin"))

        # try logging in as a facility user
        form = LoginForm(data=request.POST, request=request, initial={"facility": facility_id})
        if form.is_valid():
            user = form.get_user()

            try:
                UserLog.begin_user_activity(user, activity_type="login", language=request.language)  # Success! Log the event (ignoring validation failures)
            except ValidationError as e:
                logging.error("Failed to begin_user_activity upon login: %s" % e)

            request.session["facility_user"] = user
            messages.success(request, _("You've been logged in! We hope you enjoy your time with KA Lite ") +
                                        _("-- be sure to log out when you finish."))

            # Send them back from whence they came
            landing_page = form.cleaned_data["callback_url"]
            if not landing_page:
                # Just going back to the homepage?  We can do better than that.
                landing_page = reverse("coach_reports") if form.get_user().is_teacher else None
                landing_page = landing_page or (reverse("account_management") if not settings.package_selected("RPi") else reverse("homepage"))

            return HttpResponseRedirect(form.non_field_errors() or request.next or landing_page)

        else:
            messages.error(
                request,
                _("There was an error logging you in. Please correct any errors listed below, and try again."),
            )

    else:  # render the unbound login form
        referer = urlparse.urlparse(request.META["HTTP_REFERER"]).path if request.META.get("HTTP_REFERER") else None
        # never use the homepage as the referer
        if referer in [reverse("homepage"), reverse("add_facility_student")]:
            referer = None
        form = LoginForm(initial={"facility": facility_id, "callback_url": referer})

    return {
        "form": form,
        "facilities": facilities,
    }
Пример #2
0
def login(request, facility):
    facilities = Facility.objects.all()
    facility_id = facility and facility.id or None

    if request.method == 'POST':
        # log out any Django user or facility user
        logout(request)

        username = request.POST.get("username", "")
        password = request.POST.get("password", "")

        # first try logging in as a Django user
        user = authenticate(username=username, password=password)
        if user:
            auth_login(request, user)
            return HttpResponseRedirect(request.next or reverse("easy_admin"))

        # try logging in as a facility user
        form = LoginForm(data=request.POST,
                         request=request,
                         initial={"facility": facility_id})
        if form.is_valid():
            user = form.get_user()

            try:
                UserLog.begin_user_activity(
                    user, activity_type="login"
                )  # Success! Log the event (ignoring validation failures)
            except ValidationError as e:
                logging.debug("Failed to end_user_activity upon logout: %s" %
                              e)
            request.session["facility_user"] = user
            messages.success(
                request,
                _("You've been logged in! We hope you enjoy your time with KA Lite "
                  ) + _("-- be sure to log out when you finish."))
            return HttpResponseRedirect(form.non_field_errors(
            ) or request.next or reverse("coach_reports") if form.get_user().
                                        is_teacher else reverse("homepage"))
        else:
            messages.error(
                request,
                strip_tags(form.non_field_errors()) or
                _("There was an error logging you in. Please correct any errors listed below, and try again."
                  ))

    else:  # render the unbound login form
        form = LoginForm(initial={"facility": facility_id})

    return {"form": form, "facilities": facilities}
Пример #3
0
def login(request, facility):
    facilities = Facility.objects.all()
    facility_id = facility and facility.id or None

    if request.method == 'POST':
        # log out any Django user or facility user
        logout(request)

        username = request.POST.get("username", "")
        password = request.POST.get("password", "")

        # first try logging in as a Django user
        user = authenticate(username=username, password=password)
        if user:
            auth_login(request, user)
            return HttpResponseRedirect(request.next or reverse("easy_admin"))

        # try logging in as a facility user
        form = LoginForm(data=request.POST, request=request, initial={"facility": facility_id})
        if form.is_valid():
            user = form.get_user()

            try:
                UserLog.begin_user_activity(user, activity_type="login")  # Success! Log the event (ignoring validation failures)
            except ValidationError as e:
                logging.debug("Failed to begin_user_activity upon login: %s" % e)
            request.session["facility_user"] = user
            messages.success(request, _("You've been logged in! We hope you enjoy your time with KA Lite ") +
                                        _("-- be sure to log out when you finish."))
            return HttpResponseRedirect(
                form.non_field_errors()
                or request.next
                or reverse("coach_reports") if form.get_user().is_teacher else reverse("homepage")
            )
        else:
            messages.error(
                request,
                strip_tags(form.non_field_errors())
                or _("There was an error logging you in. Please correct any errors listed below, and try again.")
            )

    else:  # render the unbound login form
        form = LoginForm(initial={"facility": facility_id})

    return {
        "form": form,
        "facilities": facilities
    }
Пример #4
0
def login(request, facility):
    facilities = list(Facility.objects.all())
    facility_id = facility and facility.id or None

    if request.method == 'POST':
        # log out any Django user or facility user
        logout(request)

        username = request.POST.get("username", "")
        password = request.POST.get("password", "")

        # first try logging in as a Django user
        user = authenticate(username=username, password=password)
        if user:
            auth_login(request, user)
            return HttpResponseRedirect(request.next or reverse("easy_admin"))

        # try logging in as a facility user
        form = LoginForm(data=request.POST,
                         request=request,
                         initial={"facility": facility_id})
        if form.is_valid():
            user = form.get_user()

            try:
                UserLog.begin_user_activity(
                    user, activity_type="login", language=request.language
                )  # Success! Log the event (ignoring validation failures)
            except ValidationError as e:
                logging.error("Failed to begin_user_activity upon login: %s" %
                              e)

            request.session["facility_user"] = user
            messages.success(
                request,
                _("You've been logged in! We hope you enjoy your time with KA Lite "
                  ) + _("-- be sure to log out when you finish."))

            # Send them back from whence they came
            landing_page = form.cleaned_data["callback_url"]
            if not landing_page:
                # Just going back to the homepage?  We can do better than that.
                landing_page = reverse(
                    "coach_reports") if form.get_user().is_teacher else None
                landing_page = landing_page or (
                    reverse("account_management")
                    if not settings.package_selected("RPi") else
                    reverse("homepage"))

            return HttpResponseRedirect(form.non_field_errors() or request.next
                                        or landing_page)

        else:
            messages.error(
                request,
                strip_tags(form.non_field_errors()) or
                _("There was an error logging you in. Please correct any errors listed below, and try again."
                  ))

    else:  # render the unbound login form
        referer = urlparse.urlparse(
            request.META["HTTP_REFERER"]).path if request.META.get(
                "HTTP_REFERER") else None
        # never use the homepage as the referer
        if referer in [reverse("homepage"), reverse("add_facility_student")]:
            referer = None
        form = LoginForm(initial={
            "facility": facility_id,
            "callback_url": referer
        })

    return {
        "form": form,
        "facilities": facilities,
    }