<commit_before>from django.db import models
from django.contrib.auth.models import User, Group, Permission

from djangotoolbox.fields import ListField

        
class UserPermissionList(models.Model):
    user = models.ForeignKey(User)
    _fk_list = ListField(models.ForeignKey(Permission))

    def _get_permissions(self):
        if not hasattr(self, '_permissions_cache'):
            perm_ids = self._fk_list
            permissions = set()
            if len(perm_ids) > 0:
                # order_by() has to be used to override invalid default Permission filter
                permissions.update(Permission.objects.filter(id__in=perm_ids).order_by('name'))
            setattr(self, '_permissions_cache', permissions)
            
        return self._permissions_cache
    permission_list = property(_get_permissions)


class GroupPermissionList(models.Model):
    group = models.ForeignKey(Group)
    _fk_list = ListField(models.ForeignKey(Permission))

    def _get_permissions(self):
        if not hasattr(self, '_permissions_cache'):
            perm_ids = self._fk_list
            permissions = set()
            if len(perm_ids) > 0:
                # order_by() has to be used to override invalid default Permission filter
                permissions.update(Permission.objects.filter(id__in=perm_ids).order_by('name'))
            setattr(self, '_permissions_cache', permissions)
            
        return self._permissions_cache
    permissions = property(_get_permissions)

class GroupList(models.Model):
    """
    GroupLists are used to map a list of groups to a user
    """
    user = models.ForeignKey(User)
    _fk_list = ListField(models.ForeignKey(Group))

    def __unicode__(self):
        return u'%s' %(self.user.username)
    
    def _get_group_list(self):
        if not hasattr(self, '_groups_cache'):
            group_ids = self._fk_list
            groups = set()
            if len(group_ids) > 0:
                # order_by() has to be used to override invalid default Permission filter
                groups.update(Group.objects.filter(id__in=group_ids))
            setattr(self, '_groups_cache', groups)
            
        return self._groups_cache
    groups = property(_get_group_list)

    <commit_msg>add more generic get_objs function<commit_after>from django.db import models
from django.contrib.auth.models import User, Group, Permission

from djangotoolbox.fields import ListField

def get_objs(obj_cls, obj_ids):
    objs = set()
    if len(obj_ids) > 0:
        # order_by() has to be used to override invalid default Permission filter
        objs.update(obj_cls .objects.filter(id__in=obj_ids).order_by('name'))
    return objs

class UserPermissionList(models.Model):
    user = models.ForeignKey(User)
    _fk_list = ListField(models.ForeignKey(Permission))

    def _get_permissions(self):
        if not hasattr(self, '_permissions_cache'):
            setattr(self, '_permissions_cache', get_objs(Permission, self._fk_list))            
        return self._permissions_cache
    permission_list = property(_get_permissions)


class GroupPermissionList(models.Model):
    group = models.ForeignKey(Group)
    _fk_list = ListField(models.ForeignKey(Permission))

    def _get_permissions(self):
        if not hasattr(self, '_permissions_cache'):
            setattr(self, '_permissions_cache', get_objs(Permission, self._fk_list))            
        return self._permissions_cache
    permissions = property(_get_permissions)

class GroupList(models.Model):
    """
    GroupLists are used to map a list of groups to a user
    """
    user = models.ForeignKey(User)
    _fk_list = ListField(models.ForeignKey(Group))

    def __unicode__(self):
        return u'%s' %(self.user.username)
    
    def _get_group_list(self):
        if not hasattr(self, '_groups_cache'):
            setattr(self, '_groups_cache', get_objs(Group, self._fk_list))
        return self._groups_cache
    groups = property(_get_group_list)
{'fraction_non_alphanumeric': 0.09052156469408225, 'fraction_numerical': 0.0010030090270812437, 'mean_word_length': 2.961271102284012, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2747129', 'n_tokens_mistral': 1170, 'n_tokens_neox': 1145, 'n_words': 263}