class BaseAction(ObjectIDMixin, BaseModel): class Meta: abstract = True objects = IncludeManager() creator = models.ForeignKey('OSFUser', related_name='+', on_delete=models.CASCADE) trigger = models.CharField(max_length=31, choices=DefaultTriggers.choices()) from_state = models.CharField(max_length=31, choices=DefaultStates.choices()) to_state = models.CharField(max_length=31, choices=DefaultStates.choices()) comment = models.TextField(blank=True) is_deleted = models.BooleanField(default=False) @property def target(self): raise NotImplementedError()
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 = 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) auto = ser.BooleanField(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', }) 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', '') permissions = validated_data.pop('permissions', '') visible = validated_data.pop('visible', '') try: if trigger == DefaultTriggers.ACCEPT.value: return target.run_accept(user=user, comment=comment, permissions=permissions, visible=visible) 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 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