serializers.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # Copyright (C) 2014 Andrey Antukh <niwi@niwi.be>
  2. # Copyright (C) 2014 Jesús Espino <jespinog@gmail.com>
  3. # Copyright (C) 2014 David Barragán <bameda@dbarragan.com>
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Affero General Public License as
  6. # published by the Free Software Foundation, either version 3 of the
  7. # License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU Affero General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. from taiga.base.api import serializers
  17. from taiga.base.fields import TagsField
  18. from taiga.base.fields import PgArrayField
  19. from taiga.base.neighbors import NeighborsSerializerMixin
  20. from taiga.mdrender.service import render as mdrender
  21. from taiga.projects.validators import ProjectExistsValidator
  22. from taiga.projects.notifications.validators import WatchersValidator
  23. from taiga.projects.serializers import BasicIssueStatusSerializer
  24. from taiga.projects.notifications.mixins import WatchedResourceModelSerializer
  25. from taiga.projects.votes.mixins.serializers import VotedResourceSerializerMixin
  26. from taiga.users.serializers import UserBasicInfoSerializer
  27. from . import models
  28. class IssueSerializer(WatchersValidator, VotedResourceSerializerMixin, WatchedResourceModelSerializer, serializers.ModelSerializer):
  29. tags = TagsField(required=False)
  30. external_reference = PgArrayField(required=False)
  31. is_closed = serializers.Field(source="is_closed")
  32. comment = serializers.SerializerMethodField("get_comment")
  33. generated_user_stories = serializers.SerializerMethodField("get_generated_user_stories")
  34. blocked_note_html = serializers.SerializerMethodField("get_blocked_note_html")
  35. description_html = serializers.SerializerMethodField("get_description_html")
  36. status_extra_info = BasicIssueStatusSerializer(source="status", required=False, read_only=True)
  37. assigned_to_extra_info = UserBasicInfoSerializer(source="assigned_to", required=False, read_only=True)
  38. owner_extra_info = UserBasicInfoSerializer(source="owner", required=False, read_only=True)
  39. class Meta:
  40. model = models.Issue
  41. read_only_fields = ('id', 'ref', 'created_date', 'modified_date')
  42. def get_comment(self, obj):
  43. # NOTE: This method and field is necessary to historical comments work
  44. return ""
  45. def get_generated_user_stories(self, obj):
  46. return obj.generated_user_stories.values("id", "ref", "subject")
  47. def get_blocked_note_html(self, obj):
  48. return mdrender(obj.project, obj.blocked_note)
  49. def get_description_html(self, obj):
  50. return mdrender(obj.project, obj.description)
  51. class IssueListSerializer(IssueSerializer):
  52. class Meta:
  53. model = models.Issue
  54. read_only_fields = ('id', 'ref', 'created_date', 'modified_date')
  55. exclude=("description", "description_html")
  56. class IssueNeighborsSerializer(NeighborsSerializerMixin, IssueSerializer):
  57. def serialize_neighbor(self, neighbor):
  58. return NeighborIssueSerializer(neighbor).data
  59. class NeighborIssueSerializer(serializers.ModelSerializer):
  60. class Meta:
  61. model = models.Issue
  62. fields = ("id", "ref", "subject")
  63. depth = 0
  64. class IssuesBulkSerializer(ProjectExistsValidator, serializers.Serializer):
  65. project_id = serializers.IntegerField()
  66. bulk_issues = serializers.CharField()