def profile_core(request, redirect_uri=None): user = request.user if request.method == 'POST': form = UserSelfProfileForm(request.POST, instance=user, files=request.FILES) if form.is_valid(): form.save() change_message = ChangedDataList(form, []).change_message() log_change(request, user, change_message) success_url = get_profile_success_url(request, redirect_uri) return HttpResponseRedirect(success_url) else: form = UserSelfProfileForm(instance=user) try: user_organisation = user.organisations.first() except ObjectDoesNotExist: user_organisation = None context = { 'form': form, 'redirect_uri': redirect_uri, 'is_validation_period_active': is_validation_period_active(user_organisation) } return render(request, 'accounts/profile_core_form.html', context)
def app_admin_update_user( request, uuid, template='accounts/application/app_admin_update_user_form.html'): if not request.user.has_app_admin_user_access(uuid): raise PermissionDenied user = get_object_or_404(get_user_model(), uuid=uuid) if request.method == 'POST': form = AppAdminUserProfileForm(request.POST, instance=user, request=request) if form.is_valid(): user = form.save() change_message = ChangedDataList(form, []).change_message() log_change(request, user, change_message) msg_dict = { 'name': force_str(get_user_model()._meta.verbose_name), 'obj': force_str(user) } if "_continue" in request.POST: msg = format_html( _('The {name} "{obj}" was changed successfully. You may edit it again below.' ), **msg_dict) success_url = reverse('accounts:app_admin_update_user', args=[user.uuid.hex]) else: msg = format_html( _('The {name} "{obj}" was changed successfully.'), **msg_dict) success_url = reverse('accounts:app_admin_user_list' ) + "?" + request.GET.urlencode() messages.add_message(request, level=messages.SUCCESS, message=msg, fail_silently=True) return HttpResponseRedirect(success_url) else: form = AppAdminUserProfileForm(instance=user, request=request) media = form.media errors = ErrorList(form, []) context = { 'form': form, 'errors': errors, 'media': media, 'title': _('Change user roles') } return render(request, template, context)
def delete_profile(request): user = request.user if request.method == 'POST': form = UserSelfProfileDeleteForm(request.POST, instance=user) if form.is_valid(): form.save() change_message = ChangedDataList(form, None).change_message() log_change(request, user, change_message) return redirect('auth:logout') else: form = UserSelfProfileDeleteForm(instance=user) context = { 'form': form, } return render(request, 'accounts/delete_profile_form.html', context)
def profile_center_account(request, redirect_uri=None): user = request.user if request.method == 'POST': form = CenterSelfProfileForm(request.POST, instance=user, files=request.FILES) if form.is_valid(): form.save() change_message = ChangedDataList(form, []).change_message() log_change(request, user, change_message) success_url = get_profile_success_url(request, redirect_uri) return HttpResponseRedirect(success_url) else: form = CenterSelfProfileForm(instance=user) context = {'form': form, 'redirect_uri': redirect_uri} return render(request, 'accounts/profile_form_center.html', context)
def _update_center_account( request, user, template='accounts/application/update_center_form.html'): if request.method == 'POST': form = CenterProfileForm(request.POST, instance=user, request=request) if form.is_valid(): activate = None if "_deactivate" in request.POST: activate = False elif "_activate" in request.POST: activate = True user = form.save(activate=activate) change_message = ChangedDataList(form, []).change_message() log_change(request, user, change_message) msg_dict = { 'name': force_str(get_user_model()._meta.verbose_name), 'obj': force_str(user) } msg = '' if "_addanother" in request.POST: msg = format_html( _('The {name} "{obj}" was changed successfully. You may add another {name} below.' ), **msg_dict) success_url = reverse('accounts:add_user') elif "_continue" in request.POST: msg = format_html( _('The {name} "{obj}" was changed successfully. You may edit it again below.' ), **msg_dict) success_url = reverse('accounts:update_user', args=[user.uuid.hex]) elif "_deactivate" in request.POST: success_url = reverse('accounts:update_user', args=[user.uuid.hex]) elif "_activate" in request.POST: msg = _('The %(name)s "%(obj)s" was activated successfully.' ) % msg_dict success_url = reverse('accounts:update_user', args=[user.uuid.hex]) else: msg = format_html( _('The {name} "{obj}" was changed successfully.'), **msg_dict) success_url = reverse( 'accounts:user_list') + "?" + request.GET.urlencode() if msg: messages.add_message(request, level=messages.SUCCESS, message=msg, fail_silently=True) return HttpResponseRedirect(success_url) else: form = CenterProfileForm(instance=user, request=request) media, errors, active = get_media_errors_and_active_form(form) if (user.last_login is None) or (user.last_login - user.date_joined) < timedelta(seconds=1): logged_in = False else: logged_in = True usernote_set, accessible_created_by_users = get_usernotes_and_accessible_created_by_users( user, request.user) context = { 'form': form, 'media': media, 'errors': errors, 'active': active, 'usernotes': usernote_set, 'editable_created_by_users': accessible_created_by_users, 'logged_in': logged_in, 'title': _('Change user') } return render(request, template, context)
def _update_standard_user(request, user, template='accounts/application/update_user_form.html' ): if user.useremail_set.count() == 0: useremail_extra = 1 else: useremail_extra = 0 UserEmailInlineFormSet = inlineformset_factory( User, UserEmail, UserEmailForm, extra=useremail_extra, max_num=UserEmail.MAX_EMAIL_ADRESSES) if request.method == 'POST': form = get_default_admin_update_user_form_class()(request.POST, instance=user, request=request) user_email_inline_formset = UserEmailInlineFormSet(request.POST, instance=user) if form.is_valid() and user_email_inline_formset.is_valid(): formsets = [user_email_inline_formset] changed_data_list = ChangedDataList(form, formsets) activate = None if "_deactivate" in request.POST: activate = False changed_data_list.append("deactivated") elif "_activate" in request.POST: activate = True changed_data_list.append("activated") remove_org = "_remove_org" in request.POST if remove_org: changed_data_list.append("\"removed from organisation\"") extend_validity = "_extend_validity" in request.POST if extend_validity: changed_data_list.append("\"extended validity\"") make_member = "_make_member" in request.POST if extend_validity: changed_data_list.append("\"made to member\"") user = form.save(extend_validity, activate=activate, remove_org=remove_org, make_member=make_member) user_email_inline_formset.save() if not user.useremail_set.exists(): msg = _('The account %(username)s has no email address!') % { 'username': force_str(user) } messages.add_message(request, level=messages.ERROR, message=msg, fail_silently=True) else: user.ensure_single_primary_email() change_message = changed_data_list.change_message() log_change(request, user, change_message) msg_dict = { 'name': force_str(get_user_model()._meta.verbose_name), 'obj': force_str(user) } msg = '' if "_addanother" in request.POST: msg = format_html( _('The {name} "{obj}" was changed successfully. You may add another {name} below.' ), **msg_dict) success_url = reverse('accounts:add_user') elif "_continue" in request.POST: msg = format_html( _('The {name} "{obj}" was changed successfully. You may edit it again below.' ), **msg_dict) success_url = reverse('accounts:update_user', args=[user.uuid.hex]) elif "_resend_invitation" in request.POST: send_account_created_email(user, request) msg = _( 'The %(name)s "%(obj)s" was changed successfully and the invitation email was resend.' ) % msg_dict success_url = reverse('accounts:update_user', args=[user.uuid.hex]) elif "_deactivate" in request.POST: success_url = reverse('accounts:update_user', args=[user.uuid.hex]) elif "_activate" in request.POST: msg = _('The %(name)s "%(obj)s" was activated successfully.' ) % msg_dict success_url = reverse('accounts:update_user', args=[user.uuid.hex]) elif "_remove_org" in request.POST: msg = _( 'The %(name)s "%(obj)s" was removed from the organisation successfully.' ) % msg_dict success_url = reverse( 'accounts:user_list') + "?" + request.GET.urlencode() else: msg = format_html( _('The {name} "{obj}" was changed successfully.'), **msg_dict) success_url = reverse( 'accounts:user_list') + "?" + request.GET.urlencode() if msg: messages.add_message(request, level=messages.SUCCESS, message=msg, fail_silently=True) return HttpResponseRedirect(success_url) else: user_email_inline_formset = UserEmailInlineFormSet(instance=user) form = get_default_admin_update_user_form_class()(instance=user, request=request) formsets = [user_email_inline_formset] media, errors, active = get_media_errors_and_active_form(form, formsets) user_email_inline_formset.forms += [user_email_inline_formset.empty_form] if (user.last_login is None) or (user.last_login - user.date_joined) < timedelta(seconds=1): logged_in = False else: logged_in = True try: user_organisation = user.organisations.first() except ObjectDoesNotExist: user_organisation = None usernote_set, accessible_created_by_users = get_usernotes_and_accessible_created_by_users( user, request.user) context = { 'form': form, 'errors': errors, 'formsets': formsets, 'media': media, 'active': active, 'usernotes': usernote_set, 'editable_created_by_users': accessible_created_by_users, 'logged_in': logged_in, 'is_validation_period_active': is_validation_period_active(user_organisation), 'title': _('Change user') } return render(request, template, context)
def profile_with_address_and_phone(request, redirect_uri=None): address_extra = 0 phonenumber_extra = 1 user = request.user address_count = user.useraddress_set.count() if address_count == 0: address_extra = 1 AddressInlineFormSet = inlineformset_factory(User, UserAddress, AddressForm, extra=address_extra, max_num=3) PhoneNumberInlineFormSet = inlineformset_factory(User, UserPhoneNumber, PhoneNumberForm, extra=phonenumber_extra, max_num=6) if request.method == 'POST': form = UserSelfProfileForm(request.POST, instance=user, files=request.FILES) address_inline_formset = AddressInlineFormSet(request.POST, instance=user) phonenumber_inline_formset = PhoneNumberInlineFormSet(request.POST, instance=user) if form.is_valid() and address_inline_formset.is_valid( ) and phonenumber_inline_formset.is_valid(): form.save() address_inline_formset.save() phonenumber_inline_formset.save() UserAddress.ensure_single_primary(user) UserPhoneNumber.ensure_single_primary(user) formsets = [address_inline_formset, phonenumber_inline_formset] change_message = ChangedDataList(form, formsets).change_message() log_change(request, user, change_message) success_url = get_profile_success_url(request, redirect_uri) return HttpResponseRedirect(success_url) else: address_inline_formset = AddressInlineFormSet(instance=user) phonenumber_inline_formset = PhoneNumberInlineFormSet(instance=user) form = UserSelfProfileForm(instance=user) formsets = [address_inline_formset, phonenumber_inline_formset] media, errors, active = get_media_errors_and_active_form(form, formsets) phonenumber_inline_formset.forms += [phonenumber_inline_formset.empty_form] address_inline_formset.forms += [address_inline_formset.empty_form] try: user_organisation = user.organisations.first() except ObjectDoesNotExist: user_organisation = None context = { 'form': form, 'errors': errors, 'formsets': formsets, 'media': media, 'active': active, 'redirect_uri': redirect_uri, 'is_validation_period_active': is_validation_period_active(user_organisation) } return render(request, 'accounts/profile_form.html', context)
def emails(request): redirect_uri = get_safe_redirect_uri(request, allowed_hosts()) post_change_redirect = update_url(reverse('accounts:emails'), {'redirect_uri': redirect_uri}) user = request.user if request.method == 'POST': if 'send_confirmation' in request.POST: user_email = UserEmail.objects.get( id=request.POST['send_confirmation']) send_useremail_confirmation(user_email, request) messages.success( request, _('Confirmation email was sent to \"%(email)s\".') % {'email': user_email}) return redirect(post_change_redirect) elif 'delete' in request.POST: try: user_email = UserEmail.objects.get(id=request.POST['delete']) user_email.delete() messages.success( request, _('The email \"%(email)s\" was deleted successfully.') % {'email': user_email}) except UserEmail.DoesNotExist: # may be a double click on the delete button pass return redirect(post_change_redirect) elif 'set_primary' in request.POST: user_email = UserEmail.objects.get(id=request.POST['set_primary']) user_email.primary = True user_email.save() UserEmail.objects.filter( user=user_email.user, primary=True).exclude(pk=user_email.pk).update(primary=False) messages.success( request, _("The email \"%(email)s\" was changed successfully.") % {'email': user_email}) return redirect(post_change_redirect) else: add_form = SelfUserEmailAddForm(request.POST) if add_form.is_valid(): user_email = add_form.save() change_message = ChangedDataList(add_form, []).change_message() log_change(request, user, change_message) msg = _('Thank you. Your data were saved.') + '\n' msg += _('Confirmation email was sent to \"%(email)s\".') % { 'email': user_email } messages.success(request, msg) send_useremail_confirmation(user_email, request) return redirect(post_change_redirect) else: add_form = SelfUserEmailAddForm(initial={'user': user.id}) context = { 'form': add_form, 'max_email_adresses': UserEmail.MAX_EMAIL_ADRESSES, 'redirect_uri': redirect_uri } return render(request, 'accounts/user_email_detail.html', context)