From 4fceb393f042ecbdbe9f8d48e741e08cf08adcbf Mon Sep 17 00:00:00 2001 From: hadleyking Date: Wed, 3 Apr 2024 20:14:46 -0400 Subject: [PATCH] Add `access_count` incrementer to DraftRetrieveApi Changes to be committed: modified: biocompute/apis.py modified: biocompute/services.py modified: prefix/services.py --- biocompute/apis.py | 17 ++++++++++------- biocompute/services.py | 19 +++++++++++++++++-- prefix/services.py | 12 ++++-------- 3 files changed, 31 insertions(+), 17 deletions(-) diff --git a/biocompute/apis.py b/biocompute/apis.py index 226c0415..db514918 100644 --- a/biocompute/apis.py +++ b/biocompute/apis.py @@ -14,11 +14,10 @@ from rest_framework.response import Response from tests.fixtures.example_bco import BCO_000001 from config.services import legacy_api_converter, response_constructor -from biocompute.services import BcoDraftSerializer +from biocompute.services import BcoDraftSerializer, bco_counter_increment from biocompute.selectors import retrieve_bco from prefix.selectors import user_can_draft - hostname = settings.PUBLIC_HOSTNAME BCO_DRAFT_SCHEMA = openapi.Schema( @@ -171,13 +170,16 @@ class DraftRetrieveApi(APIView): API View to Retrieve a Draft Object - This view allows authenticated users to retrieve the contents of a specific draft object - identified by its BioCompute Object (BCO) accession number. The operation ensures that - only users with appropriate permissions can access the draft contents. + This view allows authenticated users to retrieve the contents of a specific + draft object identified by its BioCompute Object (BCO) accession number. + The operation ensures that only users with appropriate permissions can + access the draft contents. Upo successfull retrieval of object the + `access_count` is for this object is incremented. Parameters: - - bco_accession (str): A string parameter passed in the URL path that uniquely identifies - the draft object to be retrieved. + - bco_accession (str): + A string parameter passed in the URL path that uniquely identifies the + draft object to be retrieved. """ @swagger_auto_schema( @@ -210,4 +212,5 @@ def get(self, request, bco_accession): data={"message": f"User, {requester}, does not have draft permissions"\ + f" for {bco_accession}."}) else: + bco_counter_increment(bco_instance) return Response(status=status.HTTP_200_OK, data=bco_instance.contents) \ No newline at end of file diff --git a/biocompute/services.py b/biocompute/services.py index 028530db..92f9cbe4 100644 --- a/biocompute/services.py +++ b/biocompute/services.py @@ -1,13 +1,14 @@ #!/usr/bin/env python3 # biocopmute/services.py +from biocompute.models import Bco from django.conf import settings +from django.contrib.auth.models import User from django.db import transaction +from django.db.models import F from django.utils import timezone -from biocompute.models import Bco from prefix.models import Prefix from prefix.services import prefix_counter_increment -from django.contrib.auth.models import User from rest_framework import serializers """BioCompute Services @@ -176,3 +177,17 @@ def create_bco_id(prefix_instance: Prefix) -> str: unique_id_found = True return bco_id + +def bco_counter_increment(bco_instance: Bco) -> int: + """BCO Counter Increment + + Simple incrementing function. + Counter for BCO object_id asignment. + """ + + bco_instance.access_count = F('access_count') + 1 + bco_instance.save() + + bco_instance.refresh_from_db() + + return bco_instance.access_count \ No newline at end of file diff --git a/prefix/services.py b/prefix/services.py index 91682c55..363f1e63 100644 --- a/prefix/services.py +++ b/prefix/services.py @@ -1,19 +1,15 @@ #!/usr/bin/env python3 # prefix/services.py -import re -from urllib.parse import urlparse from django.conf import settings -from django.contrib.auth.models import Permission +from django.contrib.auth.models import User, Permission from django.contrib.contenttypes.models import ContentType -from django.db import utils +from django.db import transaction, utils +from django.db.models import F from django.utils import timezone from prefix.models import Prefix -from django.db import transaction -from django.contrib.auth.models import User -from django.db.models import F +from prefix.selectors import get_prefix_object from rest_framework import serializers -from prefix.selectors import get_prefix_permissions, get_prefix_object """Prefix Services