Example #1
0
    def test_email_already_used(self):
        another_user = User.objects.create(username='******', email='*****@*****.**')
        user_profile = UserProfile.objects.create(user=another_user, region=self.region, country=self.uganda,
                                                  organization=self.organization)
        self.global_admin.user_set.add(another_user)

        form_data = {
            'username': '******',
            'email': '*****@*****.**' }
        edit_user_profile_form = EditUserProfileForm(instance=self.saved_user, data=form_data)
        self.assertFalse(edit_user_profile_form.is_valid())
        message = "%s is already associated to a different user." % form_data['email']
        self.assertEquals(edit_user_profile_form.errors['email'], [message])
Example #2
0
    def test_valid(self):
        edit_user_profile_form = EditUserProfileForm(instance=self.saved_user, data=self.form_data)
        user = edit_user_profile_form.save()

        self.assertTrue(edit_user_profile_form.is_valid())
        self.failUnless(self.saved_user.id)
        self.assertEqual(1, len(self.saved_user.groups.all()))
        self.assertIn(self.global_admin, self.saved_user.groups.all())

        user_profile = UserProfile.objects.get(user=user)
        self.failUnless(user_profile)
        self.assertEqual(self.organization, user_profile.organization)
        self.assertEqual(self.region, user_profile.region)
        self.assertEqual(self.uganda, user_profile.country)
Example #3
0
 def get(self, *args, **kwargs):
     user = User.objects.get(id=kwargs['pk'])
     context = {'btn_label': "SAVE",
                'title': "Edit User",
                'request': self.request,
                'form': EditUserProfileForm(instance=user),
                'cancel_url': reverse("list_users_page")}
     return self.render_to_response(context)
Example #4
0
    def test_valid(self):
        saved_user = User.objects.create(username='******', email='*****@*****.**')
        user_profile = UserProfile.objects.create(user=saved_user, region=self.region, country=self.uganda,
                                                  organization=self.organization)
        self.global_admin.user_set.add(saved_user)

        edit_user_profile_form = EditUserProfileForm(instance=saved_user, data=self.form_data)
        user = edit_user_profile_form.save()

        self.assertTrue(edit_user_profile_form.is_valid())
        self.failUnless(saved_user.id)
        self.assertEqual(1, len(saved_user.groups.all()))
        self.assertIn(self.global_admin, saved_user.groups.all())

        user_profile = UserProfile.objects.get(user=user)
        self.failUnless(user_profile)
        self.assertEqual(self.organization, user_profile.organization)
        self.assertEqual(self.region, user_profile.region)
        self.assertEqual(self.uganda, user_profile.country)
Example #5
0
class EditUser(LoginRequiredMixin, UpdateView):

    def __init__(self, **kwargs):
        super(EditUser, self).__init__(**kwargs)
        self.template_name = 'users/new.html'
        self.object = User
        self.form_class = EditUserProfileForm
        self.success_url = reverse('list_users_page')

    def get(self, *args, **kwargs):
        user = User.objects.get(id=kwargs['pk'])
        context = {'btn_label': "SAVE",
                   'title': "Edit User",
                   'request': self.request,
                   'form': EditUserProfileForm(instance=user),
                   'cancel_url': reverse("list_users_page")}
        return self.render_to_response(context)

    def post(self, request, *args, **kwargs):
        user = User.objects.get(id=kwargs['pk'])
        self.form =  EditUserProfileForm(instance=user, data=request.POST)
        if self.form.is_valid():
            return self._form_valid()
        return self._form_invalid()

    def _form_valid(self):
        self.form.save()
        message = "%s was successfully updated" % self.form.cleaned_data['username']
        messages.success(self.request, message)
        return HttpResponseRedirect(reverse("list_users_page"))

    def _form_invalid(self):
        message = "User was not updated, see errors below"
        messages.error(self.request, message )
        context = {'btn_label': "SAVE",
                   'title': "Edit User",
                   'request': self.request,
                   'form': self.form}
        return self.render_to_response(context)
Example #6
0
 def post(self, request, *args, **kwargs):
     user = User.objects.get(id=kwargs['pk'])
     self.form =  EditUserProfileForm(instance=user, data=request.POST)
     if self.form.is_valid():
         return self._form_valid()
     return self._form_invalid()