mirror of
https://github.com/django/django.git
synced 2025-07-04 17:59:13 +00:00
[per-object-permissions] First version where inline objects are checked for row level permissions
git-svn-id: http://code.djangoproject.com/svn/django/branches/per-object-permissions@3891 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
ea249260dc
commit
655bee8b28
@ -1,6 +1,8 @@
|
||||
{% load admin_modify %}
|
||||
<fieldset class="module aligned">
|
||||
{% for fcw in bound_related_object.form_field_collection_wrappers %}
|
||||
{% load row_level_permission %}
|
||||
{% check_rlp_inline fcw.original %}
|
||||
<h2>{{ bound_related_object.relation.opts.verbose_name|capfirst|escape }} #{{ forloop.counter }}</h2>
|
||||
{% if bound_related_object.show_url %}{% if fcw.obj.original %}
|
||||
<p><a href="/r/{{ fcw.obj.original.content_type_id }}/{{ fcw.obj.original.id }}/">View on site</a></p>
|
||||
@ -12,5 +14,6 @@
|
||||
{% admin_field_line bound_field %}
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
{% end_check_rlp_inline %}
|
||||
{% endfor %}
|
||||
</fieldset>
|
||||
|
@ -9,6 +9,8 @@
|
||||
{% endfor %}
|
||||
</tr></thead>
|
||||
{% for fcw in bound_related_object.form_field_collection_wrappers %}
|
||||
{% load row_level_permission %}
|
||||
{% check_rlp_inline fcw.original %}
|
||||
{% if change %}{% if original_row_needed %}
|
||||
{% if fcw.obj.original %}
|
||||
<tr class="row-label {% cycle row1,row2 %}"><td colspan="{{ num_headers }}"><strong>{{ fcw.obj.original }}</strong></tr>
|
||||
@ -31,7 +33,8 @@
|
||||
{% if fcw.obj.original %}<a href="/r/{{ fcw.obj.original.content_type_id }}/{{ fcw.obj.original.id }}/">View on site</a>{% endif %}
|
||||
</td>{% endif %}
|
||||
</tr>
|
||||
|
||||
|
||||
{% end_check_rlp_inline %}
|
||||
{% endfor %} </table>
|
||||
|
||||
{% for fcw in bound_related_object.form_field_collection_wrappers %}
|
||||
|
@ -119,6 +119,7 @@ class FieldWrapper(object):
|
||||
class FormFieldCollectionWrapper(object):
|
||||
def __init__(self, field_mapping, fields, index):
|
||||
self.field_mapping = field_mapping
|
||||
self.original = field_mapping['original']
|
||||
self.fields = fields
|
||||
self.bound_fields = [AdminBoundField(field, self.field_mapping, field_mapping['original'])
|
||||
for field in self.fields]
|
||||
@ -127,13 +128,17 @@ class FormFieldCollectionWrapper(object):
|
||||
class TabularBoundRelatedObject(BoundRelatedObject):
|
||||
def __init__(self, related_object, field_mapping, original):
|
||||
super(TabularBoundRelatedObject, self).__init__(related_object, field_mapping, original)
|
||||
|
||||
self.field_wrapper_list = [FieldWrapper(field) for field in self.relation.editable_fields()]
|
||||
|
||||
|
||||
fields = self.relation.editable_fields()
|
||||
|
||||
|
||||
self.form_field_collection_wrappers = [FormFieldCollectionWrapper(field_mapping, fields, i)
|
||||
for (i,field_mapping) in self.field_mappings.items() ]
|
||||
|
||||
self.original_row_needed = max([fw.use_raw_id_admin() for fw in self.field_wrapper_list])
|
||||
if original:
|
||||
self.original = original
|
||||
self.show_url = original and hasattr(self.relation.opts, 'get_absolute_url')
|
||||
|
||||
def template_name(self):
|
||||
@ -147,6 +152,10 @@ class StackedBoundRelatedObject(BoundRelatedObject):
|
||||
|
||||
self.form_field_collection_wrappers = [FormFieldCollectionWrapper(field_mapping ,fields, i)
|
||||
for (i,field_mapping) in self.field_mappings.items()]
|
||||
|
||||
if original:
|
||||
self.original = original
|
||||
|
||||
self.show_url = original and hasattr(self.relation.opts, 'get_absolute_url')
|
||||
|
||||
def template_name(self):
|
||||
|
@ -34,6 +34,55 @@ def objref(parser, token):
|
||||
tok = "object"
|
||||
return objref_class(tok)
|
||||
|
||||
def check_rlp_inline(parser, token):
|
||||
tokens = token.split_contents()
|
||||
if len(tokens)!=2:
|
||||
raise template.TemplateSyntaxError, "%r tag requires only 1 arguments" % tokens[0]
|
||||
|
||||
nodelist = parser.parse(('end_'+tokens[0],))
|
||||
token = parser.next_token()
|
||||
|
||||
object_var = parser.compile_filter(tokens[1])
|
||||
|
||||
return CheckRLPInlineNode(object_var, nodelist)
|
||||
|
||||
class CheckRLPInlineNode(template.Node):
|
||||
def __init__(self, object_var, nodelist):
|
||||
self.object_var = object_var
|
||||
self.nodelist = nodelist
|
||||
|
||||
def render(self, context):
|
||||
if self.object_var:
|
||||
try:
|
||||
object = self.object_var.resolve(context)
|
||||
except template.VariableDoesNotExist:
|
||||
return self.nodelist.render(context)
|
||||
else:
|
||||
return self.nodelist.render(context)
|
||||
|
||||
if object is None:
|
||||
return self.nodelist.render(context)
|
||||
|
||||
if not object._meta.row_level_permissions:
|
||||
return self.nodelist.render(context)
|
||||
|
||||
try:
|
||||
user = template.resolve_variable("user", context)
|
||||
except template.VariableDoesNotExist:
|
||||
return settings.TEMPLATE_STRING_IF_INVALID
|
||||
|
||||
permission = object._meta.get_change_permission()
|
||||
|
||||
bool_perm = user.has_perm(object._meta.app_label+'.'+permission, object=object)
|
||||
|
||||
if bool_perm:
|
||||
return self.nodelist.render(context)
|
||||
return ""
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#From: http://code.djangoproject.com/wiki/PaginatorTag
|
||||
def paginator(context, adjacent_pages=2):
|
||||
"""Adds pagination context variables for first, adjacent and next page links
|
||||
@ -57,4 +106,5 @@ def paginator(context, adjacent_pages=2):
|
||||
|
||||
register.inclusion_tag("admin/paginator.html", takes_context=True)(paginator)
|
||||
|
||||
register.tag('objref', objref)
|
||||
register.tag('objref', objref)
|
||||
register.tag('check_rlp_inline', check_rlp_inline)
|
Loading…
x
Reference in New Issue
Block a user