From d1c811555c5733b767a4153a66ce27ee0b9e4a77 Mon Sep 17 00:00:00 2001 From: Daniel Quinn Date: Mon, 11 Jan 2016 12:52:19 +0000 Subject: [PATCH] Created a Sender model --- src/documents/admin.py | 3 +- src/documents/migrations/0003_sender.py | 56 +++++++++++++++++++++++++ src/documents/models.py | 17 +++++++- 3 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 src/documents/migrations/0003_sender.py diff --git a/src/documents/admin.py b/src/documents/admin.py index 9e5c934b9..d4249bfd9 100644 --- a/src/documents/admin.py +++ b/src/documents/admin.py @@ -2,7 +2,7 @@ from django.contrib import admin from django.core.urlresolvers import reverse from django.templatetags.static import static -from .models import Document +from .models import Sender, Document class DocumentAdmin(admin.ModelAdmin): @@ -26,4 +26,5 @@ class DocumentAdmin(admin.ModelAdmin): ) pdf.allow_tags = True +admin.site.register(Sender) admin.site.register(Document, DocumentAdmin) diff --git a/src/documents/migrations/0003_sender.py b/src/documents/migrations/0003_sender.py new file mode 100644 index 000000000..7a08502d0 --- /dev/null +++ b/src/documents/migrations/0003_sender.py @@ -0,0 +1,56 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9 on 2016-01-11 12:21 +from __future__ import unicode_literals + +from django.db import migrations, models +from django.template.defaultfilters import slugify + +import django.db.models.deletion + + +DOCUMENT_SENDER_MAP = {} + + +def move_sender_strings_to_sender_model(apps, schema_editor): + + sender_model = apps.get_model("documents", "Sender") + document_model = apps.get_model("documents", "Document") + + # Create the sender and log the relationship with the document + for document in document_model.objects.all(): + if document.sender: + DOCUMENT_SENDER_MAP[document.pk], created = sender_model.objects.get_or_create( + name=document.sender, + defaults={"slug": slugify(document.sender)} + ) + + +def realign_senders(apps, schema_editor): + document_model = apps.get_model("documents", "Document") + for pk, sender in DOCUMENT_SENDER_MAP.items(): + document_model.objects.filter(pk=pk).update(sender=sender) + + +class Migration(migrations.Migration): + + dependencies = [ + ('documents', '0002_auto_20151226_1316'), + ] + + operations = [ + migrations.CreateModel( + name='Sender', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=128, unique=True)), + ('slug', models.SlugField()), + ], + ), + migrations.RunPython(move_sender_strings_to_sender_model), + migrations.AlterField( + model_name='document', + name='sender', + field=models.ForeignKey(blank=True, on_delete=django.db.models.deletion.CASCADE, to='documents.Sender'), + ), + migrations.RunPython(realign_senders), + ] diff --git a/src/documents/models.py b/src/documents/models.py index e39397b1b..666457f6f 100644 --- a/src/documents/models.py +++ b/src/documents/models.py @@ -2,12 +2,27 @@ import os from django.conf import settings from django.db import models +from django.template.defaultfilters import slugify from django.utils import timezone +class Sender(models.Model): + + name = models.CharField(max_length=128, unique=True) + slug = models.SlugField() + + def save(self, *args, **kwargs): + if not self.slug: + self.slug = slugify(self.name) + models.Model.save(self, *args, **kwargs) + + def __str__(self): + return self.name + + class Document(models.Model): - sender = models.CharField(max_length=128, blank=True, db_index=True) + sender = models.ForeignKey(Sender, blank=True) title = models.CharField(max_length=128, blank=True, db_index=True) content = models.TextField(db_index=True) created = models.DateTimeField(default=timezone.now, editable=False)