Skip to content

Commit

Permalink
revert libraries attribute back for refernece of both orcabusid and l…
Browse files Browse the repository at this point in the history
…ibraryid
  • Loading branch information
raylrui committed Sep 9, 2024
1 parent d867c2f commit 4c50acf
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 68 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 5.1 on 2024-09-09 05:55

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("workflow_manager", "0001_initial"),
]

operations = [
migrations.AlterField(
model_name="state",
name="workflow_run",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="states",
to="workflow_manager.workflowrun",
),
),
]

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class WorkflowRun(OrcaBusBaseModel):
workflow = models.ForeignKey(Workflow, null=True, blank=True, on_delete=models.SET_NULL)

# Link to library table
# libraries = models.ManyToManyField(Library, through="LibraryAssociation")
libraries = models.ManyToManyField(Library, through="LibraryAssociation")

objects = WorkflowRunManager()

Expand All @@ -54,18 +54,14 @@ def get_all_states(self):
def get_latest_state(self):
# retrieve all related states and get the latest one
return self.states.order_by('-timestamp').first()

def get_libraries(self):
# retrieve all related libraries objects
return Library.objects.filter(workflow_run_association__workflow_run=self).distinct()

class LibraryAssociationManager(OrcaBusBaseManager):
pass


class LibraryAssociation(OrcaBusBaseModel):
workflow_run = models.ForeignKey(WorkflowRun, related_name="library_association", on_delete=models.CASCADE)
library = models.ForeignKey(Library, related_name="workflow_run_association", on_delete=models.CASCADE)
workflow_run = models.ForeignKey(WorkflowRun, on_delete=models.CASCADE)
library = models.ForeignKey(Library, on_delete=models.CASCADE)
association_date = models.DateTimeField()
status = models.CharField(max_length=255)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,14 @@ class Meta:
fields = '__all__'


class LibraryModelSerializer(serializers.ModelSerializer):
class Meta:
model = Library
fields = '__all__'

class WorkflowRunModelSerializer(serializers.ModelSerializer):
current_state = serializers.SerializerMethodField()
libraries = serializers.SerializerMethodField()
libraries = LibraryModelSerializer(many=True, read_only=True)
workflow = WorkflowModelSerializer(read_only=True)
class Meta:
model = WorkflowRun
Expand All @@ -50,22 +55,13 @@ class Meta:
def get_current_state(self, obj)->dict:
latest_state = obj.get_latest_state()
return StateModelSerializer(latest_state).data if latest_state else None

def get_libraries(self, obj)->list:
libraries = obj.get_libraries()
return [library.library_id for library in libraries]

class PayloadModelSerializer(serializers.ModelSerializer):
class Meta:
model = Payload
fields = '__all__'


class LibraryModelSerializer(serializers.ModelSerializer):
class Meta:
model = Library
fields = '__all__'


class StateModelSerializer(serializers.ModelSerializer):
class Meta:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class WorkflowRunViewSet(ReadOnlyModelViewSet):
search_fields = WorkflowRun.get_base_fields()

def get_queryset(self):
return WorkflowRun.objects.get_by_keyword(**self.request.query_params).prefetch_related('states').prefetch_related('library_association__library').select_related('workflow') # add prefetch_related & select_related to reduce the number of queries
return WorkflowRun.objects.get_by_keyword(**self.request.query_params).prefetch_related('states').prefetch_related('libraries').select_related('workflow') # add prefetch_related & select_related to reduce the number of queries

@action(detail=False, methods=['GET'])
def ongoing(self, request):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def test_create_wrsc_no_library(self):
db_wfr: WorkflowRun = wfr_qs.first()
self.assertEqual("ctTSO500-L000002", db_wfr.workflow_run_name)
# We don't expect any library associations here!
self.assertEqual(0, db_wfr.get_libraries().count())
self.assertEqual(0, db_wfr.libraries.count())

def test_create_wrsc_library(self):
"""
Expand Down Expand Up @@ -116,8 +116,8 @@ def test_create_wrsc_library(self):
db_wfr: WorkflowRun = wfr_qs.first()
self.assertEqual("ctTSO500-L000002", db_wfr.workflow_run_name)
# We do expect 2 library associations here!
self.assertEqual(2, db_wfr.get_libraries().count())
for lib in db_wfr.get_libraries().all():
self.assertEqual(2, db_wfr.libraries.count())
for lib in db_wfr.libraries.all():
self.assertTrue(lib.library_id in library_ids)

def test_create_wrsc_library_exists(self):
Expand Down Expand Up @@ -184,8 +184,8 @@ def test_create_wrsc_library_exists(self):
db_wfr: WorkflowRun = wfr_qs.first()
self.assertEqual("ctTSO500-L000002", db_wfr.workflow_run_name)
# We do expect 2 library associations here!
self.assertEqual(2, db_wfr.get_libraries().count())
for lib in db_wfr.get_libraries().all():
self.assertEqual(2, db_wfr.libraries.count())
for lib in db_wfr.libraries.all():
self.assertTrue(lib.library_id in library_ids)

def test_get_last_state(self):
Expand Down

0 comments on commit 4c50acf

Please sign in to comment.