def clean_article_ids(self): article_ids = self.cleaned_data["article_ids"].split("\n") article_ids = filter(bool, map(unicode.strip, article_ids)) # Parse all article ids as integer try: article_ids = map(int, article_ids) except ValueError: offender = repr(next(ifilterfalse(unicode.isnumeric, article_ids))) raise ValidationError("{offender} is not an integer".format(**locals())) # Check if they can be chosen articlesets = self.cleaned_data["articlesets"] distinct_args = ["id"] if db_supports_distinct_on() else [] all_articles = Article.objects.filter(articlesets_set__in=articlesets).distinct(*distinct_args) chosen_articles = Article.objects.filter(id__in=article_ids).distinct(*distinct_args) intersection = all_articles & chosen_articles if chosen_articles.count() != intersection.count(): # Find offenders (skipping non-existing, which we can only find when # fetching all possible articles) existing = all_articles.values_list("id", flat=True) offenders = chosen_articles.exclude(id__in=existing).values_list("id", flat=True) raise ValidationError( ("Articles {offenders} not in chosen articlesets or some non-existent" .format(**locals())), code="invalid") return article_ids
def __init__(self, project=None, data=None, *args, **kwargs): super(SelectionForm, self).__init__(data, *args, **kwargs) if data and "projects" in data and project is None: log.warning( "Passing 'projects' in `data` deprecated. Use project parameter on __init__ instead." ) project = Project.objects.get(id=data.pop("projects")[0]) elif project is None: raise ValueError("Project cannot be None") self.project = project codebooks = Codebook.objects.filter(project_id=project.id) self.fields['mediums'].queryset = self._get_mediums() self.fields['codebook'].queryset = codebooks self.fields['articlesets'].queryset = project.all_articlesets( ).order_by('-pk') distinct_args = ["id"] if db_supports_distinct_on() else [] self.fields['codebook_label_language'].queryset = self.fields[ 'codebook_replacement_language'].queryset = ( Language.objects.filter( labels__code__codebook_codes__codebook__in=codebooks). distinct(*distinct_args)) if data is not None: self.data = self.get_data()
def clean_article_ids(self): article_ids = self.cleaned_data["article_ids"].split("\n") article_ids = filter(bool, map(unicode.strip, article_ids)) # Parse all article ids as integer try: article_ids = map(int, article_ids) except ValueError: offender = repr(next(ifilterfalse(unicode.isnumeric, article_ids))) raise ValidationError( "{offender} is not an integer".format(**locals())) # Check if they can be chosen articlesets = self.cleaned_data["articlesets"] distinct_args = ["id"] if db_supports_distinct_on() else [] all_articles = Article.objects.filter( articlesets_set__in=articlesets).distinct(*distinct_args) chosen_articles = Article.objects.filter(id__in=article_ids).distinct( *distinct_args) intersection = all_articles & chosen_articles if chosen_articles.count() != intersection.count(): # Find offenders (skipping non-existing, which we can only find when # fetching all possible articles) existing = all_articles.values_list("id", flat=True) offenders = chosen_articles.exclude(id__in=existing).values_list( "id", flat=True) raise ValidationError(( "Articles {offenders} not in chosen articlesets or some non-existent" .format(**locals())), code="invalid") return article_ids
def __init__(self, project=None, data=None, *args, **kwargs): super(SelectionForm, self).__init__(data, *args, **kwargs) if data and "projects" in data and project is None: log.warning("Passing 'projects' in `data` deprecated. Use project parameter on __init__ instead.") project = Project.objects.get(id=data.pop("projects")[0]) elif project is None: raise ValueError("Project cannot be None") self.project = project codebooks = Codebook.objects.filter(Q(project_id=project.id)|Q(projects_set=project)) self.fields['mediums'].queryset = self._get_mediums() self.fields['codebook'].queryset = codebooks if data and data.get("output") == "html": self.fields['articlesets'].queryset = ArticleSet.objects.all() else: self.fields['articlesets'].queryset = project.all_articlesets().order_by('-pk') distinct_args = ["id"] if db_supports_distinct_on() else [] self.fields['codebook_label_language'].queryset = self.fields['codebook_replacement_language'].queryset = ( Language.objects.filter(labels__code__codebook_codes__codebook__in=codebooks).distinct(*distinct_args) ) if data is not None: self.data = self.get_data()
def get_queryset(articlesets, mediums=None, start_date=None, end_date=None, article_ids=None, projects=None, **kargs): queryset = article.Article.objects queryset = queryset.filter(articlesets_set__in=articlesets) # Disabling filtering on medium if mediums equals all # articles in this project. if projects and len(projects) == 1: p = projects[0] if isinstance(p, int): p = Project.objects.get(id=p) if set(p.get_mediums().values_list("id")) == set( mediums.values_list("id")): mediums = None if mediums and "WHERE" in unicode(mediums.query): queryset = queryset.filter(medium__in=mediums) if start_date: queryset = queryset.filter(date__gte=start_date) if end_date: queryset = queryset.filter(date__lt=end_date) if article_ids: queryset = queryset.filter(id__in=article_ids) if db_supports_distinct_on(): return queryset.distinct("pk") return queryset.distinct()
def test_db_supports_distinct_on(self): self.assertTrue(db_supports_distinct_on() in (True, False))