Python django.urls.reverse() Examples

The following are 30 code examples for showing how to use django.urls.reverse(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module django.urls , or try the search function .

Example 1
Project: django-classified   Author: slyapustin   File: test_urls.py    License: MIT License 6 votes vote down vote up
def test_pages(self):
        response = self.client.get(reverse('django_classified:robots'))
        self.assertEqual(response.status_code, 200)

        response = self.client.get(reverse('django_classified:search'))
        self.assertEqual(response.status_code, 200)

        response = self.client.get(reverse('django_classified:sitemap'))
        self.assertEqual(response.status_code, 200)

        response = self.client.get(reverse('django_classified:rss'))
        self.assertEqual(response.status_code, 200)

        response = self.client.get('/robots.txt')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response['Content-Type'], 'text/plain') 
Example 2
Project: django-classified   Author: slyapustin   File: test_urls.py    License: MIT License 6 votes vote down vote up
def test_user_can_add_item(self):
        self.client.login(
            username=self.username,
            password=self.password
        )

        self.assertTrue(self.profile.allow_add_item(), True)

        response = self.client.get(reverse('django_classified:item-new'))
        self.assertEqual(response.status_code, 200)

        item_data = {
            'image_set-TOTAL_FORMS': 1,
            'image_set-INITIAL_FORMS': 0,
            'group': self.group.pk,
            'title': 'iPhone X',
            'description': 'New, Unlocked. Face ID',
            'price': 999,
            'is_active': True
        }
        response = self.client.post(reverse('django_classified:item-new'), item_data, follow=True)
        self.assertContains(response, item_data['title'])
        new_item = Item.objects.filter(title=item_data['title'])
        self.assertIsNotNone(new_item) 
Example 3
Project: django-classified   Author: slyapustin   File: test_urls.py    License: MIT License 6 votes vote down vote up
def test_user_can_not_add_more_than_allowed_items(self):
        self.client.login(
            username=self.username,
            password=self.password
        )

        self.assertTrue(self.profile.allow_add_item())

        item_data = {
            'image_set-TOTAL_FORMS': 0,
            'image_set-INITIAL_FORMS': 0,
            'group': self.group.pk,
            'title': 'iPhone X',
            'description': 'New, Unlocked. Face ID',
            'price': 999,
            'is_active': True
        }
        for i in range(self.user.item_set.count(), ITEM_PER_USER_LIMIT + 10):
            self.client.post(reverse('django_classified:item-new'), item_data)

        self.assertFalse(self.profile.allow_add_item())
        self.assertEqual(self.user.item_set.count(), ITEM_PER_USER_LIMIT) 
Example 4
Project: openwisp-users   Author: openwisp   File: test_admin.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_admin_change_user_is_superuser_absent(self):
        operator = self._create_operator()
        options = {
            'organization': self._get_org(),
            'is_admin': True,
            'user': self._get_operator(),
        }
        self._create_org_user(**options)
        self.client.force_login(operator)
        response = self.client.get(
            reverse(f'admin:{self.app_label}_user_change', args=[operator.pk])
        )
        html = (
            '<input type="checkbox" name="is_superuser" checked id="id_is_superuser">'
        )
        self.assertNotContains(response, html) 
Example 5
Project: openwisp-users   Author: openwisp   File: test_admin.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_operator_can_see_organization_add_user(self):
        org1 = self._create_org(name='operator-org1')
        org2 = self._create_org(name='operator-org2')
        operator = self._create_operator()
        org_permissions = Permission.objects.filter(
            codename__endswith='organization_user'
        )
        operator.user_permissions.add(*org_permissions)
        options1 = {'organization': org1, 'is_admin': True, 'user': operator}
        options2 = {'organization': org2, 'is_admin': False, 'user': operator}
        self._create_org_user(**options1)
        self._create_org_user(**options2)
        self.client.force_login(operator)
        response = self.client.get(reverse(f'admin:{self.app_label}_user_add'))
        self.assertContains(response, 'operator-org1</option>')
        self.assertNotContains(response, 'operator-org2</option>') 
Example 6
Project: openwisp-users   Author: openwisp   File: test_admin.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_admin_operator_change_superuser_forbidden(self):
        admin = self._create_admin()
        operator = self._create_operator()
        options = {
            'organization': self._get_org(),
            'is_admin': True,
            'user': self._get_operator(),
        }
        self._create_org_user(**options)
        self.client.force_login(operator)
        response = self.client.get(
            reverse(f'admin:{self.app_label}_user_change', args=[operator.pk])
        )
        self.assertEqual(response.status_code, 200)
        # operator trying to acess change form of superuser gets redirected
        response = self.client.get(
            reverse(f'admin:{self.app_label}_user_change', args=[admin.pk])
        )
        self.assertEqual(response.status_code, 302) 
Example 7
Project: openwisp-users   Author: openwisp   File: test_admin.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_new_user_email_exists(self):
        admin = self._create_admin()
        self.client.force_login(admin)
        params = dict(
            username='testadd',
            email='test@testadd.com',
            password1='tester',
            password2='tester',
        )
        params.update(self.add_user_inline_params)
        params.update(self._additional_params_add())
        self.client.post(reverse(f'admin:{self.app_label}_user_add'), params)
        res = self.client.post(reverse(f'admin:{self.app_label}_user_add'), params)
        self.assertContains(
            res, '<li>User with this Email address already exists.</li>'
        ) 
Example 8
Project: openwisp-users   Author: openwisp   File: test_admin.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_change_staff_without_group(self):
        self.client.force_login(self._get_admin())
        user = self._create_operator()
        self._create_org_user(user=user)
        params = user.__dict__
        params.pop('_password')
        params.pop('last_login')
        params.pop('phone_number')
        params.update(self.add_user_inline_params)
        params.update(self._additional_params_add())
        params.update(self._get_edit_form_inline_params(user, None))
        path = reverse(f'admin:{self.app_label}_user_change', args=[user.pk])
        r = self.client.post(path, params, follow=True)
        self.assertEqual(r.status_code, 200)
        self.assertContains(
            r, 'A staff user must belong to a group, please select one.'
        )
        user.refresh_from_db()
        self.assertEqual(user.groups.count(), 0) 
Example 9
Project: openwisp-users   Author: openwisp   File: test_admin.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_change_staff_with_group(self):
        self.client.force_login(self._get_admin())
        user = self._create_operator()
        org = self._get_org()
        org.add_user(user)
        group = Group.objects.get(name='Administrator')
        params = user.__dict__
        params['groups'] = str(group.pk)
        params.pop('phone_number')
        params.pop('_password')
        params.pop('last_login')
        params.update(self.add_user_inline_params)
        params.update(self._additional_params_add())
        params.update(self._get_edit_form_inline_params(user, org))
        path = reverse(f'admin:{self.app_label}_user_change', args=[user.pk])
        r = self.client.post(path, params, follow=True)
        self.assertEqual(r.status_code, 200)
        self.assertNotContains(r, 'error')
        user.refresh_from_db()
        self.assertEqual(user.groups.count(), 1)
        self.assertEqual(user.groups.get(name='Administrator').pk, group.pk) 
Example 10
Project: openwisp-users   Author: openwisp   File: test_admin.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_admin_add_user_org_required(self):
        admin = self._create_admin()
        self.client.force_login(admin)
        params = dict(
            username='testadd',
            email='test@testadd.com',
            password1='tester',
            password2='tester',
            is_staff=True,
            is_superuser=False,
        )
        params.update(self.add_user_inline_params)
        params.update(self._additional_params_add())
        params.update(
            {
                f'{self.app_label}_organizationuser-TOTAL_FORMS': 1,
                f'{self.app_label}_organizationuser-INITIAL_FORMS': 0,
                f'{self.app_label}_organizationuser-MIN_NUM_FORMS': 0,
                f'{self.app_label}_organizationuser-MAX_NUM_FORMS': 1,
            }
        )
        res = self.client.post(reverse(f'admin:{self.app_label}_user_add'), params)
        queryset = User.objects.filter(username='testadd')
        self.assertEqual(queryset.count(), 0)
        self.assertContains(res, 'errors field-organization') 
Example 11
Project: openwisp-users   Author: openwisp   File: test_admin.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_action_inactive(self):
        user = User.objects.create(
            username='openwisp',
            password='test',
            email='openwisp@test.com',
            is_active=True,
        )
        path = reverse(f'admin:{self.app_label}_user_changelist')
        self.client.force_login(self._get_admin())
        post_data = {
            '_selected_action': [user.pk],
            'action': 'make_inactive',
            'csrfmiddlewaretoken': 'test',
            'confirmation': 'Confirm',
        }
        response = self.client.post(path, post_data, follow=True)
        user.refresh_from_db()
        self.assertFalse(user.is_active)
        self.assertEqual(response.status_code, 200) 
Example 12
Project: openwisp-users   Author: openwisp   File: test_admin.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_action_confirmation_page(self):
        user = User.objects.create(
            username='openwisp',
            password='test',
            email='openwisp@test.com',
            is_active=True,
        )
        path = reverse(f'admin:{self.app_label}_user_changelist')
        self.client.force_login(self._get_admin())
        post_data = {
            '_selected_action': [user.pk],
            'action': 'make_active',
            'csrfmiddlewaretoken': 'test',
        }
        response = self.client.post(path, post_data, follow=True)
        user.refresh_from_db()
        self.assertTrue(user.is_active)
        self.assertEqual(response.status_code, 200) 
Example 13
Project: openwisp-users   Author: openwisp   File: test_admin.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_superuser_delete_operator(self):
        user = self._create_operator()
        org = self._create_org()
        org_user = self._create_org_user(user=user, organization=org, is_admin=True)
        post_data = {
            '_selected_action': [user.pk],
            'action': 'delete_selected',
            'post': 'yes',
        }
        self.client.force_login(self._get_admin())
        path = reverse(f'admin:{self.app_label}_user_changelist')
        r = self.client.post(path, post_data, follow=True)
        user_qs = User.objects.filter(pk=user.pk)
        org_user_qs = OrganizationUser.objects.filter(pk=org_user.pk)
        self.assertEqual(r.status_code, 200)
        self.assertEqual(user_qs.count(), 0)
        self.assertEqual(org_user_qs.count(), 0) 
Example 14
Project: openwisp-users   Author: openwisp   File: test_admin.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_staff_delete_staff(self):
        org = self._create_org()
        staff = self._create_user(
            username='staff', is_staff=True, email='staff@gmail.com'
        )
        group = Group.objects.filter(name='Administrator')
        staff.groups.set(group)
        self._create_org_user(organization=org, user=staff, is_admin=True)
        op = self._create_operator()
        op.groups.set(group)
        self._create_org_user(organization=org, user=op, is_admin=True)
        post_data = {
            '_selected_action': [op.pk],
            'action': 'delete_selected_overridden',
            'post': 'yes',
        }
        path = reverse(f'admin:{self.app_label}_user_changelist')
        self.client.force_login(staff)
        r = self.client.post(path, post_data, follow=True)
        user_qs = User.objects.filter(pk=op.pk)
        self.assertEqual(r.status_code, 200)
        self.assertEqual(user_qs.count(), 0)
        self.assertContains(r, 'Successfully deleted 1 user') 
Example 15
Project: openwisp-users   Author: openwisp   File: test_admin.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_superuser_delete_staff(self):
        org = self._create_org()
        group = Group.objects.filter(name='Administrator')
        op = self._create_operator()
        op.groups.set(group)
        self._create_org_user(organization=org, user=op, is_admin=True)
        post_data = {
            '_selected_action': [op.pk],
            'action': 'delete_selected_overridden',
            'post': 'yes',
        }
        path = reverse(f'admin:{self.app_label}_user_changelist')
        self.client.force_login(self._get_admin())
        r = self.client.post(path, post_data, follow=True)
        user_qs = User.objects.filter(pk=op.pk)
        self.assertEqual(r.status_code, 200)
        self.assertEqual(user_qs.count(), 0)
        self.assertContains(r, 'Successfully deleted 1 user') 
Example 16
Project: openwisp-users   Author: openwisp   File: test_admin.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_superuser_delete_org_owner(self):
        org = self._create_org()
        group = Group.objects.filter(name='Administrator')
        op = self._create_operator()
        op.groups.set(group)
        org_user = self._create_org_user(organization=org, user=op, is_admin=True)
        self._create_org_owner(organization_user=org_user, organization=org)
        path = reverse(f'admin:{self.app_label}_user_changelist')
        post_data = {
            'action': 'delete_selected_overridden',
            '_selected_action': [op.pk],
            'post': 'yes',
        }
        self.client.force_login(self._get_admin())
        r = self.client.post(path, post_data, follow=True)
        user_qs = User.objects.filter(pk=op.pk)
        self.assertEqual(r.status_code, 200)
        self.assertContains(r, 'Successfully deleted 1 user')
        self.assertEqual(user_qs.count(), 0) 
Example 17
Project: openwisp-users   Author: openwisp   File: test_admin.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_superuser_bulk_delete(self):
        org = self._create_org()
        group = Group.objects.filter(name='Administrator')
        op1 = self._create_user(username='op1', is_staff=True, email='op1@gmail.com')
        op2 = self._create_user(username='op2', is_staff=True, email='op2@gmail.com')
        op1.groups.set(group)
        op2.groups.set(group)
        org_user = self._create_org_user(organization=org, user=op1, is_admin=True)
        self._create_org_owner(organization_user=org_user, organization=org)
        self._create_org_user(organization=org, user=op2, is_admin=True)
        post_data = {
            'action': 'delete_selected_overridden',
            '_selected_action': [op1.pk, op2.pk],
            'post': 'yes',
        }
        path = reverse(f'admin:{self.app_label}_user_changelist')
        self.client.force_login(self._get_admin())
        r = self.client.post(path, post_data, follow=True)
        user_qs = User.objects.all()
        self.assertEqual(r.status_code, 200)
        self.assertContains(r, 'Successfully deleted 2 users')
        self.assertEqual(user_qs.count(), 2) 
Example 18
Project: openwisp-users   Author: openwisp   File: test_admin.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_admin_add_user_with_invalid_email(self):
        admin = self._create_admin()
        self.client.force_login(admin)
        params = dict(
            username='testmail',
            email='test@invalid.com',
            password1='tester',
            password2='tester',
        )
        params.update(self.add_user_inline_params)
        params.update(self._additional_params_add())
        with patch('allauth.account.models.EmailAddress.objects.add_email') as mocked:
            mocked.side_effect = smtplib.SMTPSenderRefused(
                501, '5.1.7 Bad sender address syntax', 'test_name@test_domain'
            )
            self.client.post(reverse(f'admin:{self.app_label}_user_add'), params)
            mocked.assert_called_once() 
Example 19
Project: openwisp-users   Author: openwisp   File: test_admin.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_change_user(self):
        admin = self._create_admin()
        user = self._create_user()
        org = Organization.objects.first()
        org.add_user(user)
        self.client.force_login(admin)
        params = user.__dict__
        params['bio'] = 'Test change'
        params.pop('phone_number')
        params.pop('_password')
        params.pop('last_login')
        params = self._additional_params_pop(params)
        params.update(self._get_edit_form_inline_params(user, org))
        response = self.client.post(
            reverse(f'admin:{self.app_label}_user_change', args=[user.pk]),
            params,
            follow=True,
        )
        self.assertNotContains(response, 'error')
        user.refresh_from_db()
        self.assertEqual(user.bio, params['bio']) 
Example 20
Project: django-classified   Author: slyapustin   File: views.py    License: MIT License 5 votes vote down vote up
def dispatch(self, *args, **kwargs):
        profile = Profile.get_or_create_for_user(self.request.user)
        if not profile.allow_add_item():
            messages.error(self.request, _('You have reached the limit!'))
            return redirect(reverse('django_classified:user-items'))

        return super(ItemCreateView, self).dispatch(*args, **kwargs) 
Example 21
Project: django-classified   Author: slyapustin   File: models.py    License: MIT License 5 votes vote down vote up
def get_absolute_url(self):
        return reverse('django_classified:group', kwargs={'pk': self.pk, 'slug': self.slug}) 
Example 22
Project: django-classified   Author: slyapustin   File: models.py    License: MIT License 5 votes vote down vote up
def get_absolute_url(self):
        return reverse('django_classified:item', kwargs={
            'pk': self.pk,
            'slug': self.slug
        }) 
Example 23
Project: django-classified   Author: slyapustin   File: test_urls.py    License: MIT License 5 votes vote down vote up
def test_index_page(self):
        response = self.client.get(reverse('django_classified:index'))
        self.assertContains(response, self.group.section.title) 
Example 24
Project: django-classified   Author: slyapustin   File: test_urls.py    License: MIT License 5 votes vote down vote up
def test_item_search_by_title(self):
        response = self.client.get(reverse('django_classified:search'), {'q': self.item.title})
        self.assertContains(response, self.item.get_absolute_url()) 
Example 25
Project: django-classified   Author: slyapustin   File: test_urls.py    License: MIT License 5 votes vote down vote up
def test_item_search_by_description(self):
        response = self.client.get(reverse('django_classified:search'), {'q': self.item.description})
        self.assertContains(response, self.item.get_absolute_url()) 
Example 26
Project: django-classified   Author: slyapustin   File: test_urls.py    License: MIT License 5 votes vote down vote up
def test_item_search_by_group(self):
        response = self.client.get(reverse('django_classified:search'), {'group': self.item.group.pk})
        self.assertContains(response, self.item.get_absolute_url()) 
Example 27
Project: django-classified   Author: slyapustin   File: test_urls.py    License: MIT License 5 votes vote down vote up
def test_item_search_not_found(self):
        response = self.client.get(reverse('django_classified:search'), {'q': 'WRONG KEYWORDS'})
        self.assertNotContains(response, self.item.get_absolute_url()) 
Example 28
Project: django-classified   Author: slyapustin   File: test_urls.py    License: MIT License 5 votes vote down vote up
def test_another_user_cannot_update_other_user_item(self):
        another_user = get_user_model().objects.create_user(
            'Andy',
            'andy@hotmail.com',
            'pass'
        )

        self.client.login(
            username=another_user.username,
            password='pass'
        )

        self.assertEqual(another_user.item_set.count(), 0)

        response = self.client.get(reverse('django_classified:item-edit', kwargs={'pk': self.item.pk}))
        self.assertEqual(response.status_code, 403)

        item_data = {
            'image_set-TOTAL_FORMS': 0,
            'image_set-INITIAL_FORMS': 0,
            'group': self.group.pk,
            'title': 'iPhone X',
            'description': 'New, Unlocked. Face ID',
            'price': 999,
            'is_active': True
        }
        response = self.client.post(reverse('django_classified:item-edit', kwargs={'pk': self.item.pk}), item_data,
                                    follow=True)
        self.assertEqual(response.status_code, 403) 
Example 29
Project: django-classified   Author: slyapustin   File: test_urls.py    License: MIT License 5 votes vote down vote up
def test_user_can_delete_item(self):
        self.client.login(
            username=self.username,
            password=self.password
        )
        self.assertEqual(Item.objects.all().count(), 1)
        response = self.client.get(reverse('django_classified:my-delete', kwargs={'pk': self.item.pk}))
        self.assertEqual(response.status_code, 200)

        self.client.post(reverse('django_classified:my-delete', kwargs={'pk': self.item.pk}))
        self.assertEqual(Item.objects.all().count(), 0) 
Example 30
Project: django-classified   Author: slyapustin   File: test_urls.py    License: MIT License 5 votes vote down vote up
def test_user_can_view_own_items(self):
        self.client.login(
            username=self.username,
            password=self.password
        )
        self.assertEqual(Item.objects.all().count(), 1)
        response = self.client.get(reverse('django_classified:user-items'))
        self.assertContains(response, self.item.get_absolute_url())