From 9ac7a5613544a535383c42817a9e8c20577d8da9 Mon Sep 17 00:00:00 2001 From: Piotr Dobrowolski Date: Wed, 15 Feb 2017 04:01:47 +0100 Subject: [PATCH] Initial tree support using django-tree --- docker-compose.yml | 2 +- postgres-hstore/Dockerfile | 3 ++ postgres-hstore/create_extension.sh | 12 ++++++++ spejstore/settings.py | 1 + storage/admin.py | 5 +++- storage/migrations/0002_auto_20170215_0115.py | 30 +++++++++++++++++++ storage/models.py | 19 +++++++++--- 7 files changed, 66 insertions(+), 6 deletions(-) create mode 100644 postgres-hstore/Dockerfile create mode 100644 postgres-hstore/create_extension.sh create mode 100644 storage/migrations/0002_auto_20170215_0115.py diff --git a/docker-compose.yml b/docker-compose.yml index 7fde322..b370e13 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,7 +1,7 @@ version: '2' services: db: - image: aidanlister/postgres-hstore + build: postgres-hstore web: build: . diff --git a/postgres-hstore/Dockerfile b/postgres-hstore/Dockerfile new file mode 100644 index 0000000..14e4514 --- /dev/null +++ b/postgres-hstore/Dockerfile @@ -0,0 +1,3 @@ +FROM postgres:latest +MAINTAINER Piotr Dobrowolski +ADD create_extension.sh docker-entrypoint-initdb.d/create_extension.sh diff --git a/postgres-hstore/create_extension.sh b/postgres-hstore/create_extension.sh new file mode 100644 index 0000000..65e36b1 --- /dev/null +++ b/postgres-hstore/create_extension.sh @@ -0,0 +1,12 @@ +# Because both template1 and the user postgres database have already been created, +# we need to create the hstore extension in template1 and then recreate the postgres database. +# +# Running CREATE EXTENSION in both template1 and postgres can lead to +# the extensions having different eid's. +gosu postgres psql --dbname template1 < ' + obj.name + admin.site.register(Item, ItemAdmin) admin.site.register(Category) diff --git a/storage/migrations/0002_auto_20170215_0115.py b/storage/migrations/0002_auto_20170215_0115.py new file mode 100644 index 0000000..b2083b2 --- /dev/null +++ b/storage/migrations/0002_auto_20170215_0115.py @@ -0,0 +1,30 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.1 on 2017-02-15 01:15 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion +import tree.fields +from tree.operations import CreateTreeTrigger + + +class Migration(migrations.Migration): + + dependencies = [ + ('storage', '0001_squashed_0008_item_state'), + ('tree', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='item', + name='parent', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='storage.Item'), + ), + migrations.AddField( + model_name='item', + name='path', + field=tree.fields.PathField(), + ), + CreateTreeTrigger('storage.Item'), + ] diff --git a/storage/models.py b/storage/models.py index d4f5b10..e2b1bc3 100644 --- a/storage/models.py +++ b/storage/models.py @@ -5,6 +5,10 @@ from django.contrib.auth.models import User from django_hstore import hstore import uuid +from tree.fields import PathField +from tree.models import TreeModelMixin + + STATES = ( ('present', 'Present'), ('taken', 'Taken'), @@ -21,24 +25,31 @@ class Category(models.Model): return self.name -class Item(models.Model): +class Item(models.Model, TreeModelMixin): uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) name = models.TextField() + + parent = models.ForeignKey('self', null=True, blank=True) + path = PathField() + description = models.TextField(blank=True, null=True) state = models.CharField(max_length=31, choices=STATES, default=STATES[0][0]) - categories = models.ManyToManyField(Category) + categories = models.ManyToManyField(Category, blank=True) owner = models.ForeignKey(User, null=True, blank=True, related_name='owned_items') taken_by = models.ForeignKey(User, null=True, blank=True, related_name='taken_items') taken_on = models.DateTimeField(blank=True, null=True) taken_until = models.DateTimeField(blank=True, null=True) - props = hstore.DictionaryField() + props = hstore.DictionaryField(blank=True) objects = hstore.HStoreManager() def __str__(self): - return self.name + return '-' * (self.get_level() or 0) + ' ' +self.name + + class Meta: + ordering = ('path',) class ItemImage(models.Model):