Commits

Anonymous committed 39beb75

[per-object-permissions] First version where inline objects are checked for row level permissions

  • Participants
  • Parent commits cdd9093
  • Branches per-object-permissions

Comments (0)

Files changed (4)

File django/contrib/admin/templates/admin/edit_inline_stacked.html

 {% 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 }}&nbsp;#{{ 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>
             {% admin_field_line bound_field %}
          {% endif %}
       {% endfor %}
+    {% end_check_rlp_inline %}
     {% endfor %}
 </fieldset>

File django/contrib/admin/templates/admin/edit_inline_tabular.html

    {% 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>
          {% 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 %}

File django/contrib/admin/templatetags/admin_modify.py

 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]
 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):
 
         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):

File django/contrib/admin/templatetags/row_level_permission.py

         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
 
 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)