# HG changeset patch # User Sverre Rabbelier # Date 1232745633 0 # Node ID 09f47e08f8057a9b48b94952e50c7719f598d4f7 # Parent 9fcc08971efef8ed094d64bcca9cbad964aa2498 Adust the as_table tag to render a pick link if appropriate The templates are adjusted to pass on a 'reference' value, which is the url_name of the view from which the entity should be picked. The as_table (and related) function(s) construct and then pass on this argument and enable takes_contex so that we have access to the context of the enclosing template. We only extract ReferenceProperties that end with '_link_id' since that is how all RP's are currently named. It is not possible to create a field with the same name as the RP, as GAE will try to interpret it's contents as the key of an entity before even calling any function we can override. Patch by: Sverre Rabbelier diff -r 9fcc08971efe -r 09f47e08f805 app/soc/templates/soc/templatetags/_as_table.html --- a/app/soc/templates/soc/templatetags/_as_table.html Fri Jan 23 21:18:39 2009 +0000 +++ b/app/soc/templates/soc/templatetags/_as_table.html Fri Jan 23 21:20:33 2009 +0000 @@ -26,9 +26,9 @@ {% endif %} -{% for field, required, example_text in fields %} +{% for field, required, example_text, reference in fields %} {% block fields_loop %} - {% as_table_row form field required example_text %} + {% as_table_row field required example_text reference %} {% endblock %} {% endfor %} diff -r 9fcc08971efe -r 09f47e08f805 app/soc/templates/soc/templatetags/_as_table_row.html --- a/app/soc/templates/soc/templatetags/_as_table_row.html Fri Jan 23 21:18:39 2009 +0000 +++ b/app/soc/templates/soc/templatetags/_as_table_row.html Fri Jan 23 21:20:33 2009 +0000 @@ -40,9 +40,14 @@ {% endif %} + {% if select_url %} + select + {% comment %} no else branch, as example_text will take care of the last {% endcomment %} + {% endif %} + {% if example_text %} {{ example_text|safe }} - {% else %} + {% else %} {% if not select_url %} - {% endif %} + {% endif %} {% endif %} diff -r 9fcc08971efe -r 09f47e08f805 app/soc/templates/soc/templatetags/_as_twoline_table.html --- a/app/soc/templates/soc/templatetags/_as_twoline_table.html Fri Jan 23 21:18:39 2009 +0000 +++ b/app/soc/templates/soc/templatetags/_as_twoline_table.html Fri Jan 23 21:20:33 2009 +0000 @@ -15,5 +15,5 @@ {% load forms_helpers %} {% block fields_loop %} - {% as_twoline_table_row form field required example_text %} + {% as_twoline_table_row form field required example_text reference %} {% endblock %} diff -r 9fcc08971efe -r 09f47e08f805 app/soc/views/helper/templatetags/forms_helpers.py --- a/app/soc/views/helper/templatetags/forms_helpers.py Fri Jan 23 21:18:39 2009 +0000 +++ b/app/soc/views/helper/templatetags/forms_helpers.py Fri Jan 23 21:20:33 2009 +0000 @@ -24,6 +24,8 @@ ] +from google.appengine.ext import db + from django import template from django.forms import forms as forms_in from django.utils.encoding import force_unicode @@ -112,36 +114,67 @@ 'field_value': field_value} -@register.inclusion_tag('soc/templatetags/_as_table.html') -def as_table(form): +@register.inclusion_tag('soc/templatetags/_as_table.html', takes_context=True) +def as_table(context, form): + """Outputs a form as a properly formatted html table. + + Args: + form: the form that should be converted to a table + """ + + return as_table_helper(context, form) + + +@register.inclusion_tag('soc/templatetags/_as_twoline_table.html', + takes_context=True) +def as_twoline_table(context, form): """Outputs a form as a properly formatted html table. Args: form: the form that should be converted to a table """ - return as_table_helper(form) + return as_table_helper(context, form) -@register.inclusion_tag('soc/templatetags/_as_twoline_table.html') -def as_twoline_table(form): - """Outputs a form as a properly formatted html table. +def get_reference_url(form, name): + """Retrieves the reference url from a field Args: - form: the form that should be converted to a table + form: the form the field is defined in + name: the name of the field """ - return as_table_helper(form) + if not hasattr(form, 'Meta'): + return None + + if not hasattr(form.Meta, 'model'): + return None + + if not hasattr(form.Meta.model, name): + return None + + field = getattr(form.Meta.model, name) + + if not isinstance(field, db.ReferenceProperty): + return None + + return getattr(field, 'redirect_url', None) -def as_table_helper(form): +def as_table_helper(context, form): fields = [] hidden_fields = [] hidden_fields_errors = [] + entity = context['entity'] # Iterate over all fields and prepare it for adding for name, field in form.fields.items(): bf = forms_in.BoundField(form, field, name) + reference = None + + if name.endswith('_link_id'): + reference = get_reference_url(form, name[:-8]) # If the field is hidden we display it elsewhere if not bf.is_hidden: @@ -149,7 +182,7 @@ if hasattr(field, 'example_text'): example_text = force_unicode(field.example_text) - item = (bf, field.required, example_text) + item = (bf, field.required, example_text, reference) fields.append(item) else: hidden_fields.append(unicode(bf)) @@ -158,17 +191,19 @@ item = (name, force_unicode(e)) hidden_fields_errors.append(item) - return { + context.update({ 'top_errors': form.non_field_errors() or '', 'hidden_field_errors': hidden_fields_errors or '', - 'form': form, 'fields': fields or '', 'hidden_fields': hidden_fields or '', - } + }) + + return context -@register.inclusion_tag('soc/templatetags/_as_table_row.html') -def as_table_row(form, field, required, example_text): +@register.inclusion_tag('soc/templatetags/_as_table_row.html', + takes_context=True) +def as_table_row(context, field, required, example_text, reference): """Outputs a field as a properly formatted html row. Args: @@ -176,29 +211,40 @@ field: the field that should be converted to a row required: whether the field is required example_text: the example_text for this row + reference: the entity_suffix if the field is a reference """ - return as_table_row_helper(form, field, required, example_text) + return as_table_row_helper(context, field, required, example_text, reference) + + +@register.inclusion_tag('soc/templatetags/_as_twoline_table_row.html', + takes_context=True) +def as_twoline_table_row(context, field, required, example_text, reference): + """See as_table_row(). + """ + + return as_table_row_helper(context, field, required, example_text, reference) -@register.inclusion_tag('soc/templatetags/_as_twoline_table_row.html') -def as_twoline_table_row(form, field, required, example_text): - """Outputs a field as a properly formatted html row. - - Args: - form: the form that the row belongs to - field: the field that should be converted to a row - required: whether the field is required - example_text: the example_text for this row +def as_table_row_helper(context, field, required, example_text, reference): + """See as_table_row(). """ - return as_table_row_helper(form, field, required, example_text) - - -def as_table_row_helper(form, field, required, example_text): # Escape and cache in local variable. errors = [force_unicode(escape(error)) for error in field.errors] + form = context['form'] + entity = context['entity'] + + if reference: + from soc.views.helper import redirects + params = { + 'url_name': reference, + 'field_name': field.name, + 'return_url': context['return_url'] + } + select_url = redirects.getSelectRedirect(entity, params) + if field.label: label = escape(force_unicode(field.label)) @@ -212,12 +258,15 @@ help_text = field.help_text - return { + context.update({ 'help_text': force_unicode(help_text) if help_text else '', 'field_class_type': field_class_type, 'label': force_unicode(label) if field.label else '', 'field': unicode(field), 'required': required, 'example_text': example_text, + 'select_url': select_url if reference else None, 'errors': errors, - } + }) + + return context diff -r 9fcc08971efe -r 09f47e08f805 app/soc/views/models/base.py --- a/app/soc/views/models/base.py Fri Jan 23 21:18:39 2009 +0000 +++ b/app/soc/views/models/base.py Fri Jan 23 21:20:33 2009 +0000 @@ -675,6 +675,7 @@ context['entity_type_plural'] = params['name_plural'] context['entity_type_short'] = params['name_short'] context['entity_type_url'] = params['url_name'] + context['return_url'] = request.path if params.get('export_content_type') and entity: context['export_link'] = redirects.getExportRedirect(entity, params)