From 8b539e59267fca359a45e82bae88d7f3f9f87a81 Mon Sep 17 00:00:00 2001 From: Piotr Dobrowolski Date: Thu, 16 Feb 2017 16:14:37 +0100 Subject: [PATCH] Make add another default, add select2, remember last parent --- requirements.txt | 2 + spejstore/settings.py | 1 + spejstore/urls.py | 1 + storage/admin.py | 27 ++++++++++ storage/models.py | 3 +- .../admin/storage/item/change_form.html | 7 +++ storage/views.py | 3 +- templates/base.html | 52 +++++++++++-------- 8 files changed, 73 insertions(+), 23 deletions(-) create mode 100644 storage/templates/admin/storage/item/change_form.html diff --git a/requirements.txt b/requirements.txt index 74b2b4a..c2d3dfb 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,6 @@ Django==1.10.1 git+https://github.com/djangonauts/django-hstore@61427e474cb2f4be8fdfce225d78a5330bc77eb0#egg=django-hstore +django-appconf==1.0.2 +Django-Select2==5.8.10 Pillow==3.3.1 psycopg2==2.6.2 diff --git a/spejstore/settings.py b/spejstore/settings.py index b3bf774..f14b1e5 100644 --- a/spejstore/settings.py +++ b/spejstore/settings.py @@ -41,6 +41,7 @@ INSTALLED_APPS = [ 'django_hstore', 'tree', + 'django_select2', 'storage', ] diff --git a/spejstore/urls.py b/spejstore/urls.py index 4682e72..8d51380 100644 --- a/spejstore/urls.py +++ b/spejstore/urls.py @@ -11,6 +11,7 @@ from django.conf.urls.static import static urlpatterns = [ url(r'^admin/', admin.site.urls), + url(r'^select2/', include('django_select2.urls')), url(r'^', include('storage.urls')), ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) diff --git a/storage/admin.py b/storage/admin.py index 801b5b6..99e99df 100644 --- a/storage/admin.py +++ b/storage/admin.py @@ -1,6 +1,8 @@ from django import forms from django.contrib import admin from .models import Item, ItemImage, Category +from django_select2.forms import Select2Widget, Select2MultipleWidget + class ItemForm(forms.ModelForm): name = forms.CharField(widget=forms.TextInput()) @@ -8,6 +10,10 @@ class ItemForm(forms.ModelForm): class Meta: model = Item exclude = [] + widgets = { + 'parent': Select2Widget, + 'categories': Select2MultipleWidget + } class ItemImageInline(admin.TabularInline): model = ItemImage @@ -22,6 +28,27 @@ class ItemAdmin(admin.ModelAdmin): def _name(self, obj): return '-' * obj.get_level() + '> ' + obj.name + def save_model(self, request, obj, form, change): + super(ItemAdmin, self).save_model(request, obj, form, change) + + # Store last input parent to use as default on next creation + if obj.parent: + request.session['last-parent'] = str(obj.parent.uuid) + else: + request.session['last-parent'] = str(obj.uuid) + + def get_changeform_initial_data(self, request): + data = { + 'parent': request.session.get('last-parent') + } + data.update(super(ItemAdmin, self).get_changeform_initial_data(request)) + return data + + class Media: + js = ( + # Required by select2 + 'http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js', + ) admin.site.register(Item, ItemAdmin) admin.site.register(Category) diff --git a/storage/models.py b/storage/models.py index 5b9ee27..d3ee291 100644 --- a/storage/models.py +++ b/storage/models.py @@ -27,11 +27,12 @@ class Category(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() + name = models.TextField() + description = models.TextField(blank=True, null=True) state = models.CharField(max_length=31, choices=STATES, default=STATES[0][0]) categories = models.ManyToManyField(Category, blank=True) diff --git a/storage/templates/admin/storage/item/change_form.html b/storage/templates/admin/storage/item/change_form.html new file mode 100644 index 0000000..6e57c57 --- /dev/null +++ b/storage/templates/admin/storage/item/change_form.html @@ -0,0 +1,7 @@ +{% extends "admin/change_form.html" %} + +{% block submit_buttons_bottom %} + {# We want add another to be default submit action #} + + {{ block.super }} +{% endblock %} diff --git a/storage/views.py b/storage/views.py index 63decb5..d775a1c 100644 --- a/storage/views.py +++ b/storage/views.py @@ -15,7 +15,8 @@ def apply_smart_search(query, objects): key, value = prop.split(':', 1) if hasattr(Item, key): filters[key + '__search'] = value - + elif key == 'ancestor': + objects = Item.objects.get(pk=value).get_children() elif key == 'prop' or value: if key == 'prop': key, value = value.split(':', 1) diff --git a/templates/base.html b/templates/base.html index 8e981a6..cdb135a 100644 --- a/templates/base.html +++ b/templates/base.html @@ -7,31 +7,41 @@ - {% block body %} +
-

Warsaw Hackerspace

+ {% block content %} {% endblock %}