def test_add_tag(self): file_ = self.root.append_file('That\'s How Strong My Love Is.mp3') tag = Tag(name='Redding') tag.save() file_.tags.add(tag) file_.save() find = query_tag_file('Redding')['results'] assert_equal(len(find), 1)
def test_file_add_tag_fail_doesnt_create_log(self, mock_log): file = self.node_settings.get_root().append_file('UltraLightBeam.mp3') tag = Tag(name='The Life of Pablo') tag.save() file.tags.add(tag) file.save() url = self.project.api_url_for('osfstorage_add_tag', fid=file._id) res = self.app.post_json(url, {'tag': 'The Life of Pablo'}, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 400) mock_log.assert_not_called()
def test_tag_the_same_tag(self): file = self.node_settings.get_root().append_file('Lie,Cheat,Steal.mp3') tag = Tag(name='Run_the_Jewels') tag.save() file.tags.add(tag) file.save() assert_in('Run_the_Jewels', file.tags.values_list('name', flat=True)) url = self.project.api_url_for('osfstorage_add_tag', fid=file._id) res = self.app.post_json(url, {'tag': 'Run_the_Jewels'}, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 400) assert_equal(res.json['status'], 'failure')
def test_file_remove_tag(self): file = self.node_settings.get_root().append_file('Champion.mp3') tag = Tag(name='Graduation') tag.save() file.tags.add(tag) file.save() assert_in('Graduation', file.tags.values_list('name', flat=True)) url = self.project.api_url_for('osfstorage_remove_tag', fid=file._id) self.app.delete_json(url, {'tag': 'Graduation'}, auth=self.user.auth) file.reload() assert_not_in('Graduation', file.tags.values_list('name', flat=True))
def test_tag_the_same_tag(self): file = self.node_settings.get_root().append_file('Lie,Cheat,Steal.mp3') tag = Tag(name='Run_the_Jewels') tag.save() file.tags.add(tag) file.save() assert_in('Run_the_Jewels', file.tags.values_list('name', flat=True)) url = api_url_for('osfstorage_add_tag', guid=self.node._id, fid=file._id) res = self.app.post_json(url, {'tag': 'Run_the_Jewels'}, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 400) assert_equal(res.json['status'], 'failure')
def test_file_add_tag_fail_doesnt_create_log(self, mock_log): file = self.node_settings.get_root().append_file('UltraLightBeam.mp3') tag = Tag(name='The Life of Pablo') tag.save() file.tags.add(tag) file.save() url = api_url_for('osfstorage_add_tag', guid=self.node._id, fid=file._id) res = self.app.post_json(url, {'tag': 'The Life of Pablo'}, auth=self.user.auth, expect_errors=True) assert_equal(res.status_code, 400) mock_log.assert_not_called()
def test_remove_tag(self): file_ = self.root.append_file('I\'ve Been Loving You Too Long.mp3') tag = Tag(name='Blue') tag.save() file_.tags.add(tag) file_.save() find = query_tag_file('Blue')['results'] assert_equal(len(find), 1) file_.tags.remove(tag) file_.save() find = query_tag_file('Blue')['results'] assert_equal(len(find), 0)
def test_file_remove_tag_creates_log(self): file = self.node_settings.get_root().append_file('Formation.flac') tag = Tag(name='You that when you cause all this conversation') tag.save() file.tags.add(tag) file.save() url = self.project.api_url_for('osfstorage_remove_tag', fid=file._id) res = self.app.delete_json(url, {'tag': 'You that when you cause all this conversation'}, auth=self.user.auth) assert_equal(res.status_code, 200) self.node.reload() assert_equal(self.node.logs.latest().action, 'file_tag_removed')
def test_file_remove_tag_creates_log(self): file = self.node_settings.get_root().append_file('Formation.flac') tag = Tag(name='You that when you cause all this conversation') tag.save() file.tags.add(tag) file.save() url = api_url_for('osfstorage_remove_tag', guid=self.node._id, fid=file._id) res = self.app.delete_json(url, {'tag': 'You that when you cause all this conversation'}, auth=self.user.auth) assert_equal(res.status_code, 200) self.node.reload() assert_equal(self.node.logs.latest().action, 'file_tag_removed')
def test_qatest_quickfiles_files_not_appear_in_search(self): quickfiles = QuickFilesNode.objects.get(creator=self.node.creator) quickfiles_osf_storage = quickfiles.get_addon('osfstorage') quickfiles_root = quickfiles_osf_storage.get_root() file = quickfiles_root.append_file('GreenLight.mp3') tag = Tag(name='qatest') tag.save() file.tags.add(tag) file.save() find = query_file('GreenLight.mp3')['results'] assert_equal(len(find), 0)
def add_tag(self, tag, auth, save=True, log=True): from osf.models import Tag, NodeLog # Prevent import error if not self.tags.filter(system=False, name=tag).exists() and not self.node.is_registration: new_tag = Tag.load(tag) if not new_tag: new_tag = Tag(name=tag) new_tag.save() self.tags.add(new_tag) if log: self.add_tag_log(NodeLog.FILE_TAG_ADDED, tag, auth) if save: self.save() return True return False
def conference_submissions(**kwargs): """Return data for all OSF4M submissions. The total number of submissions for each meeting is calculated and cached in the Conference.num_submissions field. """ conferences = Conference.find(Q('is_meeting', 'ne', False)) # TODO: Revisit this loop, there has to be a way to optimize it for conf in conferences: # For efficiency, we filter by tag first, then node # instead of doing a single Node query projects = set() tags = Tag.find(Q('system', 'eq', False) & Q('name', 'iexact', conf.endpoint.lower())).values_list('pk', flat=True) nodes = Node.find( Q('tags', 'in', tags) & Q('is_public', 'eq', True) & Q('is_deleted', 'ne', True) ).include('guids') projects.update(list(nodes)) num_submissions = len(projects) # Cache the number of submissions conf.num_submissions = num_submissions bulk_update(conferences, update_fields=['num_submissions']) return {'success': True}
def conference_submissions(**kwargs): """Return data for all OSF4M submissions. The total number of submissions for each meeting is calculated and cached in the Conference.num_submissions field. """ conferences = Conference.find(Q('is_meeting', 'ne', False)) # TODO: Revisit this loop, there has to be a way to optimize it for conf in conferences: # For efficiency, we filter by tag first, then node # instead of doing a single Node query projects = set() tags = Tag.find( Q('system', 'eq', False) & Q('name', 'iexact', conf.endpoint.lower())).values_list( 'pk', flat=True) nodes = AbstractNode.find( Q('tags', 'in', tags) & Q('is_public', 'eq', True) & Q('is_deleted', 'ne', True)).include('guids') projects.update(list(nodes)) num_submissions = len(projects) # Cache the number of submissions conf.num_submissions = num_submissions bulk_update(conferences, update_fields=['num_submissions']) return {'success': True}
def legacy_system_tags(self): tags = [] for item in [item[0] for item in PROVIDER_SOURCE_TAGS]: tag = Tag(name=item, system=True) tag.save() for item in [item[0] for item in CAMPAIGN_SOURCE_TAGS]: tag = Tag(name=item, system=True) tag.save()
def project_tag(tag, auth, **kwargs): tag_obj = Tag.load(tag) if tag_obj: nodes = AbstractNode.find(Q('tags', 'eq', tag_obj._id)) else: nodes = [] visible_nodes = [obj for obj in nodes if obj.can_view(auth)] return { 'nodes': [{ 'title': node.title, 'url': node.url, } for node in visible_nodes], 'tag': tag, }
def test_has_an_integer_pk(self): tag = Tag(name='FooBar') tag.save() assert type(tag.pk) is int
def test_uniqueness_on_name_and_system(self): tag = Tag(name='FooBar', system=False) tag.save() tag2 = Tag(name='FooBar', system=False) with pytest.raises(ValidationError): tag2.save() tag3 = Tag(name='FooBar', system=True) try: tag3.save() except Exception: pytest.fail('Saving system tag with non-unique name should not error.')
def test_load_loads_by_name(self): tag_name = 'NeONDreams' tag = Tag(name=tag_name) tag.save() assert Tag.load(tag_name).pk == tag.pk