Exemple #1
0
class ReviewActionSerializer(BaseActionSerializer):
    class Meta:
        type_ = 'review-actions'

    filterable_fields = frozenset([
        'id',
        'trigger',
        'from_state',
        'to_state',
        'date_created',
        'date_modified',
        'provider',
        'target',
    ])

    comment = HideIfProviderCommentsPrivate(
        ser.CharField(max_length=65535, required=False))

    provider = RelationshipField(
        read_only=True,
        related_view='preprint_providers:preprint_provider-detail',
        related_view_kwargs={'provider_id': '<target.provider._id>'},
        filter_key='target__provider___id',
    )

    creator = HideIfProviderCommentsAnonymous(
        RelationshipField(
            read_only=True,
            related_view='users:user-detail',
            related_view_kwargs={'user_id': '<creator._id>'},
            filter_key='creator__guids___id',
            always_embed=True,
        ))

    target = TargetRelationshipField(
        target_class=PreprintService,
        read_only=False,
        required=True,
        related_view='preprints:preprint-detail',
        related_view_kwargs={'preprint_id': '<target._id>'},
        filter_key='target__guids___id',
    )
Exemple #2
0
class ReviewActionSerializer(BaseActionSerializer):
    class Meta:
        type_ = 'review-actions'

    filterable_fields = frozenset([
        'id',
        'trigger',
        'from_state',
        'to_state',
        'date_created',
        'date_modified',
        'provider',
        'target',
    ])

    comment = HideIfProviderCommentsPrivate(
        ser.CharField(max_length=65535, required=False))
    trigger = ser.ChoiceField(choices=ReviewTriggers.choices())
    from_state = ser.ChoiceField(choices=ReviewStates.choices(),
                                 read_only=True)
    to_state = ser.ChoiceField(choices=ReviewStates.choices(), read_only=True)

    provider = RelationshipField(
        read_only=True,
        related_view='providers:preprint-providers:preprint-provider-detail',
        related_view_kwargs={'provider_id': '<target.provider._id>'},
        filter_key='target__provider___id',
    )

    creator = HideIfProviderCommentsAnonymous(
        RelationshipField(
            read_only=True,
            related_view='users:user-detail',
            related_view_kwargs={'user_id': '<creator._id>'},
            filter_key='creator__guids___id',
            always_embed=True,
        ))

    target = TargetRelationshipField(
        target_class=PreprintService,
        read_only=False,
        required=True,
        related_view='preprints:preprint-detail',
        related_view_kwargs={'preprint_id': '<target._id>'},
        filter_key='target__guids___id',
    )

    def create(self, validated_data):
        trigger = validated_data.get('trigger')
        if trigger != ReviewTriggers.WITHDRAW.value:
            return super(ReviewActionSerializer, self).create(validated_data)
        user = validated_data.pop('user')
        target = validated_data.pop('target')
        comment = validated_data.pop('comment', '')
        try:
            return target.run_withdraw(user=user, comment=comment)
        except InvalidTriggerError as e:
            # Invalid transition from the current state
            raise Conflict(e.message)
        else:
            raise JSONAPIAttributeException(attribute='trigger',
                                            detail='Invalid trigger.')
Exemple #3
0
class BaseActionSerializer(JSONAPISerializer):
    filterable_fields = frozenset([
        'id',
        'trigger',
        'from_state',
        'to_state',
        'date_created',
        'date_modified',
        'target',
    ])

    id = ser.CharField(source='_id', read_only=True)

    trigger = ser.ChoiceField(choices=DefaultTriggers.choices())

    comment = HideIfProviderCommentsPrivate(ser.CharField(max_length=65535, required=False))

    from_state = ser.ChoiceField(choices=DefaultStates.choices(), read_only=True)
    to_state = ser.ChoiceField(choices=DefaultStates.choices(), read_only=True)

    date_created = ser.DateTimeField(source='created', read_only=True)
    date_modified = ser.DateTimeField(source='modified', read_only=True)

    creator = RelationshipField(
        read_only=True,
        related_view='users:user-detail',
        related_view_kwargs={'user_id': '<creator._id>'},
        filter_key='creator__guids___id',
        always_embed=True,
    )

    links = LinksField(
        {
            'self': 'get_action_url',
        }
    )

    @property
    def get_action_url(self):
        raise NotImplementedError()

    def get_absolute_url(self, obj):
        return self.get_action_url(obj)

    def create(self, validated_data):
        trigger = validated_data.pop('trigger')
        user = validated_data.pop('user')
        target = validated_data.pop('target')
        comment = validated_data.pop('comment', '')
        try:
            if trigger == DefaultTriggers.ACCEPT.value:
                return target.run_accept(user, comment)
            if trigger == DefaultTriggers.REJECT.value:
                return target.run_reject(user, comment)
            if trigger == DefaultTriggers.EDIT_COMMENT.value:
                return target.run_edit_comment(user, comment)
            if trigger == DefaultTriggers.SUBMIT.value:
                return target.run_submit(user)
        except InvalidTriggerError as e:
            # Invalid transition from the current state
            raise Conflict(e.message)
        else:
            raise JSONAPIAttributeException(attribute='trigger', detail='Invalid trigger.')

    class Meta:
        type_ = 'actions'
        abstract = True
class ActionSerializer(JSONAPISerializer):
    filterable_fields = frozenset([
        'id',
        'trigger',
        'from_state',
        'to_state',
        'date_created',
        'date_modified',
        'provider',
        'target',
    ])

    id = ser.CharField(source='_id', read_only=True)

    trigger = ser.ChoiceField(choices=Triggers.choices())

    comment = HideIfProviderCommentsPrivate(
        ser.CharField(max_length=65535, required=False))

    from_state = ser.ChoiceField(choices=States.choices(), read_only=True)
    to_state = ser.ChoiceField(choices=States.choices(), read_only=True)

    date_created = ser.DateTimeField(source='created', read_only=True)
    date_modified = ser.DateTimeField(source='modified', read_only=True)

    provider = RelationshipField(
        read_only=True,
        related_view='preprint_providers:preprint_provider-detail',
        related_view_kwargs={'provider_id': '<target.provider._id>'},
        filter_key='target__provider___id',
    )

    target = TargetRelationshipField(
        read_only=False,
        required=True,
        related_view='preprints:preprint-detail',
        related_view_kwargs={'preprint_id': '<target._id>'},
        filter_key='target__guids___id',
    )

    creator = HideIfProviderCommentsAnonymous(
        RelationshipField(
            read_only=True,
            related_view='users:user-detail',
            related_view_kwargs={'user_id': '<creator._id>'},
            filter_key='creator__guids___id',
            always_embed=True,
        ))

    links = LinksField({
        'self': 'get_action_url',
    })

    def get_absolute_url(self, obj):
        return self.get_action_url(obj)

    def get_action_url(self, obj):
        return utils.absolute_reverse(
            'actions:action-detail',
            kwargs={
                'action_id':
                obj._id,
                'version':
                self.context['request'].parser_context['kwargs']['version']
            })

    def create(self, validated_data):
        trigger = validated_data.pop('trigger')
        user = validated_data.pop('user')
        target = validated_data.pop('target')
        comment = validated_data.pop('comment', '')
        try:
            if trigger == Triggers.ACCEPT.value:
                return target.reviews_accept(user, comment)
            if trigger == Triggers.REJECT.value:
                return target.reviews_reject(user, comment)
            if trigger == Triggers.EDIT_COMMENT.value:
                return target.reviews_edit_comment(user, comment)
            if trigger == Triggers.SUBMIT.value:
                return target.reviews_submit(user)
        except InvalidTriggerError as e:
            # Invalid transition from the current state
            raise Conflict(e.message)
        else:
            raise JSONAPIAttributeException(attribute='trigger',
                                            detail='Invalid trigger.')

    class Meta:
        type_ = 'actions'