Пример #1
0
    def post(self, request, customer_uuid):
        """
        Handle POST request - handle form submissions.

        Arguments:
            request (django.http.request.HttpRequest): Request instance
            customer_uuid (str): Enterprise Customer UUID

        Returns:
            django.http.response.HttpResponse: HttpResponse
        """
        enterprise_customer = EnterpriseCustomer.objects.get(
            uuid=customer_uuid)  # pylint: disable=no-member
        manage_learners_form = ManageLearnersForm(request.POST, request.FILES)

        # initial form validation - check that form data is well-formed
        if manage_learners_form.is_valid():
            # The form is valid. Call the appropriate helper depending on the mode:
            mode = manage_learners_form.cleaned_data[
                ManageLearnersForm.Fields.MODE]
            if mode == ManageLearnersForm.Modes.MODE_SINGULAR:
                linked_learners = self._handle_singular(
                    enterprise_customer, manage_learners_form)
            else:
                linked_learners = self._handle_bulk_upload(
                    enterprise_customer, manage_learners_form, request)

        # _handle_form might add form errors, so we check if it is still valid
        if manage_learners_form.is_valid():

            # Enroll linked users in a course if requested
            course_details = manage_learners_form.cleaned_data.get(
                ManageLearnersForm.Fields.COURSE)
            if course_details:
                course_mode = manage_learners_form.cleaned_data[
                    ManageLearnersForm.Fields.COURSE_MODE]
                self._enroll_users(linked_learners, course_details,
                                   course_mode, request)

            # Redirect to GET if everything went smooth.
            return HttpResponseRedirect("")

        # if something went wrong - display bound form on the page
        context = self._build_context(request, customer_uuid)
        context.update({
            self.ContextParameters.MANAGE_LEARNERS_FORM:
            manage_learners_form
        })
        return render(request, self.template, context)
Пример #2
0
    def post(self, request, customer_uuid):
        """
        Handle POST request - handle form submissions.

        Arguments:
            request (django.http.request.HttpRequest): Request instance
            customer_uuid (str): Enterprise Customer UUID

        Returns:
            django.http.response.HttpResponse: HttpResponse
        """
        # TODO: bulk upload
        enterprise_customer = EnterpriseCustomer.objects.get(
            uuid=customer_uuid)  # pylint: disable=no-member
        manage_learners_form = ManageLearnersForm(request.POST)
        if manage_learners_form.is_valid():
            EnterpriseCustomerUser.objects.link_user(
                enterprise_customer,
                manage_learners_form.cleaned_data["email"])
            manage_learners_form = ManageLearnersForm()

        context = self._build_context(request, customer_uuid)

        context.update({
            self.ContextParameters.MANAGE_LEARNERS_FORM:
            manage_learners_form
        })

        return render(request, self.template, context)
Пример #3
0
    def post(self, request, customer_uuid):
        """
        Handle POST request - handle form submissions.

        Arguments:
            request (django.http.request.HttpRequest): Request instance
            customer_uuid (str): Enterprise Customer UUID

        Returns:
            django.http.response.HttpResponse: HttpResponse
        """
        enterprise_customer = EnterpriseCustomer.objects.get(
            uuid=customer_uuid)  # pylint: disable=no-member
        manage_learners_form = ManageLearnersForm(
            request.POST,
            request.FILES,
            user=request.user,
            enterprise_customer=enterprise_customer)
        course_id_with_emails = {}

        # initial form validation - check that form data is well-formed
        if manage_learners_form.is_valid():
            email_field_as_bulk_input = split_usernames_and_emails(
                manage_learners_form.cleaned_data[
                    ManageLearnersForm.Fields.EMAIL_OR_USERNAME])
            is_bulk_entry = len(email_field_as_bulk_input) > 1
            # The form is valid. Call the appropriate helper depending on the mode:
            mode = manage_learners_form.cleaned_data[
                ManageLearnersForm.Fields.MODE]
            if mode == ManageLearnersForm.Modes.MODE_SINGULAR and not is_bulk_entry:
                linked_learners = self._handle_singular(
                    request, enterprise_customer, manage_learners_form)
            elif mode == ManageLearnersForm.Modes.MODE_SINGULAR:
                linked_learners, __ = self._handle_bulk_upload(
                    enterprise_customer,
                    manage_learners_form,
                    request,
                    email_list=email_field_as_bulk_input)
            else:
                linked_learners, course_id_with_emails = self._handle_bulk_upload(
                    enterprise_customer,
                    manage_learners_form,
                    request,
                )

        # _handle_form might add form errors, so we check if it is still valid
        if manage_learners_form.is_valid():
            # If we aren't installed in Open edX, blank out enrollment reason so downstream methods don't attempt to
            # create audit items
            if create_manual_enrollment_audit is not None:
                manual_enrollment_reason = manage_learners_form.cleaned_data.get(
                    ManageLearnersForm.Fields.REASON)
            else:
                manual_enrollment_reason = None
                logging.exception(
                    "To create enrollment audits for enterprise learners, "
                    "this package must be installed in an Open edX environment."
                )

            notification_type = manage_learners_form.cleaned_data.get(
                ManageLearnersForm.Fields.NOTIFY)
            notify = notification_type == ManageLearnersForm.NotificationTypes.BY_EMAIL
            discount = manage_learners_form.cleaned_data.get(
                ManageLearnersForm.Fields.DISCOUNT)
            sales_force_id = manage_learners_form.cleaned_data.get(
                ManageLearnersForm.Fields.SALES_FORCE_ID)
            course_mode = manage_learners_form.cleaned_data.get(
                ManageLearnersForm.Fields.COURSE_MODE)
            course_id = None

            if not course_id_with_emails:
                course_details = manage_learners_form.cleaned_data.get(
                    ManageLearnersForm.Fields.COURSE) or {}
                course_id = course_details.get('course_id')
                if course_id and linked_learners:
                    self._enroll_users(
                        request=request,
                        enterprise_customer=enterprise_customer,
                        emails=linked_learners,
                        mode=course_mode,
                        course_id=course_id,
                        notify=notify,
                        enrollment_reason=manual_enrollment_reason,
                        sales_force_id=sales_force_id,
                        discount=discount)
            else:
                for course_id in course_id_with_emails:
                    emails_to_enroll = course_id_with_emails[course_id]
                    if emails_to_enroll:
                        self._enroll_users(
                            request=request,
                            enterprise_customer=enterprise_customer,
                            emails=list(emails_to_enroll),
                            mode=course_mode,
                            course_id=course_id,
                            notify=notify,
                            enrollment_reason=manual_enrollment_reason,
                            sales_force_id=sales_force_id,
                            discount=discount)

            # Redirect to GET if everything went smooth.
            manage_learners_url = reverse("admin:" + UrlNames.MANAGE_LEARNERS,
                                          args=(customer_uuid, ))
            search_keyword = self.get_search_keyword(request)
            if search_keyword:
                manage_learners_url = manage_learners_url + "?q=" + search_keyword
            return HttpResponseRedirect(manage_learners_url)

        # if something went wrong - display bound form on the page
        return self.get_form_view(
            request,
            customer_uuid,
            additional_context={
                self.ContextParameters.MANAGE_LEARNERS_FORM:
                manage_learners_form
            })
Пример #4
0
    def post(self, request, customer_uuid):
        """
        Handle POST request - handle form submissions.

        Arguments:
            request (django.http.request.HttpRequest): Request instance
            customer_uuid (str): Enterprise Customer UUID

        Returns:
            django.http.response.HttpResponse: HttpResponse
        """
        enterprise_customer = EnterpriseCustomer.objects.get(
            uuid=customer_uuid)  # pylint: disable=no-member
        manage_learners_form = ManageLearnersForm(
            request.POST,
            request.FILES,
            user=request.user,
            enterprise_customer=enterprise_customer)

        # initial form validation - check that form data is well-formed
        if manage_learners_form.is_valid():
            email_field_as_bulk_input = split_usernames_and_emails(
                manage_learners_form.cleaned_data[
                    ManageLearnersForm.Fields.EMAIL_OR_USERNAME])
            is_bulk_entry = len(email_field_as_bulk_input) > 1
            # The form is valid. Call the appropriate helper depending on the mode:
            mode = manage_learners_form.cleaned_data[
                ManageLearnersForm.Fields.MODE]
            if mode == ManageLearnersForm.Modes.MODE_SINGULAR and not is_bulk_entry:
                linked_learners = self._handle_singular(
                    enterprise_customer, manage_learners_form)
            elif mode == ManageLearnersForm.Modes.MODE_SINGULAR:
                linked_learners = self._handle_bulk_upload(
                    enterprise_customer,
                    manage_learners_form,
                    request,
                    email_list=email_field_as_bulk_input)
            else:
                linked_learners = self._handle_bulk_upload(
                    enterprise_customer, manage_learners_form, request)

        # _handle_form might add form errors, so we check if it is still valid
        if manage_learners_form.is_valid():
            course_details = manage_learners_form.cleaned_data.get(
                ManageLearnersForm.Fields.COURSE)
            program_details = manage_learners_form.cleaned_data.get(
                ManageLearnersForm.Fields.PROGRAM)

            notification_type = manage_learners_form.cleaned_data.get(
                ManageLearnersForm.Fields.NOTIFY)
            notify = notification_type == ManageLearnersForm.NotificationTypes.BY_EMAIL

            course_id = None
            if course_details:
                course_id = course_details['course_id']

            if course_id or program_details:
                course_mode = manage_learners_form.cleaned_data[
                    ManageLearnersForm.Fields.COURSE_MODE]
                self._enroll_users(
                    request=request,
                    enterprise_customer=enterprise_customer,
                    emails=linked_learners,
                    mode=course_mode,
                    course_id=course_id,
                    program_details=program_details,
                    notify=notify,
                )

            # Redirect to GET if everything went smooth.
            manage_learners_url = reverse("admin:" + UrlNames.MANAGE_LEARNERS,
                                          args=(customer_uuid, ))
            search_keyword = self.get_search_keyword(request)
            if search_keyword:
                manage_learners_url = manage_learners_url + "?q=" + search_keyword
            return HttpResponseRedirect(manage_learners_url)

        # if something went wrong - display bound form on the page
        context = self._build_context(request, customer_uuid)
        context.update({
            self.ContextParameters.MANAGE_LEARNERS_FORM:
            manage_learners_form
        })
        return render(request, self.template, context)