def account(request, domain, couch_user_id, template="users/account.html"): context = _users_context(request, domain) try: couch_user = CouchUser.get_by_user_id(couch_user_id, domain) except ResourceNotFound: raise Http404 if not couch_user: raise Http404 editing_commcare_user = couch_user.is_commcare_user() and request.couch_user.can_edit_commcare_users context.update({ 'couch_user': couch_user, 'editing_commcare_user': editing_commcare_user, 'archive_url': reverse('commcare_users', args=[domain]), }) if request.project.commtrack_enabled: # https://confluence.dimagi.com/display/commtrack/CommTrack+Roles+and+Responsibilities user_data_roles = dict((u['slug'], u) for u in [ { 'slug': 'commtrack_requester', 'name': _("CommTrack Requester"), 'description': _("Responsible for creating requisitions."), }, { 'slug': 'commtrack_approver', 'name': _("CommTrack Approver"), 'description': _( "Responsible for approving requisitions, including " "updating or modifying quantities as needed. Will receive " "a notification when new requisitions are created."), }, { 'slug': 'commtrack_supplier', 'name': _("CommTrack Supplier"), 'description': _( "Responsible for packing orders. Will receive a " "notification when the approver indicates that " "requisitions are approved, so that he or she can start " "packing it."), }, { 'slug': 'commtrack_receiver', 'name': _("CommTrack Receiver"), 'description': _( "Responsible for receiving orders. Will receive a " "notification when the supplier indicates that requisitions " "are packed and are ready for pickup, so that he or she can " "come pick it up or better anticipate the delivery."), } ]) else: user_data_roles = [] if couch_user.is_commcare_user(): user_data = copy.copy(dict(couch_user.user_data)) for k, v in user_data.items(): if k in user_data_roles: user_data_roles[k]['selected'] = (user_data[k] == 'true') del user_data[k] context.update({ 'reset_password_form': SetPasswordForm(user=""), 'only_numeric': (request.project.password_format() == 'n'), 'user_data_roles': user_data_roles, 'user_data': user_data, }) if couch_user.is_deleted(): if couch_user.is_commcare_user(): return render(request, 'users/deleted_account.html', context) else: raise Http404 # phone-numbers tab if request.method == "POST" and \ request.POST['form_type'] == "phone-numbers" and \ (couch_user.is_current_web_user(request) or couch_user.is_commcare_user()): phone_number = request.POST['phone_number'] phone_number = re.sub('\s', '', phone_number) if re.match(r'\d+$', phone_number): couch_user.add_phone_number(phone_number) couch_user.save() else: messages.error(request, "Please enter digits only") # domain-accounts tab if not couch_user.is_commcare_user(): all_domains = couch_user.get_domains() admin_domains = [] for d in all_domains: if couch_user.is_domain_admin(d): admin_domains.append(d) context.update({"user": request.user, "domains": admin_domains }) # scheduled reports tab context.update({ 'phone_numbers_extended': couch_user.phone_numbers_extended(request.couch_user), }) #project settings tab if couch_user.user_id == request.couch_user.user_id: dm = couch_user.get_domain_membership(domain) if dm: domain_obj = Domain.get_by_name(domain) if request.method == "POST" and request.POST['form_type'] == "project-settings": # deal with project settings data project_settings_form = ProjectSettingsForm(request.POST) if project_settings_form.is_valid(): if project_settings_form.save(couch_user, domain): messages.success(request, "Your project settings were successfully saved!") else: messages.error(request, "There seems to have been an error saving your project settings. Please try again!") else: project_settings_form = ProjectSettingsForm(initial={'global_timezone': domain_obj.default_timezone, 'user_timezone': dm.timezone, 'override_global_tz': dm.override_global_tz}) context.update({ 'proj_settings_form': project_settings_form, 'override_global_tz': dm.override_global_tz }) # commtrack if request.method == "POST" and request.POST['form_type'] == "commtrack": commtrack_form = CommtrackUserForm(request.POST, domain=domain) if commtrack_form.is_valid(): commtrack_form.save(couch_user) else: linked_loc = couch_user.dynamic_properties().get('commtrack_location') # FIXME update user model appropriately commtrack_form = CommtrackUserForm(domain=domain, initial={'supply_point': linked_loc}) context.update({ 'commtrack_enabled': Domain.get_by_name(domain).commtrack_enabled, 'commtrack_form': commtrack_form, }) # for basic tab context.update(_handle_user_form(request, domain, couch_user)) return render(request, template, context)
def account(request, domain, couch_user_id, template="users/account.html"): context = _users_context(request, domain) couch_user = CouchUser.get_by_user_id(couch_user_id, domain) if not couch_user: raise Http404 context.update({ 'couch_user': couch_user, }) if couch_user.is_commcare_user(): context.update({ 'reset_password_form': SetPasswordForm(user=""), 'only_numeric': (request.project.password_format() == 'n'), }) if couch_user.is_deleted(): if couch_user.is_commcare_user(): return render_to_response(request, 'users/deleted_account.html', context) else: raise Http404 # phone-numbers tab if request.method == "POST" and request.POST['form_type'] == "phone-numbers": phone_number = request.POST['phone_number'] if re.match(r'\d+', phone_number): couch_user.add_phone_number(phone_number) couch_user.save() #messages.success(request, 'Phone number added') else: messages.error(request, "Please enter digits only") # domain-accounts tab if not couch_user.is_commcare_user(): all_domains = couch_user.get_domains() admin_domains = [] for d in all_domains: if couch_user.is_domain_admin(d): admin_domains.append(d) context.update({"user": request.user, "domains": admin_domains }) # scheduled reports tab context.update({ # for phone-number tab 'phone_numbers': couch_user.phone_numbers, # for commcare-accounts tab # "other_commcare_accounts": other_commcare_accounts, }) #project settings tab if couch_user.user_id == request.couch_user.user_id and not couch_user.is_commcare_user(): web_user = WebUser.get_by_user_id(couch_user.user_id) dm = web_user.get_domain_membership(domain) if dm: domain_obj = Domain.get_by_name(domain) if request.method == "POST" and request.POST['form_type'] == "project-settings": # deal with project settings data project_settings_form = ProjectSettingsForm(request.POST) if project_settings_form.is_valid(): if project_settings_form.save(web_user, domain): messages.success(request, "Your project settings were successfully saved!") else: messages.error(request, "There seems to have been an error saving your project settings. Please try again!") else: project_settings_form = ProjectSettingsForm(initial={'global_timezone': domain_obj.default_timezone, 'user_timezone': dm.timezone, 'override_global_tz': dm.override_global_tz}) context.update({ 'proj_settings_form': project_settings_form, 'override_global_tz': dm.override_global_tz }) # for basic tab context.update(_handle_user_form(request, domain, couch_user)) return render_to_response(request, template, context)
def account(request, domain, couch_user_id, template="users/account.html"): context = _users_context(request, domain) try: couch_user = CouchUser.get_by_user_id(couch_user_id, domain) except ResourceNotFound: raise Http404 if not couch_user: raise Http404 editing_commcare_user = couch_user.is_commcare_user() and request.couch_user.can_edit_commcare_users context.update({ 'couch_user': couch_user, 'editing_commcare_user': editing_commcare_user, }) if couch_user.is_commcare_user(): context.update({ 'reset_password_form': SetPasswordForm(user=""), 'only_numeric': (request.project.password_format() == 'n'), }) if couch_user.is_deleted(): if couch_user.is_commcare_user(): return render(request, 'users/deleted_account.html', context) else: raise Http404 # phone-numbers tab if request.method == "POST" and \ request.POST['form_type'] == "phone-numbers" and \ (couch_user.is_current_web_user(request) or couch_user.is_commcare_user()): phone_number = request.POST['phone_number'] phone_number = re.sub('\s', '', phone_number) if re.match(r'\d+$', phone_number): couch_user.add_phone_number(phone_number) couch_user.save() else: messages.error(request, "Please enter digits only") # domain-accounts tab if not couch_user.is_commcare_user(): all_domains = couch_user.get_domains() admin_domains = [] for d in all_domains: if couch_user.is_domain_admin(d): admin_domains.append(d) context.update({"user": request.user, "domains": admin_domains }) # scheduled reports tab context.update({ 'phone_numbers_extended': couch_user.phone_numbers_extended(request.couch_user), }) #project settings tab if couch_user.user_id == request.couch_user.user_id: dm = couch_user.get_domain_membership(domain) if dm: domain_obj = Domain.get_by_name(domain) if request.method == "POST" and request.POST['form_type'] == "project-settings": # deal with project settings data project_settings_form = ProjectSettingsForm(request.POST) if project_settings_form.is_valid(): if project_settings_form.save(couch_user, domain): messages.success(request, "Your project settings were successfully saved!") else: messages.error(request, "There seems to have been an error saving your project settings. Please try again!") else: project_settings_form = ProjectSettingsForm(initial={'global_timezone': domain_obj.default_timezone, 'user_timezone': dm.timezone, 'override_global_tz': dm.override_global_tz}) context.update({ 'proj_settings_form': project_settings_form, 'override_global_tz': dm.override_global_tz }) # commtrack if request.method == "POST" and request.POST['form_type'] == "commtrack": commtrack_form = CommtrackUserForm(request.POST, domain=domain) if commtrack_form.is_valid(): commtrack_form.save(couch_user) else: linked_loc = couch_user.dynamic_properties().get('commtrack_location') # FIXME update user model appropriately commtrack_form = CommtrackUserForm(domain=domain, initial={'supply_point': linked_loc}) context.update({ 'commtrack_enabled': Domain.get_by_name(domain).commtrack_enabled, 'commtrack_form': commtrack_form, }) # for basic tab context.update(_handle_user_form(request, domain, couch_user)) return render(request, template, context)
def account(request, domain, couch_user_id, template="users/account.html"): context = _users_context(request, domain) try: couch_user = CouchUser.get_by_user_id(couch_user_id, domain) except ResourceNotFound: raise Http404 if not couch_user: raise Http404 editing_commcare_user = couch_user.is_commcare_user() and request.couch_user.can_edit_commcare_users context.update({"couch_user": couch_user, "editing_commcare_user": editing_commcare_user}) if couch_user.is_commcare_user(): context.update( { "reset_password_form": SetPasswordForm(user=""), "only_numeric": (request.project.password_format() == "n"), } ) if couch_user.is_deleted(): if couch_user.is_commcare_user(): return render(request, "users/deleted_account.html", context) else: raise Http404 # phone-numbers tab if request.method == "POST" and request.POST["form_type"] == "phone-numbers": phone_number = request.POST["phone_number"] phone_number = re.sub("\s", "", phone_number) if re.match(r"\d+$", phone_number): couch_user.add_phone_number(phone_number) couch_user.save() else: messages.error(request, "Please enter digits only") # domain-accounts tab if not couch_user.is_commcare_user(): all_domains = couch_user.get_domains() admin_domains = [] for d in all_domains: if couch_user.is_domain_admin(d): admin_domains.append(d) context.update({"user": request.user, "domains": admin_domains}) # scheduled reports tab context.update({"phone_numbers_extended": couch_user.phone_numbers_extended(request.couch_user)}) # project settings tab if couch_user.user_id == request.couch_user.user_id: dm = couch_user.get_domain_membership(domain) if dm: domain_obj = Domain.get_by_name(domain) if request.method == "POST" and request.POST["form_type"] == "project-settings": # deal with project settings data project_settings_form = ProjectSettingsForm(request.POST) if project_settings_form.is_valid(): if project_settings_form.save(couch_user, domain): messages.success(request, "Your project settings were successfully saved!") else: messages.error( request, "There seems to have been an error saving your project settings. Please try again!" ) else: project_settings_form = ProjectSettingsForm( initial={ "global_timezone": domain_obj.default_timezone, "user_timezone": dm.timezone, "override_global_tz": dm.override_global_tz, } ) context.update({"proj_settings_form": project_settings_form, "override_global_tz": dm.override_global_tz}) # for basic tab context.update(_handle_user_form(request, domain, couch_user)) return render(request, template, context)