Skip to content
This repository has been archived by the owner on Feb 16, 2023. It is now read-only.

Commit

Permalink
Add labels (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielquinn committed Jan 23, 2016
1 parent 976cbdf commit 669cf1c
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 7 deletions.
19 changes: 15 additions & 4 deletions src/documents/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
from django.core.urlresolvers import reverse
from django.templatetags.static import static

from .models import Sender, Document
from .models import Sender, Tag, Document


class DocumentAdmin(admin.ModelAdmin):

search_fields = ("sender__name", "title", "content",)
list_display = ("edit", "created", "sender", "title", "pdf")
list_filter = ("created", "sender")
save_on_top = True
list_display = ("edit", "created", "sender", "title", "tags_", "pdf")
list_filter = ("created", "tags", "sender")
list_per_page = 25

def edit(self, obj):
return '<img src="{}" width="22" height="22" alt="Edit icon" />'.format(
Expand All @@ -26,5 +26,16 @@ def pdf(self, obj):
)
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(Tag)
admin.site.register(Document, DocumentAdmin)
37 changes: 37 additions & 0 deletions src/documents/migrations/0006_auto_20160123_0430.py
Original file line number Diff line number Diff line change
@@ -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'),
),
]
33 changes: 30 additions & 3 deletions src/documents/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
from django.utils import timezone


class Sender(models.Model):
class SluggedModel(models.Model):

name = models.CharField(max_length=128, unique=True)
slug = models.SlugField()
slug = models.SlugField(blank=True)

class Meta(object):
ordering = ("name",)
abstract = True

def save(self, *args, **kwargs):
if not self.slug:
Expand All @@ -23,12 +23,39 @@ def __str__(self):
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):

sender = models.ForeignKey(
Sender, blank=True, null=True, related_name="documents")
title = models.CharField(max_length=128, blank=True, db_index=True)
content = models.TextField(db_index=True)
tags = models.ManyToManyField(Tag, related_name="documents")
created = models.DateTimeField(default=timezone.now, editable=False)
modified = models.DateTimeField(auto_now=True, editable=False)

Expand Down

0 comments on commit 669cf1c

Please sign in to comment.