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 get_success_url(self): if '_continue' in self.request.POST: success_url = self.request.path elif '_cancel' in self.request.POST: success_url = reverse('accounts:profile') else: if is_validation_period_active(self.object.organisation): success_url = reverse('accounts:organisationchange_detail', args=[self.object.pk]) else: success_url = reverse('accounts:profile') # save exiting get parameters (i.e. redirect_uri) return urlunsplit( ('', '', success_url, self.request.GET.urlencode(safe='/'), ''))
def verify(self, user): self.user.set_organisations([self.organisation]) self.status = 'v' self.completed_by_user = user self.user.remove_organisation_related_permissions() # check if organisation uses user activation if is_validation_period_active(self.organisation): if self.user.valid_until is None: self.user.valid_until = now() + datetime.timedelta( days=settings.SSO_VALIDATION_PERIOD_DAYS) else: self.user.valid_until = None # save changes and update last_modified field! self.user.save() self.save()
def verify(self, user): self.status = 'v' self.completed_by_user = user if self.organisation: self.user.set_organisations([self.organisation]) # check if organisation uses user activation validation_period_active = False for organisation in self.user.organisations.all(): if is_validation_period_active(organisation): self.user.valid_until = now() + timedelta( days=settings.SSO_VALIDATION_PERIOD_DAYS) self.user.save() validation_period_active = True if not validation_period_active: self.user.valid_until = None self.user.save() # add default member profile self.user.role_profiles.add(user.get_default_role_profile()) self.user.role_profiles.remove(user.get_default_guest_profile()) self.save()
def save(self): cd = self.cleaned_data current_user = self.request.user # registrationprofile data self.registrationprofile.known_person1_first_name = cd[ 'known_person1_first_name'] self.registrationprofile.known_person1_last_name = cd[ 'known_person1_last_name'] self.registrationprofile.known_person2_first_name = cd[ 'known_person2_first_name'] self.registrationprofile.known_person2_last_name = cd[ 'known_person2_last_name'] self.registrationprofile.save() # user data self.user.username = cd['username'] self.user.first_name = cd['first_name'] self.user.last_name = cd['last_name'] self.user.is_stored_permanently = cd['is_stored_permanently'] if cd['notes']: UserNote.objects.create_note(user=self.user, notes=[cd['notes']], created_by_user=current_user) # userprofile data self.update_user_m2m_fields('organisations', current_user) self.update_user_m2m_fields('application_roles', current_user) self.update_user_m2m_fields('role_profiles', current_user) organisation = self.cleaned_data['organisations'] if is_validation_period_active(organisation): # a new registered user is valid_until from now for the validation period self.user.valid_until = now() + datetime.timedelta( days=settings.SSO_VALIDATION_PERIOD_DAYS) self.user.save() return self.registrationprofile
def save(self, commit=True): user = super().save(commit=False) user.set_password(get_random_string(40)) user.username = default_username_generator( capfirst(self.cleaned_data.get('first_name')), capfirst(self.cleaned_data.get('last_name'))) organisation = self.cleaned_data["organisations"] if is_validation_period_active(organisation): user.valid_until = now() + datetime.timedelta( days=settings.SSO_VALIDATION_PERIOD_DAYS) user.save() # use mixin to send notification self.user = user current_user = self.request.user self.update_user_m2m_fields('organisations', current_user) self.update_user_m2m_fields('application_roles', current_user) self.update_user_m2m_fields('role_profiles', current_user) self.create_note_if_required(current_user, self.cleaned_data) user.create_primary_email(email=self.cleaned_data["email"]) return user
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)