def _handle_user_form(request, domain, couch_user=None): context = {} if couch_user: create_user = False else: create_user = True can_change_admin_status = ( request.user.is_superuser or request.couch_user.can_edit_web_users(domain=domain) ) and request.couch_user.user_id != couch_user.user_id if couch_user.is_commcare_user(): role_choices = UserRole.commcareuser_role_choices(domain) else: role_choices = UserRole.role_choices(domain) if request.method == "POST" and request.POST["form_type"] == "basic-info": form = UserForm(request.POST, role_choices=role_choices) if form.is_valid(): if create_user: django_user = User() django_user.username = form.cleaned_data["email"] django_user.save() couch_user = CouchUser.from_django_user(django_user) couch_user.first_name = form.cleaned_data["first_name"] couch_user.last_name = form.cleaned_data["last_name"] couch_user.email = form.cleaned_data["email"] couch_user.language = form.cleaned_data["language"] if can_change_admin_status: role = form.cleaned_data["role"] if role: couch_user.set_role(domain, role) couch_user.save() if request.couch_user.get_id == couch_user.get_id and couch_user.language: # update local language in the session request.session["django_language"] = couch_user.language messages.success(request, 'Changes saved for user "%s"' % couch_user.username) else: form = UserForm(role_choices=role_choices) if not create_user: form.initial["first_name"] = couch_user.first_name form.initial["last_name"] = couch_user.last_name form.initial["email"] = couch_user.email form.initial["language"] = couch_user.language if can_change_admin_status: if couch_user.is_commcare_user(): role = couch_user.get_role(domain) if role is None: initial = "none" else: initial = role.get_qualified_id() form.initial["role"] = initial else: form.initial["role"] = couch_user.get_role(domain, include_teams=False).get_qualified_id() or "" if not can_change_admin_status: del form.fields["role"] context.update({"form": form}) return context
def _handle_user_form(request, domain, couch_user=None): from corehq.apps.reports.util import get_possible_reports context = {} if couch_user: create_user = False else: create_user = True can_change_admin_status = \ (request.user.is_superuser or request.couch_user.can_edit_web_users(domain=domain))\ and request.couch_user.user_id != couch_user.user_id if couch_user.is_commcare_user(): role_choices = UserRole.commcareuser_role_choices(domain) else: role_choices = UserRole.role_choices(domain) if request.method == "POST" and request.POST['form_type'] == "basic-info": form = UserForm(request.POST, role_choices=role_choices) if form.is_valid(): if create_user: django_user = User() django_user.username = form.cleaned_data['email'] django_user.save() couch_user = CouchUser.from_django_user(django_user) couch_user.first_name = form.cleaned_data['first_name'] couch_user.last_name = form.cleaned_data['last_name'] couch_user.email = form.cleaned_data['email'] couch_user.language = form.cleaned_data['language'] if can_change_admin_status: role = form.cleaned_data['role'] if role: couch_user.set_role(domain, role) couch_user.save() messages.success(request, 'Changes saved for user "%s"' % couch_user.username) else: form = UserForm(role_choices=role_choices) if not create_user: form.initial['first_name'] = couch_user.first_name form.initial['last_name'] = couch_user.last_name form.initial['email'] = couch_user.email form.initial['language'] = couch_user.language if can_change_admin_status: if couch_user.is_commcare_user(): role = couch_user.get_role(domain) if role is None: initial = "none" else: initial = role.get_qualified_id() form.initial['role'] = initial else: form.initial['role'] = couch_user.get_role(domain).get_qualified_id() or '' if not can_change_admin_status: del form.fields['role'] context.update({"form": form}) return context
def _handle_user_form(request, domain, couch_user=None): context = {} if couch_user: create_user = False else: create_user = True can_change_admin_status = ((request.user.is_superuser or request.couch_user.can_edit_web_users(domain=domain)) and request.couch_user.user_id != couch_user.user_id) if couch_user.is_commcare_user(): role_choices = UserRole.commcareuser_role_choices(domain) else: role_choices = UserRole.role_choices(domain) results = get_db().view('languages/list', startkey=[domain], endkey=[domain, {}], group='true').all() language_choices = [] if results: for result in results: lang_code = result['key'][1] label = result['key'][1] long_form = langcodes.get_name(lang_code) if long_form: label += " (" + langcodes.get_name(lang_code) + ")" language_choices.append((lang_code, label)) else: language_choices = langcodes.get_all_langs_for_select() if request.method == "POST" and request.POST['form_type'] == "basic-info": if couch_user.is_commcare_user(): form = UserForm(request.POST, role_choices=role_choices, language_choices=language_choices) else: form = WebUserForm(request.POST, role_choices=role_choices, language_choices=language_choices) if form.is_valid(): if create_user: django_user = User() django_user.username = form.cleaned_data['email'] django_user.save() couch_user = CouchUser.from_django_user(django_user) if couch_user.is_current_web_user(request) or couch_user.is_commcare_user(): couch_user.first_name = form.cleaned_data['first_name'] couch_user.last_name = form.cleaned_data['last_name'] couch_user.email = form.cleaned_data['email'] if not couch_user.is_commcare_user(): couch_user.email_opt_out = form.cleaned_data['email_opt_out'] couch_user.language = form.cleaned_data['language'] if can_change_admin_status: role = form.cleaned_data['role'] if role: couch_user.set_role(domain, role) couch_user.save() if request.couch_user.get_id == couch_user.get_id and couch_user.language: # update local language in the session request.session['django_language'] = couch_user.language messages.success(request, 'Changes saved for user "%s"' % couch_user.username) else: form = UserForm(role_choices=role_choices, language_choices=language_choices) if couch_user.is_commcare_user(): form = UserForm(role_choices=role_choices, language_choices=language_choices) else: form = WebUserForm(role_choices=role_choices, language_choices=language_choices) if not create_user: form.initial['first_name'] = couch_user.first_name form.initial['last_name'] = couch_user.last_name form.initial['email'] = couch_user.email form.initial['email_opt_out'] = couch_user.email_opt_out form.initial['language'] = couch_user.language if can_change_admin_status: if couch_user.is_commcare_user(): role = couch_user.get_role(domain) if role is None: initial = "none" else: initial = role.get_qualified_id() form.initial['role'] = initial else: form.initial['role'] = couch_user.get_role(domain, include_teams=False).get_qualified_id() or '' if not can_change_admin_status: del form.fields['role'] context.update({"form": form, "current_users_page": couch_user.is_current_web_user(request)}) return context
def _handle_user_form(request, domain, couch_user=None): context = {} if couch_user: create_user = False else: create_user = True can_change_admin_status = ((request.user.is_superuser or request.couch_user.can_edit_web_users(domain=domain)) and request.couch_user.user_id != couch_user.user_id) if couch_user.is_commcare_user(): role_choices = UserRole.commcareuser_role_choices(domain) else: role_choices = UserRole.role_choices(domain) results = get_db().view('languages/list', startkey=[domain], endkey=[domain, {}], group='true').all() language_choices = [] if results: for result in results: lang_code = result['key'][1] label = result['key'][1] long_form = langcodes.get_name(lang_code) if long_form: label += " (" + langcodes.get_name(lang_code) + ")" language_choices.append((lang_code, label)) else: language_choices = langcodes.get_all_langs_for_select() if request.method == "POST" and request.POST['form_type'] == "basic-info": if couch_user.is_commcare_user(): form = UserForm(request.POST, role_choices=role_choices, language_choices=language_choices) else: form = WebUserForm(request.POST, role_choices=role_choices, language_choices=language_choices) if form.is_valid(): if create_user: django_user = User() django_user.username = form.cleaned_data['email'] django_user.save() couch_user = CouchUser.from_django_user(django_user) if couch_user.is_current_web_user(request) or couch_user.is_commcare_user(): couch_user.first_name = form.cleaned_data['first_name'] couch_user.last_name = form.cleaned_data['last_name'] couch_user.email = form.cleaned_data['email'] if not couch_user.is_commcare_user(): couch_user.email_opt_in = form.cleaned_data['email_opt_in'] couch_user.language = form.cleaned_data['language'] if can_change_admin_status: role = form.cleaned_data['role'] if role: couch_user.set_role(domain, role) couch_user.save() if request.couch_user.get_id == couch_user.get_id and couch_user.language: # update local language in the session request.session['django_language'] = couch_user.language messages.success(request, 'Changes saved for user "%s"' % couch_user.username) else: form = UserForm(role_choices=role_choices, language_choices=language_choices) if couch_user.is_commcare_user(): form = UserForm(role_choices=role_choices, language_choices=language_choices) else: form = WebUserForm(role_choices=role_choices, language_choices=language_choices) if not create_user: form.initial['first_name'] = couch_user.first_name form.initial['last_name'] = couch_user.last_name form.initial['email'] = couch_user.email form.initial['email_opt_in'] = couch_user.email_opt_in form.initial['language'] = couch_user.language if can_change_admin_status: if couch_user.is_commcare_user(): role = couch_user.get_role(domain) if role is None: initial = "none" else: initial = role.get_qualified_id() form.initial['role'] = initial else: form.initial['role'] = couch_user.get_role(domain, include_teams=False).get_qualified_id() or '' if not can_change_admin_status: del form.fields['role'] context.update({"form": form, "current_users_page": couch_user.is_current_web_user(request)}) return context