Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added the ability to pass an integer as slug. This will cause that th…

…e templatetag fetches the specific *for* model with the primary key named in *slug*.
  • Loading branch information...
commit 8b0a637e309c038497258b2580685b1071c4898a 1 parent 23a8189
@bartTC authored
View
15 README
@@ -50,6 +50,16 @@ The arguments in detail:
"homepage","headline" becomes "homepage_headline"
"homepage_title",LANGUAGE_CODE becomes "homepage_title_en" (depends on the users locale code)
+ You can pass an *integer* as the slug. This will cause the templatetag to fetch
+ the model named in *for* with the primary key you named in *slug*. Example::
+
+ {% gblock 1 for "auth.user" with "path/to/template.html" %}
+
+ This will fetch the auth.User with the primary key 1 and renders this model
+ object with the template "path/to/template.html". In this case, the
+ ``generic_object`` in ``None``. Basically this is a ``{% include %}`` Tag on
+ model level.
+
- *for* **"applabel.modelname"** (required): The *for* argument defines, what
content-node (model) will be used to store and display the content. The
format is *appname.modelname*. For some contributed content-nodes see
@@ -145,6 +155,11 @@ example::
Changelog
=========
+**v0.2.0** (2009-03-20)
+ Added the ability to pass an integer as slug. This will cause that the
+ templatetag fetches the specific *for* model with the primary key named
+ in *slug*.
+
**v0.1.2** (2009-03-20)
Switched from distutils to setuptools. Fixed whitespace.
View
32 django_generic_flatblocks/templatetags/generic_flatblocks.py
@@ -23,6 +23,10 @@ def generate_slug(self, slug, context):
"website","title" -> website_title
"website",LANGUAGE_CODE -> website_en
"""
+ # If the user passed a integer as slug, use it as a primary key in
+ # self.get_content_object()
+ if slug.isdigit():
+ return slug
return slugify('_'.join([self.resolve(i, context) for i in slug.split(',')]))
def generate_admin_link(self, related_object, context):
@@ -41,14 +45,22 @@ def generate_admin_link(self, related_object, context):
return None
def get_content_object(self, related_model, slug):
+
+ # If the user passed a Integer as a slug, assume that we should fetch
+ # this specific object
+ if slug.isdigit():
+ related_object = related_model._default_manager.get(pk=slug)
+ print related_object
+ return None, related_object
+
+ # Otherwise, try to generate a new, related object
try:
- # Objekt laden
- obj = GenericFlatblock.objects.get(slug=slug)
+ generic_object = GenericFlatblock._default_manager.get(slug=slug)
+ related_object = generic_object.content_object
except GenericFlatblock.DoesNotExist:
- # Objekt exisitert noch nicht, neu erstellen
- related_obj = related_model.objects.create()
- obj = GenericFlatblock.objects.create(slug=slug, content_object=related_obj)
- return obj
+ related_object = related_model._default_manager.create()
+ generic_object = GenericFlatblock._default_manager.create(slug=slug, content_object=related_object)
+ return generic_object, related_object
def resolve(self, var, context):
"""Resolves a variable out of context if it's not in quotes"""
@@ -68,13 +80,13 @@ def render(self, context):
slug = self.generate_slug(self.slug, context)
related_model = self.resolve_model_for_label(self.modelname, context)
- # Get the content and related content object
- generic_object = self.get_content_object(related_model, slug)
+ # Get the generic and related object
+ generic_object, related_object = self.get_content_object(related_model, slug)
# Add the model instances to the current context
context['generic_object'] = generic_object
- context['object'] = generic_object.content_object
- context['admin_url'] = self.generate_admin_link(generic_object.content_object, context)
+ context['object'] = related_object
+ context['admin_url'] = self.generate_admin_link(related_object, context)
# Resolve the template(s)
template_paths = []
View
1  example_project/templates/current_user.html
@@ -0,0 +1 @@
+<p>Selected user: {{ object }}</p>
View
8 example_project/templates/example.html
@@ -18,6 +18,8 @@
</body>
<div id="outline">
+ <p><em>See the <strong>template</strong> sources for further details.</em></p>
+
{% comment %}
You can join unlimited of slug-strings or context-variables
to one slug. Most commonly you will do this if you need to
@@ -39,6 +41,12 @@
{% endcomment %}
{% gblock "website_urgent_notice" for "gblocks.Text" with "urgent.html" %}
+ {% comment %}
+ You can pass an integer as slug. In this case, generic-flatblocks
+ will fetch the model instance with the primary key you named in slug.
+ Basically this is a {% include %} tag on model level.
+ {% endcomment %}
+ {% gblock 1 for "auth.user" with "current_user.html" %}
<hr/>
{% comment %}
View
4 setup.py
@@ -2,7 +2,7 @@
setup(
name='django-generic-flatblocks',
- version='0.1.2',
+ version='0.2.0',
description='A flatpages/flatblock application using generic relations to content models.',
long_description=open('README').read(),
author='Martin Mahner',
@@ -20,4 +20,4 @@
'Framework :: Django',
],
zip_safe=False,
-)
+)
Please sign in to comment.
Something went wrong with that request. Please try again.