示例#1
0
class TicketSharedUserSerializer(ModelSerializer):
    user = UserSerializer(read_only=True, fields=(["first_name", "last_name", "username", "avatar_url", "id"]))
    sharer = UserSerializer(read_only=True, fields=(["first_name", "last_name", "username", "avatar_url", "id"]))

    class Meta:
        model = TicketSharedUser
        fields = ["id", "user", "sharer", "date_created"]
示例#2
0
class TicketSharedWithRetrieveSerializer(ModelSerializer):
    shared_with = UserSerializer(many=True, read_only=True,
                                 fields=(["first_name", "last_name", "username", "avatar_url", "id"]))

    class Meta:
        model = Ticket
        fields = ["shared_with"]
示例#3
0
class TicketLabelEventSerializer(ModelSerializer):
    initiator = UserSerializer(read_only=True, fields=(["first_name", "last_name", "username", "avatar_url", "id"]))
    label = LabelSerializer(read_only=True)

    class Meta:
        model = TicketLabelEvent
        fields = ["id", "ticket", "initiator", "date_created", "label", "is_added"]
示例#4
0
    def get(self, request, *args, **kwargs):
        inbox = get_object_or_404(Inbox, pk=self.kwargs["inbox_id"])
        ticket = get_object_or_404(Ticket, inbox=inbox, ticket_inbox_id=self.kwargs["ticket_inbox_id"])
        current_role = Role.MANAGER if request.user.is_superuser else request.user.get_entry_by_inbox(inbox).role

        response = {}

        unread_related_ticket_notifications(ticket, request.user)

        if json.loads(request.GET.get("role", "false")):
            response["role"] = current_role

        if json.loads(request.GET.get("ticket", "false")):
            ticket_data = TicketSerializer(ticket, fields=(
                "id", "inbox", "title", "ticket_inbox_id", "author", "content", "date_created", "status", "labels",
                "assignee", "attachments", "participants", "author_role", "attachments", "shared_with_by",
                "shared_with")).data
            response["ticket"] = ticket_data

        if json.loads(request.GET.get("me", "false")):
            user_data = UserSerializer(request.user,
                                       fields=(["first_name", "last_name", "username", "avatar_url", "id",
                                                "is_superuser"])).data
            response["me"] = user_data

        if json.loads(request.GET.get("inbox", "false")):
            inbox_data = InboxSerializer(inbox, fields=("name", "id", "labels")).data
            response["inbox"] = inbox_data

        if json.loads(request.GET.get("replies", "false")):
            replies = Comment.objects.filter(ticket=ticket, is_reply=True).order_by("date_created")
            replies_data = CommentSerializer(replies, many=True).data
            response["replies"] = replies_data

        if json.loads(request.GET.get("events", "false")):
            if self.request.user.is_assistant_or_coordinator(inbox):
                events = TicketEvent.objects.filter(ticket=ticket).select_subclasses()
            else:
                events = TicketEvent.objects.filter(ticket=ticket).exclude(
                    ticketlabelevent__label__is_visible_to_guest=False).select_subclasses()

            events_data = TicketEventSerializer(events, many=True).data
            response["events"] = events_data

        if request.user.is_assistant_or_coordinator(inbox):
            if json.loads(request.GET.get("staff", "false")):
                staff = User.objects.filter(inbox_relationship__role__in=[Role.AGENT, Role.MANAGER],
                                            inbox_relationship__inbox_id=self.kwargs["inbox_id"]) \
                    .values("first_name", "last_name", "username", "avatar_url", "id")

                response["staff"] = staff

            if json.loads(request.GET.get("comments", "false")):
                comments = Comment.objects.filter(ticket=ticket, is_reply=False).order_by("date_created")
                comments_data = CommentSerializer(comments, many=True).data

                response["comments"] = comments_data

        return Response(response)
示例#5
0
    def get_assignee(self, obj):
        user = CurrentUserMiddleware.get_current_user()

        if user and (user.is_assistant_or_coordinator(obj.inbox) or obj.inbox.show_assignee_to_guest):
            return UserSerializer(obj.assignee,
                                  fields=(["first_name", "last_name", "username", "avatar_url", "id"])).data

        return None
示例#6
0
class CommentSerializer(ModelSerializer):
    author = UserSerializer(read_only=True, fields=(["first_name", "last_name", "username", "avatar_url", "id"]))
    role = serializers.SerializerMethodField()

    def get_role(self, obj):
        role = UserInbox.objects.get(user=obj.author, inbox=obj.ticket.inbox).role
        return RoleSerializer(role).data

    class Meta:
        model = Comment
        fields = ["author", "content", "id", "date_created", "role"]
示例#7
0
    def test_get_shared_with_as_ta_in_inbox(self):
        """
        Test to verify an assistant of the inbox can change shared_with
        """
        self.client.force_authenticate(self.assistant)

        response = self.client.get(f"/api/inboxes/{self.inbox.id}/tickets/{self.ticket2.ticket_inbox_id}",
                                   {"ticket": "true"})
        self.assertEqual(response.status_code, 200)
        self.assertTrue(UserSerializer(self.student2, fields=(
            ["first_name", "last_name", "username", "avatar_url", "id"])).data in response.data["ticket"][
                            "shared_with"])
示例#8
0
    def get_participants(self, obj):
        participants = list(User.objects.filter(comments__ticket=obj).distinct())

        if obj.author not in participants:
            participants.append(obj.author)

        for user in obj.shared_with.all():
            if user not in participants:
                participants.append(user)

        return UserSerializer(participants, many=True,
                              fields=(["first_name", "last_name", "username", "avatar_url", "id"])).data
示例#9
0
class TicketAssigneeEventSerializer(ModelSerializer):
    initiator = serializers.SerializerMethodField()
    assignee = UserSerializer(read_only=True, fields=(["first_name", "last_name", "username", "avatar_url", "id"]))

    def get_initiator(self, event):
        if event.initiator and event.initiator.is_assistant_or_coordinator(event.ticket.inbox):
            return UserSerializer(event.initiator, read_only=True,
                                  fields=(["first_name", "last_name", "username", "avatar_url", "id"])).data

        return None

    class Meta:
        model = TicketAssigneeEvent
        fields = ["id", "ticket", "initiator", "date_created", "assignee"]
示例#10
0
    def retrieve(self, request, *args, **kwargs):
        inbox = self.get_object()

        response = {
            "inbox":
            self.get_serializer(inbox).data,
            "scheduling_options":
            SchedulingAlgorithm.choices,
            "staff":
            UserSerializer(inbox.get_assistants_and_coordinators(),
                           many=True).data
        }

        return Response(response)
示例#11
0
class NotificationSerializer(ModelSerializer):
    receiver = UserSerializer(
        read_only=True,
        fields=(["first_name", "last_name", "username", "avatar_url", "id"]))
    ticket = TicketSerializer(read_only=True,
                              fields=("id", "title", "name", "ticket_inbox_id",
                                      "date_created"))
    inbox = InboxSerializer(read_only=True, fields=("id", "name", "color"))

    class Meta:
        model = Notification
        fields = [
            "id", "receiver", "date_created", "is_read", "ticket", "author",
            "content", "inbox"
        ]
示例#12
0
class TicketEventSerializer(ModelSerializer):
    initiator = UserSerializer(read_only=True, fields=(["first_name", "last_name", "username", "avatar_url", "id"]))

    def to_representation(self, instance):
        if isinstance(instance, TicketStatusEvent):
            return TicketStatusEventSerializer(instance=instance).data
        elif isinstance(instance, TicketAssigneeEvent):
            return TicketAssigneeEventSerializer(instance=instance).data
        elif isinstance(instance, TicketLabelEvent):
            return TicketLabelEventSerializer(instance=instance).data
        elif isinstance(instance, TicketTitleEvent):
            return TicketTitleEventSerializer(instance=instance).data

        return super().to_representation(instance)

    class Meta:
        model = TicketEvent
        fields = "__all__"
示例#13
0
    def post(self, request):
        serializer = LoginSerializer(data=request.data)
        if not serializer.is_valid():
            return Response(serializer.errors, status=HTTP_400_BAD_REQUEST)

        user = authenticate(username=serializer.data['username'],
                            password=serializer.data['password'])
        if not user:
            return Response(
                {'detail': 'Invalid Credentials or activate account'},
                status=HTTP_404_NOT_FOUND)

        # Retrieving token of user, checking if not expired otherwise a create new one.
        token, _ = Token.objects.get_or_create(user=user)
        is_expired, token = token_expire_handler(token)

        return Response({
            'user': UserSerializer(user).data,
            'expires_in': expires_in(token),
            'token': token.key
        })
示例#14
0
 def get_coordinator(self, obj):
     return UserSerializer(obj.get_coordinator()).data
示例#15
0
class TicketSerializer(DynamicFieldsModelSerializer):
    """
    Allows data to be converted into Python datatypes for the ticket.
    """
    author = UserSerializer(read_only=True, fields=(["first_name", "last_name", "username", "avatar_url", "id"]))
    shared_with = UserSerializer(read_only=True, many=True,
                                 fields=(["first_name", "last_name", "username", "avatar_url", "id"]))
    assignee = serializers.SerializerMethodField()
    labels = serializers.SerializerMethodField()
    participants = serializers.SerializerMethodField()
    author_role = serializers.SerializerMethodField()
    attachments = TicketAttachmentSerializer(many=True, read_only=True)
    shared_with_by = TicketSharedUserSerializer(many=True, read_only=True)

    def get_author_role(self, obj):
        author_role = UserInbox.objects.get(user=obj.author, inbox=obj.inbox).role
        return RoleSerializer(author_role).data

    def get_participants(self, obj):
        participants = list(User.objects.filter(comments__ticket=obj).distinct())

        if obj.author not in participants:
            participants.append(obj.author)

        for user in obj.shared_with.all():
            if user not in participants:
                participants.append(user)

        return UserSerializer(participants, many=True,
                              fields=(["first_name", "last_name", "username", "avatar_url", "id"])).data

    def get_labels(self, obj):
        user = CurrentUserMiddleware.get_current_user()
        labels = obj.labels.filter(is_active=True)
        if user and not user.is_assistant_or_coordinator(obj.inbox):
            labels = labels.filter(is_visible_to_guest=True)
            return LabelSerializer(labels, many=True, read_only=True).data
        return LabelSerializer(labels, many=True, read_only=True).data

    def get_assignee(self, obj):
        user = CurrentUserMiddleware.get_current_user()

        if user and (user.is_assistant_or_coordinator(obj.inbox) or obj.inbox.show_assignee_to_guest):
            return UserSerializer(obj.assignee,
                                  fields=(["first_name", "last_name", "username", "avatar_url", "id"])).data

        return None

    class Meta:
        """
        Define the model and fields.

        :var Ticket model: The model.
        :var list fields: Field defined to the model.
        """

        #: Tells the serializer to use the :class:`Ticket` model.
        model = Ticket
        #: Tells the serializer to use these fields from the :class:`Ticket` model.
        fields = ["id", "inbox", "title", "ticket_inbox_id", "author", "content", "date_created", "status", "labels",
                  "assignee", "shared_with", "participants", "author_role", "attachments", "shared_with_by",
                  "attachments"]
示例#16
0
 def get_serializer(self, *args, **kwargs):
     return UserSerializer(*args,
                           **kwargs,
                           fields=("first_name", "last_name", "username",
                                   "avatar_url", "id"))
示例#17
0
    def get_initiator(self, event):
        if event.initiator and event.initiator.is_assistant_or_coordinator(event.ticket.inbox):
            return UserSerializer(event.initiator, read_only=True,
                                  fields=(["first_name", "last_name", "username", "avatar_url", "id"])).data

        return None
示例#18
0
class TicketTitleEventSerializer(ModelSerializer):
    initiator = UserSerializer(read_only=True, fields=(["first_name", "last_name", "username", "avatar_url", "id"]))

    class Meta:
        model = TicketTitleEvent
        fields = ["id", "ticket", "initiator", "date_created", "old_title", "new_title"]
示例#19
0
class TicketAttachmentSerializer(ModelSerializer):
    uploader = UserSerializer(read_only=True, fields=(["first_name", "last_name", "username", "avatar_url", "id"]))

    class Meta:
        model = TicketAttachment
        fields = ["id", "file", "uploader", "date_created"]