This repository has been archived by the owner on Jan 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #540 from mitodl/feature/skm/512_search_empty_facet
WIP Feature/skm/512 search empty facet
- Loading branch information
Showing
19 changed files
with
514 additions
and
112 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
learningresources/migrations/0015_backfill_curator_vocabularies.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import unicode_literals | ||
|
||
from django.db import models, migrations | ||
from django.utils.text import slugify | ||
|
||
from taxonomy.signals import create_default_vocabulary | ||
|
||
# pylint: skip-file | ||
|
||
VOCAB_NAME = "curation status" | ||
EMPTY_VALUE = "--not set--" | ||
|
||
|
||
def backfill_curator_vocabs(apps, schema_editor): | ||
"""Backfill auto-generated vocabulary to existing repos.""" | ||
Repository = apps.get_model("learningresources", "Repository") | ||
LearningResourceType = apps.get_model( | ||
"learningresources", "LearningResourceType") | ||
Vocabulary = apps.get_model("taxonomy", "Vocabulary") | ||
learning_resource_types = LearningResourceType.objects.all() | ||
for repo in Repository.objects.all().iterator(): | ||
if Vocabulary.objects.filter( | ||
name=VOCAB_NAME, repository__id=repo.id).exists(): | ||
continue | ||
vocab = Vocabulary.objects.create( | ||
repository_id=repo.id, | ||
name=VOCAB_NAME, description=VOCAB_NAME, | ||
required=True, vocabulary_type='m', | ||
weight=0, multi_terms=False, | ||
slug=slugify("{0}_{1}_backfill".format(VOCAB_NAME, repo.id)), | ||
) | ||
for resource_type in learning_resource_types: | ||
vocab.learning_resource_types.add(resource_type) | ||
create_terms(apps, vocab.id) | ||
backfill_resources(apps, repo.id) | ||
|
||
|
||
def create_terms(apps, vocab_id): | ||
"""Create the curation status terms for a newly-created Vocabulary.""" | ||
Term = apps.get_model("taxonomy", "Term") | ||
labels = ( | ||
'ready to use', 'tagged', 'course information', | ||
'discarded', 'hidden', EMPTY_VALUE, | ||
) | ||
for label in labels: | ||
Term.objects.create( | ||
vocabulary_id=vocab_id, | ||
label=label, | ||
slug=slugify("{0}_{1}_backfill".format(label, vocab_id)), | ||
weight=0, | ||
) | ||
|
||
|
||
def backfill_resources(apps, repo_id): | ||
"""Add terms to existing LearningResource instances.""" | ||
LearningResource = apps.get_model("learningresources", "LearningResource") | ||
Term = apps.get_model("taxonomy", "Term") | ||
empty_term = Term.objects.get( | ||
vocabulary__repository__id=repo_id, | ||
vocabulary__name=VOCAB_NAME, | ||
label=EMPTY_VALUE, | ||
) | ||
resources = LearningResource.objects.filter( | ||
course__repository__id=repo_id | ||
) | ||
for resource in resources.iterator(): | ||
resource.terms.add(empty_term) | ||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('learningresources', '0014_learning_resource_related_name'), | ||
('taxonomy', '0007_vocabulary_multi_terms'), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(backfill_curator_vocabs) | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.