class ChangeUserOrganizationRoleSerializer(serializers.Serializer):
    """
    TODO: Can we merge this with the actual UserSerializer?
    A: I dunno, but i doubled down in the next serializer
    """
    role = serializers.ChoiceField(
        choices=['Member', 'Admin']
    )

    user = serializers.PrimaryKeyRelatedField(
        queryset=User.objects.get_queryset()
    )

    organization = serializers.PrimaryKeyRelatedField(
        queryset=Organization.objects.get_queryset()
    )

    def save(self, **kwargs):
        role = self.validated_data['role']
        user = self.validated_data['user']
        organization = self.validated_data['organization']
        org_user, _ = organization.get_or_add_user(user)

        if role == "Admin":
            org_user.is_admin = True
        else:
            org_user.is_admin = False
        org_user.save()

        return True

    class Meta:
        model = User
Beispiel #2
0
class PageSerializer(BaseInternalModelSerializer):
    id = HashidSerializerCharField(source_field='groups.Page.id',
                                   required=False)
    organization = ResourceRelatedField(many=False,
                                        queryset=Organization.objects)

    group = ResourceRelatedField(many=False, queryset=Group.objects)

    author = ResourceRelatedField(many=False,
                                  queryset=get_user_model().objects)

    bill_layout = serializers.ChoiceField(choices=['table', 'slides', 'list'],
                                          required=False)

    visibility = serializers.ChoiceField(choices=['anyone', 'organization'])

    viewers = ResourceRelatedField(queryset=get_user_model().objects,
                                   many=True,
                                   required=False)

    included_serializers = {
        'author': 'users.api.app.serializers.UserSerializer',
        'organization': AnonOrganizationSerializer,
        'group': AnonGroupSerializer,
        'viewers': 'users.api.app.serializers.UserSerializer'
    }

    class Meta:
        model = Page
        fields = ('id', 'metadata', 'created', 'modified', 'visibility',
                  'title', 'description', 'published', 'organization', 'group',
                  'author', 'bill_layout', 'avatar', 'viewers',
                  'structured_page_info')
        read_only_fields = ('id', )

    class JSONAPIMeta:
        included_resources = ['author', 'organization', 'group', 'viewers']
class ItemSerializer(serializers.Serializer):
    id = serializers.CharField(read_only=True)
    source_id = serializers.CharField()
    title = serializers.CharField(required=True)
    type = serializers.ChoiceField(
        choices=['project', 'preprint', 'registration', 'meeting', 'website'])
    status = serializers.ChoiceField(
        choices=['approved', 'pending', 'rejected'])
    url = serializers.URLField()
    created_by = UserSerializer(read_only=True)
    metadata = serializers.JSONField(required=False)
    date_added = serializers.DateTimeField(read_only=True, allow_null=True)
    date_submitted = serializers.DateTimeField(read_only=True)

    class Meta:
        model = Item

    class JSONAPIMeta:
        resource_name = 'items'

    def create(self, validated_data):
        user = self.context['request'].user
        collection_id = self.context.get(
            'collection_id',
            None) or self.context['request'].parser_context['kwargs'].get(
                'pk', None)
        collection = Collection.objects.get(id=collection_id)

        allow_all = None
        if collection.settings:
            collection_settings = json.loads(collection.settings)
            allow_all = collection_settings.get('allow_all', None)
            collection_type = collection_settings.get('type', None)
            if collection_type and validated_data['type'] != collection_type:
                raise ValueError('Collection only accepts items of type ' +
                                 collection_type)

        status = 'pending'
        if user.has_perm('api.approve_items', collection) or allow_all:
            status = 'approved'
            validated_data['date_added'] = timezone.now()

        group_id = self.context.get(
            'group_id',
            None) or self.context['request'].parser_context['kwargs'].get(
                'group_id', None)
        if group_id:
            validated_data['group'] = Group.objects.get(id=group_id)

        validated_data['status'] = status
        item = Item.objects.create(created_by=user,
                                   collection=collection,
                                   **validated_data)
        return item

    def update(self, item, validated_data):
        user = self.context['request'].user
        status = validated_data.get('status', item.status)
        collection_id = self.context.get(
            'collection_id',
            None) or self.context['request'].parser_context['kwargs'].get(
                'pk', None)
        if collection_id:
            collection = Collection.objects.get(id=collection_id)
        else:
            collection = item.collection

        if status != item.status and user.has_perm('api.approve_items',
                                                   collection):
            raise exceptions.PermissionDenied(
                detail='Cannot change submission status.')
        elif user.id != item.created_by_id and validated_data.keys() != [
                'status'
        ]:
            raise exceptions.PermissionDenied(
                detail='Cannot update another user\'s submission.')

        group_id = self.context.get(
            'group_id',
            None) or self.context['request'].parser_context['kwargs'].get(
                'group_id', None)
        if group_id:
            group = Group.objects.get(id=group_id)
        else:
            group = None

        item_type = validated_data.get('type', item.type)
        if collection.settings:
            collection_settings = json.loads(collection.settings)
            collection_type = collection_settings.get('type', None)
            if collection_type and item_type != collection_type:
                raise ValueError('Collection only accepts items of type ' +
                                 collection_type)

        item.group = group
        item.source_id = validated_data.get('source_id', item.source_id)
        item.title = validated_data.get('title', item.title)
        item.type = item_type
        item.status = status
        item.url = validated_data.get('url', item.url)
        item.metadata = validated_data.get('metadata', item.metadata)
        item.save()
        return item