<commit_before>from lingcod.sharing.models import ShareableContent, SharingError, NotShareable
from lingcod.common.utils import get_logger
from django.http import HttpResponse, Http404
from django.contrib.auth.models import User, Group, Permission
from django.contrib.contenttypes.models import ContentType
from django.conf import settings

logger = get_logger()

def get_shareables():
    """
    Introspects the current project and looks for 
    * a can_share* permission and its associated model name
    * if the model is a registered ShareableContent instance
    * whether those models have a user field (fk to auth Users)
    * whether those models have a sharing_groups ManyToMany field 
    * whether those model managers implement the all_for_user() method (ie the ShareableGeoManager)

    returns dict of models and their associated sharing permission 
     i.e. {'mlpampa': (<MplaMpa model class>, <CanShareMpa permission instance>) }
    """
    perms = Permission.objects.filter(codename__startswith="can_share")
    shareable = {}
    entries = ShareableContent.objects.all()
    registered_shareable = [x.shared_content_type for x in entries]
    for p in perms:
        model_class = p.content_type.model_class()
        # MP TODO this checks if field exists but need to do more robust checking on what type of field, where the fk points, etc
        if model_class and p.content_type in registered_shareable and \
           model_class.__dict__.has_key('sharing_groups') and \
           model_class.__dict__.has_key('user') and \
           model_class.__dict__['user'].__class__.__name__ == 'ReverseSingleRelatedObjectDescriptor' and \
           model_class.__dict__['sharing_groups'].__class__.__name__ == 'ReverseManyRelatedObjectsDescriptor':<commit_msg>don't worry about the validation in the get_shareables function call<commit_after>from lingcod.sharing.models import ShareableContent, SharingError, NotShareable
from lingcod.common.utils import get_logger
from django.http import HttpResponse, Http404
from django.contrib.auth.models import User, Group, Permission
from django.contrib.contenttypes.models import ContentType
from django.conf import settings

logger = get_logger()

def get_shareables():
    """
    Introspects the current project and looks for 
    * a can_share* permission and its associated model name
    * if the model is a registered ShareableContent instance
    * whether those models have a user field (fk to auth Users)
    * whether those models have a sharing_groups ManyToMany field 
    * whether those model managers implement the all_for_user() method (ie the ShareableGeoManager)

    returns dict of models and their associated sharing permission 
     i.e. {'mlpampa': (<MplaMpa model class>, <CanShareMpa permission instance>) }
    """
    perms = Permission.objects.filter(codename__startswith="can_share")
    shareable = {}
    entries = ShareableContent.objects.all()
    registered_shareable = [x.shared_content_type for x in entries]
    for p in perms:
        model_class = p.content_type.model_class()
        if model_class and p.content_type in registered_shareable and \
           model_class.__dict__.has_key('sharing_groups') and \
           model_class.__dict__.has_key('user') and \
           model_class.__dict__['user'].__class__.__name__ == 'ReverseSingleRelatedObjectDescriptor' and \
           model_class.__dict__['sharing_groups'].__class__.__name__ == 'ReverseManyRelatedObjectsDescriptor':
{'fraction_non_alphanumeric': 0.07226842558072842, 'fraction_numerical': 0.0017206767995411529, 'mean_word_length': 4.121879588839941, 'pattern_counts': {'":': 0, '<': 7, '<?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': '1040252', 'n_tokens_mistral': 949, 'n_tokens_neox': 915, 'n_words': 304}