Created a Sender model

This commit is contained in:
Daniel Quinn 2016-01-11 12:52:19 +00:00
parent 680b084203
commit d1c811555c
3 changed files with 74 additions and 2 deletions

View File

@ -2,7 +2,7 @@ from django.contrib import admin
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.templatetags.static import static from django.templatetags.static import static
from .models import Document from .models import Sender, Document
class DocumentAdmin(admin.ModelAdmin): class DocumentAdmin(admin.ModelAdmin):
@ -26,4 +26,5 @@ class DocumentAdmin(admin.ModelAdmin):
) )
pdf.allow_tags = True pdf.allow_tags = True
admin.site.register(Sender)
admin.site.register(Document, DocumentAdmin) admin.site.register(Document, DocumentAdmin)

View File

@ -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),
]

View File

@ -2,12 +2,27 @@ import os
from django.conf import settings from django.conf import settings
from django.db import models from django.db import models
from django.template.defaultfilters import slugify
from django.utils import timezone 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): 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) title = models.CharField(max_length=128, blank=True, db_index=True)
content = models.TextField(db_index=True) content = models.TextField(db_index=True)
created = models.DateTimeField(default=timezone.now, editable=False) created = models.DateTimeField(default=timezone.now, editable=False)