<commit_before># Copyright 2011 the Melange authors.
#
# 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.

"""Module for the GCI participant dashboard."""

import json
import logging

from google.appengine.ext import db

from django import http
from django.utils.translation import ugettext

from codein.views.helper import urls

from melange.request import exception
from melange.request import links

from soc.logic import document as document_logic
from soc.logic import org_app as org_app_logic
from soc.models.org_app_record import OrgAppRecord
from soc.views.dashboard import Component
from soc.views.dashboard import Dashboard
from soc.views.helper import lists
from soc.views.helper import url_patterns

from soc.modules.gci.logic import document as gsoc_document_logic
from soc.modules.gci.logic import task as task_logic
from soc.modules.gci.models.organization import GCIOrganization
from soc.modules.gci.models.profile import GCIProfile
from soc.modules.gci.models.task import GCITask
from soc.modules.gci.models.task import UNPUBLISHED
from soc.modules.gci.views.base import GCIRequestHandler
from soc.modules.gci.views.helper import url_names
from soc.modules.gci.views.helper.url_patterns import url


BACKLINKS_TO_MAIN = {'to': 'main', 'title': 'Main dashboard'}


class MainDashboard(Dashboard):
  """Dashboard for user's main-dashboard
  """

  def __init__(self, data):
    """Initializes the dashboard.

    Args:
      data: The RequestData object
    """
    super(MainDashboard, self).__init__(data)
    self.subpages = _initMainDashboardSubpages(data)

  def context(self):
    """Returns the context of main dashboard."""
    return {
        'title': 'Participant dashboard',
        'name': 'main',
        'subpages': self._divideSubPages(self.subpages),
        'enabled': True
    }

  def addSubpages(self, subpage):
    self.subpages.append(subpage)


def _initMainDashboardSubpages(data):
  """Initializes list of subpages for the main dashboard.

  Args:
    request_data.RequestData for the current request.

  Returns:
    initial list of subpages to set for the main dashboard.
  """
  if not data.profile.is_student and data.timeline.orgsAnnounced():
    connection_dashboard = ConnectionsDashboard(data)

    return [{
        'name': 'connections_dashboard',
        'description': ugettext(
            'Connect with organizations, check current status and '
            'participate in the program.'),
        'title': 'Connections',
        'link': '',
        'subpage_links': connection_dashboard.getSubpagesLink(),
        }]
  else:
    return []


class ComponentsDashboard(Dashboard):
  """Dashboard that holds component list."""

  def __init__(self, data, component_property):
    """Initializes the dashboard.

    Args:
      data: The RequestData object
      component_property: Component property
    """
    super(ComponentsDashboard, self).__init__(data)
    self.name = component_property.get('name')
    self.title = component_property.get('title')
    self.components = [component_property.get('component'),]
    self.backlinks = [component_property.get('backlinks'),]

  def context(self):
    """Returns the context of components dashboard.
    """
    return {
        'title': self.title,
        'name': self.name,
        'backlinks': self.backlinks,
        'components': self.components,
    }


class ConnectionsDashboard(Dashboard):
  """Dashboard grouping connection related elements."""

  def __init__(self, data):
    """Initializes new instance of this class.

    Args:
      data: request_data.RequestData for the current request.
    """
    super(ConnectionsDashboard, self).__init__(data)
    self.subpages = _initConnectionDashboardSubpages(data)


  def context(self):
    """See dashboard.Dashboard.context for specification."""
    subpages = self._divideSubPages(self.subpages)

    return {
        'title': 'Connections',
        'name': 'connections_dashboard',
        'backlinks': [
            {
                'to': 'main',
                'title': 'Participant dashboard'
            },
        ],
        'subpages': subpages
    }


def _initConnectionDashboardSubpages(data):
  """Initializes list of subpages for the connection dashboard.

  Args:
    data: request_data.RequestData for the current request.

  Returns:
    initial list of subpages to set for the connection dashboard.
  """
  subpages = [
      {
          'name': 'list_connections_for_user',
          'description': ugettext(
              'Check status of your existing connections with '
              'organizations and communicate with administrators.'),
          'title': ugettext('See your connections'),
          'link': links.LINKER.program(
              data.program, urls.UrlNames.CONNECTION_PICK_ORG)
      },
      {
          'name': 'connect',
          'description': ugettext(
              'Connect with organizations and request a role to '
              'participate in the program.'),
          'title': ugettext('Connect with organizations'),
          'link': links.LINKER.program(
              data.program, urls.UrlNames.CONNECTION_PICK_ORG)
      }]

  # add organization admin specific items
  if data.profile.is_org_admin:
    subpages.append({
        'name': 'list_connections_for_org_admin',
        'description': ugettext(
            'Manage connections for the organizations for which you have '
            'administrator role at this moment.'),
        'title': ugettext('See organization\'s connections'),
        'link': links.LINKER.profile(
            data.profile, urls.UrlNames.CONNECTION_LIST_FOR_ORG_ADMIN)
        })

    for org in data.org_admin_for:
      subpages.append({
          'name': 'connect_for_%s' % org.link_id,
          'description': ugettext(
              'Connect with users and offer them role in your '
              'organization.'),
          'title': ugettext('Connect users with %s' % org.name),
          'link': links.LINKER.organization(
              org.key(), urls.UrlNames.CONNECTION_START_AS_ORG)
          })

  return subpages


# TODO(nathaniel): Make all attributes of this class private except
# those that fulfill the RequestHandler type.
class DashboardPage(GCIRequestHandler):
  """View for the participant dashboard."""

  def djangoURLPatterns(self):
    """The URL pattern for the dashboard."""
    return [
        url(r'dashboard/%s$' % url_patterns.PROGRAM, self,
            name='gci_dashboard')]

  def checkAccess(self, data, check, mutator):
    """Denies access if you are not logged in."""
    check.isProfileActive()

  def templatePath(self):
    """Returns the path to the template."""
    return 'modules/gci/dashboard/base.html'

  def populateDashboards(self, data):
    """Populates the various dashboard subpages and components for each subpage.
    """
    # dashboard container, will hold each component list
    dashboards = []

    # main container that contains all component list
    main = MainDashboard(data)

    # retrieve active links and add it to the main dashboard
    links = self.links(data)
    for link in links:
      main.addSubpages(link)

    # retrieve active component(s) for currently logged-in user
    components = self.components(data)

    # add components as children of main dashboard and treat the component
    # as dashboard element
    for component in components:
      c = {
          'name': component.context().get('name'),
          'description': component.context().get('description'),
          'title': component.context().get('title'),
          'component_link': True,
          }
      main.addSubpages(c)

      dashboards.append(ComponentsDashboard(data, {
          'name': component.context().get('name'),
          'title': component.context().get('title'),
          'component': component,
          'backlinks': BACKLINKS_TO_MAIN,
          }))

    dashboards.append(main)
    dashboards.append(ConnectionsDashboard(data))<commit_msg>Component with connection items removed from dashboard temporarily.<commit_after># Copyright 2011 the Melange authors.
#
# 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.

"""Module for the GCI participant dashboard."""

import json
import logging

from google.appengine.ext import db

from django import http
from django.utils.translation import ugettext

from codein.views.helper import urls

from melange.request import exception
from melange.request import links

from soc.logic import document as document_logic
from soc.logic import org_app as org_app_logic
from soc.models.org_app_record import OrgAppRecord
from soc.views.dashboard import Component
from soc.views.dashboard import Dashboard
from soc.views.helper import lists
from soc.views.helper import url_patterns

from soc.modules.gci.logic import document as gsoc_document_logic
from soc.modules.gci.logic import task as task_logic
from soc.modules.gci.models.organization import GCIOrganization
from soc.modules.gci.models.profile import GCIProfile
from soc.modules.gci.models.task import GCITask
from soc.modules.gci.models.task import UNPUBLISHED
from soc.modules.gci.views.base import GCIRequestHandler
from soc.modules.gci.views.helper import url_names
from soc.modules.gci.views.helper.url_patterns import url


BACKLINKS_TO_MAIN = {'to': 'main', 'title': 'Main dashboard'}


class MainDashboard(Dashboard):
  """Dashboard for user's main-dashboard
  """

  def __init__(self, data):
    """Initializes the dashboard.

    Args:
      data: The RequestData object
    """
    super(MainDashboard, self).__init__(data)
    self.subpages = _initMainDashboardSubpages(data)

  def context(self):
    """Returns the context of main dashboard."""
    return {
        'title': 'Participant dashboard',
        'name': 'main',
        'subpages': self._divideSubPages(self.subpages),
        'enabled': True
    }

  def addSubpages(self, subpage):
    self.subpages.append(subpage)


def _initMainDashboardSubpages(data):
  """Initializes list of subpages for the main dashboard.

  Args:
    request_data.RequestData for the current request.

  Returns:
    initial list of subpages to set for the main dashboard.
  """
  if False:
  # TODO(daniel): re-enable when connection views are back
  #if not data.profile.is_student and data.timeline.orgsAnnounced():
    connection_dashboard = ConnectionsDashboard(data)

    return [{
        'name': 'connections_dashboard',
        'description': ugettext(
            'Connect with organizations, check current status and '
            'participate in the program.'),
        'title': 'Connections',
        'link': '',
        'subpage_links': connection_dashboard.getSubpagesLink(),
        }]
  else:
    return []


class ComponentsDashboard(Dashboard):
  """Dashboard that holds component list."""

  def __init__(self, data, component_property):
    """Initializes the dashboard.

    Args:
      data: The RequestData object
      component_property: Component property
    """
    super(ComponentsDashboard, self).__init__(data)
    self.name = component_property.get('name')
    self.title = component_property.get('title')
    self.components = [component_property.get('component'),]
    self.backlinks = [component_property.get('backlinks'),]

  def context(self):
    """Returns the context of components dashboard.
    """
    return {
        'title': self.title,
        'name': self.name,
        'backlinks': self.backlinks,
        'components': self.components,
    }


class ConnectionsDashboard(Dashboard):
  """Dashboard grouping connection related elements."""

  def __init__(self, data):
    """Initializes new instance of this class.

    Args:
      data: request_data.RequestData for the current request.
    """
    super(ConnectionsDashboard, self).__init__(data)
    self.subpages = _initConnectionDashboardSubpages(data)


  def context(self):
    """See dashboard.Dashboard.context for specification."""
    subpages = self._divideSubPages(self.subpages)

    return {
        'title': 'Connections',
        'name': 'connections_dashboard',
        'backlinks': [
            {
                'to': 'main',
                'title': 'Participant dashboard'
            },
        ],
        'subpages': subpages
    }


def _initConnectionDashboardSubpages(data):
  """Initializes list of subpages for the connection dashboard.

  Args:
    data: request_data.RequestData for the current request.

  Returns:
    initial list of subpages to set for the connection dashboard.
  """
  subpages = [
      {
          'name': 'list_connections_for_user',
          'description': ugettext(
              'Check status of your existing connections with '
              'organizations and communicate with administrators.'),
          'title': ugettext('See your connections'),
          'link': links.LINKER.program(
              data.program, urls.UrlNames.CONNECTION_PICK_ORG)
      },
      {
          'name': 'connect',
          'description': ugettext(
              'Connect with organizations and request a role to '
              'participate in the program.'),
          'title': ugettext('Connect with organizations'),
          'link': links.LINKER.program(
              data.program, urls.UrlNames.CONNECTION_PICK_ORG)
      }]

  # add organization admin specific items
  if data.profile.is_org_admin:
    subpages.append({
        'name': 'list_connections_for_org_admin',
        'description': ugettext(
            'Manage connections for the organizations for which you have '
            'administrator role at this moment.'),
        'title': ugettext('See organization\'s connections'),
        'link': links.LINKER.profile(
            data.profile, urls.UrlNames.CONNECTION_LIST_FOR_ORG_ADMIN)
        })

    for org in data.org_admin_for:
      subpages.append({
          'name': 'connect_for_%s' % org.link_id,
          'description': ugettext(
              'Connect with users and offer them role in your '
              'organization.'),
          'title': ugettext('Connect users with %s' % org.name),
          'link': links.LINKER.organization(
              org.key(), urls.UrlNames.CONNECTION_START_AS_ORG)
          })

  return subpages


# TODO(nathaniel): Make all attributes of this class private except
# those that fulfill the RequestHandler type.
class DashboardPage(GCIRequestHandler):
  """View for the participant dashboard."""

  def djangoURLPatterns(self):
    """The URL pattern for the dashboard."""
    return [
        url(r'dashboard/%s$' % url_patterns.PROGRAM, self,
            name='gci_dashboard')]

  def checkAccess(self, data, check, mutator):
    """Denies access if you are not logged in."""
    check.isProfileActive()

  def templatePath(self):
    """Returns the path to the template."""
    return 'modules/gci/dashboard/base.html'

  def populateDashboards(self, data):
    """Populates the various dashboard subpages and components for each subpage.
    """
    # dashboard container, will hold each component list
    dashboards = []

    # main container that contains all component list
    main = MainDashboard(data)

    # retrieve active links and add it to the main dashboard
    links = self.links(data)
    for link in links:
      main.addSubpages(link)

    # retrieve active component(s) for currently logged-in user
    components = self.components(data)

    # add components as children of main dashboard and treat the component
    # as dashboard element
    for component in components:
      c = {
          'name': component.context().get('name'),
          'description': component.context().get('description'),
          'title': component.context().get('title'),
          'component_link': True,
          }
      main.addSubpages(c)

      dashboards.append(ComponentsDashboard(data, {
          'name': component.context().get('name'),
          'title': component.context().get('title'),
          'component': component,
          'backlinks': BACKLINKS_TO_MAIN,
          }))

    dashboards.append(main)
    # TODO(daniel): re-enable when connection views are back
    #dashboards.append(ConnectionsDashboard(data))
{'fraction_non_alphanumeric': 0.1002453557658605, 'fraction_numerical': 0.0009346886318495151, 'mean_word_length': 3.098395977974623, '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': '395558', 'n_tokens_mistral': 4776, 'n_tokens_neox': 4656, 'n_words': 1402}