<commit_before>from django.conf import settings
from django.conf.urls import patterns, include
from tastypie.api import Api
from tastypie.resources import Resource
import importlib
import inspect
from south.signals import post_migrate
from django.contrib.contenttypes.models import ContentType
from django.contrib.auth.models import Permission
from tastypie.authorization import DjangoAuthorization

def add_view_permissions(sender, **kwargs):
  """
  This syncdb hooks takes care of adding a view permission too all our 
  content types.
  """
  # for each of our content types
  for content_type in ContentType.objects.all():
    for action in ['view', 'list']:
      # build our permission slug
      codename = "%s_%s" % (action, content_type.model)

      # if it doesn't exist..
      if not Permission.objects.filter(content_type=content_type, codename=codename):
        # add it
        Permission.objects.create(content_type=content_type,
                                  codename=codename,
                                  name="Can %s %s" % (action, content_type.name))

# check for all our view permissions after a syncdb
post_migrate.connect(add_view_permissions)

class SpiffAuthorization(DjangoAuthorization):
  def read_list(self, object_list, bundle):
    ret = super(SpiffAuthorization, self).read_list(object_list, bundle)
    klass = self.base_checks(bundle.request, object_list.model)
    permName = '%s.list_%s' % (klass._meta.app_label, klass._meta.module_name)
    if not bundle.request.user.has_perm(permName):
      return []
    return ret

  def read_detail(self, object_list, bundle):
    if super(SpiffAuthorization, self).read_detail(object_list, bundle):
      klass = self.base_checks(bundle.request, bundle.obj.__class__)
      permName = '%s.view_%s' % (klass._meta.app_label, klass._meta.module_name)
      return bundle.request.user.has_perm(permName)
    return False

v1_api = Api(api_name='v1')
for app in map(lambda x:'%s.v1_api'%(x), settings.INSTALLED_APPS):
  try:
    appAPI = importlib.import_module(app)
  except ImportError, e:
    continue
  for name, cls in inspect.getmembers(appAPI):
    if inspect.isclass(cls) and issubclass(cls, Resource) and hasattr(cls, 'Meta'):
      v1_api.register(cls())<commit_msg>Genericise the function to find class implementations<commit_after>from django.conf import settings
from django.conf.urls import patterns, include
from tastypie.api import Api
from tastypie.resources import Resource
import importlib
import inspect
from south.signals import post_migrate
from django.contrib.contenttypes.models import ContentType
from django.contrib.auth.models import Permission
from tastypie.authorization import DjangoAuthorization

def add_view_permissions(sender, **kwargs):
  """
  This syncdb hooks takes care of adding a view permission too all our 
  content types.
  """
  # for each of our content types
  for content_type in ContentType.objects.all():
    for action in ['view', 'list']:
      # build our permission slug
      codename = "%s_%s" % (action, content_type.model)

      # if it doesn't exist..
      if not Permission.objects.filter(content_type=content_type, codename=codename):
        # add it
        Permission.objects.create(content_type=content_type,
                                  codename=codename,
                                  name="Can %s %s" % (action, content_type.name))

# check for all our view permissions after a syncdb
post_migrate.connect(add_view_permissions)

class SpiffAuthorization(DjangoAuthorization):
  def read_list(self, object_list, bundle):
    ret = super(SpiffAuthorization, self).read_list(object_list, bundle)
    klass = self.base_checks(bundle.request, object_list.model)
    permName = '%s.list_%s' % (klass._meta.app_label, klass._meta.module_name)
    if not bundle.request.user.has_perm(permName):
      return []
    return ret

  def read_detail(self, object_list, bundle):
    if super(SpiffAuthorization, self).read_detail(object_list, bundle):
      klass = self.base_checks(bundle.request, bundle.obj.__class__)
      permName = '%s.view_%s' % (klass._meta.app_label, klass._meta.module_name)
      return bundle.request.user.has_perm(permName)
    return False

def find_api_classes(module, superclass, test=lambda x: True):
  for app in map(lambda x:'%s.%s'%(x, module), settings.INSTALLED_APPS):
    try:
      appAPI = importlib.import_module(app)
    except ImportError, e:
      continue
    for name, cls in inspect.getmembers(appAPI):
      if inspect.isclass(cls) and issubclass(cls, superclass) and not cls is superclass and test(cls):
        yield cls

v1_api = Api(api_name='v1')
for api in find_api_classes('v1_api', Resource, lambda x:hasattr(x, 'Meta')):
  v1_api.register(api())
{'fraction_non_alphanumeric': 0.09775068320369981, 'fraction_numerical': 0.0016817321841496743, 'mean_word_length': 3.9001029866117403, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2447416', 'n_tokens_mistral': 1439, 'n_tokens_neox': 1446, 'n_words': 378}