Django

Code

Changeset 3674

Show
Ignore:
Timestamp:
08/28/06 14:53:26 (2 years ago)
Author:
clong
Message:

[per-object-permissions] New method: get_model_list in the RowLevelPermission? manager. This returns a list of ids for the given model that the user has the given permission on. It should work better then doing something like has_perm or {% if_has_perm %}
[per-object-permissions] Modified the change_list to use get_model_list instead of the current checking each perm. Note: this method has not been tested extensively, and might cause some problems

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/per-object-permissions/django/contrib/admin/templatetags/admin_list.py

    r3628 r3674  
    106106    pk = cl.lookup_opts.pk.attname 
    107107    #If show_all_rows is set to False, then we have to check the permission on the object 
    108     if not cl.opts.admin.show_all_rows: 
    109         if not cl.user.has_perm(cl.opts.app_label + "." + cl.opts.get_change_permission(), object=result): 
    110             return 
    111         #Update the count 
    112         cl.result_count = cl.result_count +1 
     108 
    113109    for field_name in cl.lookup_opts.admin.list_display: 
    114110        row_class = '' 
  • django/branches/per-object-permissions/django/contrib/admin/views/main.py

    r3669 r3674  
    652652        #is done later in the result_list tag at which point it will calculate the correct 
    653653        #number of rows shown 
    654         if self.opts.admin.show_all_rows: 
    655             self.result_count = result_count 
    656         else: 
    657             self.result_count = 0 
     654         
     655        self.result_count = result_count 
    658656        self.full_result_count = full_result_count 
    659657        self.result_list = result_list 
     
    693691 
    694692    def get_query_set(self): 
    695         qs = self.manager.get_query_set() 
     693        if (not self.opts.admin.show_all_rows) and self.opts.row_level_permissions and (not self.user.has_perm(self.opts.app_label + "."+self.opts.get_change_permission()): 
     694            from django.contrib.auth.models import RowLevelPermission 
     695            qs = self.manager.filter(id__in=RowLevelPermission.objects.get_model_list(self.user,  
     696                                                                                      self.model,  
     697                                                                                      self.opts.get_change_permission())) 
     698        else: 
     699            qs = self.manager.get_query_set() 
    696700        lookup_params = self.params.copy() # a dictionary of the query string 
    697701        for i in (ALL_VAR, ORDER_VAR, ORDER_TYPE_VAR, SEARCH_VAR, IS_POPUP_VAR): 
  • django/branches/per-object-permissions/django/contrib/auth/models.py

    r3657 r3674  
    7272            ret_dict[delete_str]=self.create_row_level_permission(model_instance, owner, delete_str, negative=negDel) 
    7373        return ret_dict     
     74     
     75    def get_model_list(self,user, model, perm): 
     76        model_ct=ContentType.objects.get_for_model(model) 
     77        if isinstance(perm, str): 
     78            perm = Permission.objects.get(codename__exact=perm, content_type=model_ct.id) 
     79        user_model_ids = RowLevelPermission.objects.filter(owner_ct=ContentType.objects.get_for_model(User),  
     80                                          owner_id=user.id, permission=perm.id,  
     81                                          model_ct=model_ct 
     82                                          ).values('model_id')    
     83        user_group_list = [g['id'] for g in user.groups.select_related().values('id')] 
     84        group_model_ids = RowLevelPermission.objects.filter(owner_ct=ContentType.objects.get_for_model(Group).id, 
     85                                          owner_id__in=user_group_list, 
     86                                          model_ct = model_ct 
     87                                          ).values('model_id') 
     88        id_list = [o['model_id'] for o in user_model_ids] + [o['model_id'] for o in group_model_ids] 
     89        return id_list 
    7490 
    7591class RowLevelPermission(models.Model):