Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace jsonfield with newer django-jsonfield-backport #67

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion django_prbac/arbitrary.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import uuid
from random import choice

from django.contrib.auth.models import User
from django.contrib.auth import get_user_model

User = get_user_model()

from django_prbac.models import *

Expand Down
19 changes: 19 additions & 0 deletions django_prbac/migrations/0002_auto_20210930_1858.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 2.2.24 on 2021-09-30 18:58

from django.db import migrations
import django_jsonfield_backport.models


class Migration(migrations.Migration):

dependencies = [
('django_prbac', '0001_initial'),
]

operations = [
migrations.AlterField(
model_name='grant',
name='assignment',
field=django_jsonfield_backport.models.JSONField(blank=True, default=dict, help_text='Assignment from parameters (strings) to values (any JSON-compatible value)'),
),
]
5 changes: 2 additions & 3 deletions django_prbac/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
def python_2_unicode_compatible(fn):
return fn

import jsonfield

from django_prbac.fields import StringSetField
from django_jsonfield_backport.models import JSONField


__all__ = [
Expand Down Expand Up @@ -208,7 +207,7 @@ class Grant(ValidatingModel, models.Model):
on_delete=models.CASCADE,
)

assignment = jsonfield.JSONField(
assignment = JSONField(
help_text='Assignment from parameters (strings) to values (any JSON-compatible value)',
blank=True,
default=dict,
Expand Down
14 changes: 13 additions & 1 deletion django_prbac/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.test import TestCase # https://code.djangoproject.com/ticket/20913

from django_prbac.models import Role
from django_prbac.models import Role, Grant
from django_prbac import arbitrary


Expand Down Expand Up @@ -95,6 +95,18 @@ def test_instantiated_to_role_smoke_test(self):
grant = arbitrary.grant(to_role=superrole, assignment={'two': 'goodbye'})
self.assertEqual(grant.instantiated_to_role({}).assignment, {})

def test_query_grant_assignments(self):
"""
Test we can search grants using features in newer JSONField
"""

user = arbitrary.role()
privilege = arbitrary.role(parameters={'thing'})
for thing in ['thingone', 'thingtwo']:
arbitrary.grant(to_role=privilege, from_role=user, assignment={'thing': thing})

assert Grant.objects.filter(from_role=user, assignment__thing='thingone').count() == 1


class TestUserRole(TestCase):

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def get_readme():
# avoid django 2 <2.2.10 and django 3 < 3.0.7
# https://github.com/advisories/GHSA-hmr4-m2h5-33qx
'django>=2.2.13,!=3.0.0,!=3.0.1,!=3.0.2,!=3.0.3,!=3.0.4,!=3.0.5,!=3.0.6,<4',
'jsonfield>=1.0.3,<4',
'django-jsonfield-backport>=1.0,<2',
'simplejson',
],
classifiers=[
Expand Down