Exemplo n.º 1
0
Arquivo: users.py Projeto: webkom/lego
class Oauth2UserDataSerializer(serializers.ModelSerializer):
    """
    Basic serailizer
    """

    abakus_groups = PublicAbakusGroupSerializer(many=True)
    memberships = MembershipSerializer(many=True)
    profile_picture = ImageField(required=False, options={"height": 200, "width": 200})
    is_student = serializers.SerializerMethodField()
    is_abakus_member = serializers.BooleanField()

    class Meta:
        model = User
        fields = (
            "id",
            "username",
            "first_name",
            "last_name",
            "full_name",
            "email_address",
            "profile_picture",
            "gender",
            "is_active",
            "is_student",
            "abakus_groups",
            "is_abakus_member",
            "is_abakom_member",
            "memberships",
        )

    def get_is_student(self, user):
        return user.is_verified_student()
Exemplo n.º 2
0
Arquivo: users.py Projeto: webkom/lego
class DetailedUserSerializer(serializers.ModelSerializer):

    abakus_groups = PublicAbakusGroupSerializer(many=True)
    past_memberships = PastMembershipSerializer(many=True)
    penalties = serializers.SerializerMethodField("get_valid_penalties")
    profile_picture = ImageField(required=False, options={"height": 200, "width": 200})

    def get_valid_penalties(self, user):
        qs = Penalty.objects.valid().filter(user=user)
        serializer = PenaltySerializer(instance=qs, many=True)
        return serializer.data

    class Meta:
        model = User
        fields = (
            "id",
            "username",
            "first_name",
            "last_name",
            "full_name",
            "gender",
            "email",
            "email_address",
            "email_lists_enabled",
            "profile_picture",
            "allergies",
            "is_active",
            "penalties",
            "abakus_groups",
            "past_memberships",
            "permissions_per_group",
        )
Exemplo n.º 3
0
class PublicUserWithGroupsSerializer(PublicUserSerializer):
    abakus_groups = PublicAbakusGroupSerializer(many=True)
    past_memberships = PastMembershipSerializer(many=True)
    memberships = MembershipSerializer(many=True)

    class Meta(PublicUserSerializer.Meta):
        fields = PublicUserSerializer.Meta.fields + (
            'abakus_groups', 'past_memberships', 'memberships')
Exemplo n.º 4
0
class PastMembershipSerializer(serializers.ModelSerializer):
    abakus_group = PublicAbakusGroupSerializer()

    class Meta:
        model = MembershipHistory
        fields = ("id", "abakus_group", "role", "start_date", "end_date")
        read_only_fields = ("id", "abakus_group", "role", "start_date",
                            "end_date")
Exemplo n.º 5
0
class MeSerializer(serializers.ModelSerializer):
    """
    Serializer for the /me, retrieve and update endpoint with EDIT permissions.
    Also used by our JWT handler and returned to the user when a user obtains a JWT token.
    """

    abakus_groups = PublicAbakusGroupSerializer(many=True)
    memberships = MembershipSerializer(many=True)
    profile_picture = ImageField(required=False,
                                 options={
                                     'height': 200,
                                     'width': 200
                                 })
    ical_token = serializers.SerializerMethodField('get_user_ical_token')
    penalties = serializers.SerializerMethodField('get_valid_penalties')
    is_student = serializers.SerializerMethodField()
    is_abakus_member = serializers.BooleanField()
    past_memberships = PastMembershipSerializer(many=True)

    def get_user_ical_token(self, user):
        ical_token = ICalToken.objects.get_or_create(user=user)[0]
        return ical_token.token

    def get_valid_penalties(self, user):
        qs = Penalty.objects.valid().filter(user=user)
        serializer = PenaltySerializer(instance=qs, many=True)
        return serializer.data

    def get_is_student(self, user):
        return user.is_verified_student()

    def validate_username(self, username):
        """
        It is not possible to change username tom something that exists.
        Used to remove case-sensitivity.
        """
        username_exists = User.objects \
            .filter(username__iexact=username) \
            .exclude(id=self.instance.id) \
            .exists()

        if username_exists:
            raise exceptions.ValidationError('Username exists')

        return username

    class Meta:
        model = User
        fields = ('id', 'username', 'first_name', 'last_name', 'full_name',
                  'email', 'email_address', 'email_lists_enabled',
                  'profile_picture', 'gender', 'allergies', 'is_active',
                  'is_student', 'abakus_groups', 'is_abakus_member',
                  'is_abakom_member', 'penalties', 'ical_token', 'memberships',
                  'past_memberships')
Exemplo n.º 6
0
class AnnouncementListSerializer(BasisModelSerializer):

    users = PublicUserSerializer(many=True, read_only=True)
    groups = PublicAbakusGroupSerializer(many=True, read_only=True)
    events = EventReadSerializer(many=True, read_only=True)
    meetings = MeetingDetailSerializer(many=True, read_only=True)
    from_group = PublicAbakusGroupSerializer(read_only=True)

    class Meta:
        model = Announcement
        fields = (
            "id",
            "message",
            "from_group",
            "sent",
            "users",
            "groups",
            "events",
            "meetings",
        )
        read_only_fields = ("sent",)
Exemplo n.º 7
0
class AnnouncementListSerializer(BasisModelSerializer):

    users = PublicUserSerializer(many=True, read_only=True)
    groups = PublicAbakusGroupSerializer(many=True, read_only=True)
    events = EventReadSerializer(many=True, read_only=True)
    meetings = MeetingDetailSerializer(many=True, read_only=True)

    class Meta:
        model = Announcement
        fields = (
            'id',
            'message',
            'sent',
            'users',
            'groups',
            'events',
            'meetings',
        )
        read_only_fields = ('sent', )
Exemplo n.º 8
0
class PastMembershipSerializer(serializers.ModelSerializer):
    abakus_group = PublicAbakusGroupSerializer()

    class Meta:
        model = MembershipHistory
        fields = (
            'id',
            'abakus_group',
            'role',
            'start_date',
            'end_date',
        )
        read_only_fields = (
            'id',
            'abakus_group',
            'role',
            'start_date',
            'end_date',
        )
Exemplo n.º 9
0
class DetailedUserSerializer(serializers.ModelSerializer):

    abakus_groups = PublicAbakusGroupSerializer(many=True)
    penalties = serializers.SerializerMethodField('get_valid_penalties')
    profile_picture = ImageField(required=False,
                                 options={
                                     'height': 200,
                                     'width': 200
                                 })

    def get_valid_penalties(self, user):
        qs = Penalty.objects.valid().filter(user=user)
        serializer = PenaltySerializer(instance=qs, many=True)
        return serializer.data

    class Meta:
        model = User
        fields = ('id', 'username', 'first_name', 'last_name', 'full_name',
                  'gender', 'email', 'email_address', 'email_lists_enabled',
                  'profile_picture', 'allergies', 'is_active', 'penalties',
                  'abakus_groups')
Exemplo n.º 10
0
class PoolReadSerializer(BasisModelSerializer):
    permission_groups = PublicAbakusGroupSerializer(many=True)

    class Meta:
        model = Pool
        fields = (
            'id',
            'name',
            'capacity',
            'activation_date',
            'permission_groups',
            'registration_count',
        )
        read_only = True

    def create(self, validated_data):
        event = Event.objects.get(pk=self.context['view'].kwargs['event_pk'])
        permission_groups = validated_data.pop('permission_groups')
        pool = Pool.objects.create(event=event, **validated_data)
        pool.permission_groups.set(permission_groups)

        return pool
Exemplo n.º 11
0
Arquivo: pools.py Projeto: webkom/lego
class PoolReadSerializer(BasisModelSerializer):
    permission_groups = PublicAbakusGroupSerializer(many=True)

    class Meta:
        model = Pool
        fields = (
            "id",
            "name",
            "capacity",
            "activation_date",
            "permission_groups",
            "registration_count",
        )
        read_only = True

    def create(self, validated_data):
        event = Event.objects.get(pk=self.context["view"].kwargs["event_pk"])
        permission_groups = validated_data.pop("permission_groups")
        pool = Pool.objects.create(event=event, **validated_data)
        pool.permission_groups.set(permission_groups)

        return pool
Exemplo n.º 12
0
Arquivo: users.py Projeto: webkom/lego
class PublicUserWithAbakusGroupsSerializer(PublicUserSerializer):
    abakus_groups = PublicAbakusGroupSerializer(many=True)

    class Meta(PublicUserSerializer.Meta):
        fields = PublicUserSerializer.Meta.fields + ("abakus_groups",)
Exemplo n.º 13
0
Arquivo: users.py Projeto: webkom/lego
class MeSerializer(serializers.ModelSerializer):
    """
    Serializer for the /me, retrieve and update endpoint with EDIT permissions.
    Also used by our JWT handler and returned to the user when a user obtains a JWT token.
    """

    abakus_groups = PublicAbakusGroupSerializer(many=True)
    memberships = MembershipSerializer(many=True)
    profile_picture = ImageField(
        required=False, options={"height": 200, "width": 200}, allow_null=True
    )
    ical_token = serializers.SerializerMethodField("get_user_ical_token")
    penalties = serializers.SerializerMethodField("get_valid_penalties")
    is_student = serializers.SerializerMethodField()
    is_abakus_member = serializers.BooleanField()
    past_memberships = PastMembershipSerializer(many=True)
    abakus_email_lists = PublicEmailListSerializer(many=True)

    def get_user_ical_token(self, user):
        ical_token = ICalToken.objects.get_or_create(user=user)[0]
        return ical_token.token

    def get_valid_penalties(self, user):
        qs = Penalty.objects.valid().filter(user=user)
        serializer = PenaltySerializer(instance=qs, many=True)
        return serializer.data

    def get_is_student(self, user):
        return user.is_verified_student()

    def validate_username(self, username):
        """
        It is not possible to change username tom something that exists.
        Used to remove case-sensitivity.
        """
        username_exists = (
            User.objects.filter(username__iexact=username)
            .exclude(id=self.instance.id)
            .exists()
        )

        if username_exists:
            raise exceptions.ValidationError("Username exists")

        return username

    class Meta:
        model = User
        fields = (
            "id",
            "username",
            "first_name",
            "last_name",
            "full_name",
            "email",
            "email_address",
            "phone_number",
            "email_lists_enabled",
            "profile_picture",
            "gender",
            "allergies",
            "is_active",
            "is_student",
            "abakus_email_lists",
            "abakus_groups",
            "is_abakus_member",
            "is_abakom_member",
            "penalties",
            "ical_token",
            "memberships",
            "past_memberships",
            "internal_email_address",
            "selected_theme",
            "permissions_per_group",
        )