Skip to content

Commit

Permalink
Merge pull request #2244 from uktrade/uat
Browse files Browse the repository at this point in the history
PROD Release
  • Loading branch information
depsiatwal authored Oct 23, 2024
2 parents c282cc4 + bcfd3b8 commit 0484887
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 738 deletions.
10 changes: 1 addition & 9 deletions api/flags/libraries/get_flag.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
from django.http import Http404

from api.core.exceptions import NotFoundError
from api.flags.models import Flag, FlaggingRule
from api.flags.models import Flag


def get_flag(pk):
try:
return Flag.objects.get(pk=pk)
except Flag.DoesNotExist:
raise Http404


def get_flagging_rule(pk):
try:
return FlaggingRule.objects.get(pk=pk)
except FlaggingRule.DoesNotExist:
raise NotFoundError({"flagging_rule": "Flagging rule not found - " + str(pk)})
219 changes: 11 additions & 208 deletions api/flags/serializers.py
Original file line number Diff line number Diff line change
@@ -1,126 +1,34 @@
from rest_framework import serializers
from rest_framework.relations import PrimaryKeyRelatedField
from rest_framework.validators import UniqueValidator

from api.core.serializers import PrimaryKeyRelatedSerializerField
from api.flags.enums import FlagLevels, FlagStatuses, FlagColours, FlagPermissions
from api.flags.models import Flag, FlaggingRule
from api.flags.enums import (
FlagPermissions,
FlagStatuses,
)
from api.flags.models import Flag
from api.teams.models import Team
from api.teams.serializers import TeamSerializer, TeamReadOnlySerializer
from lite_content.lite_api import strings


class FlagReadOnlySerializer(serializers.Serializer):
"""
More performant read_only flag serializer
"""

id = serializers.UUIDField(read_only=True)
name = serializers.CharField(read_only=True)
alias = serializers.CharField(read_only=True)
colour = serializers.CharField(read_only=True)
level = serializers.CharField(read_only=True)
label = serializers.CharField(read_only=True)
status = serializers.CharField(read_only=True)
priority = serializers.IntegerField(read_only=True)
blocks_finalising = serializers.BooleanField(read_only=True)
removable_by = serializers.CharField(read_only=True)
team = PrimaryKeyRelatedSerializerField(queryset=Team.objects.all(), serializer=TeamReadOnlySerializer)


class FlaggingRuleReadOnlySerializer(serializers.Serializer):
level = serializers.CharField(read_only=True)
status = serializers.CharField(read_only=True)
matching_values = serializers.ListField()
matching_groups = serializers.ListField()
excluded_values = serializers.ListField()


class FlagwithFlaggingRulesReadOnlySerializer(FlagReadOnlySerializer):
flagging_rules = FlaggingRuleReadOnlySerializer(many=True, read_only=True)
from api.teams.serializers import TeamReadOnlySerializer


class FlagSerializer(serializers.ModelSerializer):
name = serializers.CharField(
max_length=100,
validators=[UniqueValidator(queryset=Flag.objects.all(), lookup="iexact", message=strings.Flags.NON_UNIQUE)],
error_messages={"blank": strings.Flags.BLANK_NAME},
)
colour = serializers.ChoiceField(choices=FlagColours.choices, default=FlagColours.DEFAULT)
level = serializers.ChoiceField(
choices=FlagLevels.choices,
error_messages={"invalid_choice": "Select a parameter"},
)
label = serializers.CharField(
max_length=15,
required=False,
allow_blank=True,
error_messages={
"blank": strings.Flags.ValidationErrors.LABEL_MISSING,
},
)
status = serializers.ChoiceField(choices=FlagStatuses.choices, default=FlagStatuses.ACTIVE)
priority = serializers.IntegerField(
default=0,
min_value=0,
max_value=100,
error_messages={
"invalid": strings.Flags.ValidationErrors.PRIORITY,
"max_value": strings.Flags.ValidationErrors.PRIORITY_TOO_LARGE,
"min_value": strings.Flags.ValidationErrors.PRIORITY_NEGATIVE,
},
)
team = PrimaryKeyRelatedSerializerField(queryset=Team.objects.all(), serializer=TeamSerializer)
blocks_finalising = serializers.BooleanField(
required=True,
allow_null=False,
error_messages={
"required": strings.Flags.ValidationErrors.BLOCKING_APPROVAL_MISSING,
},
)
removable_by = serializers.ChoiceField(
choices=FlagPermissions.choices,
default=FlagPermissions.DEFAULT,
allow_null=False,
)

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

if self.context and not self.context.get("request").method == "GET":
self.initial_data["team"] = self.context.get("request").user.govuser.team_id

if hasattr(self, "initial_data"):
if self.initial_data.get("colour") != FlagColours.DEFAULT:
self.fields["label"].required = True
self.fields["label"].allow_blank = False
team = PrimaryKeyRelatedSerializerField(queryset=Team.objects.all(), serializer=TeamReadOnlySerializer)

class Meta:
model = Flag
fields = (
"id",
"name",
"alias",
"colour",
"level",
"team",
"status",
"label",
"colour",
"status",
"priority",
"blocks_finalising",
"removable_by",
"team",
)

def update(self, instance, validated_data):
instance.name = validated_data.get("name", instance.name)
instance.label = validated_data.get("label", instance.label)
instance.colour = validated_data.get("colour", instance.colour)
instance.priority = validated_data.get("priority", instance.priority)
instance.status = validated_data.get("status", instance.status)
instance.blocks_finalising = validated_data.get("blocks_finalising", instance.blocks_finalising)
instance.removable_by = validated_data.get("removable_by", instance.removable_by)
instance.save()
return instance


class FlagAssignmentSerializer(serializers.Serializer):
flags = serializers.PrimaryKeyRelatedField(queryset=Flag.objects.all(), many=True)
Expand Down Expand Up @@ -184,108 +92,3 @@ class CaseListFlagSerializer(serializers.Serializer):
colour = serializers.CharField()
priority = serializers.IntegerField()
level = serializers.CharField()


class FlaggingRuleSerializer(serializers.ModelSerializer):
team = PrimaryKeyRelatedSerializerField(queryset=Team.objects.all(), serializer=TeamSerializer)
level = serializers.ChoiceField(
choices=FlagLevels.choices,
error_messages={
"required": "Select a parameter",
"null": "Select a parameter",
},
)
status = serializers.ChoiceField(choices=FlagStatuses.choices, default=FlagStatuses.ACTIVE)
flag = PrimaryKeyRelatedField(queryset=Flag.objects.all(), error_messages={"null": strings.FlaggingRules.NO_FLAG})
matching_values = serializers.ListField(child=serializers.CharField(), required=False)
matching_groups = serializers.ListField(child=serializers.CharField(), required=False)
excluded_values = serializers.ListField(child=serializers.CharField(), required=False)
is_for_verified_goods_only = serializers.BooleanField(required=False, allow_null=True)

class Meta:
model = FlaggingRule
fields = (
"id",
"team",
"level",
"flag",
"status",
"matching_values",
"matching_groups",
"excluded_values",
"is_for_verified_goods_only",
)

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

if hasattr(self, "initial_data"):
if "level" not in self.initial_data:
self.initial_data["level"] = self.instance.level if self.instance else None
if "matching_values" not in self.initial_data:
self.initial_data["matching_values"] = self.instance.matching_values if self.instance else []
if "matching_groups" not in self.initial_data:
self.initial_data["matching_groups"] = self.instance.matching_groups if self.instance else []
if "excluded_values" not in self.initial_data:
self.initial_data["excluded_values"] = self.instance.excluded_values if self.instance else []
if "is_for_verified_goods_only" not in self.initial_data:
self.initial_data["is_for_verified_goods_only"] = (
self.instance.is_for_verified_goods_only if self.instance else None
)

def update(self, instance, validated_data):
instance.status = validated_data.get("status", instance.status)
instance.matching_values = validated_data.get("matching_values", instance.matching_values)
instance.matching_groups = validated_data.get("matching_groups", instance.matching_groups)
instance.excluded_values = validated_data.get("excluded_values", instance.excluded_values)
instance.flag = validated_data.get("flag", instance.flag)
instance.is_for_verified_goods_only = validated_data.get(
"is_for_verified_goods_only", instance.is_for_verified_goods_only
)
instance.save()
return instance

def validate(self, data):
errors = {}
validated_data = super().validate(data)

matching_values = validated_data.get("matching_values")
matching_groups = validated_data.get("matching_groups")
excluded_values = validated_data.get("excluded_values")

if validated_data["level"] == FlagLevels.GOOD:
if not matching_values and not matching_groups and not excluded_values:
errors["matching_values"] = "Enter a control list entry"
errors["matching_groups"] = "Enter a control list entry"
errors["excluded_values"] = "Enter a control list entry"
elif validated_data["level"] == FlagLevels.DESTINATION:
if not matching_values:
errors["matching_values"] = "Enter a destination"
elif validated_data["level"] == FlagLevels.CASE:
if not matching_values:
errors["matching_values"] = "Enter an application type"

if (
"level" in validated_data
and validated_data["level"] == FlagLevels.GOOD
and validated_data["is_for_verified_goods_only"] is None
):
errors["is_for_verified_goods_only"] = strings.FlaggingRules.NO_ANSWER_VERIFIED_ONLY

if errors:
raise serializers.ValidationError(errors)

return validated_data


class FlaggingRuleListSerializer(serializers.Serializer):
id = serializers.UUIDField()
team = TeamReadOnlySerializer()
level = serializers.ChoiceField(choices=FlagLevels.choices)
status = serializers.ChoiceField(choices=FlagStatuses.choices)
flag = PrimaryKeyRelatedField(queryset=Flag.objects.all())
flag_name = serializers.CharField(source="flag.name")
matching_values = serializers.ListField(child=serializers.CharField())
matching_groups = serializers.ListField(child=serializers.CharField())
excluded_values = serializers.ListField(child=serializers.CharField())
is_for_verified_goods_only = serializers.BooleanField()
93 changes: 0 additions & 93 deletions api/flags/tests/test_create_flags.py

This file was deleted.

6 changes: 0 additions & 6 deletions api/flags/tests/test_endpoints_response_flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,3 @@ def test_flags_list(self):

def test_flags_detail(self):
self.call_endpoint(self.get_gov_headers(), self.url + self.get_flag_id())

def test_flagging_rules_list(self):
self.call_endpoint(self.get_gov_headers(), self.url + "rules/")

def test_flagging_rules_detail(self):
self.call_endpoint(self.get_gov_headers(), self.url + "rules/" + self.get_flagging_rules_id())
Loading

0 comments on commit 0484887

Please sign in to comment.