From 85284f7fcaffa30945d09508212e81a8ffd80f35 Mon Sep 17 00:00:00 2001 From: eric Date: Wed, 9 Sep 2015 22:23:51 -0400 Subject: [PATCH] load bisac headings into mptt tree table Requires django-mptt table migration --- bisac/management/__init__.py | 0 bisac/management/commands/__init__.py | 0 bisac/management/commands/load_bisac.py | 12 +++++++ bisac/migrations/0001_initial.py | 46 +++++++++++++++++++++++++ bisac/migrations/__init__.py | 0 bisac/models.py | 36 +++++++++++++++++++ requirements_versioned.pip | 1 + settings/common.py | 4 ++- 8 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 bisac/management/__init__.py create mode 100644 bisac/management/commands/__init__.py create mode 100644 bisac/management/commands/load_bisac.py create mode 100644 bisac/migrations/0001_initial.py create mode 100644 bisac/migrations/__init__.py create mode 100644 bisac/models.py diff --git a/bisac/management/__init__.py b/bisac/management/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/bisac/management/commands/__init__.py b/bisac/management/commands/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/bisac/management/commands/load_bisac.py b/bisac/management/commands/load_bisac.py new file mode 100644 index 00000000..123e4e5e --- /dev/null +++ b/bisac/management/commands/load_bisac.py @@ -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" diff --git a/bisac/migrations/0001_initial.py b/bisac/migrations/0001_initial.py new file mode 100644 index 00000000..0038905c --- /dev/null +++ b/bisac/migrations/0001_initial.py @@ -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'] \ No newline at end of file diff --git a/bisac/migrations/__init__.py b/bisac/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/bisac/models.py b/bisac/models.py new file mode 100644 index 00000000..5d3dca55 --- /dev/null +++ b/bisac/models.py @@ -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() + + diff --git a/requirements_versioned.pip b/requirements_versioned.pip index 9d896be0..cd31093b 100644 --- a/requirements_versioned.pip +++ b/requirements_versioned.pip @@ -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 diff --git a/settings/common.py b/settings/common.py index 572d0d16..fa9773ca 100644 --- a/settings/common.py +++ b/settings/common.py @@ -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',