Skip to content

Commit 54b7c49

Browse files
committed
Add prefix creation function
Changes to be committed: modified: authentication/services.py modified: biocompute/models.py modified: config/settings.py modified: docs/refactor.md modified: prefix/apis.py modified: prefix/models.py modified: prefix/services.py modified: tests/test_views/test_api_objects_drafts_create.py
1 parent 2530f7d commit 54b7c49

File tree

8 files changed

+55
-59
lines changed

8 files changed

+55
-59
lines changed

authentication/services.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,6 @@ def create_bcodb_user(email: str) -> User:
190190

191191
return user
192192

193-
194193
def send_bcodb(data: str, request_info: dict):
195194
"""
196195
"""

biocompute/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class Bco(models.Model):
4949
prefix = models.ForeignKey(Prefix, on_delete=models.CASCADE, to_field="prefix")
5050
owner = models.ForeignKey(
5151
User,
52+
to_field="username",
5253
on_delete=models.CASCADE,
5354
related_name="owned_bcos"
5455
)

config/settings.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,18 +123,13 @@
123123
},
124124
]
125125

126-
# Object-level permissions with django-guardian
127-
# Source: https://github.com/django-guardian/django-guardian#configuration
128126
AUTHENTICATION_BACKENDS = [
129127
"django.contrib.auth.backends.ModelBackend",
130-
"guardian.backends.ObjectPermissionBackend",
131128
]
132129

133130
# --- APPLICATION --- #
134131
# Application definition
135132

136-
# Token-based authentication.
137-
# Source: https://www.django-rest-framework.org/api-guide/authentication/#tokenau thentication
138133
INSTALLED_APPS = [
139134
"django.contrib.admin",
140135
"django.contrib.admindocs",
@@ -150,8 +145,6 @@
150145
'rest_framework_jwt.blacklist',
151146
"rest_framework_swagger",
152147
"reset_migrations",
153-
"guardian",
154-
# "api",
155148
"authentication",
156149
"biocompute",
157150
"prefix"

docs/refactor.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@
2727
### Refactor the BCO permission system
2828
- same situation as prefix
2929

30+
## Permissions
31+
32+
- BCO has `owner`, `auth_group` and `auth_user`
33+
- Prefix has `owner`, and `auth_group`
34+
3035
## Items to look at later
3136
- `authentication.apis.RegisterUserNoVerificationAPI` has no swagger or tests
3237
- fix email and secrets
@@ -40,3 +45,11 @@
4045
- unwanted swagger endpoints
4146
- need tests for token
4247
- prefix api documentation and portal docs for prefix
48+
49+
Prefix Perms:
50+
add -> create new DRAFT
51+
edit -> Change existing Draft
52+
delete -> Delete Draft
53+
publish -> Publish Draft
54+
view -> View/download
55+
ONLY if private

prefix/apis.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,15 @@
2727
description="A description of what this prefix should represent. For example, the prefix 'GLY' would be related to BCOs which were derived from GlyGen workflows.",
2828
example="Test prefix description."
2929
),
30-
"authorized_groups": openapi.Schema(
31-
type=openapi.TYPE_ARRAY,
32-
description="Groups which can access the BCOs using this prefix. If it is none then anyone can access.",
33-
items=openapi.Schema(type=openapi.TYPE_STRING, example="")
30+
"certifying_key": openapi.Schema(
31+
type=openapi.TYPE_STRING,
32+
description="Hash of server and date-time of creation.",
33+
example="12345678910"
34+
),
35+
"public": openapi.Schema(
36+
type=openapi.TYPE_BOOLEAN,
37+
description="Flag to set permissions.",
38+
example=True
3439
)
3540
},
3641
)

prefix/models.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@ class Prefix(models.Model):
1919
on_delete=models.CASCADE,
2020
to_field="username"
2121
)
22-
authorized_groups = models.ManyToManyField(
23-
Group,
24-
blank=True,
25-
related_name='authorized_prefix'
26-
)
2722
counter = models.IntegerField(
2823
default=0,
2924
help_text="Counter for object_id asignment"

prefix/services.py

Lines changed: 28 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import re
55
from urllib.parse import urlparse
66
from django.conf import settings
7+
from django.contrib.auth.models import Permission
8+
from django.contrib.contenttypes.models import ContentType
79
from django.utils import timezone
810
from prefix.models import Prefix
911
from django.db import transaction
@@ -20,6 +22,7 @@ class PrefixSerializer(serializers.Serializer):
2022
prefix = serializers.CharField(min_length=3, max_length=5)
2123
description = serializers.CharField()
2224
authorized_groups = serializers.ListField(child=serializers.CharField(allow_blank=True), required=False)
25+
public = serializers.BooleanField(required=False)
2326

2427
def validate(self, attrs):
2528
"""Prefix Validator
@@ -41,65 +44,52 @@ def validate(self, attrs):
4144
if "create" in request.path_info:
4245
pass
4346
else:
44-
errors["prefix_name"] = f"That Prefix, {prefix_name}, was not found."
45-
46-
47-
48-
# remove blank 'authorized_groups' relic from legacy conversion
49-
if attrs['authorized_groups'][0] == "":
50-
attrs.pop("authorized_groups")
51-
52-
#check for groups
53-
if 'authorized_groups' in attrs:
54-
for group in attrs['authorized_groups']:
55-
try:
56-
Group.objects.get(name=group)
57-
except Group.DoesNotExist as err:
58-
errors['authorized_groups'] = f"Invalid group: {group}"
59-
60-
# If erros exist than raise and exception and return it, otherwise
61-
# return validated data
62-
if errors:
63-
raise serializers.ValidationError(errors)
47+
raise serializers.ValidationError({"prefix_name": f"That Prefix, {prefix_name}, was not found."})
6448

6549
return attrs
6650

6751
@transaction.atomic
6852
def create(self, validated_data):
6953
"""Create function for Prefix
7054
"""
71-
authorized_group_names = validated_data.pop('authorized_groups', [])
55+
public = validated_data.pop('public', [])
56+
import pdb; pdb.set_trace()
7257
prefix_instance = Prefix.objects.create(**validated_data, created=timezone.now())
73-
# Set ManyToMany relations
74-
if authorized_group_names:
75-
authorized_groups = Group.objects.filter(name__in=authorized_group_names)
76-
prefix_instance.authorized_groups.set(authorized_groups)
58+
7759
return prefix_instance
7860

7961
@transaction.atomic
8062
def update(self, validated_data):
8163
"""Update function for Prefix."""
8264
prefix_instance = Prefix.objects.get(prefix=validated_data['prefix'])
8365
if prefix_instance.owner != validated_data['owner']:
84-
# import pdb; pdb.set_trace()
8566
return "denied"
8667
prefix_instance.description = validated_data.get('description', prefix_instance.description)
8768
prefix_instance.save()
8869

89-
if 'authorized_groups' in validated_data:
90-
authorized_group_names = validated_data['authorized_groups']
91-
# If the list is empty or contains only an empty string, clear the groups
92-
if not authorized_group_names or authorized_group_names == [""]:
93-
prefix_instance.authorized_groups.clear()
70+
return prefix_instance
9471

95-
else:
96-
# Filter groups that exist in the database
97-
authorized_groups = Group.objects.filter(name__in=authorized_group_names)
98-
99-
# Set the new groups, which automatically handles adding, keeping, or removing
100-
prefix_instance.authorized_groups.set(authorized_groups)
72+
def create_permissions_for_prefix(instance=None, owner=User):
73+
"""Prefix Permission Creation
10174
102-
return prefix_instance
75+
Creates permissions for a Prefix if it is not public. Owner is assigned
76+
all permissions and then can add permissions to other users.
77+
78+
'view' -> View/download Prefix drafts
79+
'add' -> create new drafts for Prefix
80+
'change' -> Change existing drafts for Prefix
81+
'delete' -> Delete drafts for Prefix
82+
'publish' -> Publish drafts for Prefix
83+
"""
84+
try:
85+
for perm in [ "view", "add", "change", "delete", "publish"]:
86+
print(instance)
87+
Permission.objects.create(
88+
name="Can " + perm + " BCOs with prefix " + instance.prefix,
89+
content_type=ContentType.objects.get(app_label="api", model="bco"),
90+
codename=perm + "_" + instance.prefix,)
91+
except:
92+
return 0
10393

10494
def prefix_counter_increment(prefix: Prefix) -> int:
10595
"""Prefix Counter Increment

tests/test_views/test_api_objects_drafts_create.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ def setUp(self):
4242
"prefix": "BCO",
4343
"authorized_users": ["hivelab"],
4444
"contents": {
45-
"object_id": "https://test.portal.biochemistry.gwu.edu/BCO_000001/DRAFT",
46-
"spec_version": "https://w3id.org/ieee/ieee-2791-schema/2791object.json",
47-
"etag": "11ee4c3b8a04ad16dcca19a6f478c0870d3fe668ed6454096ab7165deb1ab8ea"
48-
}
45+
"object_id": "https://test.portal.biochemistry.gwu.edu/BCO_000001/DRAFT",
46+
"spec_version": "https://w3id.org/ieee/ieee-2791-schema/2791object.json",
47+
"etag": "11ee4c3b8a04ad16dcca19a6f478c0870d3fe668ed6454096ab7165deb1ab8ea"
48+
}
4949
},
5050
{
5151
"object_id": "http://127.0.0.1:8000/TEST_000001",

0 commit comments

Comments
 (0)