def update_app_permissions(**kwargs):
  # Map app->action->HuePermission.

  # The HuePermission model needs to be sync'd for the following code to work
  # The point of 'if u'useradmin_huepermission' in connection.introspection.table_names():'
  # is to check if Useradmin has been installed.
  # It is okay to follow appmanager.DESKTOP_APPS before they've been sync'd
  # because apps are referenced by app name in Hue permission and not by model ID.
  if u'useradmin_huepermission' in connection.introspection.table_names():
    current = {}

    try:
      for dp in HuePermission.objects.all():
        current.setdefault(dp.app, {})[dp.action] = dp
    except:
      LOG.exception('failed to get permissions')
      return

    updated = 0
    uptodate = 0
    added = [ ]

    for app_obj in appmanager.DESKTOP_APPS:
      app = app_obj.name
      actions = set([("access", "Launch this application")])
      actions.update(getattr(app_obj.settings, "PERMISSION_ACTIONS", []))

      if app not in current:
        current[app] = {}

      for action, description in actions:
        c = current[app].get(action)
        if c:
          if c.description != description:
            c.description = description
            c.save()
            updated += 1
          else:
            uptodate += 1
        else:
          new_dp = HuePermission(app=app, action=action, description=description)
          new_dp.save()
          added.append(new_dp)

    # Add all hue permissions to default group.
    default_group = get_default_user_group()
    if default_group:
      for new_dp in added:
        if not (new_dp.app == 'useradmin' and new_dp.action == 'access') and \
           not (new_dp.app == 'metastore' and new_dp.action == 'write') and \
           not (new_dp.app == 'hbase' and new_dp.action == 'write') and \
           not (new_dp.app == 'security' and new_dp.action == 'impersonate') and \
           not (new_dp.app == 'oozie' and new_dp.action == 'disable_editor_access'):
          GroupPermission.objects.create(group=default_group, hue_permission=new_dp)

    available = HuePermission.objects.count()

    LOG.info("HuePermissions: %d added, %d updated, %d up to date, %d stale" %
           (len(added),
            updated,
            uptodate,
            available - len(added) - updated - uptodate))
Clone a cookbook from teh opscode cookbooks repository
dataset/ETHPy150Open cloudera/hue/apps/useradmin/src/useradmin/models.py/update_app_permissions--crossover with--dataset/ETHPy150Open nacx/kahuna/kahuna/utils/git.py/clone_opscode_cookbook