Skip to content
Snippets Groups Projects
Commit dcab4da1 authored by Calum Mackervoy's avatar Calum Mackervoy
Browse files

bugfix: resolve_id raises error if given a base container

parent ee354b9c
No related branches found
Tags v0.6.25
No related merge requests found
......@@ -2,7 +2,7 @@ import json
import uuid
from urllib.parse import urlparse
from django.conf import settings
from django.core.exceptions import ObjectDoesNotExist
from django.core.exceptions import ObjectDoesNotExist, ValidationError
from django.contrib.auth import get_user_model
from django.db import models
from django.db.models import BinaryField, DateField
......@@ -116,8 +116,15 @@ class Model(models.Model):
@classonlymethod
def resolve_id(cls, id):
'''
Resolves the id of a given path (e.g. /container/1/)
Raises ValidationError if the path has no id, a Resolver404 if the path cannot be found
and an ObjectDoesNotExist exception if the resource does not exist
'''
id = cls.__clean_path(id)
view, args, kwargs = get_resolver().resolve(id)
if len(kwargs.keys()) == 0:
raise ValidationError('no id in given path')
return view.initkwargs['model'].objects.get(**kwargs)
@classonlymethod
......@@ -128,12 +135,18 @@ class Model(models.Model):
@classonlymethod
def resolve_container(cls, path):
'''retruns the model container of passed URL path'''
path = cls.__clean_path(path)
view, args, kwargs = get_resolver().resolve(path)
return view.initkwargs['model']
@classonlymethod
def resolve(cls, path):
'''
resolves the containing model and associated id in the path. If there is no id in the path returns None
:param path: a URL path to check
:return: the container model and resolved id in a tuple
'''
if settings.BASE_URL in path:
path = path[len(settings.BASE_URL):]
container = cls.resolve_container(path)
......
......@@ -188,7 +188,8 @@ class Post(Model):
class Circle(Model):
description = models.CharField(max_length=255, null=True, blank=False)
name = models.CharField(max_length=255, blank=True)
description = models.CharField(max_length=255, blank=True)
team = models.ManyToManyField(settings.AUTH_USER_MODEL, through="CircleMember", blank=True)
owner = models.ForeignKey(settings.AUTH_USER_MODEL, related_name="owned_circles", on_delete=models.DO_NOTHING,
null=True, blank=True)
......
......@@ -38,13 +38,6 @@ class TestsGuardian(APITestCase):
response = self.client.get('/permissionless-dummys/')
self.assertEqual(response.status_code, 403)
# tests that dummy with permissions set enforces these permissions
def test_list_dummy_permission_granted(self):
self.setUpLoggedInUser()
self.setUpGuardianDummyWithPerms(['view'])
response = self.client.get('/permissionless-dummys/')
self.assertEqual(response.status_code, 200)
def test_get_dummy_permission_granted(self):
self.setUpLoggedInUser()
self.setUpGuardianDummyWithPerms(['view'])
......@@ -58,13 +51,6 @@ class TestsGuardian(APITestCase):
response = self.client.get('/permissionless-dummys/{}/'.format(dummy_without.slug))
self.assertEqual(response.status_code, 403)
def test_post_dummy_permission_granted(self):
self.setUpLoggedInUser()
self.setUpGuardianDummyWithPerms(['add'])
post = {'some': "some_new", "slug": 'slug1'}
response = self.client.post('/permissionless-dummys/', data=json.dumps(post), content_type='application/ld+json')
self.assertEqual(response.status_code, 201)
def test_patch_dummy_permission_granted(self):
self.setUpLoggedInUser()
self.setUpGuardianDummyWithPerms(['change'])
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment