def test_util_tags_resources_tool_filtered_data(tag_matrix, tag_test_kwargs): # tests getting resources for given filters name, kwargs = tag_test_kwargs resources_tool = TagsResourcesTool(**kwargs) expected = resources_tool.data_manager.all() if 'projects' in kwargs: expected = expected.filter(project__in=kwargs['projects']) if 'locales' in kwargs: expected = expected.filter( translatedresources__locale__in=kwargs["locales"] ).distinct() if 'slug' in kwargs: expected = expected.filter( tag__slug__regex=glob_to_regex(kwargs['slug']) ).distinct() if kwargs.get("path"): expected = list( t.pk for t in expected.order_by('pk') if fnmatch.fnmatch(t.path, kwargs["path"]) ) else: expected = list(expected.order_by('pk').values_list('pk', flat=True)) result = resources_tool.filtered_data assert isinstance(result, QuerySet) assert ( sorted(list(result.values_list('pk', flat=True))) == expected )
def test_util_tags_resources_tool_filtered_data(tag_matrix, tag_test_kwargs): # tests getting resources for given filters name, kwargs = tag_test_kwargs resources_tool = TagsResourcesTool(**kwargs) expected = resources_tool.data_manager.all() if 'projects' in kwargs: expected = expected.filter(project__in=kwargs['projects']) if 'locales' in kwargs: expected = expected.filter( translatedresources__locale__in=kwargs["locales"] ).distinct() if 'slug' in kwargs: expected = expected.filter( tag__slug__regex=glob_to_regex(kwargs['slug']) ).distinct() if kwargs.get("path"): expected = list( t.pk for t in expected.order_by('pk') if fnmatch.fnmatch(t.path, kwargs["path"]) ) else: expected = list(expected.order_by('pk').values_list('pk', flat=True)) result = resources_tool.filtered_data assert isinstance(result, QuerySet) assert ( sorted(list(result.values_list('pk', flat=True))) == expected )
def get_tags(self, slug=None): """Get `values` of associated tags, filtering by slug if given """ tags = self.tag_manager.filter(project__in=self.projects).values( "pk", "name", "slug", "priority", "project") if slug: return tags.filter(slug__regex=glob_to_regex(slug)) return tags
def get_tags(self, slug=None): """Get `values` of associated tags, filtering by slug if given """ tags = self.tag_manager.filter(project__in=self.projects).values( "pk", "name", "slug", "priority", "project") if slug: return tags.filter(slug__regex=glob_to_regex(slug)) return tags
def _clean_paths_for_select(self): """Returns a list of paths that can be un/linked for a given search filter """ qs = self.resources if self.cleaned_data['search']: qs = qs.filter( path__regex=glob_to_regex(self.cleaned_data['search'])) return qs.values_list('path')
def find(self, glob, include=None, exclude=None): """ Find filtered resources for a glob match, and optionally include/exclude resources linked to given tags """ if include: resources = self.resource_manager.filter(tag__slug=include) elif exclude: resources = self.resource_manager.exclude(tag__slug=exclude) else: resources = self.resource_manager if self.projects: resources = resources.filter(project__in=self.projects) return resources.filter(path__regex=glob_to_regex(glob))
def test_util_glob_to_regex_db(resource_a, resource_b): assert resource_a in Resource.objects.filter( path__regex=glob_to_regex('*')) assert resource_b in Resource.objects.filter( path__regex=glob_to_regex('*')) assert (list(Resource.objects.filter( path__regex=glob_to_regex('*'))) == list(Resource.objects.all())) assert resource_a in Resource.objects.filter( path__regex=glob_to_regex('**')) assert resource_b in Resource.objects.filter( path__regex=glob_to_regex('**')) assert (list(Resource.objects.filter( path__regex=glob_to_regex('**'))) == list(Resource.objects.all())) assert (resource_a in Resource.objects.filter(path__regex=glob_to_regex('*a*'))) assert (resource_b not in Resource.objects.filter(path__regex=glob_to_regex('*a*'))) assert (list( Resource.objects.filter(path__regex=glob_to_regex('*a*'))) == list( Resource.objects.filter(path__contains='a')))
def test_util_glob_to_regex_db(resource0, resource1): assert resource0 in Resource.objects.filter(path__regex=glob_to_regex('*')) assert resource1 in Resource.objects.filter(path__regex=glob_to_regex('*')) assert (list(Resource.objects.filter( path__regex=glob_to_regex('*'))) == list(Resource.objects.all())) assert (resource0 in Resource.objects.filter(path__regex=glob_to_regex('*0*'))) assert (resource1 not in Resource.objects.filter(path__regex=glob_to_regex('*0*'))) assert (list( Resource.objects.filter(path__regex=glob_to_regex('*0*'))) == list( Resource.objects.filter(path__contains='0')))
def filter_tag(self, trs): """Filters on tag.slug and tag.priority """ q = Q() if not self.slug: # if slug is not specified, then just remove all resources # that have no tag q &= ~Q(resource__tag__isnull=True) if self.slug: q &= Q(resource__tag__slug__regex=glob_to_regex(self.slug)) if self.priority is not None: if self.priority is False: # if priority is False, exclude tags with priority q &= Q(resource__tag__priority__isnull=True) elif self.priority is True: # if priority is True show only tags with priority q &= Q(resource__tag__priority__isnull=False) elif isinstance(self.priority, int): # if priority is an int, filter on that priority q &= Q(resource__tag__priority=self.priority) return trs.filter(q)
def filter_tag(self, trs): """Filters on tag.slug and tag.priority """ q = Q() if not self.slug: # if slug is not specified, then just remove all resources # that have no tag q &= ~Q(resource__tag__isnull=True) if self.slug: q &= Q(resource__tag__slug__regex=glob_to_regex(self.slug)) if self.priority is not None: if self.priority is False: # if priority is False, exclude tags with priority q &= Q(resource__tag__priority__isnull=True) elif self.priority is True: # if priority is True show only tags with priority q &= Q(resource__tag__priority__isnull=False) elif isinstance(self.priority, int): # if priority is an int, filter on that priority q &= Q(resource__tag__priority=self.priority) return trs.filter(q)
def test_util_glob_to_regex_db(resource_a, resource_b): assert resource_a in Resource.objects.filter(path__regex=glob_to_regex('*')) assert resource_b in Resource.objects.filter(path__regex=glob_to_regex('*')) assert ( list(Resource.objects.filter(path__regex=glob_to_regex('*'))) == list(Resource.objects.all()) ) assert ( resource_a in Resource.objects.filter( path__regex=glob_to_regex('*a*') ) ) assert ( resource_b not in Resource.objects.filter( path__regex=glob_to_regex('*a*') ) ) assert ( list(Resource.objects.filter(path__regex=glob_to_regex('*a*'))) == list(Resource.objects.filter(path__contains='a')) )
def test_util_glob_to_regex(): assert glob_to_regex('*') == '^([^/]*)$' assert glob_to_regex('*foo') == '^([^/]*)foo$' assert glob_to_regex('*foo*') == '^([^/]*)foo([^/]*)$'
def filter_path(self, trs): return (trs.filter( resource__path__regex=glob_to_regex(self.path)).distinct() if self.path else trs)
def test_util_glob_to_regex_unsupported_variables(): """Raise an error if the user tries to use variables in the glob expression.""" with pytest.raises(ValueError): glob_to_regex('{ variable }/smth')
def test_util_glob_to_regex(): assert glob_to_regex('*') == '^.*$' assert glob_to_regex('/foo*') == '^\\/foo.*$' assert glob_to_regex('*foo') == '^.*foo$' assert glob_to_regex('*foo*') == '^.*foo.*$'
def test_util_glob_to_regex_python3(): assert glob_to_regex('bar/**/foo*') == '^bar/(.+/)?foo([^/]*)$'
def filter_slug(self, resources): return (resources.filter(tag__slug__regex=glob_to_regex(self.slug)) if self.slug else resources)
def filter_path(self, resources): return (resources.filter( path__regex=glob_to_regex(self.path)) if self.path else resources)
def test_util_glob_to_regex(): assert glob_to_regex('*') == '^.*$' assert glob_to_regex('/foo*') == '^\\/foo.*$' assert glob_to_regex('*foo') == '^.*foo$' assert glob_to_regex('*foo*') == '^.*foo.*$'
def filter_path(self, trs): return ( trs.filter( resource__path__regex=glob_to_regex(self.path)).distinct() if self.path else trs)