Skip to content

Commit 085c3ef

Browse files
committed
add usecase to collaborative
1 parent 5112105 commit 085c3ef

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

api/models/Collaborative.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class Collaborative(models.Model):
3535
choices=CollaborativeStatus.choices,
3636
)
3737
datasets = models.ManyToManyField("api.Dataset", blank=True)
38+
use_cases = models.ManyToManyField("api.UseCase", blank=True)
3839
tags = models.ManyToManyField("api.Tag", blank=True)
3940
sectors = models.ManyToManyField(
4041
"api.Sector", blank=True, related_name="collaboratives"

api/schema/collborative_schema.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
Organization,
2525
Sector,
2626
Tag,
27+
UseCase,
2728
)
2829
from api.schema.extensions import TrackActivity, TrackModelActivity
2930
from api.types.type_collaborative import (
@@ -37,6 +38,7 @@
3738
)
3839
from api.types.type_dataset import TypeDataset
3940
from api.types.type_organization import TypeOrganization
41+
from api.types.type_usecase import TypeUseCase
4042
from api.utils.enums import CollaborativeStatus, OrganizationRelationshipType
4143
from api.utils.graphql_telemetry import trace_resolver
4244
from authorization.models import User
@@ -453,6 +455,32 @@ def add_dataset_to_collaborative(
453455
collaborative.save()
454456
return TypeCollaborative.from_django(collaborative)
455457

458+
@strawberry_django.mutation(handle_django_errors=True)
459+
def add_usecase_to_collaborative(
460+
self, info: Info, collaborative_id: str, usecase_id: str
461+
) -> TypeCollaborative:
462+
"""Add a usecase to a collaborative."""
463+
try:
464+
usecase = UseCase.objects.get(id=usecase_id)
465+
except UseCase.DoesNotExist:
466+
raise ValueError(f"UseCase with ID {usecase_id} does not exist.")
467+
468+
try:
469+
collaborative = Collaborative.objects.get(id=collaborative_id)
470+
except Collaborative.DoesNotExist:
471+
raise ValueError(
472+
f"Collaborative with ID {collaborative_id} does not exist."
473+
)
474+
475+
if collaborative.status != CollaborativeStatus.DRAFT:
476+
raise ValueError(
477+
f"Collaborative with ID {collaborative_id} is not in draft status."
478+
)
479+
480+
collaborative.use_cases.add(usecase)
481+
collaborative.save()
482+
return TypeCollaborative.from_django(collaborative)
483+
456484
@strawberry_django.mutation(handle_django_errors=True)
457485
def remove_dataset_from_collaborative(
458486
self, info: Info, collaborative_id: str, dataset_id: uuid.UUID
@@ -477,6 +505,30 @@ def remove_dataset_from_collaborative(
477505
collaborative.save()
478506
return TypeCollaborative.from_django(collaborative)
479507

508+
@strawberry_django.mutation(handle_django_errors=True)
509+
def remove_usecase_from_collaborative(
510+
self, info: Info, collaborative_id: str, usecase_id: str
511+
) -> TypeCollaborative:
512+
"""Remove a usecase from a collaborative."""
513+
try:
514+
usecase = UseCase.objects.get(id=usecase_id)
515+
except UseCase.DoesNotExist:
516+
raise ValueError(f"UseCase with ID {usecase_id} does not exist.")
517+
try:
518+
collaborative = Collaborative.objects.get(id=collaborative_id)
519+
except Collaborative.DoesNotExist:
520+
raise ValueError(
521+
f"Collaborative with ID {collaborative_id} does not exist."
522+
)
523+
524+
if collaborative.status != CollaborativeStatus.DRAFT:
525+
raise ValueError(
526+
f"Collaborative with ID {collaborative_id} is not in draft status."
527+
)
528+
collaborative.use_cases.remove(usecase)
529+
collaborative.save()
530+
return TypeCollaborative.from_django(collaborative)
531+
480532
@strawberry_django.mutation(handle_django_errors=True)
481533
@trace_resolver(
482534
name="update_collaborative_datasets",
@@ -501,6 +553,30 @@ def update_collaborative_datasets(
501553
collaborative.save()
502554
return TypeCollaborative.from_django(collaborative)
503555

556+
@strawberry_django.mutation(handle_django_errors=True)
557+
@trace_resolver(
558+
name="update_collaborative_use_cases",
559+
attributes={"component": "collaborative", "operation": "mutation"},
560+
)
561+
def update_collaborative_use_cases(
562+
self, info: Info, collaborative_id: str, use_case_ids: List[uuid.UUID]
563+
) -> TypeCollaborative:
564+
"""Update the use cases of a collaborative."""
565+
try:
566+
use_cases = UseCase.objects.filter(id__in=use_case_ids)
567+
collaborative = Collaborative.objects.get(id=collaborative_id)
568+
except Collaborative.DoesNotExist:
569+
raise ValueError(f"Collaborative with ID {collaborative_id} doesn't exist")
570+
571+
if collaborative.status != CollaborativeStatus.DRAFT:
572+
raise ValueError(
573+
f"Collaborative with ID {collaborative_id} is not in draft status."
574+
)
575+
576+
collaborative.use_cases.set(use_cases)
577+
collaborative.save()
578+
return TypeCollaborative.from_django(collaborative)
579+
504580
@strawberry_django.mutation(
505581
handle_django_errors=True,
506582
extensions=[

api/types/type_collaborative.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from api.types.type_dataset import TypeDataset, TypeTag
2121
from api.types.type_organization import TypeOrganization
2222
from api.types.type_sector import TypeSector
23+
from api.types.type_usecase import TypeUseCase
2324
from api.utils.enums import CollaborativeStatus, OrganizationRelationshipType
2425
from authorization.types import TypeUser
2526

@@ -81,6 +82,17 @@ def datasets(self) -> Optional[List["TypeDataset"]]:
8182
except Exception:
8283
return []
8384

85+
@strawberry.field(description="Get use cases associated with this collaborative.")
86+
def use_cases(self) -> Optional[List["TypeUseCase"]]:
87+
"""Get use cases associated with this collaborative."""
88+
try:
89+
queryset = self.use_cases.all() # type: ignore
90+
if not queryset.exists():
91+
return []
92+
return TypeUseCase.from_django_list(queryset)
93+
except Exception:
94+
return []
95+
8496
@strawberry.field(
8597
description="Get the count of datasets associated with this collaborative."
8698
)
@@ -91,6 +103,16 @@ def dataset_count(self: "TypeCollaborative", info: Info) -> int:
91103
except Exception:
92104
return 0
93105

106+
@strawberry.field(
107+
description="Get the count of use cases associated with this collaborative."
108+
)
109+
def use_case_count(self: "TypeCollaborative", info: Info) -> int:
110+
"""Get the count of use cases associated with this collaborative."""
111+
try:
112+
return self.use_cases.count() # type: ignore
113+
except Exception:
114+
return 0
115+
94116
@strawberry.field(description="Get publishers associated with this collaborative.")
95117
def publishers(self) -> Optional[List["TypeOrganization"]]:
96118
"""Get publishers associated with this collaborative."""

0 commit comments

Comments
 (0)