Automatically Assigning Role Profiles During User Sign-Up in Frappe 15

I’m currently working on a Learning Management System (LMS) using Frappe and I’m facing an issue with user sign-ups. I have two types of users in my system: ‘Teacher’ and ‘Student’. During the sign-up process, I want users to be able to select their user type, and based on this selection, I want to automatically assign them a Role Profile (‘Teacher’ or ‘Student’).

I have already created the Role Profiles and added the ‘User Type’ field to the sign-up form. I’ve also written a server-side script that is supposed to assign the Role Profile to the user when they sign up. However, the Role Profiles are not being assigned automatically as expected.

Here’s my user.py on /home/azureuser/frappe-bench/apps/lms/lms/overrides:

@frappe.whitelist(allow_guest=True)
def sign_up(email, full_name, verify_terms, user_type):
    if is_signup_disabled():
        frappe.throw(_("Sign Up is disabled"), _("Not Allowed"))

    if not user_type:
        return 0, _("User type is required")

    user = frappe.db.get("User", {"email": email})
    if user:
        if user.enabled:
            return 0, _("Already Registered")
        else:
            return 0, _("Registered but disabled")
    else:
        if frappe.db.get_creation_count("User", 60) > 300:
            frappe.respond_as_web_page(
                _("Temporarily Disabled"),
                _(
                    "Too many users signed up recently, so the registration is disabled. Please try back in an hour"
                ),
                http_status_code=429,
            )

    user = frappe.get_doc(
        {
            "doctype": "User",
            "email": email,
            "first_name": escape_html(full_name),
            "verify_terms": verify_terms,
            "user_category": user_type,  # use the user_type from the form
            "country": "",
            "enabled": user_type != 'Teacher',  # disable the user account for Teacher
            "new_password": random_string(10),
            "user_type": "Website User",
            "role_profile_name": user_type,  # assign Role Profile based on user type
        }
    )
    user.flags.ignore_permissions = True
    user.flags.ignore_password_policy = True
    user.insert()

    set_country_from_ip(None, user.name)

    if user.flags.email_sent:
        return 1, _("Please check your email for verification")
    else:
        return 2, _("Please ask your administrator to verify your sign-up")

and here’s my signup.html on /home/azureuser/frappe-bench/apps/frappe/frappe/templates :

<form class="form-signin form-signup hide" role="form">
    <div class="page-card-body">
        <div class="form-group">
            <label class="form-label sr-only" for="signup_fullname">{{ _("Full Name") }}</label>
            <input type="text" id="signup_fullname" class="form-control" placeholder="{{ _('Jane Doe') }}"
                required autofocus autocomplete="name">
        </div>
        <div class="form-group">
            <label class="form-label sr-only" for="signup_email">{{ _("Email") }}</label>
            <input type="email" id="signup_email" class="form-control"
                placeholder="{{ _('jane@example.com') }}" required autocomplete="username">
        </div>

        <!-- New User Type Field -->
        <div class="form-group">
            <label class="form-label sr-only" for="signup_usertype">{{ _("User Type") }}</label>
            <select id="signup_usertype" class="form-control" required>
                <option value="">{{ _("Select User Type") }}</option>
                <option value="Student">{{ _("Student") }}</option>
                <option value="Teacher">{{ _("Teacher") }}</option>
            </select>
        </div>
    </div>
    <div class="page-card-actions">
        <button class="btn btn-sm btn-primary btn-block btn-signup"
            type="submit">{{ _("Sign up") }}</button>

        <p class="text-center sign-up-message">
            <a href="#login" class="blue">{{ _("Have an account? Login") }}</a>
        </p>
    </div>
</form>