コード例 #1
0
    def update(self, request, *args, **kwargs):
        """
        Override to remove resource term links if resource type is
        removed. Note that partial_update will call this function too.
        """
        vocab = self.get_object()
        new_types = self.request.data.get('learning_resource_types', None)
        if new_types is not None:
            # Since LearningResourceTypes indicate which relationships
            # are valid between Terms and LearningResources, we need to
            # remove the newly invalid relationships caused by this update.
            old_types = set(
                t.name for t in vocab.learning_resource_types.all()
            )
            removed_types = old_types - set(new_types)

            resource_ids_to_reindex = []
            with transaction.atomic():
                for term in vocab.term_set.all():
                    for resource in term.learning_resources.all():
                        if (resource.learning_resource_type.name in
                                removed_types):
                            resource_ids_to_reindex.append(resource.id)
                            term.learning_resources.remove(resource)
            if len(resource_ids_to_reindex) > 0:
                index_resources.delay(resource_ids_to_reindex)

        return super(VocabularyDetail, self).update(
            request, *args, **kwargs)
コード例 #2
0
 def delete(self, request, *args, **kwargs):
     """
     Override delete to also update index for deleted term.
     """
     term = self.get_object()
     resource_ids = list(LearningResource.objects.filter(
         terms__id=term.id
     ).values_list("id", flat=True))
     ret = super(TermDetail, self).delete(request, *args, **kwargs)
     index_resources.delay(resource_ids)
     return ret