Esempio n. 1
0
class EventSerializer(serializers.ModelSerializer):
    """Serializer for Event.

    This serializer returns only the count of followers in
    each category, i.e. interested and going and minimal
    venue info. Use `EventFullSerializer` if you want information
    on individual users and venues.
    """

    from locations.serializers import LocationSerializerMin
    from bodies.serializer_min import BodySerializerMin

    interested_count = serializers.IntegerField(read_only=True)
    going_count = serializers.IntegerField(read_only=True)

    user_ues = serializers.SerializerMethodField()
    get_user_ues = get_user_ues

    venues = LocationSerializerMin(many=True, read_only=True)

    bodies = BodySerializerMin(many=True, read_only=True)

    class Meta:
        model = Event
        fields = ('id', 'str_id', 'name', 'description', 'image_url',
                  'start_time', 'end_time', 'all_day', 'venues', 'bodies',
                  'interested_count', 'going_count', 'website_url', 'weight',
                  'user_ues')

    @staticmethod
    def setup_eager_loading(queryset, request, extra_prefetch=None):
        """Perform necessary eager loading of data."""

        # Get the fields to be prefetched
        fields = ['bodies', 'venues', 'user_tags']

        # Add prefetch for user_ues
        if request.user.is_authenticated and hasattr(request.user, 'profile'):
            user_query = UserEventStatus.objects.filter(
                user_id=request.user.profile.id)
            fields.append(Prefetch('ues', queryset=user_query, to_attr='uues'))

        # Add extra prefetch fields
        if extra_prefetch:
            fields += extra_prefetch

        queryset = queryset.prefetch_related(*fields)

        # Prefetch counts
        interested_count = Count('followers',
                                 distinct=True,
                                 filter=Q(ues__status=1))
        going_count = Count('followers',
                            distinct=True,
                            filter=Q(ues__status=2))
        queryset = queryset.annotate(
            interested_count=interested_count).annotate(
                going_count=going_count)

        return queryset
Esempio n. 2
0
class EventSerializer(serializers.ModelSerializer):
    """Serializer for Event.

    This serializer returns only the count of followers in
    each category, i.e. interested and going and minimal
    venue info. Use `EventFullSerializer` if you want information
    on individual users and venues.
    """

    from locations.serializers import LocationSerializerMin
    from bodies.serializer_min import BodySerializerMin

    interested_count = serializers.SerializerMethodField()
    get_interested_count = lambda self, obj: FollowersMethods.get_count(obj, 1)

    going_count = serializers.SerializerMethodField()
    get_going_count = lambda self, obj: FollowersMethods.get_count(obj, 2)

    venues = LocationSerializerMin(many=True, read_only=True)

    bodies = BodySerializerMin(many=True, read_only=True)

    class Meta:
        model = Event
        fields = ('id', 'str_id', 'name', 'description', 'image_url',
                  'start_time', 'end_time', 'all_day', 'venues', 'bodies',
                  'interested_count', 'going_count', 'website_url', 'weight')
Esempio n. 3
0
class EventFullSerializer(serializers.ModelSerializer):
    """Serializer for Event with more information.

    Returns a nested list of followers of each status and
    detailed information on venues.
    """

    from bodies.serializer_min import BodySerializerMin
    from locations.serializers import LocationSerializerMin
    from locations.models import Location
    from bodies.models import Body

    interested_count = serializers.IntegerField(read_only=True)
    going_count = serializers.IntegerField(read_only=True)

    interested = serializers.SerializerMethodField()
    get_interested = lambda self, obj: get_followers(obj, 1)

    going = serializers.SerializerMethodField()
    get_going = lambda self, obj: get_followers(obj, 2)

    user_ues = serializers.SerializerMethodField()
    get_user_ues = get_user_ues

    venues = LocationSerializerMin(many=True, read_only=True)
    venue_names = serializers.SlugRelatedField(
        many=True, read_only=True, slug_field='name', source='venues')
    venue_ids = serializers.PrimaryKeyRelatedField(
        many=True, read_only=False, source='venues',
        queryset=Location.objects.all(), required=False)

    bodies = BodySerializerMin(many=True, read_only=True)
    bodies_id = serializers.PrimaryKeyRelatedField(
        many=True, read_only=False, queryset=Body.objects.all(), source='bodies')

    user_tags = serializers.PrimaryKeyRelatedField(
        many=True, read_only=False, queryset=UserTag.objects.all(), default=[])

    class Meta:
        model = Event
        fields = ('id', 'str_id', 'name', 'description', 'image_url', 'start_time',
                  'end_time', 'all_day', 'venues', 'venue_names', 'bodies', 'bodies_id',
                  'interested_count', 'going_count', 'interested', 'going', 'venue_ids',
                  'website_url', 'user_ues', 'notify', 'user_tags')

    def to_representation(self, instance):
        result = super().to_representation(instance)
        # Remove unnecessary fields
        result.pop('venue_ids')
        return result

    def create(self, validated_data):
        validated_data['created_by'] = self.context['request'].user.profile
        return super().create(validated_data)
Esempio n. 4
0
class EventSerializer(serializers.ModelSerializer):
    """Serializer for Event.

    This serializer returns only the count of followers in
    each category, i.e. interested and going and minimal
    venue info. Use `EventFullSerializer` if you want information
    on individual users and venues.
    """

    from locations.serializers import LocationSerializerMin
    from bodies.serializer_min import BodySerializerMin

    interested_count = serializers.IntegerField(read_only=True)
    going_count = serializers.IntegerField(read_only=True)

    user_ues = serializers.SerializerMethodField()
    get_user_ues = FollowersMethods.get_user_ues

    venues = LocationSerializerMin(many=True, read_only=True)

    bodies = BodySerializerMin(many=True, read_only=True)

    class Meta:
        model = Event
        fields = ('id', 'str_id', 'name', 'description', 'image_url',
                  'start_time', 'end_time', 'all_day', 'venues', 'bodies',
                  'interested_count', 'going_count', 'website_url', 'weight',
                  'user_ues')

    @staticmethod
    def setup_eager_loading(queryset):
        """Perform necessary eager loading of data.
        To be used for EventFullSerializer as well."""
        queryset = queryset.prefetch_related('bodies', 'venues', 'ues',
                                             'ues__user', 'user_tags')

        # Prefetch counts
        interested_count = Count('followers',
                                 distinct=True,
                                 filter=Q(ues__status=1))
        going_count = Count('followers',
                            distinct=True,
                            filter=Q(ues__status=2))
        queryset = queryset.annotate(
            interested_count=interested_count).annotate(
                going_count=going_count)

        return queryset