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

Get published object by #201

Merged
merged 16 commits into from
Jul 27, 2023
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/django.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ jobs:
pip install -r requirements.txt
- name: Run Tests
run: |
cd bco_api;python3.9 manage.py test
python3.9 manage.py test
Binary file added admin_only/db.sqlite3
Binary file not shown.
Binary file modified admin_only/db.sqlite3.dev
Binary file not shown.
7 changes: 5 additions & 2 deletions api/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Source: https://docs.djangoproject.com/en/3.2/ref/applications/#django.apps.AppConfig.ready
"""

import sys
from django.apps import AppConfig
from django.db.models.signals import post_migrate
from api.signals import populate_models
Expand All @@ -15,7 +16,9 @@ class ApiConfig(AppConfig):

default_auto_field = "django.db.models.AutoField"
name = "api"

def ready(self):
"""Create the anonymous user if they don't exist."""
post_migrate.connect(populate_models, sender=self)

if not 'test' in sys.argv:
post_migrate.connect(populate_models, sender=self)
25 changes: 14 additions & 11 deletions api/model/groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"""Functions for operations with groups
"""

import sys
from django.db import models
from django.db.models.signals import post_save
from django.contrib.auth.models import Group, User
Expand Down Expand Up @@ -430,14 +431,16 @@ def associate_user_group(sender, instance, created, **kwargs):
if the user isn't anon or the already existent bco_drafter or bco_publisher.
"""

if created:
Group.objects.create(name=instance)
group = Group.objects.get(name=instance)
group.user_set.add(instance)
if instance.username not in ["anon", "bco_drafter", "bco_publisher", "AnonymousUser"]:
User.objects.get(username=instance).groups.add(
Group.objects.get(name="bco_drafter")
)
User.objects.get(username=instance).groups.add(
Group.objects.get(name="bco_publisher")
)
if not 'test' in sys.argv:
if created:
print(instance)
Group.objects.create(name=instance)
group = Group.objects.get(name=instance)
group.user_set.add(instance)
if instance.username not in ["anon", "bco_drafter", "bco_publisher", "AnonymousUser"]:
User.objects.get(username=instance).groups.add(
Group.objects.get(name="bco_drafter")
)
User.objects.get(username=instance).groups.add(
Group.objects.get(name="bco_publisher")
)
57 changes: 30 additions & 27 deletions api/model/prefix.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@


import re
import sys
from django.db import models
from django.contrib.auth.models import Group, Permission, User
from django.db.models.signals import post_save, post_delete, pre_save
Expand Down Expand Up @@ -689,30 +690,32 @@ def create_permissions_for_prefix(sender, instance=None, **kwargs):
# max_n_members=-1,
# owner_user=User.objects.get(username='wheel')
# )
owner_user = User.objects.get(username=instance.owner_user)
owner_group = Group.objects.get(name=instance.owner_group_id)
drafters = Group.objects.get(name=instance.prefix.lower() + "_drafter")
publishers = Group.objects.get(name=instance.prefix.lower() + "_publisher")

try:
for perm in ["add", "change", "delete", "view", "draft", "publish"]:
Permission.objects.create(
name="Can " + perm + " BCOs with prefix " + instance.prefix,
content_type=ContentType.objects.get(app_label="api", model="bco"),
codename=perm + "_" + instance.prefix,
)
new_perm = Permission.objects.get(codename=perm + "_" + instance.prefix)
owner_user.user_permissions.add(new_perm)
owner_group.permissions.add(new_perm)
publishers.permissions.add(new_perm)
if perm == "publish":
pass
else:
drafters.permissions.add(new_perm)

except PermErrors.IntegrityError:
# The permissions already exist.
pass

if not 'test' in sys.argv:
owner_user = User.objects.get(username=instance.owner_user)
owner_group = Group.objects.get(name=instance.owner_group_id)
drafters = Group.objects.get(name=instance.prefix.lower() + "_drafter")
publishers = Group.objects.get(name=instance.prefix.lower() + "_publisher")

try:
for perm in ["add", "change", "delete", "view", "draft", "publish"]:
Permission.objects.create(
name="Can " + perm + " BCOs with prefix " + instance.prefix,
content_type=ContentType.objects.get(app_label="api", model="bco"),
codename=perm + "_" + instance.prefix,
)
new_perm = Permission.objects.get(codename=perm + "_" + instance.prefix)
owner_user.user_permissions.add(new_perm)
owner_group.permissions.add(new_perm)
publishers.permissions.add(new_perm)
if perm == "publish":
pass
else:
drafters.permissions.add(new_perm)

except PermErrors.IntegrityError:
# The permissions already exist.
pass


@receiver(post_save, sender=Prefix)
Expand All @@ -727,9 +730,9 @@ def create_counter_for_prefix(sender, instance=None, created=False, **kwargs):
instance: api.model.prefix.Prefix
created: bool
"""

if created:
prefix_table.objects.create(n_objects=1, prefix=instance.prefix)
if not 'test' in sys.argv:
if created:
prefix_table.objects.create(n_objects=1, prefix=instance.prefix)


@receiver(post_delete, sender=Prefix)
Expand Down
4 changes: 2 additions & 2 deletions api/scripts/method_specific/GET_published_object_by_id.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# The BCO model
from ...models import BCO
from api.models import BCO

# Responses
from rest_framework import status
Expand Down Expand Up @@ -107,5 +107,5 @@ def GET_published_object_by_id(oi_root):
print("No objects were found for the root ID provided.")
return Response(
data="No objects were found for the root ID provided.",
status=status.HTTP_400_BAD_REQUEST,
status=status.HTTP_404_NOT_FOUND,
)
17 changes: 14 additions & 3 deletions api/scripts/method_specific/POST_api_objects_publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,16 @@ def post_api_objects_publish(incoming):
any_failed = False
results = {}
for publish_object in bulk_request:
results = parse_bco(publish_object["contents"], results)
try:
results = parse_bco(publish_object["contents"], results)
except KeyError as error:
returning.append(
db_utils().messages(parameters={"errors": str(error)})[
"400_non_publishable_object"
]
)
any_failed = True
continue
object_key = publish_object["contents"]["object_id"]
if results[object_key]["number_of_errors"] > 0:
returning.append(
Expand All @@ -47,6 +56,7 @@ def post_api_objects_publish(incoming):
if "publish_" + prefix in px_perms:
if "object_id" in publish_object:
accession = publish_object["object_id"].split("/")[-2]
version = publish_object["object_id"].split("/")[-1]
object_num = int(
publish_object["object_id"].split("_")[1].split("/")[0]
)
Expand All @@ -57,9 +67,10 @@ def post_api_objects_publish(incoming):
+ "/"
+ publish_object["contents"]["provenance_domain"]["version"]
)
if BCO.objects.filter(object_id__contains=accession).exists():
if BCO.objects.filter(object_id__contains=accession+'/'+version).exists():
# import pdb; pdb.set_trace()
returning.append(
db_utils().messages(parameters={"object_id": accession})[
db_utils().messages(parameters={"object_id": accession+'/'+version})[
"409_object_conflict"
]
)
Expand Down
4 changes: 2 additions & 2 deletions api/scripts/utilities/JsonUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ def validate(schema, json_object, results):
return results


def parse_bco(bco, results):
def parse_bco(bco: dict, results: dict):
"""BCO Parsing for Validation

Parameters
----------
bco : JSON
bco : dict
The BCO JSON to be processed for validation.
results : dict
A dictionary to be populated with the BCO validation results
Expand Down
Loading
Loading