Python rest_framework.reverse.reverse() Examples

The following are 30 code examples of rest_framework.reverse.reverse(). 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 also want to check out all available functions/classes of the module rest_framework.reverse , or try the search function .
Example #1
Source File: views.py    From desec-stack with MIT License 6 votes vote down vote up
def get(self, request, *_):
        if self.request.user.is_authenticated:
            routes = {
                'account': {
                    'show': reverse('account', request=request),
                    'delete': reverse('account-delete', request=request),
                    'change-email': reverse('account-change-email', request=request),
                    'reset-password': reverse('account-reset-password', request=request),
                },
                'logout': reverse('logout', request=request),
                'tokens': reverse('token-list', request=request),
                'domains': reverse('domain-list', request=request),
            }
        else:
            routes = {
                'register': reverse('register', request=request),
                'login': reverse('login', request=request),
                'reset-password': reverse('account-reset-password', request=request),
            }
        return Response(routes) 
Example #2
Source File: test_api.py    From normandy with Mozilla Public License 2.0 6 votes vote down vote up
def test_it_serves_actions(self, api_client):
        action = ActionFactory(
            name="foo", implementation="foobar", arguments_schema={"type": "object"}
        )

        res = api_client.get("/api/v1/action/")
        action_url = reverse(
            "recipes:v1:action-implementation",
            kwargs={"name": action.name, "impl_hash": action.implementation_hash},
        )
        assert res.status_code == 200
        assert res.data == [
            {
                "name": "foo",
                "implementation_url": Whatever.endswith(action_url),
                "arguments_schema": {"type": "object"},
            }
        ] 
Example #3
Source File: test_api.py    From normandy with Mozilla Public License 2.0 6 votes vote down vote up
def test_it_serves_actions(self, api_client):
        action = ActionFactory(
            name="foo", implementation="foobar", arguments_schema={"type": "object"}
        )

        res = api_client.get("/api/v3/action/")
        action_url = reverse(
            "recipes:v1:action-implementation",
            kwargs={"name": action.name, "impl_hash": action.implementation_hash},
        )
        assert res.status_code == 200
        assert res.data == {
            "count": 1,
            "next": None,
            "previous": None,
            "results": [
                {
                    "id": action.id,
                    "name": "foo",
                    "implementation_url": Whatever.endswith(action_url),
                    "arguments_schema": {"type": "object"},
                }
            ],
        } 
Example #4
Source File: tests.py    From REST-API with MIT License 6 votes vote down vote up
def test_status_delete(self):
        data = self.create_item()
        data_id = data.get("id")
        rud_url = api_reverse('api-status:detail', kwargs={"id": data_id})
        rud_data = {
            'content': "another new content"
        }
        '''
        delete method / delete
        '''
        del_response = self.client.delete(rud_url, format='json')
        self.assertEqual(del_response.status_code, status.HTTP_204_NO_CONTENT)
        '''
        Not found
        '''
        get_response = self.client.get(rud_url, format='json')
        self.assertEqual(get_response.status_code, status.HTTP_404_NOT_FOUND) 
Example #5
Source File: views.py    From desec-stack with MIT License 6 votes vote down vote up
def post(self, request, *args, **kwargs):
        # Check password and extract email
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        new_email = serializer.validated_data['new_email']

        action = models.AuthenticatedChangeEmailUserAction(user=request.user, new_email=new_email)
        verification_code = serializers.AuthenticatedChangeEmailUserActionSerializer(action).data['code']
        request.user.send_email('change-email', recipient=new_email, context={
            'confirmation_link': reverse('confirm-change-email', request=request, args=[verification_code]),
            'old_email': request.user.email,
            'new_email': new_email,
        })

        # At this point, we know that we are talking to the user, so we can tell that we sent an email.
        return Response(data={'detail': 'Please check your mailbox to confirm email address change.'},
                        status=status.HTTP_202_ACCEPTED) 
Example #6
Source File: tests.py    From REST-API with MIT License 6 votes vote down vote up
def test_status_create_with_img_and_desc(self):
        self.status_user_token()
        url = api_reverse('api-status:list')
        # (w, h) = (800, 1280)
        # (255, 255, 255)
        image_item  = Image.new('RGB', (800, 1280), (0, 124, 174))
        tmp_file    = tempfile.NamedTemporaryFile(suffix='.jpg')
        image_item.save(tmp_file, format='JPEG')
        with open(tmp_file.name, 'rb') as file_obj:
            data = {
                'content': None,
                'image': file_obj
            }
            response = self.client.post(url, data, format='multipart')
            self.assertEqual(response.status_code, status.HTTP_201_CREATED)
            

            self.assertEqual(Status.objects.count(), 2)
        temp_img_dir = os.path.join(settings.MEDIA_ROOT, 'status', 'testcfeuser')
        if os.path.exists(temp_img_dir):
            shutil.rmtree(temp_img_dir) 
Example #7
Source File: test_donations.py    From desec-stack with MIT License 6 votes vote down vote up
def test_create_donation(self):
        url = reverse('v1:donation')
        data = {
            'name': 'Komplizierter Vörnämü-ßßß 马大为',
            'iban': 'DE89370400440532013000',
            'bic': 'BYLADEM1SWU',
            'amount': 123.45,
            'message': 'hi there, thank you. Also, some random chars:  ™ • ½ ¼ ¾ ⅓ ⅔ † ‡ µ ¢ £ € « » ♤ ♧ ♥ ♢ ¿ ',
            'email': 'email@example.com',
        }
        response = self.client.post(url, data)
        self.assertTrue(mail.outbox)
        email_internal = str(mail.outbox[0].message())
        direct_debit = str(mail.outbox[0].attachments[0][1])
        self.assertStatus(response, status.HTTP_201_CREATED)
        self.assertEqual(len(mail.outbox), 2)
        self.assertEqual(response.data['iban'], data['iban'])
        self.assertTrue('Komplizierter Vornamu' in direct_debit)
        self.assertTrue(data['iban'] in email_internal) 
Example #8
Source File: serializers.py    From REST-API with MIT License 5 votes vote down vote up
def get_uri(self, obj):
        request = self.context.get('request')
        return api_reverse("api-user:detail", kwargs={"username": obj.username}, request=request) 
Example #9
Source File: FieldSightXFormSerializer.py    From kobo-predict with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def get_url(self, obj):
        kwargs = {'pk': obj.pk}
        request = self.context.get('request')

        return reverse('forms:download_xform', kwargs=kwargs, request=request) 
Example #10
Source File: FieldSightXFormSerializer.py    From kobo-predict with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def get_manifest_url(self, obj):
        site_id = obj.site.id if obj.site else 0
        kwargs = {'pk': obj.pk, 'site_id': site_id}
        request = self.context.get('request')

        return reverse('forms:manifest-url', kwargs=kwargs, request=request) 
Example #11
Source File: FieldSightXFormSerializer.py    From kobo-predict with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def get_url(self, obj):
        kwargs = {'pk': obj.pk}
        request = self.context.get('request')

        return reverse('forms:download_xform', kwargs=kwargs, request=request) 
Example #12
Source File: FieldSightXFormSerializer.py    From kobo-predict with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def get_manifest_url(self, obj):
        site_id = obj.site.id if obj.site else 0
        kwargs = {'pk': obj.pk, 'site_id': site_id}
        request = self.context.get('request')

        return reverse('forms:manifest-url', kwargs=kwargs, request=request) 
Example #13
Source File: tests.py    From REST-API with MIT License 5 votes vote down vote up
def test_status_create(self):
        data = self.create_item()
        data_id = data.get("id")
        rud_url = api_reverse('api-status:detail', kwargs={"id": data_id})
        rud_data = {
            'content': "another new content"
        }

        '''
        get method / retrieve
        '''
        get_response = self.client.get(rud_url, format='json')
        self.assertEqual(get_response.status_code, status.HTTP_200_OK) 
Example #14
Source File: hyperlinked_multi_identity_field.py    From kobo-predict with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def get_url(self, obj, view_name, request, format):
        kwargs = {}
        for slug, field in self.lookup_fields:
            lookup_field = getattr(obj, field)
            kwargs[slug] = lookup_field
        return reverse(
            view_name, kwargs=kwargs, request=request, format=format) 
Example #15
Source File: tests.py    From REST-API with MIT License 5 votes vote down vote up
def status_user_token(self):
        auth_url = api_reverse('api-auth:login')
        auth_data = {
            'username': 'testcfeuser',
            'password': 'yeahhhcfe',
        }
        auth_response = self.client.post(auth_url, auth_data, format='json')
        token = auth_response.data.get("token", None)
        if token is not None:
            self.client.credentials(HTTP_AUTHORIZATION='JWT ' + token) 
Example #16
Source File: tests.py    From REST-API with MIT License 5 votes vote down vote up
def create_item(self):
        self.status_user_token()
        url = api_reverse('api-status:list')
        data = {
            'content': "some cool test content"
        }
        response = self.client.post(url, data, format='json')
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(Status.objects.count(), 2)
        return response.data 
Example #17
Source File: tests.py    From REST-API with MIT License 5 votes vote down vote up
def test_empty_create_item(self):
        self.status_user_token()
        url = api_reverse('api-status:list')
        data = {
            'content': None,
            'image': None
        }
        response = self.client.post(url, data, format='json')
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
        return response.data 
Example #18
Source File: tests.py    From REST-API with MIT License 5 votes vote down vote up
def test_status_no_token_create(self):
        url = api_reverse('api-status:list')
        data = {
            'content': "some cool test content"
        }
        response = self.client.post(url, data, format='json')
        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) 
Example #19
Source File: FieldSightXFormApiSerializer.py    From kobo-predict with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def get_manifest_url(self, obj):
        kwargs = {'pk': obj.pk, 'site_id':obj.site.id}
        request = self.context.get('request')

        return reverse('forms:manifest-url', kwargs=kwargs, request=request) 
Example #20
Source File: hyperlinked_multi_related_field.py    From kobo-predict with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def get_url(self, obj, view_name, request, format):
        kwargs = {}

        for slug, field in self.lookup_fields:
            lookup_field = get_obj_property_value(obj, field)

            kwargs[slug] = lookup_field
        return reverse(
            view_name, kwargs=kwargs, request=request, format=format) 
Example #21
Source File: FieldSightXformManifestSerializer.py    From kobo-predict with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def get_url(self, obj):
        kwargs = {'pk': obj.xf.xform.pk,
                  'username': obj.xform.user.username,
                  'metadata': obj.pk}
        request = self.context.get('request')
        format = obj.data_value[obj.data_value.rindex('.') + 1:]

        return reverse('xform-media', kwargs=kwargs,
                       request=request, format=format.lower()) 
Example #22
Source File: xform_serializer.py    From kobo-predict with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def get_url(self, obj):
        kwargs = {'pk': obj.xform.pk,
                  'username': obj.xform.user.username,
                  'metadata': obj.pk}
        request = self.context.get('request')
        format = obj.data_value[obj.data_value.rindex('.') + 1:]

        return reverse('xform-media', kwargs=kwargs,
                       request=request, format=format.lower()) 
Example #23
Source File: xform_serializer.py    From kobo-predict with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def get_manifest_url(self, obj):
        kwargs = {'pk': obj.pk, 'username': obj.user.username}
        request = self.context.get('request')

        return reverse('manifest-url', kwargs=kwargs, request=request) 
Example #24
Source File: xform_serializer.py    From kobo-predict with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def get_url(self, obj):
        kwargs = {'pk': obj.pk, 'username': obj.user.username}
        request = self.context.get('request')

        return reverse('download_xform', kwargs=kwargs, request=request) 
Example #25
Source File: test_views.py    From drf-access-policy with MIT License 5 votes vote down vote up
def test_dev_can_only_get_logs(self):
        dev_group = Group.objects.create(name="dev")
        dev_user = User.objects.create()
        dev_user.groups.add(dev_group)
        self.client.force_authenticate(user=dev_user)

        url = reverse("get-logs")
        response = self.client.get(url, format="json")
        self.assertEqual(response.status_code, 200)

        url = reverse("delete-logs")
        response = self.client.delete(url, format="json")
        self.assertEqual(response.status_code, 403) 
Example #26
Source File: test_views.py    From drf-access-policy with MIT License 5 votes vote down vote up
def test_admin_can_do_anything_with_logs(self):
        admin_group = Group.objects.create(name="admin")
        admin_user = User.objects.create()
        admin_user.groups.add(admin_group)
        self.client.force_authenticate(user=admin_user)

        url = reverse("get-logs")
        response = self.client.get(url, format="json")
        self.assertEqual(response.status_code, 200)

        url = reverse("delete-logs")
        response = self.client.delete(url, format="json")
        self.assertEqual(response.status_code, 200) 
Example #27
Source File: test_view_set.py    From drf-access-policy with MIT License 5 votes vote down vote up
def test_retrieve_denied(self):
        account = UserAccount.objects.create(
            username="fred", first_name="Fred", last_name="Rogers"
        )
        banned_group = Group.objects.create(name="banned")
        banned_user = User.objects.create()
        banned_user.groups.add(banned_group)
        self.client.force_authenticate(user=banned_user)

        url = reverse("account-detail", args=[account.id])

        response = self.client.get(url, format="json")
        self.assertEqual(response.status_code, 403) 
Example #28
Source File: test_view_set.py    From drf-access-policy with MIT License 5 votes vote down vote up
def test_create_allowed(self):
        admin_group = Group.objects.create(name="admin")
        admin_user = User.objects.create()
        admin_user.groups.add(admin_group)
        self.client.force_authenticate(user=admin_user)

        url = reverse("account-list")

        response = self.client.post(
            url,
            {"username": "fred", "first_name": "Fred", "last_name": "Rogers"},
            format="json",
        )

        self.assertEqual(response.status_code, 201) 
Example #29
Source File: test_captcha.py    From desec-stack with MIT License 5 votes vote down vote up
def obtain(self, **kwargs):
        return self.post(reverse('v1:captcha')) 
Example #30
Source File: base.py    From desec-stack with MIT License 5 votes vote down vote up
def _assertDynDNS12Update(self, requests, mock_remote_addr='', **kwargs):
        with self.assertPdnsRequests(requests):
            if mock_remote_addr:
                return self.client.get(self.reverse('v1:dyndns12update'), kwargs, REMOTE_ADDR=mock_remote_addr)
            else:
                return self.client.get(self.reverse('v1:dyndns12update'), kwargs)