"""Schemas for POST /conflicts/status/bulk endpoint.""" import marshmallow from conflict_manager import config from conflict_manager.constants import conflict from conflict_manager.constants import error from conflict_manager.utils import schema_utils def validate_conflict_status(status): """Verify that conflict status is one of possible statuses. Args: status (str): conflict status Returns: bool """ return status.upper() in conflict.POSSIBLE_CONFLICT_STATUSES class GroupedConflictsId(marshmallow.Schema): """Validation schema for grouped conflicts id .""" grouped_conflict_id = marshmallow.fields.String(required=True) tuid = marshmallow.fields.Integer(required=True) conflict_date = marshmallow.fields.String( required=True, validate=schema_utils.validate_conflict_date, error_messages=schema_utils.make_schema_error_messages( error.INVALID_DATE_MSG)) conflicting_owner = marshmallow.fields.String(required=True) action = marshmallow.fields.String(required=True) @marshmallow.pre_load def parse_grouped_conflicts_id(self, grouped_conflicts_id, **kwargs): """Parse grouped conflicts id to a dict. The method receives the data to be deserialized and returns the processed data. Args: grouped_conflicts_id (string): grouped conflicts id Returns: dict """ try: splitted_id = [i.strip() for i in grouped_conflicts_id.split('|')] tuid, date, owner, action = filter(bool, splitted_id) except ValueError: raise marshmallow.ValidationError(error.INVALID_GROUPED_ID_FORMAT) return { 'grouped_conflict_id': grouped_conflicts_id, 'tuid': tuid, 'conflict_date': date, 'conflicting_owner': owner, 'action': action } class BulkUpdateConflictStatusSchema(marshmallow.Schema): """Validation schema for POST /conflicts/status/bulk endpoint.""" grouped_conflicts_ids = marshmallow.fields.List( marshmallow.fields.Nested(GroupedConflictsId), required=True, validate=marshmallow.validate.Length( min=1, max=config.MAX_GROUPED_CONFLICTS_IDS_COUNT)) status = marshmallow.fields.String( required=True, validate=validate_conflict_status) note = marshmallow.fields.String( required=False, validate=marshmallow.validate.Length(max=config.MAX_NOTE_LENGTH))