Refresh permission:
       Remove deprecated permissions,
       Add generated permissions.

def refresh_permissions(permissions):
    """Refresh permission:
       Remove deprecated permissions,
       Add generated permissions."""
    available_permissions = [x.codename for x in
                             Permission.objects.filter(
                                 codename__startswith=ACL_CODE_PREFIX)]
    active_permissions = permissions.keys()

    perms_to_add = set(active_permissions) - set(available_permissions)
    perms_to_remove = set(available_permissions) - set(active_permissions)

    # Remove deprecated permissions
    for codename in perms_to_remove:
        Permission.objects.filter(codename=codename).delete()

    # Add generated permissions
    ct = ContentType.objects.get_for_model(model=auth_models.User)
    for codename in perms_to_add:
        Permission.objects.get_or_create(codename=codename,
                                         name=permissions[codename],
                                         content_type=ct)