From 1a6872f821959ade6a7382012338d628a4244877 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Aleksander=20=C5=81ukasz?= Date: Thu, 9 May 2013 16:33:36 +0200 Subject: [PATCH 1/1] Grouping libraries into different catalogs --- apps/libraries/admin.py | 5 +- .../libraries/locale/pl/LC_MESSAGES/django.mo | Bin 721 -> 795 bytes .../libraries/locale/pl/LC_MESSAGES/django.po | 22 ++++++-- ..._add_catalog__add_field_library_catalog.py | 50 ++++++++++++++++++ apps/libraries/models.py | 16 ++++++ .../templates/libraries/main_view.html | 4 +- apps/libraries/urls.py | 2 +- apps/libraries/views.py | 9 ++-- 8 files changed, 93 insertions(+), 15 deletions(-) create mode 100644 apps/libraries/migrations/0002_auto__add_catalog__add_field_library_catalog.py diff --git a/apps/libraries/admin.py b/apps/libraries/admin.py index c43dd6a35..5fa88df54 100644 --- a/apps/libraries/admin.py +++ b/apps/libraries/admin.py @@ -1,6 +1,7 @@ from django.contrib import admin -from libraries.models import Library +from libraries.models import Library, Catalog -admin.site.register(Library) \ No newline at end of file +admin.site.register(Catalog) +admin.site.register(Library) diff --git a/apps/libraries/locale/pl/LC_MESSAGES/django.mo b/apps/libraries/locale/pl/LC_MESSAGES/django.mo index d8c29f1b0abfa5066bd94b07e32a71446002f600..a29a7b08d7b8a126e69de54e5167727901a43e6a 100644 GIT binary patch delta 305 zcmcb}I-9Njo)F7a1|Z-BVi_P#0b*VtUIWA+@BoN~f%qX1vjg#0Am#w#zd+0l#B7WZ zy+S}5BrgM`S%9<(kY)zbnm~Ff5bFZ5Adr8V38L;PkOrDl&+rz=0BQIJq`83fZz#kDm=lO^0x?L>V<2V$ z;@3dT48$LRcqt!H43NgNKpLnI4A`JF phyuxhECK\n" "Language-Team: LANGUAGE \n" @@ -19,23 +19,35 @@ msgstr "" "Plural-Forms: nplurals=3; plural=(n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 " "|| n%100>=20) ? 1 : 2);\n" -#: models.py:8 +#: models.py:8 models.py:22 msgid "name" msgstr "nazwa" #: models.py:9 +msgid "slug" +msgstr "" + +#: models.py:12 +msgid "catalog" +msgstr "katalog" + +#: models.py:13 +msgid "catalogs" +msgstr "katalogi" + +#: models.py:24 msgid "url" msgstr "url" -#: models.py:10 +#: models.py:25 msgid "description" msgstr "opis" -#: models.py:13 +#: models.py:28 msgid "library" msgstr "biblioteka" -#: models.py:14 +#: models.py:29 msgid "libraries" msgstr "biblioteki" diff --git a/apps/libraries/migrations/0002_auto__add_catalog__add_field_library_catalog.py b/apps/libraries/migrations/0002_auto__add_catalog__add_field_library_catalog.py new file mode 100644 index 000000000..c37abf526 --- /dev/null +++ b/apps/libraries/migrations/0002_auto__add_catalog__add_field_library_catalog.py @@ -0,0 +1,50 @@ +# -*- coding: utf-8 -*- +import datetime +from south.db import db +from south.v2 import SchemaMigration +from django.db import models + + +class Migration(SchemaMigration): + + def forwards(self, orm): + # Adding model 'Catalog' + db.create_table(u'libraries_catalog', ( + (u'id', self.gf('django.db.models.fields.AutoField')(primary_key=True)), + ('name', self.gf('django.db.models.fields.CharField')(max_length=120)), + ('slug', self.gf('django.db.models.fields.SlugField')(unique=True, max_length=120)), + )) + db.send_create_signal(u'libraries', ['Catalog']) + + # Adding field 'Library.catalog' + db.add_column(u'libraries_library', 'catalog', + self.gf('django.db.models.fields.related.ForeignKey')(default=None, related_name='libraries', on_delete=models.PROTECT, to=orm['libraries.Catalog']), + keep_default=False) + + + def backwards(self, orm): + # Deleting model 'Catalog' + db.delete_table(u'libraries_catalog') + + # Deleting field 'Library.catalog' + db.delete_column(u'libraries_library', 'catalog_id') + + + models = { + u'libraries.catalog': { + 'Meta': {'object_name': 'Catalog'}, + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '120'}), + 'slug': ('django.db.models.fields.SlugField', [], {'unique': 'True', 'max_length': '120'}) + }, + u'libraries.library': { + 'Meta': {'object_name': 'Library'}, + 'catalog': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'libraries'", 'on_delete': 'models.PROTECT', 'to': u"orm['libraries.Catalog']"}), + 'description': ('django.db.models.fields.TextField', [], {'blank': 'True'}), + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '120', 'blank': 'True'}), + 'url': ('django.db.models.fields.CharField', [], {'max_length': '120', 'blank': 'True'}) + } + } + + complete_apps = ['libraries'] \ No newline at end of file diff --git a/apps/libraries/models.py b/apps/libraries/models.py index ff2afc07e..9856608ae 100644 --- a/apps/libraries/models.py +++ b/apps/libraries/models.py @@ -1,10 +1,26 @@ from django.db import models from django.utils.translation import ugettext_lazy as _ + +class Catalog(models.Model): + """Represents a dictionary of libraries""" + + name = models.CharField(_('name'), max_length = 120, null = False) + slug = models.SlugField(_('slug'), max_length = 120, unique = True, db_index = True) + + class Meta: + verbose_name = _('catalog') + verbose_name_plural = _('catalogs') + + def __unicode__(self): + return self.name + + class Library(models.Model): """Represent a single library in the libraries dictionary""" name = models.CharField(_('name'), max_length = 120, blank = True) + catalog = models.ForeignKey(Catalog, null = False, related_name = 'libraries', on_delete = models.PROTECT) url = models.CharField(_('url'), max_length = 120, blank = True) description = models.TextField(_('description'), blank = True) diff --git a/apps/libraries/templates/libraries/main_view.html b/apps/libraries/templates/libraries/main_view.html index 9e16b1911..b109fa1b3 100644 --- a/apps/libraries/templates/libraries/main_view.html +++ b/apps/libraries/templates/libraries/main_view.html @@ -5,10 +5,10 @@ {% block body %} -

{% trans 'Libraries directory' %}

+

{% trans 'Libraries directory' %}: {{ catalog.name }}

- {% for library in libraries %} + {% for library in catalog.libraries.all %}
diff --git a/apps/libraries/urls.py b/apps/libraries/urls.py index 5b13dc526..b4af54be7 100644 --- a/apps/libraries/urls.py +++ b/apps/libraries/urls.py @@ -2,5 +2,5 @@ from django.conf.urls.defaults import patterns, url urlpatterns = patterns('libraries.views', - url(r'^$', 'main_view', name='libraries_main_view'), + url(r'^(?P[a-zA-Z0-9_-]+)$', 'main_view', name='libraries_main_view'), ) \ No newline at end of file diff --git a/apps/libraries/views.py b/apps/libraries/views.py index d75c8b468..45e60ddbf 100644 --- a/apps/libraries/views.py +++ b/apps/libraries/views.py @@ -1,11 +1,10 @@ from django.shortcuts import render_to_response, get_object_or_404 from django.template import RequestContext, Template, TemplateSyntaxError -from libraries.models import Library +from libraries.models import Catalog -def main_view(request): - context = RequestContext(request) - context['libraries'] = Library.objects.all() - +def main_view(request, slug): + context = RequestContext(request) + context['catalog'] = get_object_or_404(Catalog.objects.filter(slug = slug).select_related()) return render_to_response('libraries/main_view.html', context_instance = context) \ No newline at end of file -- 2.20.1