[damned-lies] Add a new CategoryName model to replace hard-coded choices
- From: Claude Paroz <claudep src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [damned-lies] Add a new CategoryName model to replace hard-coded choices
- Date: Wed, 9 Sep 2015 16:42:03 +0000 (UTC)
commit bce56afcaa821d2e29d10b2d7a0e80ca35b95d47
Author: Claude Paroz <claude 2xlibre net>
Date: Wed Sep 9 17:39:14 2015 +0200
Add a new CategoryName model to replace hard-coded choices
stats/admin.py | 6 ++-
stats/migrations/0002_add_category_name.py | 30 +++++++++++++++
stats/migrations/0003_migrate_category_names.py | 46 +++++++++++++++++++++++
stats/models.py | 11 +++++
4 files changed, 92 insertions(+), 1 deletions(-)
---
diff --git a/stats/admin.py b/stats/admin.py
index 9d3e0fb..46db227 100644
--- a/stats/admin.py
+++ b/stats/admin.py
@@ -23,7 +23,10 @@ from django.contrib.admin import helpers
from django.shortcuts import render
from django.utils.encoding import force_unicode
from django import forms
-from stats.models import Statistics, Information, PoFile, Module, Branch, Domain, Category, Release
+from stats.models import (
+ Statistics, Information, PoFile, Module, Branch, Domain, Category,
+ CategoryName, Release,
+)
class BranchInline(admin.TabularInline):
model = Branch
@@ -130,6 +133,7 @@ admin.site.register(Statistics, StatisticsAdmin)
admin.site.register(PoFile, PoFileAdmin)
admin.site.register(Branch, BranchAdmin)
admin.site.register(Domain, DomainAdmin)
+admin.site.register(CategoryName)
admin.site.register(Category, CategoryAdmin)
admin.site.register(Module, ModuleAdmin)
admin.site.register(Release, ReleaseAdmin)
diff --git a/stats/migrations/0002_add_category_name.py b/stats/migrations/0002_add_category_name.py
new file mode 100644
index 0000000..d1ef0a2
--- /dev/null
+++ b/stats/migrations/0002_add_category_name.py
@@ -0,0 +1,30 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import models, migrations
+import django.db.models.deletion
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('stats', '0001_initial'),
+ ]
+
+ operations = [
+ migrations.CreateModel(
+ name='CategoryName',
+ fields=[
+ ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True,
primary_key=True)),
+ ('name', models.CharField(unique=True, max_length=30)),
+ ],
+ options={
+ 'db_table': 'categoryname',
+ },
+ ),
+ migrations.AddField(
+ model_name='category',
+ name='name_id',
+ field=models.ForeignKey(db_column='name_id', on_delete=django.db.models.deletion.PROTECT,
to='stats.CategoryName', null=True),
+ ),
+ ]
diff --git a/stats/migrations/0003_migrate_category_names.py b/stats/migrations/0003_migrate_category_names.py
new file mode 100644
index 0000000..1cac78e
--- /dev/null
+++ b/stats/migrations/0003_migrate_category_names.py
@@ -0,0 +1,46 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import models, migrations
+
+CATEGORY_CHOICES = (
+ ('default', 'Default'),
+ ('admin-tools', 'Administration Tools'),
+ ('dev-tools', 'Development Tools'),
+ ('desktop', 'GNOME Desktop'),
+ ('dev-platform', 'GNOME Developer Platform'),
+ ('proposed', 'New Module Proposals'),
+ ('g3-core', 'Core'),
+ ('g3-utils', 'Utils'),
+ ('g3-apps', 'Apps'),
+ ('g3-a11y', 'Accessibility'),
+ ('g3-games', 'Games'),
+ ('g3-backends', 'Backends'),
+ ('g3-core-libs', 'Core Libraries'),
+ ('g3-extra-libs', 'Extra Libraries'),
+ ('g2-legacy', 'Legacy Desktop'),
+ ('stable', 'Stable Branches'),
+ ('dev', 'Development Branches'),
+)
+
+
+def migrate_categs(apps, schema_editor):
+ Category = apps.get_model("stats", "Category")
+ CategoryName = apps.get_model("stats", "CategoryName")
+
+ for cat_key, cat_name in CATEGORY_CHOICES:
+ cn = CategoryName.objects.create(name=cat_name)
+ Category.objects.filter(name=cat_key).update(name_id=cn)
+ if cat_name == 'Development Tools':
+ Category.objects.filter(name='g3-dev-tools').update(name_id=cn)
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('stats', '0002_add_category_name'),
+ ]
+
+ operations = [
+ migrations.RunPython(migrate_categs),
+ ]
diff --git a/stats/models.py b/stats/models.py
index 83a1a9b..9c48531 100644
--- a/stats/models.py
+++ b/stats/models.py
@@ -1145,6 +1145,16 @@ class Release(models.Model):
return last_modif_date, lang_files
+class CategoryName(models.Model):
+ name = models.CharField(max_length=30, unique=True)
+
+ class Meta:
+ db_table = 'categoryname'
+
+ def __unicode__(self):
+ return self.name
+
+
CATEGORY_CHOICES = (
('default', ugettext_noop('Default')),
('admin-tools', ugettext_noop('Administration Tools')),
@@ -1169,6 +1179,7 @@ class Category(models.Model):
release = models.ForeignKey(Release)
branch = models.ForeignKey(Branch)
name = models.CharField(max_length=30, choices=CATEGORY_CHOICES, default='default')
+ name_id = models.ForeignKey(CategoryName, db_column='name_id', on_delete=models.PROTECT, null=True)
class Meta:
db_table = 'category'
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]