serializers.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 JsonField, I18NJsonField
  18. from taiga.users.services import get_photo_or_gravatar_url
  19. from . import models
  20. HISTORY_ENTRY_I18N_FIELDS=("points", "status", "severity", "priority", "type")
  21. class HistoryEntrySerializer(serializers.ModelSerializer):
  22. diff = JsonField()
  23. snapshot = JsonField()
  24. values = I18NJsonField(i18n_fields=HISTORY_ENTRY_I18N_FIELDS)
  25. values_diff = I18NJsonField(i18n_fields=HISTORY_ENTRY_I18N_FIELDS)
  26. user = serializers.SerializerMethodField("get_user")
  27. delete_comment_user = JsonField()
  28. class Meta:
  29. model = models.HistoryEntry
  30. def get_user(self, entry):
  31. user = {"pk": None, "username": None, "name": None, "photo": None, "is_active": False}
  32. user.update(entry.user)
  33. user["photo"] = get_photo_or_gravatar_url(entry.owner)
  34. user["is_active"] = entry.owner.is_active
  35. if entry.owner.is_active or entry.owner.is_system:
  36. user["name"] = entry.owner.get_full_name()
  37. user["username"] = entry.owner.username
  38. return user