Skip to content

Commit

Permalink
Merge pull request #496 from alan-turing-institute/carlos-development
Browse files Browse the repository at this point in the history
[FIX] Fixing ordering when updating identifiers
  • Loading branch information
cptanalatriste authored Jun 19, 2024
2 parents 686bcf4 + ffbdb2b commit 1b56e3e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 46 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/backend-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.7", "3.8"]
python-version: ["3.7", "3.8"] # TODO(cgavidia): Check with Nick about these versions.
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
Expand Down
13 changes: 3 additions & 10 deletions eap_backend/eap_api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ class AssuranceCase(models.Model):
name = models.CharField(max_length=200)
description = models.CharField(max_length=1000)
created_date = models.DateTimeField(auto_now_add=True)
lock_uuid = models.CharField(
max_length=50, default=None, null=True, blank=True
)
lock_uuid = models.CharField(max_length=50, default=None, null=True, blank=True)
owner = models.ForeignKey(
EAPUser, related_name="cases", on_delete=models.CASCADE, null=True
)
Expand Down Expand Up @@ -188,10 +186,7 @@ def save(self, *args, **kwargs):
error_message = "A PropertyClaim should have exactly one parent."
raise ValueError(error_message)

if (
self.property_claim is not None
and self.property_claim.pk == self.pk
):
if self.property_claim is not None and self.property_claim.pk == self.pk:
error_message = "A PropertyClaim cannot be the parent of itself."
raise ValueError(error_message)

Expand All @@ -208,6 +203,4 @@ def save(self, *args, **kwargs):
class Evidence(CaseItem):
URL = models.CharField(max_length=3000)
shape = Shape.CYLINDER
property_claim = models.ManyToManyField(
PropertyClaim, related_name="evidence"
)
property_claim = models.ManyToManyField(PropertyClaim, related_name="evidence")
53 changes: 19 additions & 34 deletions eap_backend/eap_api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,7 @@ def goal_list(request):
elif request.method == "POST":

data = JSONParser().parse(request)
assurance_case_id = AssuranceCase.objects.get(
id=data["assurance_case_id"]
)
assurance_case_id = AssuranceCase.objects.get(id=data["assurance_case_id"])
data["assurance_case"] = assurance_case_id
serializer = TopLevelNormativeGoalSerializer(data=data)
if serializer.is_valid():
Expand Down Expand Up @@ -328,9 +326,7 @@ def goal_detail(request, pk):
return JsonResponse(data)
elif request.method == "PUT":
data = JSONParser().parse(request)
serializer = TopLevelNormativeGoalSerializer(
goal, data=data, partial=True
)
serializer = TopLevelNormativeGoalSerializer(goal, data=data, partial=True)
if serializer.is_valid():
serializer.save()
data = serializer.data
Expand Down Expand Up @@ -665,19 +661,13 @@ def reply_to_comment(request, comment_id):
if request.method == "POST":
data = JSONParser().parse(request)
data["parent"] = comment_id
data[
"author"
] = request.user.id # Ensure the author is set to the current user
data["author"] = request.user.id # Ensure the author is set to the current user
data["assurance_case"] = parent_comment.assurance_case_id
serializer = CommentSerializer(data=data)
if serializer.is_valid():
serializer.save()
return JsonResponse(
serializer.data, status=status.HTTP_201_CREATED
)
return JsonResponse(
serializer.errors, status=status.HTTP_400_BAD_REQUEST
)
return JsonResponse(serializer.data, status=status.HTTP_201_CREATED)
return JsonResponse(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
return JsonResponse(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


Expand All @@ -687,26 +677,25 @@ def update_identifiers(case_id: Optional[int] = None):
if case_id is None:
raise ValueError(error_message)

if TopLevelNormativeGoal.objects.filter(
assurance_case_id=case_id
).exists():
if TopLevelNormativeGoal.objects.filter(assurance_case_id=case_id).exists():

current_case_goal: TopLevelNormativeGoal = (
TopLevelNormativeGoal.objects.get(assurance_case_id=case_id)
current_case_goal: TopLevelNormativeGoal = TopLevelNormativeGoal.objects.get(
assurance_case_id=case_id
)
goal_id: int = current_case_goal.pk

update_sequential_identifiers(
TopLevelNormativeGoal.objects.filter(id=goal_id), "G"
TopLevelNormativeGoal.objects.filter(id=goal_id).order_by("id"),
"G",
)

update_sequential_identifiers(
Context.objects.filter(goal_id=goal_id), "C"
Context.objects.filter(goal_id=goal_id).order_by("id"), "C"
)

current_case_strategies: QuerySet = Strategy.objects.filter(
goal_id=goal_id
)
).order_by("id")
update_sequential_identifiers(current_case_strategies, "S")

top_level_claim_ids, child_claim_ids = get_case_property_claims(
Expand All @@ -716,13 +705,13 @@ def update_identifiers(case_id: Optional[int] = None):
update_sequential_identifiers(
Evidence.objects.filter(
property_claim__id__in=top_level_claim_ids + child_claim_ids
),
).order_by("id"),
"E",
)

parent_property_claims: QuerySet = PropertyClaim.objects.filter(
pk__in=top_level_claim_ids
)
).order_by("id")

update_sequential_identifiers(parent_property_claims, "P")

Expand All @@ -744,19 +733,17 @@ def get_case_property_claims(
claim.pk
for claim in PropertyClaim.objects.filter(
Q(goal_id=goal.pk) | Q(strategy__id__in=strategy_ids)
)
).order_by("id")
]

child_claim_ids: list[int] = []
for parent_claim_id in top_level_claim_ids:
traverse_child_property_claims(
lambda _, child, parent: child_claim_ids.append(
child.pk
), # noqa: ARG005
lambda _, child, parent: child_claim_ids.append(child.pk), # noqa: ARG005
parent_claim_id,
)

return top_level_claim_ids, child_claim_ids
return top_level_claim_ids, sorted(child_claim_ids)


def traverse_child_property_claims(
Expand All @@ -766,7 +753,7 @@ def traverse_child_property_claims(

child_property_claims = PropertyClaim.objects.filter(
property_claim_id=parent_claim_id
)
).order_by("id")

if len(child_property_claims) == 0:
return
Expand All @@ -777,9 +764,7 @@ def traverse_child_property_claims(
child_property_claim,
PropertyClaim.objects.get(pk=parent_claim_id),
)
traverse_child_property_claims(
on_child_claim, child_property_claim.pk
)
traverse_child_property_claims(on_child_claim, child_property_claim.pk)


def update_item_name(case_item: CaseItem, prefix: str, number: int) -> None:
Expand Down
2 changes: 1 addition & 1 deletion next_frontend/components/common/NewLinkForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ const NewLinkForm: React.FC<NewLinkFormProps> = ({
name: `E${identifier}`,
short_description: description,
long_description: description,
URL: 'ww.some-evidence.com',
URL: 'www.some-evidence.com',
property_claim_id
};

Expand Down

0 comments on commit 1b56e3e

Please sign in to comment.