class NodeContributorsCreateSerializer(NodeContributorsSerializer): """ Overrides NodeContributorsSerializer to add target_type field """ target_type = TargetTypeField(target_type='users') def create(self, validated_data): auth = Auth(self.context['request'].user) node = self.context['view'].get_node() contributor = get_object_or_error(User, validated_data['_id'], display_name='user') # Node object checks for contributor existence but can still change permissions anyway if contributor in node.contributors: raise exceptions.ValidationError( '{} is already a contributor'.format(contributor.fullname)) bibliographic = validated_data['bibliographic'] permissions = osf_permissions.expand_permissions( validated_data.get('permission') ) or osf_permissions.DEFAULT_CONTRIBUTOR_PERMISSIONS node.add_contributor(contributor=contributor, auth=auth, visible=bibliographic, permissions=permissions, save=True) contributor.permission = osf_permissions.reduce_permissions( node.get_permissions(contributor)) contributor.bibliographic = node.get_visible(contributor) contributor.node_id = node._id return contributor
class NodeLinksSerializer(JSONAPISerializer): id = IDField(source='_id') type = TypeField() target_type = TargetTypeField(target_type='nodes') # TODO: We don't show the title because the current user may not have access to this node. We may want to conditionally # include this field in the future. # title = ser.CharField(read_only=True, source='node.title', help_text='The title of the node that this Node Link ' # 'points to') target_node = RelationshipField( related_view='nodes:node-detail', related_view_kwargs={'node_id': '<pk>'}, always_embed=True ) class Meta: type_ = 'node_links' links = LinksField({ 'self': 'get_absolute_url' }) def get_absolute_url(self, obj): node_id = self.context['request'].parser_context['kwargs']['node_id'] return absolute_reverse( 'nodes:node-pointer-detail', kwargs={ 'node_id': node_id, 'node_link_id': obj._id } ) def create(self, validated_data): request = self.context['request'] user = request.user auth = Auth(user) node = self.context['view'].get_node() target_node_id = validated_data['_id'] pointer_node = Node.load(target_node_id) if not pointer_node or pointer_node.is_folder: raise InvalidModelValueError( source={'pointer': '/data/relationships/node_links/data/id'}, detail='Target Node \'{}\' not found.'.format(target_node_id) ) try: pointer = node.add_pointer(pointer_node, auth, save=True) return pointer except ValueError: raise InvalidModelValueError( source={'pointer': '/data/relationships/node_links/data/id'}, detail='Target Node \'{}\' already pointed to by \'{}\'.'.format(target_node_id, node._id) ) def update(self, instance, validated_data): pass