load bisac headings into mptt tree table

Requires django-mptt
table migration
pull/1/head
eric 2015-09-09 22:23:51 -04:00
parent a82c45644a
commit 85284f7fca
8 changed files with 98 additions and 1 deletions

View File

View File

View File

@ -0,0 +1,12 @@
import string
from django.core.management.base import BaseCommand
from regluit.bisac.models import populate_bisac_headings,attach_dangling_branches
class Command(BaseCommand):
help = "build the bisac heading db"
def handle(self, **options):
populate_bisac_headings()
attach_dangling_branches()
print "bisac table is ready"

View File

@ -0,0 +1,46 @@
# -*- coding: utf-8 -*-
import datetime
from south.db import db
from south.v2 import SchemaMigration
from django.db import models
class Migration(SchemaMigration):
def forwards(self, orm):
# Adding model 'BisacHeading'
db.create_table('bisac_bisacheading', (
('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
('full_label', self.gf('django.db.models.fields.CharField')(unique=True, max_length=100)),
('label', self.gf('django.db.models.fields.CharField')(max_length=60)),
('notation', self.gf('django.db.models.fields.CharField')(max_length=9)),
('parent', self.gf('mptt.fields.TreeForeignKey')(blank=True, related_name='children', null=True, to=orm['bisac.BisacHeading'])),
(u'lft', self.gf('django.db.models.fields.PositiveIntegerField')(db_index=True)),
(u'rght', self.gf('django.db.models.fields.PositiveIntegerField')(db_index=True)),
(u'tree_id', self.gf('django.db.models.fields.PositiveIntegerField')(db_index=True)),
(u'level', self.gf('django.db.models.fields.PositiveIntegerField')(db_index=True)),
))
db.send_create_signal('bisac', ['BisacHeading'])
def backwards(self, orm):
# Deleting model 'BisacHeading'
db.delete_table('bisac_bisacheading')
models = {
'bisac.bisacheading': {
'Meta': {'object_name': 'BisacHeading'},
'full_label': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '100'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'label': ('django.db.models.fields.CharField', [], {'max_length': '60'}),
u'level': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}),
u'lft': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}),
'notation': ('django.db.models.fields.CharField', [], {'max_length': '9'}),
'parent': ('mptt.fields.TreeForeignKey', [], {'blank': 'True', 'related_name': "'children'", 'null': 'True', 'to': "orm['bisac.BisacHeading']"}),
u'rght': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}),
u'tree_id': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'})
}
}
complete_apps = ['bisac']

View File

36
bisac/models.py Normal file
View File

@ -0,0 +1,36 @@
from django.db import models
from mptt.models import MPTTModel, TreeForeignKey
from . import bisac
class BisacHeading(MPTTModel):
full_label = models.CharField(max_length=100, unique=True)
label = models.CharField(max_length=60, unique=False)
notation = models.CharField(max_length=9, unique=False)
parent = TreeForeignKey('self', null=True, blank=True, related_name='children', db_index=True)
class MPTTMeta:
order_insertion_by = ['notation']
def populate_bisac_headings():
for key in bisac.keys():
full_label = key[0:-10] if key.endswith(' / General') else key
(heading, created)= BisacHeading.objects.get_or_create(full_label = full_label)
cats = full_label.split('/')
heading.label = cats[-1].strip()
heading.notation = bisac[key]['notation']
if len(cats)>1:
parent_label = '/'.join(cats[0:-1]).strip()
(heading.parent, created) = BisacHeading.objects.get_or_create(full_label = parent_label)
heading.save()
def attach_dangling_branches():
# there was no "General" heading to link to
for heading in BisacHeading.objects.filter(notation = "", parent = None):
cats = heading.full_label.split('/')
heading.label = cats[-1].strip()
if len(cats)>1:
parent_label = '/'.join(cats[0:-1]).strip()
(heading.parent, created) = BisacHeading.objects.get_or_create(full_label = parent_label)
heading.save()

View File

@ -30,6 +30,7 @@ django-extensions==0.9
django-jsonfield==0.9.10
django-kombu==0.9.4
django-maintenancemode==0.10
django-mptt==0.7.4
django-nose-selenium==0.7.3
#django-notification==0.2
git+git://github.com/aladagemre/django-notification.git@2927346f4c513a217ac8ad076e494dd1adbf70e1

View File

@ -131,6 +131,7 @@ INSTALLED_APPS = (
'django_extensions',
'regluit.frontend',
'regluit.api',
'regluit.bisac',
'regluit.core',
'regluit.marc',
'regluit.payment',
@ -147,7 +148,8 @@ INSTALLED_APPS = (
'email_change',
'ckeditor',
'storages',
'sorl.thumbnail',
'sorl.thumbnail',
'mptt',
# this must appear *after* django.frontend or else it overrides the
# registration templates in frontend/templates/registration
'django.contrib.admin',