Add labels (#9)

This commit is contained in:
Daniel Quinn 2016-01-23 04:40:35 +00:00
parent 976cbdf112
commit 669cf1cb70
3 changed files with 82 additions and 7 deletions

View File

@ -2,15 +2,15 @@ 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 Sender, Document from .models import Sender, Tag, Document
class DocumentAdmin(admin.ModelAdmin): class DocumentAdmin(admin.ModelAdmin):
search_fields = ("sender__name", "title", "content",) search_fields = ("sender__name", "title", "content",)
list_display = ("edit", "created", "sender", "title", "pdf") list_display = ("edit", "created", "sender", "title", "tags_", "pdf")
list_filter = ("created", "sender") list_filter = ("created", "tags", "sender")
save_on_top = True list_per_page = 25
def edit(self, obj): def edit(self, obj):
return '<img src="{}" width="22" height="22" alt="Edit icon" />'.format( return '<img src="{}" width="22" height="22" alt="Edit icon" />'.format(
@ -26,5 +26,16 @@ class DocumentAdmin(admin.ModelAdmin):
) )
pdf.allow_tags = True pdf.allow_tags = True
def tags_(self, obj):
r = ""
for tag in obj.tags.all():
r += '<span style="padding: 0 0.5em; background-color: {}; color: #ffffff; border-radius: 0.2em; margin: 1px; display: inline-block;">{}</span>'.format(
tag.get_colour_display(),
tag.slug
)
return r
tags_.allow_tags = True
admin.site.register(Sender) admin.site.register(Sender)
admin.site.register(Tag)
admin.site.register(Document, DocumentAdmin) admin.site.register(Document, DocumentAdmin)

View File

@ -0,0 +1,37 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9 on 2016-01-23 04:30
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('documents', '0005_auto_20160123_0313'),
]
operations = [
migrations.CreateModel(
name='Tag',
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(blank=True)),
('colour', models.PositiveIntegerField(choices=[(1, '#a6cee3'), (2, '#1f78b4'), (3, '#b2df8a'), (4, '#33a02c'), (5, '#fb9a99'), (6, '#e31a1c'), (7, '#fdbf6f'), (8, '#ff7f00'), (9, '#cab2d6'), (10, '#6a3d9a'), (11, '#ffff99'), (12, '#b15928'), (13, '#000000'), (14, '#cccccc')], default=1)),
],
options={
'abstract': False,
},
),
migrations.AlterField(
model_name='sender',
name='slug',
field=models.SlugField(blank=True),
),
migrations.AddField(
model_name='document',
name='tags',
field=models.ManyToManyField(related_name='documents', to='documents.Tag'),
),
]

View File

@ -6,13 +6,13 @@ from django.template.defaultfilters import slugify
from django.utils import timezone from django.utils import timezone
class Sender(models.Model): class SluggedModel(models.Model):
name = models.CharField(max_length=128, unique=True) name = models.CharField(max_length=128, unique=True)
slug = models.SlugField() slug = models.SlugField(blank=True)
class Meta(object): class Meta(object):
ordering = ("name",) abstract = True
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
if not self.slug: if not self.slug:
@ -23,12 +23,39 @@ class Sender(models.Model):
return self.name return self.name
class Sender(SluggedModel):
class Meta(object):
ordering = ("name",)
class Tag(SluggedModel):
COLOURS = (
(1, "#a6cee3"),
(2, "#1f78b4"),
(3, "#b2df8a"),
(4, "#33a02c"),
(5, "#fb9a99"),
(6, "#e31a1c"),
(7, "#fdbf6f"),
(8, "#ff7f00"),
(9, "#cab2d6"),
(10, "#6a3d9a"),
(11, "#b15928"),
(12, "#000000"),
(13, "#cccccc")
)
colour = models.PositiveIntegerField(choices=COLOURS, default=1)
class Document(models.Model): class Document(models.Model):
sender = models.ForeignKey( sender = models.ForeignKey(
Sender, blank=True, null=True, related_name="documents") Sender, blank=True, null=True, related_name="documents")
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)
tags = models.ManyToManyField(Tag, related_name="documents")
created = models.DateTimeField(default=timezone.now, editable=False) created = models.DateTimeField(default=timezone.now, editable=False)
modified = models.DateTimeField(auto_now=True, editable=False) modified = models.DateTimeField(auto_now=True, editable=False)