def get_tags(tags, create=False, str_pk=False): if isinstance(tags, (str, unicode)): tags = parse_tags(tags) tag_set = set() names = set() tag_lookups = set() for tag in tags: if isinstance(tag, Tag): tag_set.add(tag) if create and not tag.pk: tag.save() elif isinstance(tag, basestring): try: tag_lookups.add(models.Q(pk=int(tag))) except ValueError: if create: names.add(tag) tag_lookups.add(models.Q(name=tag)) elif isinstance(tag, (int, long)): tag_lookups.add(models.Q(pk=tag)) if tag_lookups: for tag in Tag.objects.filter(reduce_or(tag_lookups)): tag_set.add(tag) for name in names.difference(tag.name for tag in tag_set): tag_set.add(Tag.objects.create(name=name)) return tag_set
def add_to_query(self, query, aliases=None): if self.trivial: return elif self.trivial is False: force_empty(query) elif self.use_aggregation and len(self.conjunction) > 1 and max(len(disj) for disj in self.conjunction) == 1 and not self.contains_negation(): conj = [iter(disj).next() for disj in self.conjunction] query.add_q(reduce_or(self.q_for_obj(obj, negated) for obj, negated in conj)) opts = query.model._meta if query.group_by is None: field_names = [f.attname for f in opts.fields] query.add_fields(field_names, False) query.set_group_by() query.add_aggregate(models.Count(self.field), query.model, 'mrj_count', is_summary=False) query.add_q(models.Q(mrj_count=len(conj))) else: for disj in self.conjunction: query.add_q(reduce_or(self.q_for_obj(obj, negated) for obj, negated in disj), set())
def tags(self): return reduce_or(qs.tags() for qs in self.querysets)
def get_term_q(self, request, term): field_q_objs = [] for field in self.search_fields: q = models.Q(**{"%s__%s" % (field, self.search_lookup): term}) field_q_objs.append(q) return reduce_or(field_q_objs)