<commit_before># Copyright (C) 2014 Universidad Politecnica de Madrid
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import os
import logging

from django.conf import settings
from django.core.urlresolvers import reverse

from horizon import exceptions
from horizon import forms
from horizon import tables
from horizon import tabs
from horizon import workflows

from openstack_dashboard import api
from openstack_dashboard import fiware_api
from openstack_dashboard.dashboards.idm import utils as idm_utils
from openstack_dashboard.dashboards.idm import views as idm_views
from openstack_dashboard.dashboards.idm.organizations \
    import tables as organization_tables
from openstack_dashboard.dashboards.idm.organizations \
    import tabs as organization_tabs
from openstack_dashboard.dashboards.idm.organizations \
    import forms as organization_forms
from openstack_dashboard.dashboards.idm.organizations \
    import workflows as organization_workflows


LOG = logging.getLogger('idm_logger')
AVATAR_ROOT = os.path.abspath(os.path.join(
    settings.MEDIA_ROOT, 'OrganizationAvatar'))

class IndexView(tabs.TabbedTableView):
    tab_group_class = organization_tabs.PanelTabs
    template_name = 'idm/organizations/index.html'


class CreateOrganizationView(forms.ModalFormView):
    form_class = organization_forms.CreateOrganizationForm
    template_name = 'idm/organizations/create.html'


class DetailOrganizationView(tables.MultiTableView):
    template_name = 'idm/organizations/detail.html'
    table_classes = (organization_tables.MembersTable,
                     organization_tables.AuthorizingApplicationsTable)
    
    def get_members_data(self):
        users = []
        try:
            # NOTE(garcianavalon) Filtering by project in user_list
            # filters by default_project_id.
            # We need to get the role_assignments for the user's
            # id's and then filter the user list ourselves
            all_users = api.keystone.user_list(self.request)
            project_users_roles = api.keystone.get_project_users_roles(
                self.request,
                project=self.kwargs['organization_id'])
            users = [user for user in all_users if user.id in project_users_roles]
        except Exception:
            exceptions.handle(self.request,
                              ("Unable to retrieve member information."))
        return users

    def get_applications_data(self):
        applications = []
        try:
            # TODO(garcianavalon) extract to fiware_api
            all_apps = fiware_api.keystone.application_list(self.request)
            apps_with_roles = [
                a.application_id for a 
                in fiware_api.keystone.organization_role_assignments(
                self.request, organization=self.kwargs['organization_id'])
            ]
            applications = [app for app in all_apps 
                            if app.id in apps_with_roles]
            for app in applications:
                users = idm_utils.get_counter(self, application=app)
                setattr(app, 'counter', users)
        except Exception:
            exceptions.handle(self.request,
                              ("Unable to retrieve application list."))
        return idm_utils.filter_default(applications)

    def _can_edit(self):
        # Allowed if he is an admin in the organization
        # TODO(garcianavalon) move to fiware_api
        org_id = self.kwargs['organization_id']
        user_roles = api.keystone.roles_for_user(
            self.request, self.request.user.id, project=org_id)
        return 'admin' in [r.name for r in user_roles]

    def get_context_data(self, **kwargs):
        context = super(DetailOrganizationView, self).get_context_data(**kwargs)
        organization_id = self.kwargs['organization_id']
        organization = api.keystone.tenant_get(self.request, organization_id, admin=True)
        context['contact_info'] = organization.description
        context['organization.id'] = organization.id
        context['organization_name'] = organization.name
        if hasattr(organization, 'img_original'):<commit_msg>updated allowed to use owner role<commit_after># Copyright (C) 2014 Universidad Politecnica de Madrid
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import os
import logging

from django.conf import settings
from django.core.urlresolvers import reverse

from horizon import exceptions
from horizon import forms
from horizon import tables
from horizon import tabs
from horizon import workflows

from openstack_dashboard import api
from openstack_dashboard import fiware_api
from openstack_dashboard.dashboards.idm import utils as idm_utils
from openstack_dashboard.dashboards.idm import views as idm_views
from openstack_dashboard.dashboards.idm.organizations \
    import tables as organization_tables
from openstack_dashboard.dashboards.idm.organizations \
    import tabs as organization_tabs
from openstack_dashboard.dashboards.idm.organizations \
    import forms as organization_forms
from openstack_dashboard.dashboards.idm.organizations \
    import workflows as organization_workflows


LOG = logging.getLogger('idm_logger')
AVATAR_ROOT = os.path.abspath(os.path.join(
    settings.MEDIA_ROOT, 'OrganizationAvatar'))

class IndexView(tabs.TabbedTableView):
    tab_group_class = organization_tabs.PanelTabs
    template_name = 'idm/organizations/index.html'


class CreateOrganizationView(forms.ModalFormView):
    form_class = organization_forms.CreateOrganizationForm
    template_name = 'idm/organizations/create.html'


class DetailOrganizationView(tables.MultiTableView):
    template_name = 'idm/organizations/detail.html'
    table_classes = (organization_tables.MembersTable,
                     organization_tables.AuthorizingApplicationsTable)
    
    def get_members_data(self):
        users = []
        try:
            # NOTE(garcianavalon) Filtering by project in user_list
            # filters by default_project_id.
            # We need to get the role_assignments for the user's
            # id's and then filter the user list ourselves
            all_users = api.keystone.user_list(self.request)
            project_users_roles = api.keystone.get_project_users_roles(
                self.request,
                project=self.kwargs['organization_id'])
            users = [user for user in all_users if user.id in project_users_roles]
        except Exception:
            exceptions.handle(self.request,
                              ("Unable to retrieve member information."))
        return users

    def get_applications_data(self):
        applications = []
        try:
            # TODO(garcianavalon) extract to fiware_api
            all_apps = fiware_api.keystone.application_list(self.request)
            apps_with_roles = [
                a.application_id for a 
                in fiware_api.keystone.organization_role_assignments(
                self.request, organization=self.kwargs['organization_id'])
            ]
            applications = [app for app in all_apps 
                            if app.id in apps_with_roles]
            for app in applications:
                users = idm_utils.get_counter(self, application=app)
                setattr(app, 'counter', users)
        except Exception:
            exceptions.handle(self.request,
                              ("Unable to retrieve application list."))
        return idm_utils.filter_default(applications)

    def _can_edit(self):
        # Allowed if he is an owner in the organization
        # TODO(garcianavalon) move to fiware_api
        org_id = self.kwargs['organization_id']
        user_roles = api.keystone.roles_for_user(
            self.request, self.request.user.id, project=org_id)
        owner_role = fiware_api.keystone.get_owner_role(self.request)
        return owner_role.id in [r.id for r in user_roles]

    def get_context_data(self, **kwargs):
        context = super(DetailOrganizationView, self).get_context_data(**kwargs)
        organization_id = self.kwargs['organization_id']
        organization = api.keystone.tenant_get(self.request, organization_id, admin=True)
        context['contact_info'] = organization.description
        context['organization.id'] = organization.id
        context['organization_name'] = organization.name
        if hasattr(organization, 'img_original'):
{'fraction_non_alphanumeric': 0.06905724622260055, 'fraction_numerical': 0.0017024898914662695, 'mean_word_length': 3.0425806451612902, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5322486', 'n_tokens_mistral': 2543, 'n_tokens_neox': 2483, 'n_words': 690}