refactor add_subject

pull/94/head
eric 2019-11-30 18:00:32 -05:00
parent 4ec9b73a1a
commit c69de41628
5 changed files with 22 additions and 23 deletions

View File

@ -13,6 +13,17 @@ class BisacHeading(MPTTModel):
def __unicode__(self):
return self.full_label
def interpret_notation(notation):
#translate a notation
if notation:
try:
bisac_heading = BisacHeading.objects.get(notation=notation)
return bisac_heading.full_label
except BisacHeading.DoesNotExist:
pass
return notation
def populate_bisac_headings():
for key in bisac.keys():

View File

@ -102,13 +102,6 @@ def get_subjects(book):
logger.warning("Please add BISAC %s", code)
return subjects
def add_subject(subject_name, work, authority=''):
try:
subject = Subject.objects.get(name=subject_name)
except Subject.DoesNotExist:
subject = Subject.objects.create(name=subject_name, authority=authority)
subject.works.add(work)
def get_title(book):
title = book.get('FullTitle', '') #UMICH
if title:

View File

@ -2,7 +2,7 @@ import re
from django.core.management.base import BaseCommand
from regluit.bisac.models import BisacHeading
from regluit.core.models import Subject
from regluit.core.loaders.utils import add_subject
bisac_pattern = re.compile(r'[A-Z]{3}\d+')
@ -19,7 +19,7 @@ class Command(BaseCommand):
bisac_heading = BisacHeading.objects.get(notation=bisac_code)
for work in subject.works.all():
while bisac_heading:
add_subject(bisac_heading.full_label, work, authority="bisacsh")
Subject.set_by_name(bisac_heading.full_label, work, authority="bisacsh")
bisac_heading = bisac_heading.parent
subject.delete()
except BisacHeading.DoesNotExist:

View File

@ -29,6 +29,7 @@ import regluit
from regluit.marc.models import MARCRecord as NewMARC
from questionnaire.models import Landing
from regluit.bisac.models import interpret_notation
from regluit.core import mobi
import regluit.core.cc as cc
from regluit.core.epub import test_epub
@ -796,6 +797,8 @@ class Relator(models.Model):
except Relation.DoesNotExist:
logger.warning("relation not found: code = %s" % relation_code)
AUTHMATCH = re.compile(r'\s*!([a-z]+):?\s+(.*)')
class Subject(models.Model):
created = models.DateTimeField(auto_now_add=True)
name = models.CharField(max_length=200, unique=True)
@ -820,7 +823,7 @@ class Subject(models.Model):
cls.set_by_name(additional_subject, work, authority)
subject = subjects[0]
# make sure there's no heading
headingmatch = re.match(r'^!(.+):(.+)', subject)
headingmatch = AUTHMATCH.match(subject)
if headingmatch:
subject = headingmatch.group(2).strip()
authority = headingmatch.group(1).strip()
@ -832,7 +835,8 @@ class Subject(models.Model):
subject = subject[6:].split('=')[0].replace('_', ' ').strip().capitalize()
subject = 'Award Winner - {}'.format(subject)
authority = 'award'
if authority == 'bisacsh':
subject = interpret_notation(subject)
if valid_subject(subject):
(subject_obj, created) = cls.objects.get_or_create(name=subject)
if not subject_obj.authority and authority:

View File

@ -23,6 +23,7 @@ from regluit.core.parameters import WORK_IDENTIFIERS
from regluit.core.loaders import add_by_webpage
from regluit.core.loaders.doab import add_by_doab
from regluit.core.loaders.utils import ids_from_urls
from regluit.core.models import Subject
from regluit.frontend.forms import EditionForm, IdentifierForm
from .rh_views import user_is_rh
@ -56,16 +57,6 @@ def safe_get_work(work_id):
raise Http404
return work
def add_subject(subject_name, work, authority=''):
'''
add a subject to a work
'''
try:
subject = models.Subject.objects.get(name=subject_name)
except models.Subject.DoesNotExist:
subject = models.Subject.objects.create(name=subject_name, authority=authority)
subject.works.add(work)
def get_edition(edition_id):
'''
get edition and 404 if not found
@ -336,10 +327,10 @@ def edit_edition(request, work_id, edition_id, by=None):
if form.cleaned_data.has_key('bisac'):
bisacsh = form.cleaned_data['bisac']
while bisacsh:
add_subject(bisacsh.full_label, work, authority="bisacsh")
Subject.set_by_name(bisacsh.full_label, work, authority="bisacsh")
bisacsh = bisacsh.parent
for subject_name in edition.new_subjects:
add_subject(subject_name, work)
Subject.set_by_name(subject_name, work)
work_url = reverse('work', kwargs={'work_id': edition.work_id})
cover_file = form.cleaned_data.get("coverfile", None)
if cover_file: