Example #1
0
def delete_redemption_entry(request, code_redemption, course_key):
    """
    delete the redemption entry from the table and
    unenroll the user who used the registration code
    for the enrollment and send him/her the unenrollment email.
    """
    user = code_redemption.redeemed_by
    email_address = code_redemption.redeemed_by.email
    full_name = code_redemption.redeemed_by.profile.name
    CourseEnrollment.unenroll(user, course_key, skip_refund=True)

    course = get_course_by_id(course_key, depth=0)
    email_params = get_email_params(course, True, secure=request.is_secure())
    email_params['message_type'] = 'enrolled_unenroll'
    email_params['email_address'] = email_address
    email_params['full_name'] = full_name
    send_mail_to_student(email_address, email_params)

    # remove the redemption entry from the database.
    log.info('deleting redemption entry (%s) from the database.', code_redemption.id)
    code_redemption.delete()
def delete_redemption_entry(request, code_redemption, course_key):
    """
    delete the redemption entry from the table and
    unenroll the user who used the registration code
    for the enrollment and send him/her the unenrollment email.
    """
    user = code_redemption.redeemed_by
    email_address = code_redemption.redeemed_by.email
    full_name = code_redemption.redeemed_by.profile.name
    CourseEnrollment.unenroll(user, course_key, skip_refund=True)

    course = get_course_by_id(course_key, depth=0)
    email_params = get_email_params(course, True, secure=request.is_secure())
    email_params['message_type'] = 'enrolled_unenroll'
    email_params['email_address'] = email_address
    email_params['full_name'] = full_name
    send_mail_to_student(email_address, email_params)

    # remove the redemption entry from the database.
    log.info(u'deleting redemption entry (%s) from the database.', code_redemption.id)
    code_redemption.delete()
Example #3
0
    def create_and_enroll_user(self, email, username, custom_field, password,
                               complete_name, course_id, course_mode,
                               enrolled_by, email_params, first_name,
                               last_name):
        """
        Create a new user and enroll him/her to the given course, return list of errors in the following format
            Error format:
                each error is key-value pait dict with following key-value pairs.
                1. username: username of the user to enroll
                1. email: email of the user to enroll
                1. response: readable error message
        :param email: user's email address
        :param username: user's username
        :param name: user's name
        :param country: user's country
        :param password: user's password
        :param course_id: course identifier of the course in which to enroll the user.
        :param course_mode: mode for user enrollment, e.g. 'honor', 'audit' etc.
        :param enrolled_by: User who made the manual enrollment entry (usually instructor or support)
        :param email_params: information to send to the user via email
        :return: list of errors
        """
        errors = list()
        user = ''
        try:
            with transaction.atomic():
                # Create a new user
                user = self.create_user_and_user_profile(
                    email, username, password, custom_field, complete_name,
                    first_name, last_name)
                # Enroll user to the course and add manual enrollment audit trail
                create_manual_course_enrollment(
                    user=user,
                    course_id=self.course_key,
                    mode=course_mode,
                    enrolled_by=enrolled_by,
                    reason='Enrolling via csv upload',
                    state_transition=UNENROLLED_TO_ENROLLED,
                )

                #add custom_field
        except IntegrityError:
            errors.append({
                'username':
                username,
                'email':
                email,
                'response':
                _('Username {user} already exists.').format(user=username)
            })
        except Exception as ex:  # pylint: disable=broad-except
            log.exception(type(ex).__name__)
            errors.append({
                'username': username,
                'email': email,
                'response': type(ex).__name__,
            })
        else:
            try:
                # It's a new user, an email will be sent to each newly created user.
                email_params.update({
                    'message': 'account_creation_and_enrollment',
                    'email_address': email,
                    'password': password,
                    'platform_name': self.site_name,
                    'first_name': first_name,
                })
                #update sitename params
                email_params['site_name'] = self.site_name
                send_mail_to_student(email, email_params)
            except Exception as ex:  # pylint: disable=broad-except
                log.exception(
                    "Exception '{exception}' raised while sending email to new user."
                    .format(exception=type(ex).__name__))
                errors.append({
                    'username':
                    username,
                    'email':
                    email,
                    'response':
                    _("Error '{error}' while sending email to new user (user email={email}). "
                      "Without the email student would not be able to login. "
                      "Please contact support for further information.").
                    format(error=type(ex).__name__, email=email),
                })
            else:
                log.info(u'email sent to new created user at %s', email)

        return user