Ejemplo n.º 1
0
    def add(self, *tags):
        str_tags = set(
            [t for t in tags if not isinstance(t, self.through.tag_model())])
        tag_objs = set(tags) - str_tags
        # If str_tags has 0 elements Django actually optimizes that to not do a
        # query.  Malcolm is very smart.
        if len(str_tags) > 0:
            # Assuming tags are [foo, bar]: do an insensitive regexp search for
            # (^foo$|^bar$). This is necessary b/c mysql will match accents on
            # characters with IN syntax. ie: vidéo == video
            existing = self.through.tag_model().objects.filter(
                name__iregex=r'(%s)' %
                '|'.join(['^%s$' % re.escape(x) for x in str_tags]))
        else:
            existing = {}
        tag_objs.update(existing)

        for new_tag in str_tags - set(t.name for t in existing):
            tag_objs.add(self.through.tag_model().objects.create(name=new_tag))

        for tag in tag_objs:
            self.through.objects.get_or_create(tag=tag,
                                               **self._lookup_kwargs())

        tags_add.send(sender=self.instance.__class__,
                      instance=self.instance,
                      tags=tag_objs)
Ejemplo n.º 2
0
    def add(self, *tags):
        str_tags = set([
            t
            for t in tags
            if not isinstance(t, self.through.tag_model())
        ])
        tag_objs = set(tags) - str_tags
        # If str_tags has 0 elements Django actually optimizes that to not do a
        # query.  Malcolm is very smart.
        if len(str_tags) > 0:
            # Assuming tags are [foo, bar]: do an insensitive regexp search for
            # (^foo$|^bar$). This is necessary b/c mysql will match accents on
            # characters with IN syntax. ie: vidéo == video
            existing = self.through.tag_model().objects.filter(
                name__iregex = r'(%s)' % '|'.join(
                    ['^%s$' % re.escape(x) for x in str_tags])
                )
        else:
            existing = {}
        tag_objs.update(existing)

        for new_tag in str_tags - set(t.name for t in existing):
            tag_objs.add(self.through.tag_model().objects.create(name=new_tag))

        for tag in tag_objs:
            self.through.objects.get_or_create(tag=tag, **self._lookup_kwargs())

        tags_add.send(sender=self.instance.__class__, instance=self.instance,
            tags=tag_objs)
Ejemplo n.º 3
0
    def add(self, *tags):
        str_tags = set([
            t
            for t in tags
            if not isinstance(t, self.through.tag_model())
        ])
        tag_objs = set(tags) - str_tags
        # If str_tags has 0 elements Django actually optimizes that to not do a
        # query.  Malcolm is very smart.
        existing = self.through.tag_model().objects.filter(
            name__in=str_tags
        )
        tag_objs.update(existing)

        for new_tag in str_tags - set(t.name for t in existing):
            tag_objs.add(self.through.tag_model().objects.create(name=new_tag))

        for tag in tag_objs:
            self.through.objects.get_or_create(tag=tag, **self._lookup_kwargs())

        tags_add.send(sender=self.instance.__class__, instance=self.instance, tags=tag_objs)