class UpdateMilestoneBulkValidator(ProjectExistsValidator, validators.Validator): project_id = serializers.IntegerField() milestone_id = serializers.IntegerField() bulk_stories = _UserStoryMilestoneBulkValidator(many=True) def validate_milestone_id(self, attrs, source): filters = {"project__id": attrs["project_id"], "id": attrs[source]} if not Milestone.objects.filter(**filters).exists(): raise ValidationError( _("The milestone isn't valid for the project")) return attrs def validate_bulk_stories(self, attrs, source): filters = { "project__id": attrs["project_id"], "id__in": [us["us_id"] for us in attrs[source]], } if UserStory.objects.filter(**filters).count() != len( filters["id__in"]): raise ValidationError( _("All the user stories must be from the same project")) return attrs
class UpdateUserStoriesOrderBulkValidator(ProjectExistsValidator, UserStoryStatusExistsValidator, validators.Validator): project_id = serializers.IntegerField() status_id = serializers.IntegerField(required=False) milestone_id = serializers.IntegerField(required=False) bulk_stories = _UserStoryOrderBulkValidator(many=True)
class TasksBulkSerializer(ProjectExistsValidator, SprintExistsValidator, TaskExistsValidator, serializers.Serializer): project_id = serializers.IntegerField() sprint_id = serializers.IntegerField() status_id = serializers.IntegerField(required=False) us_id = serializers.IntegerField(required=False) bulk_tasks = serializers.CharField()
class TasksBulkValidator(ProjectExistsValidator, validators.Validator): project_id = serializers.IntegerField() milestone_id = serializers.IntegerField() status_id = serializers.IntegerField(required=False) us_id = serializers.IntegerField(required=False) bulk_tasks = serializers.CharField() def validate_milestone_id(self, attrs, source): filters = {"project__id": attrs["project_id"], "id": attrs[source]} if not Milestone.objects.filter(**filters).exists(): raise ValidationError(_("Invalid milestone id.")) return attrs def validate_status_id(self, attrs, source): filters = {"project__id": attrs["project_id"], "id": attrs[source]} if not TaskStatus.objects.filter(**filters).exists(): raise ValidationError(_("Invalid task status id.")) return attrs def validate_us_id(self, attrs, source): filters = {"project__id": attrs["project_id"]} if "milestone_id" in attrs: filters["milestone__id"] = attrs["milestone_id"] filters["id"] = attrs["us_id"] if not UserStory.objects.filter(**filters).exists(): raise ValidationError(_("Invalid user story id.")) return attrs
class UpdateTasksOrderBulkValidator(ProjectExistsValidator, validators.Validator): project_id = serializers.IntegerField() status_id = serializers.IntegerField(required=False) us_id = serializers.IntegerField(required=False) milestone_id = serializers.IntegerField(required=False) bulk_tasks = _TaskOrderBulkValidator(many=True) def validate(self, data): filters = {"project__id": data["project_id"]} if "status_id" in data: filters["status__id"] = data["status_id"] if "us_id" in data: filters["user_story__id"] = data["us_id"] if "milestone_id" in data: filters["milestone__id"] = data["milestone_id"] filters["id__in"] = [t["task_id"] for t in data["bulk_tasks"]] if models.Task.objects.filter(**filters).count() != len( filters["id__in"]): raise ValidationError( _("Invalid task ids. All tasks must belong to the same project and, " "if it exists, to the same status, user story and/or milestone." )) return data
class ResolverSerializer(serializers.Serializer): project = serializers.CharField(max_length=512, required=True) milestone = serializers.CharField(max_length=512, required=False) us = serializers.IntegerField(required=False) task = serializers.IntegerField(required=False) issue = serializers.IntegerField(required=False) wikipage = serializers.CharField(max_length=512, required=False)
class UpdateMilestoneBulkValidator(ProjectExistsValidator, validators.Validator): project_id = serializers.IntegerField() milestone_id = serializers.IntegerField() bulk_tasks = _TaskMilestoneBulkValidator(many=True) def validate_milestone_id(self, attrs, source): filters = {"project__id": attrs["project_id"], "id": attrs[source]} if not Milestone.objects.filter(**filters).exists(): raise ValidationError( _("The milestone isn't valid for the project")) return attrs def validate_bulk_tasks(self, attrs, source): filters = { "project__id": attrs["project_id"], "id__in": [task["task_id"] for task in attrs[source]] } if models.Task.objects.filter(**filters).count() != len( filters["id__in"]): raise ValidationError( _("All the tasks must be from the same project")) return attrs
class ResolverSerializer(serializers.Serializer): project = serializers.CharField(max_length=512, required=True) milestone = serializers.CharField(max_length=512, required=False) us = serializers.IntegerField(required=False) task = serializers.IntegerField(required=False) issue = serializers.IntegerField(required=False) ref = serializers.IntegerField(required=False) wikipage = serializers.CharField(max_length=512, required=False) def validate(self, attrs): if "ref" in attrs: if "us" in attrs: raise serializers.ValidationError( "'us' param is incompatible with 'ref' in the same request" ) if "task" in attrs: raise serializers.ValidationError( "'task' param is incompatible with 'ref' in the same request" ) if "issue" in attrs: raise serializers.ValidationError( "'issue' param is incompatible with 'ref' in the same request" ) return attrs
class UserStoriesBulkValidator(ProjectExistsValidator, validators.Validator): project_id = serializers.IntegerField() status_id = serializers.IntegerField(required=False) swimlane_id = serializers.IntegerField(required=False) bulk_stories = serializers.CharField() def validate_status_id(self, attrs, source): filters = {"project__id": attrs["project_id"], "id": attrs[source]} if not UserStoryStatus.objects.filter(**filters).exists(): raise ValidationError( _("Invalid user story status id. The status must belong to " "the same project.")) return attrs def validate_swimlane_id(self, attrs, source): if attrs.get(source, None) is not None: filters = {"project__id": attrs["project_id"], "id": attrs[source]} if not Swimlane.objects.filter(**filters).exists(): raise ValidationError( _("Invalid swimlane id. The swimlane must belong to " "the same project.")) return attrs
class UpdateTasksOrderBulkValidator(ProjectExistsValidator, validators.Validator): project_id = serializers.IntegerField() milestone_id = serializers.IntegerField(required=False) status_id = serializers.IntegerField(required=False) us_id = serializers.IntegerField(required=False) bulk_tasks = _TaskOrderBulkValidator(many=True)
class TasksBulkValidator(ProjectExistsValidator, MilestoneExistsValidator, TaskExistsValidator, validators.Validator): project_id = serializers.IntegerField() sprint_id = serializers.IntegerField() status_id = serializers.IntegerField(required=False) us_id = serializers.IntegerField(required=False) bulk_tasks = serializers.CharField()
class EpicRelatedUserStoryExportValidator(validators.ModelValidator): user_story = ProjectRelatedField(slug_field="ref") order = serializers.IntegerField() class Meta: model = epics_models.RelatedUserStory exclude = ("id", "epic")
class EpicRelatedUserStoryExportValidator(validators.ModelValidator): user_story = ProjectRelatedField(slug_field="ref") order = serializers.IntegerField() source_project_slug = serializers.CharField(required=False) def validate_source_project_slug(self, attrs, source): if source in attrs and attrs[ source] is not None and attrs[source] != "": msg = _( "An Epic has a related story from an external project (%(project)s) and cannot be imported" ) % { "project": attrs[source] } raise ValidationError(msg) attrs.pop(source, None) return attrs class Meta: model = epics_models.RelatedUserStory extra_kwargs = { 'source_project_slug': { 'write_only': True }, } exclude = ('id', 'epic')
class UpdateUserStoriesOrderBulkValidator(ProjectExistsValidator, validators.Validator): project_id = serializers.IntegerField() status_id = serializers.IntegerField(required=False) milestone_id = serializers.IntegerField(required=False) bulk_stories = _UserStoryOrderBulkValidator(many=True) def validate_status_id(self, attrs, source): filters = {"project__id": attrs["project_id"], "id": attrs[source]} if not UserStoryStatus.objects.filter(**filters).exists(): raise ValidationError( _("Invalid user story status id. The status must belong " "to the same project.")) return attrs def validate_milestone_id(self, attrs, source): filters = {"project__id": attrs["project_id"], "id": attrs[source]} if not Milestone.objects.filter(**filters).exists(): raise ValidationError( _("Invalid milestone id. The milistone must belong to the " "same project.")) return attrs def validate_bulk_stories(self, attrs, source): filters = {"project__id": attrs["project_id"]} if "status_id" in attrs: filters["status__id"] = attrs["status_id"] if "milestone_id" in attrs: filters["milestone__id"] = attrs["milestone_id"] filters["id__in"] = [us["us_id"] for us in attrs[source]] if models.UserStory.objects.filter(**filters).count() != len( filters["id__in"]): raise ValidationError( _("Invalid user story ids. All stories must belong to the same project " "and, if it exists, to the same status and milestone.")) return attrs
class UpdateMilestoneBulkSerializer(ProjectExistsValidator, SprintExistsValidator, serializers.Serializer): project_id = serializers.IntegerField() milestone_id = serializers.IntegerField() bulk_stories = _UserStoryMilestoneBulkSerializer(many=True) def validate(self, data): """ All the userstories and the milestone are from the same project """ user_story_ids = [us["us_id"] for us in data["bulk_stories"]] project = get_object_or_404(Project, pk=data["project_id"]) if project.user_stories.filter(id__in=user_story_ids).count() != len(user_story_ids): raise serializers.ValidationError("all the user stories must be from the same project") if project.milestones.filter(id=data["milestone_id"]).count() != 1: raise serializers.ValidationError("the milestone isn't valid for the project") return data
class UpdateAttachmentsOrderBulkValidator(validators.Validator): content_type_id = serializers.IntegerField() object_id = serializers.IntegerField() after_attachment_id = serializers.IntegerField(required=False) bulk_attachments = ListField(child=serializers.IntegerField(min_value=1)) def validate_after_attachment_id(self, attrs, source): if (attrs.get(source, None) is not None and attrs.get("content_type_id", None) is not None and attrs.get("object_id", None) is not None): filters = { "content_type__id": attrs["content_type_id"], "object_id": attrs["object_id"], "id": attrs[source] } if not models.Attachment.objects.filter(**filters).exists(): raise ValidationError( _("Invalid attachment id to move after. The attachment must belong " "to the same item (epic, userstory, task, issue or wiki page)." )) return attrs def validate_bulk_attachments(self, attrs, source): if (attrs.get("content_type_id", None) is not None and attrs.get("object_id", None) is not None): filters = { "content_type__id": attrs["content_type_id"], "object_id": attrs["object_id"], "id__in": attrs[source] } if models.Attachment.objects.filter(**filters).count() != len( filters["id__in"]): raise ValidationError( _("Invalid attachment ids. All attachments must belong to the same " "item (epic, userstory, task, issue or wiki page).")) return attrs
class VotedObjectSerializer(HighLightedContentSerializer): is_voter = serializers.SerializerMethodField("get_is_voter") total_voters = serializers.IntegerField() def __init__(self, *args, **kwargs): # Don't pass the extra ids args up to the superclass self.user_votes = kwargs.pop("user_votes", {}) # Instantiate the superclass normally super().__init__(*args, **kwargs) def get_is_voter(self, obj): return obj["id"] in self.user_votes.get(obj["type"], [])
class MembersBulkValidator(ProjectExistsValidator, validators.Validator): project_id = serializers.IntegerField() bulk_memberships = _MemberBulkValidator(many=True) invitation_extra_text = serializers.CharField(required=False, max_length=255) def validate_bulk_memberships(self, attrs, source): project_id = attrs["project_id"] role_ids = [r["role_id"] for r in attrs["bulk_memberships"]] if Role.objects.filter(project_id=project_id, id__in=role_ids).count() != len(set(role_ids)): raise ValidationError(_("Invalid role ids. All roles must belong to the same project.")) return attrs
class UpdateUserStoriesOrderBulkValidator(ProjectExistsValidator, UserStoryStatusExistsValidator, validators.Validator): project_id = serializers.IntegerField() status_id = serializers.IntegerField(required=False) milestone_id = serializers.IntegerField(required=False) bulk_stories = _UserStoryOrderBulkValidator(many=True) def validate(self, data): filters = {"project__id": data["project_id"]} if "status_id" in data: filters["status__id"] = data["status_id"] if "milestone_id" in data: filters["milestone__id"] = data["milestone_id"] filters["id__in"] = [us["us_id"] for us in data["bulk_stories"]] if models.UserStory.objects.filter(**filters).count() != len( filters["id__in"]): raise ValidationError( _("Invalid user story ids. All stories must belong to the same project and, " "if it exists, to the same status and milestone.")) return data
class ProjectMemberSerializer(serializers.ModelSerializer): id = serializers.IntegerField(source="user.id", read_only=True) username = serializers.CharField(source='user.username', read_only=True) full_name = serializers.CharField(source='user.full_name', read_only=True) full_name_display = serializers.CharField(source='user.get_full_name', read_only=True) color = serializers.CharField(source='user.color', read_only=True) photo = serializers.SerializerMethodField("get_photo") is_active = serializers.BooleanField(source='user.is_active', read_only=True) role_name = serializers.CharField(source='role.name', read_only=True, i18n=True) class Meta: model = models.Membership exclude = ("project", "email", "created_at", "token", "invited_by", "invitation_extra_text", "user_order") def get_photo(self, membership): return get_photo_or_gravatar_url(membership.user)
class _MemberBulkValidator(validators.Validator): username = serializers.CharField() role_id = serializers.IntegerField() def validate_username(self, attrs, source): username = attrs.get(source) try: validate_user_email_allowed_domains(username) except InvalidEmailValidationError: # If the validation comes from a request let's check the user is a valid contact request = self.context.get("request", None) if request is not None and request.user.is_authenticated: valid_usernames = set(request.user.contacts_visible_by_user(request.user).values_list("username", flat=True)) if username not in valid_usernames: raise ValidationError(_("The user must be a valid contact")) return attrs
class ProjectMemberSerializer(serializers.ModelSerializer): id = serializers.IntegerField(source="user.id", read_only=True) username = serializers.CharField(source='user.username', read_only=True) full_name = serializers.CharField(source='user.full_name', read_only=True) full_name_display = serializers.CharField(source='user.get_full_name', read_only=True) color = serializers.CharField(source='user.color', read_only=True) photo = serializers.SerializerMethodField("get_photo") is_active = serializers.BooleanField(source='user.is_active', read_only=True) role_name = serializers.CharField(source='role.name', read_only=True, i18n=True) class Meta: model = models.Membership def get_photo(self, membership): return get_photo_or_gravatar_url(membership.user)
class UpdateProjectOrderBulkValidator(ProjectExistsValidator, validators.Validator): project_id = serializers.IntegerField() order = serializers.IntegerField()
class DueDatesCreationValidator(ProjectExistsValidator, validators.Validator): project_id = serializers.IntegerField()
class UpdateTasksOrderBulkSerializer(ProjectExistsValidator, serializers.Serializer): project_id = serializers.IntegerField() bulk_tasks = _TaskOrderBulkSerializer(many=True)
class UpdateUserStoriesOrderBulkSerializer(ProjectExistsValidator, UserStoryStatusExistsValidator, serializers.Serializer): project_id = serializers.IntegerField() bulk_stories = _UserStoryOrderBulkSerializer(many=True)
class _UserStoryOrderBulkSerializer(UserStoryExistsValidator, serializers.Serializer): us_id = serializers.IntegerField() order = serializers.IntegerField()
class UserStoriesBulkSerializer(ProjectExistsValidator, UserStoryStatusExistsValidator, serializers.Serializer): project_id = serializers.IntegerField() status_id = serializers.IntegerField(required=False) bulk_stories = serializers.CharField()
class MembersBulkSerializer(ProjectExistsValidator, serializers.Serializer): project_id = serializers.IntegerField() bulk_memberships = MemberBulkSerializer(many=True) invitation_extra_text = serializers.CharField(required=False, max_length=255)
class MemberBulkSerializer(RoleExistsValidator, serializers.Serializer): email = serializers.EmailField() role_id = serializers.IntegerField()