From 948866cb20453892f057d497b3abf6d4b0677d8e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Aleksander=20=C5=81ukasz?= Date: Mon, 13 May 2013 14:07:49 +0200 Subject: [PATCH] Adding library own page --- .../0003_auto__add_field_library_slug.py | 40 +++++++++++++++++++ apps/libraries/models.py | 4 ++ .../templates/libraries/catalog_view.html | 4 +- .../templates/libraries/library_view.html | 15 +++++++ apps/libraries/urls.py | 1 + apps/libraries/views.py | 9 ++++- 6 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 apps/libraries/migrations/0003_auto__add_field_library_slug.py create mode 100644 apps/libraries/templates/libraries/library_view.html diff --git a/apps/libraries/migrations/0003_auto__add_field_library_slug.py b/apps/libraries/migrations/0003_auto__add_field_library_slug.py new file mode 100644 index 000000000..de0cdf0b1 --- /dev/null +++ b/apps/libraries/migrations/0003_auto__add_field_library_slug.py @@ -0,0 +1,40 @@ +# -*- 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 field 'Library.slug' + db.add_column(u'libraries_library', 'slug', + self.gf('django.db.models.fields.SlugField')(max_length=120, unique=True, null=True), + keep_default=False) + + + def backwards(self, orm): + # Deleting field 'Library.slug' + db.delete_column(u'libraries_library', 'slug') + + + 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'}), + 'slug': ('django.db.models.fields.SlugField', [], {'max_length': '120', 'unique': 'True', 'null': '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 c6db612b9..5f92421fc 100644 --- a/apps/libraries/models.py +++ b/apps/libraries/models.py @@ -24,6 +24,7 @@ class Library(models.Model): """Represent a single library in the libraries dictionary""" name = models.CharField(_('name'), max_length = 120, blank = True) + slug = models.SlugField(_('slug'), max_length = 120, unique = True, db_index = True, null = 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) @@ -35,3 +36,6 @@ class Library(models.Model): def __unicode__(self): return self.name + @models.permalink + def get_absolute_url(self): + return ('libraries_library_view', [self.catalog.slug, self.slug]) \ No newline at end of file diff --git a/apps/libraries/templates/libraries/catalog_view.html b/apps/libraries/templates/libraries/catalog_view.html index b109fa1b3..1a56cbc64 100644 --- a/apps/libraries/templates/libraries/catalog_view.html +++ b/apps/libraries/templates/libraries/catalog_view.html @@ -10,10 +10,10 @@
{% for library in catalog.libraries.all %}
- +
- {% autoescape off %}{{library.description}}{% endautoescape%} +
{% endfor %} diff --git a/apps/libraries/templates/libraries/library_view.html b/apps/libraries/templates/libraries/library_view.html new file mode 100644 index 000000000..153ead00d --- /dev/null +++ b/apps/libraries/templates/libraries/library_view.html @@ -0,0 +1,15 @@ +{% extends "base.html" %} +{% load i18n %} + +{% block titleextra %}{{ library.name }}{% endblock %} + + +{% block body %} +

{{ library.name }}

+
+
+

Adres strony www: {{library.url}}

+ {% autoescape off %}{{library.description}}{% endautoescape%} +
+
+{% endblock %} \ No newline at end of file diff --git a/apps/libraries/urls.py b/apps/libraries/urls.py index fe0fd6b96..01531a9ed 100644 --- a/apps/libraries/urls.py +++ b/apps/libraries/urls.py @@ -6,4 +6,5 @@ urlpatterns = patterns('libraries.views', url(r'^$', 'main_view', name='libraries_main_view'), url(r'^/$', lambda x: HttpResponseRedirect(x.path[:-1])), url(r'^/(?P[a-zA-Z0-9_-]+)$', 'catalog_view', name='libraries_catalog_view'), + url(r'^/(?P[a-zA-Z0-9_-]+)/(?P[a-zA-Z0-9_-]+)$', 'library_view', name='libraries_library_view'), ) \ No newline at end of file diff --git a/apps/libraries/views.py b/apps/libraries/views.py index 88f993d68..f79f2a1ea 100644 --- a/apps/libraries/views.py +++ b/apps/libraries/views.py @@ -1,7 +1,7 @@ from django.shortcuts import render_to_response, get_object_or_404 from django.template import RequestContext, Template, TemplateSyntaxError -from libraries.models import Catalog +from libraries.models import Catalog, Library def main_view(request): @@ -12,4 +12,9 @@ def main_view(request): def catalog_view(request, slug): context = RequestContext(request) context['catalog'] = get_object_or_404(Catalog.objects.filter(slug = slug).select_related()) - return render_to_response('libraries/catalog_view.html', context_instance = context) \ No newline at end of file + return render_to_response('libraries/catalog_view.html', context_instance = context) + +def library_view(request, catalog_slug, slug): + context = RequestContext(request) + context['library'] = get_object_or_404(Library.objects.filter(slug = slug).filter(catalog__slug = catalog_slug)) + return render_to_response('libraries/library_view.html', context_instance = context) \ No newline at end of file -- 2.20.1