Ejemplo n.º 1
0
class BookingSerializer(serializers.ModelSerializer):

    title = serializers.CharField(max_length=64)
    remark = serializers.CharField(required=False, allow_blank=True, allow_null=True, max_length=1024)
    author = serializers.StringRelatedField(read_only=True)
    author_id = serializers.IntegerField(default=serializers.CreateOnlyDefault(CurrentUserDefault()))

    # messages = MessageSerializer(read_only=True, many=True)
    messages = serializers.SerializerMethodField()
    comments = CommentSerializer(read_only=True, many=True)
    uploads = UpLoadSerializer(read_only=True, many=True)
    itineraries = serializers.SerializerMethodField()
    
    class Meta:
        model = models.Booking
        fields = '__all__'

    def get_itineraries(self, obj):
        query = None
        if self.context['request'].user.has_perm('ticket.view_itinerary'):
            query = obj.itineraries.all()
        else:
            query = obj.itineraries.filter(user_id=self.context['request'].user.id)

        serializer = ItinerarySerializer(instance=query, many=True)

        return serializer.data

    def get_messages(self, obj):
        query = obj.messages.filter(user_id=self.context['request'].user.id)
        serializer = MessageSerializer(instance=query, many=True)
        return serializer.data
Ejemplo n.º 2
0
class ContactSerializer(serializers.ModelSerializer):
    username = serializers.ReadOnlyField(source='user.username')
    email = serializers.ReadOnlyField(source='user.email')
    full_name = serializers.ReadOnlyField(source='user.get_full_name')
    role = serializers.SerializerMethodField()

    class Meta:
        model = UserProfile
        fields = ('username', 'email', 'address', 'gender', 'phone', 'skype',
                  'twitter', 'tango', 'hike', 'qq', 'google_talk',
                  'profile_picture', 'viber', 'whatsapp', 'wechat',
                  'full_name', 'role', 'primary_number', 'secondary_number',
                  'office_number')

    def get_role(self, obj):
        #exclude site supervisors.
        group = Group.objects.get(name__exact="Site Supervisor")
        roles = UserRole.objects.filter(~Q(group=group),
                                        user=obj.user,
                                        ended_at__isnull=True)
        role_list = []
        for r in roles:
            role_list.append({
                'group': u'{}'.format(r.group),
                'project': u'{}'.format(r.project),
                'site': u'{}'.format(r.site)
            })
        return role_list
Ejemplo n.º 3
0
class TicketSerializer(serializers.ModelSerializer):
    serial_no = serializers.CharField(max_length=32)
    remark = serializers.CharField(required=False, allow_blank=True, allow_null=True, max_length=1024)
    author_id = serializers.IntegerField(default=serializers.CreateOnlyDefault(CurrentUserDefault()))
    author = serializers.StringRelatedField(read_only=True)
    
    itinerary = ItinerarySerializer(read_only=True, many=False)
    itinerary_id = serializers.IntegerField(required=True)
    
    # messages = MessageSerializer(read_only=True, many=True)
    messages = serializers.SerializerMethodField()
    comments = CommentSerializer(read_only=True, many=True)
    uploads = UpLoadSerializer(read_only=True, many=True) 
    
    parent_id = serializers.IntegerField(required=False, allow_null=True)

    class Meta:
        model = models.Ticket
        fields = '__all__'

    def get_fields(self):
        fields = super(TicketSerializer, self).get_fields()
        fields['children'] = TicketSerializer(many=True, read_only=True)
        return fields 

    def get_messages(self, obj):
        query = obj.messages.filter(user_id=self.context['request'].user.id)
        serializer = MessageSerializer(instance=query, many=True)
        return serializer.data
Ejemplo n.º 4
0
class UpLoadSerializer(serializers.ModelSerializer):

    content_type = ContentTypeField()

    author_id = serializers.IntegerField(default=serializers.CreateOnlyDefault(CurrentUserDefault()))

    name = serializers.SerializerMethodField()

    url = serializers.SerializerMethodField()

    class Meta:
        model = models.UpLoad
        fields = '__all__'

    def get_name(self, obj):
        if obj.file is not None and obj.file != '':
            return str(obj.file).split("/")[-1]

    def get_url(self, obj):
        if obj.file is not None and obj.file != '':
            return self.context['request'].build_absolute_uri(obj.file.url)
Ejemplo n.º 5
0
class CommentSerializer(serializers.ModelSerializer):

    content_type = ContentTypeField()

    user = UserListSerializer(read_only=True, many=False)

    user_id = serializers.IntegerField(default=serializers.CreateOnlyDefault(CurrentUserDefault()))

    children = serializers.SerializerMethodField()

    class Meta:
        model = models.Comment
        fields = '__all__'

    def get_children(self, obj):
        queryset = models.Comment.objects.filter(object_id=obj.id, content_type__model='comment')
        serializers = CommentSerializer(queryset, many=True, context=self.context)
        return serializers.data