def reset_password(self, password: Optional[str] = None, email: bool = True) -> None:
     """Resets the user's password and emails them their password."""
     self.is_reset_password = True
     self.save()
     if password is None:
         password = make_password(self.user)
     self.user.set_password(password)
     self.user.save()
     if email:
         self.email_password(
             password, template='accounts/reset_password.txt')
 def create_user(self,
                 first_name: str,
                 last_name: str,
                 password: Optional[str] = None,
                 email=None,
                 email_password=True,
                 **kwargs):
     # Username will default to email, but if not provided, it will
     # generate one.
     passed_username = kwargs.pop('username', None)
     if passed_username:
         username = passed_username
     else:
         username = PatientProfile.generate_username(email=email,
                                                     first_name=first_name,
                                                     last_name=last_name)
     # Users without emails get a standard password.
     if password is None and not email:
         password = make_password()
     # Inherit some defaults from parent.
     company = kwargs.get('company')
     if company:
         if company.bin_id:
             kwargs['bin_number'] = company.bin_id
         if company.pcn_id:
             kwargs['pcn_number'] = company.pcn_id
         inherit_fields = ('glucose_strip_refill_frequency',
                           'glucose_control_refill_frequency',
                           'lancing_refill_frequency', 'refill_method',
                           'billing_method', 'nursing_group')
         for field in inherit_fields:
             val = getattr(company, field)
             if val is not None:
                 kwargs[field] = val
     patient = super(MyGHRPatientManager,
                     self).create_user(username,
                                       email or '',
                                       password,
                                       email_password=email_password,
                                       first_name=first_name,
                                       last_name=last_name,
                                       **kwargs)
     if company and company.api_partner:
         patient.patient_profile.partners.add(company.api_partner)
     return patient
 def create_user(
         self,
         username: str,
         email: str,
         password: Optional[str] = None,
         email_password: bool = True,
         **kwargs: Any) -> User:
     """Creates a user with the proper profile."""
     if password is None:
         password = make_password()
     user = User.objects.create_user(username, email, password)
     for k, v in filter_dict_to_model_fields(kwargs, User).items():
         setattr(user, k, v)
     user.save()
     self.create_profile(user, **kwargs)
     if email_password:
         user.get_profile().email_password(password)
     return user
Exemple #4
0
def manage_login(request, user_id):  # REFACTOR
    try:
        user = User.objects.get(id=user_id)
    except User.DoesNotExist as e:  # pragma: no cover
        return debug_response(e)

    form_classes = {
        'change_username': AdminChangeUsernameForm,
        'check_security_question': AdminCheckSecurityQuestionForm,
        'change_password': AdminChangePasswordForm,
        'set_default_password_form': AdminSetDefaultPasswordForm
    }
    forms = {}
    form_kwargs = {'instance': user}

    if request.method == 'POST':
        form_class = form_classes[request.POST['form_name']]
        form = form_class(request.POST, **form_kwargs)
        if form.is_valid():
            form.save()
            form_success_messages = {
                'change_username':
                '******'s username has been updated.',
                'check_security_question':
                'The user\'s security question has '
                'been answered correctly.',
                'change_password':
                '******'s password has been updated.',
                'set_default_password_form':
                'The user\'s password has been updated.'
            }
            message = form_success_messages[request.POST['form_name']]
            return redirect_with_message(
                request,
                None,
                message,
                go_back_until=['accounts:manage-login'])
        forms[request.POST['form_name']] = form
        cj = filter(lambda x: x[1] != form_class, form_classes.items())
        for other_form_name, other_form_class in cj:
            forms[other_form_name] = other_form_class(**form_kwargs)
    else:
        for form_name, form_class in form_classes.items():
            forms[form_name] = form_class(**form_kwargs)
    if user.is_patient():
        group = user.patient_profile.get_group()
        if group:
            breadcrumbs = [
                Breadcrumb('Business Partners',
                           reverse('accounts:manage-groups')),
                Breadcrumb(
                    'Business Partner: {0}'.format(group.name),
                    reverse('accounts:manage-groups-detail', args=[group.pk])),
                Breadcrumb(
                    'Patients'.format(group.name),
                    reverse('accounts:manage-groups-patients',
                            args=[group.pk]))
            ]
        else:
            breadcrumbs = [
                Breadcrumb('Users', reverse('accounts:manage-users'))
            ]
        breadcrumbs.append(
            Breadcrumb(
                'Patient: {0}'.format(user.get_reversed_name()),
                reverse('accounts:manage-patients-detail', args=[user.pk])))
    else:
        group = user.professional_profile.parent_group
        breadcrumbs = [
            Breadcrumb('Business Partners', reverse('accounts:manage-groups')),
            Breadcrumb(
                'Business Partner: {0}'.format(group.name),
                reverse('accounts:manage-groups-detail', args=[group.pk])),
            Breadcrumb(
                'Professionals'.format(group.name),
                reverse('accounts:manage-groups-professionals',
                        args=[group.pk])),
            Breadcrumb(
                'Professional: {0}'.format(user.get_reversed_name()),
                reverse('accounts:manage-professionals-detail',
                        args=[user.pk]))
        ]

    ctx = {
        'target_user': user,
        'forms': forms,
        'breadcrumbs': breadcrumbs,
        'new_password': make_password(user)
    }
    return render(request, 'accounts/manage_login.html', ctx)
 def generate_standard_password(self):
     return make_password(self.user)