Exemple #1
0
class EventReadDetailedSerializer(TagSerializerMixin, BasisModelSerializer):
    comments = CommentSerializer(read_only=True, many=True)
    comment_target = CharField(read_only=True)
    cover = ImageField(required=False, options={'height': 500})
    company = CompanyField(queryset=Company.objects.all())
    responsible_group = AbakusGroupField(queryset=AbakusGroup.objects.all(),
                                         required=False,
                                         allow_null=True)
    pools = PoolReadSerializer(many=True)
    active_capacity = serializers.ReadOnlyField()
    text = ContentSerializerField()
    created_by = PublicUserSerializer()

    class Meta:
        model = Event
        fields = ('id', 'title', 'description', 'cover', 'text', 'event_type',
                  'location', 'comments', 'comment_target', 'start_time',
                  'end_time', 'merge_time', 'pools', 'unregistration_deadline',
                  'company', 'responsible_group', 'active_capacity',
                  'feedback_description', 'feedback_required', 'is_priced',
                  'price_member', 'price_guest', 'use_stripe',
                  'payment_due_date', 'use_captcha',
                  'waiting_registration_count', 'tags', 'is_merged',
                  'heed_penalties', 'created_by', 'is_abakom_only',
                  'registration_count', 'survey')
        read_only = True
Exemple #2
0
class EventCreateAndUpdateSerializer(TagSerializerMixin, BasisModelSerializer):
    cover = ImageField(required=False, options={'height': 500})
    responsible_group = AbakusGroupField(queryset=AbakusGroup.objects.all(),
                                         required=False,
                                         allow_null=True)
    pools = PoolCreateAndUpdateSerializer(many=True, required=False)
    text = ContentSerializerField()
    is_abakom_only = BooleanField(required=False, default=False)

    registration_close_time = serializers.DateTimeField(read_only=True)

    class Meta:
        model = Event
        fields = ('id', 'title', 'cover', 'description', 'text', 'company',
                  'responsible_group', 'feedback_description',
                  'feedback_required', 'event_type', 'location', 'is_priced',
                  'price_member', 'price_guest', 'use_stripe',
                  'payment_due_date', 'start_time', 'end_time', 'merge_time',
                  'use_captcha', 'tags', 'pools', 'unregistration_deadline',
                  'pinned', 'heed_penalties', 'is_abakom_only',
                  'registration_deadline_hours', 'registration_close_time')

    def create(self, validated_data):
        pools = validated_data.pop('pools', [])
        is_abakom_only = validated_data.pop('is_abakom_only', False)
        with transaction.atomic():
            event = super().create(validated_data)
            for pool in pools:
                permission_groups = pool.pop('permission_groups')
                created_pool = Pool.objects.create(event=event, **pool)
                created_pool.permission_groups.set(permission_groups)
            event.set_abakom_only(is_abakom_only)
            return event

    def update(self, instance, validated_data):
        pools = validated_data.pop('pools', None)
        is_abakom_only = validated_data.pop('is_abakom_only', False)
        with transaction.atomic():
            if pools is not None:
                existing_pools = list(instance.pools.all().values_list(
                    'id', flat=True))
                for pool in pools:
                    pool_id = pool.get('id', None)
                    if pool_id in existing_pools:
                        existing_pools.remove(pool_id)
                    permission_groups = pool.pop('permission_groups')
                    created_pool = Pool.objects.update_or_create(
                        event=instance,
                        id=pool_id,
                        defaults={
                            'name': pool.get('name'),
                            'capacity': pool.get('capacity', 0),
                            'activation_date': pool.get('activation_date'),
                        })[0]
                    created_pool.permission_groups.set(permission_groups)
                for pool_id in existing_pools:
                    Pool.objects.get(id=pool_id).delete()
            instance.set_abakom_only(is_abakom_only)
            return super().update(instance, validated_data)
Exemple #3
0
class MembershipHistorySerializer(serializers.ModelSerializer):

    abakus_group = AbakusGroupField(read_only=True)
    user = PublicUserField(read_only=True)

    class Meta:
        model = MembershipHistory
        fields = ("id", "user", "abakus_group", "role", "start_date", "end_date")
Exemple #4
0
class EventReadDetailedSerializer(TagSerializerMixin, BasisModelSerializer):
    comments = CommentSerializer(read_only=True, many=True)
    content_target = CharField(read_only=True)
    cover = ImageField(required=False, options={"height": 500})
    company = CompanyField(queryset=Company.objects.all())
    responsible_group = AbakusGroupField(queryset=AbakusGroup.objects.all(),
                                         required=False,
                                         allow_null=True)
    pools = PoolReadSerializer(many=True)
    active_capacity = serializers.ReadOnlyField()
    text = ContentSerializerField()
    created_by = PublicUserSerializer()

    registration_close_time = serializers.DateTimeField(read_only=True)

    class Meta:
        model = Event
        fields = (
            "id",
            "title",
            "description",
            "cover",
            "text",
            "event_type",
            "event_status_type",
            "location",
            "comments",
            "content_target",
            "start_time",
            "end_time",
            "merge_time",
            "pools",
            "registration_close_time",
            "registration_deadline_hours",
            "unregistration_deadline",
            "company",
            "responsible_group",
            "active_capacity",
            "feedback_description",
            "feedback_required",
            "is_priced",
            "price_member",
            "price_guest",
            "use_stripe",
            "payment_due_date",
            "use_captcha",
            "waiting_registration_count",
            "tags",
            "is_merged",
            "heed_penalties",
            "created_by",
            "is_abakom_only",
            "registration_count",
            "survey",
            "use_consent",
            "youtube_url",
        )
        read_only = True
Exemple #5
0
class ObjectPermissionsSerializerMixin(serializers.Serializer):
    class Meta:
        fields = (
            "can_edit_users",
            "can_view_groups",
            "can_edit_groups",
            "require_auth",
        )

    can_edit_users = PublicUserField(
        queryset=User.objects.all(), allow_null=True, required=False, many=True
    )
    can_edit_groups = AbakusGroupField(
        queryset=AbakusGroup.objects.all(), allow_null=True, required=False, many=True
    )
    can_view_groups = AbakusGroupField(
        queryset=AbakusGroup.objects.all(), allow_null=True, required=False, many=True
    )
Exemple #6
0
class MembershipHistorySerializer(serializers.ModelSerializer):

    abakus_group = AbakusGroupField(read_only=True)
    user = PublicUserField(read_only=True)

    class Meta:
        model = MembershipHistory
        fields = ('id', 'user', 'abakus_group', 'role', 'start_date',
                  'end_date')
Exemple #7
0
class EventCreateAndUpdateSerializer(TagSerializerMixin, BasisModelSerializer):
    cover = ImageField(required=False, options={"height": 500})
    responsible_group = AbakusGroupField(queryset=AbakusGroup.objects.all(),
                                         required=False,
                                         allow_null=True)
    pools = PoolCreateAndUpdateSerializer(many=True, required=False)
    text = ContentSerializerField()
    is_abakom_only = BooleanField(required=False, default=False)

    registration_close_time = serializers.DateTimeField(read_only=True)
    unregistration_close_time = serializers.DateTimeField(read_only=True)

    class Meta:
        model = Event
        fields = (
            "id",
            "title",
            "cover",
            "description",
            "text",
            "company",
            "responsible_group",
            "feedback_description",
            "feedback_required",
            "event_type",
            "event_status_type",
            "location",
            "is_priced",
            "price_member",
            "price_guest",
            "use_stripe",
            "payment_due_date",
            "start_time",
            "end_time",
            "merge_time",
            "use_captcha",
            "tags",
            "pools",
            "unregistration_deadline",
            "unregistration_deadline_hours",
            "pinned",
            "use_consent",
            "heed_penalties",
            "is_abakom_only",
            "registration_deadline_hours",
            "registration_close_time",
            "unregistration_close_time",
            "youtube_url",
            "use_contact_tracing",
            "mazemap_poi",
        )

    def validate(self, data):
        """
        Check that start is before finish.
        """
        if hasattr(data, "start_time") and hasattr(data, "end_time"):
            if data["start_time"] > data["end_time"]:
                raise serializers.ValidationError({
                    "end_time":
                    "User does not have the required permissions for time travel"
                })
        if (self.instance is not None and "use_contact_tracing" in data
                and data["use_contact_tracing"] !=
                self.instance.use_contact_tracing
                and self.instance.registrations.exists()):
            raise serializers.ValidationError({
                "use_contact_tracing":
                "Cannot change this field after registration has started"
            })

        return data

    def create(self, validated_data):
        pools = validated_data.pop("pools", [])
        is_abakom_only = validated_data.pop("is_abakom_only", False)
        event_status_type = validated_data.get(
            "event_status_type",
            Event._meta.get_field("event_status_type").default)
        if event_status_type == constants.TBA:
            pools = []
            validated_data["location"] = "TBA"
        elif event_status_type == constants.OPEN:
            pools = []
        elif event_status_type == constants.INFINITE:
            pools = [pools[0]]
            pools[0]["capacity"] = 0
        with transaction.atomic():
            event = super().create(validated_data)
            for pool in pools:
                permission_groups = pool.pop("permission_groups")
                created_pool = Pool.objects.create(event=event, **pool)
                created_pool.permission_groups.set(permission_groups)
            event.set_abakom_only(is_abakom_only)
            return event

    def update(self, instance, validated_data):
        pools = validated_data.pop("pools", None)
        is_abakom_only = validated_data.pop("is_abakom_only", False)
        event_status_type = validated_data.get(
            "event_status_type",
            Event._meta.get_field("event_status_type").default)
        if event_status_type == constants.TBA:
            pools = []
            validated_data["location"] = "TBA"
        elif event_status_type == constants.OPEN:
            pools = []
        elif event_status_type == constants.INFINITE:
            pools = [pools[0]]
            pools[0]["capacity"] = 0
        with transaction.atomic():
            if pools is not None:
                existing_pools = list(instance.pools.all().values_list(
                    "id", flat=True))
                for pool in pools:
                    pool_id = pool.get("id", None)
                    if pool_id in existing_pools:
                        existing_pools.remove(pool_id)
                    permission_groups = pool.pop("permission_groups")
                    created_pool = Pool.objects.update_or_create(
                        event=instance,
                        id=pool_id,
                        defaults={
                            "name": pool.get("name"),
                            "capacity": pool.get("capacity", 0),
                            "activation_date": pool.get("activation_date"),
                        },
                    )[0]
                    created_pool.permission_groups.set(permission_groups)
                for pool_id in existing_pools:
                    Pool.objects.get(id=pool_id).delete()
            instance.set_abakom_only(is_abakom_only)
            return super().update(instance, validated_data)
Exemple #8
0
class EventCreateAndUpdateSerializer(TagSerializerMixin, BasisModelSerializer):
    cover = ImageField(required=False, options={"height": 500})
    responsible_group = AbakusGroupField(queryset=AbakusGroup.objects.all(),
                                         required=False,
                                         allow_null=True)
    pools = PoolCreateAndUpdateSerializer(many=True, required=False)
    text = ContentSerializerField()
    is_abakom_only = BooleanField(required=False, default=False)

    registration_close_time = serializers.DateTimeField(read_only=True)

    class Meta:
        model = Event
        fields = (
            "id",
            "title",
            "cover",
            "description",
            "text",
            "company",
            "responsible_group",
            "feedback_description",
            "feedback_required",
            "event_type",
            "location",
            "is_priced",
            "price_member",
            "price_guest",
            "use_stripe",
            "payment_due_date",
            "start_time",
            "end_time",
            "merge_time",
            "use_captcha",
            "tags",
            "pools",
            "unregistration_deadline",
            "pinned",
            "heed_penalties",
            "is_abakom_only",
            "registration_deadline_hours",
            "registration_close_time",
        )

    def create(self, validated_data):
        pools = validated_data.pop("pools", [])
        is_abakom_only = validated_data.pop("is_abakom_only", False)
        with transaction.atomic():
            event = super().create(validated_data)
            for pool in pools:
                permission_groups = pool.pop("permission_groups")
                created_pool = Pool.objects.create(event=event, **pool)
                created_pool.permission_groups.set(permission_groups)
            event.set_abakom_only(is_abakom_only)
            return event

    def update(self, instance, validated_data):
        pools = validated_data.pop("pools", None)
        is_abakom_only = validated_data.pop("is_abakom_only", False)
        with transaction.atomic():
            if pools is not None:
                existing_pools = list(instance.pools.all().values_list(
                    "id", flat=True))
                for pool in pools:
                    pool_id = pool.get("id", None)
                    if pool_id in existing_pools:
                        existing_pools.remove(pool_id)
                    permission_groups = pool.pop("permission_groups")
                    created_pool = Pool.objects.update_or_create(
                        event=instance,
                        id=pool_id,
                        defaults={
                            "name": pool.get("name"),
                            "capacity": pool.get("capacity", 0),
                            "activation_date": pool.get("activation_date"),
                        },
                    )[0]
                    created_pool.permission_groups.set(permission_groups)
                for pool_id in existing_pools:
                    Pool.objects.get(id=pool_id).delete()
            instance.set_abakom_only(is_abakom_only)
            return super().update(instance, validated_data)