<commit_before>#!/usr/bin/env python2.5
#
# Copyright 2008 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.

"""Helpers used to render lists.
"""

__authors__ = [
  '"<NAME>" <<EMAIL>>',
  '"<NAME>" <<EMAIL>>',
  '"<NAME>" <<EMAIL>>',
  '"<NAME>" <<EMAIL>>',
  ]


from django.utils import simplejson

from soc.logic import dicts
from soc.logic.models.user import logic as user_logic

import soc.views.helper.forms


URL_PATTERN = '<a href="%(url)s"%(target)s%(nofollow)s>%(name)s</a>'


def urlize(url, name=None, target="_blank", nofollow=True):
  """Make an url clickable.

  Args:
    url: the actual url, such as '/user/list'
    name: the display name, such as 'List Users', defaults to url
    target: the 'target' attribute of the <a> element
    nofollow: whether to add the 'rel="nofollow"' attribute
  """

  from django.utils.safestring import mark_safe
  from django.utils.html import escape

  safe_url = escape(url)
  safe_name = escape(name)

  link = URL_PATTERN % {
      'url': safe_url,
      'name': safe_name if name else safe_url,
      'target': ' target="%s"' % target if target else '',
      'nofollow': ' rel="nofollow"' if nofollow else "",
  }

  return mark_safe(link)


def entityToRowDict(entity, key_order, no_filter, extra_cols_func,
                    button_ops_func, row_ops_func, args):
  """Returns the row dict for the specified entity.
  """

  extra_cols = extra_cols_func(entity, *args)
  button_ops = button_ops_func(entity, *args)
  row_ops = row_ops_func(entity, *args)

  fields = set(key_order).difference(set(extra_cols))

  columns = entity.toDict(list(fields))
  columns.update(extra_cols)
  columns['key'] = str(entity.key().id_or_name())

  filter_fields = [i for i in columns.keys() if i not in no_filter]

  columns = dicts.cleanDict(columns, filter_fields)

  operations = {
      "row": row_ops,
      "buttons": button_ops,
  }

  result = {
      "columns": columns,
      "operations": operations,
  }

  return result


def keyToColumnProperties(key, col_props, hidden):
  """Returns the column properties for the specified key.
  """

  props = {
    'name': key,
    'index': key,
    'resizable': True,
  }

  if key == 'key' or key in hidden:
    props['hidden'] = True

  if key in hidden:
    props['searchoptions'] = {"searchhidden": True}

  extra_props = col_props.get(key, {})
  props.update(extra_props)

  return props


def getKeyOrderAndColNames(params, visibility):
  """Retrieve key order and col names
  """

  key_order = ["key"] + params.get('%s_field_keys' % visibility)
  col_names = ["Key"] + params.get('%s_field_names' % visibility)
  ignore = params.get('%s_field_ignore' % visibility, [])

  for field in ignore:
    if field not in key_order:
      continue

    pos = key_order.index(field)
    key_order = key_order[:pos] + key_order[pos+1:]
    col_names = col_names[:pos] + col_names[pos+1:]

  if not (key_order and col_names):
    key_order = col_names = ['kind']

  return key_order, col_names


def getListConfiguration(request, params, visibility, order):
  """Returns the list data for the specified params.

  Args:
    visibility: determines which list will be used
    order: the order the data should be sorted in
  """

  key_order, col_names = getKeyOrderAndColNames(params, visibility)

  conf_extra = params.get('%s_conf_extra' % visibility, {})
  conf_min_num = params.get('%s_conf_min_num' % visibility, 0)
  button_global = params.get('%s_button_global' % visibility, [])
  row_action = params.get('%s_row_action' % visibility, {})
  col_props = params.get('%s_field_props' % visibility, {})<commit_msg>Add list helper functions<commit_after>#!/usr/bin/env python2.5
#
# Copyright 2008 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.

"""Helpers used to render lists.
"""

__authors__ = [
  '"<NAME>" <<EMAIL>>',
  '"<NAME>" <<EMAIL>>',
  '"<NAME>" <<EMAIL>>',
  '"<NAME>" <<EMAIL>>',
  ]


from django.utils import simplejson

from soc.logic import dicts
from soc.logic.models.user import logic as user_logic

import soc.views.helper.forms


URL_PATTERN = '<a href="%(url)s"%(target)s%(nofollow)s>%(name)s</a>'


def urlize(url, name=None, target="_blank", nofollow=True):
  """Make an url clickable.

  Args:
    url: the actual url, such as '/user/list'
    name: the display name, such as 'List Users', defaults to url
    target: the 'target' attribute of the <a> element
    nofollow: whether to add the 'rel="nofollow"' attribute
  """

  from django.utils.safestring import mark_safe
  from django.utils.html import escape

  safe_url = escape(url)
  safe_name = escape(name)

  link = URL_PATTERN % {
      'url': safe_url,
      'name': safe_name if name else safe_url,
      'target': ' target="%s"' % target if target else '',
      'nofollow': ' rel="nofollow"' if nofollow else "",
  }

  return mark_safe(link)


def entityToRowDict(entity, key_order, no_filter, extra_cols_func,
                    button_ops_func, row_ops_func, args):
  """Returns the row dict for the specified entity.
  """

  extra_cols = extra_cols_func(entity, *args)
  button_ops = button_ops_func(entity, *args)
  row_ops = row_ops_func(entity, *args)

  fields = set(key_order).difference(set(extra_cols))

  columns = entity.toDict(list(fields))
  columns.update(extra_cols)
  columns['key'] = str(entity.key().id_or_name())

  filter_fields = [i for i in columns.keys() if i not in no_filter]

  columns = dicts.cleanDict(columns, filter_fields)

  operations = {
      "row": row_ops,
      "buttons": button_ops,
  }

  result = {
      "columns": columns,
      "operations": operations,
  }

  return result


def keyToColumnProperties(key, col_props, hidden):
  """Returns the column properties for the specified key.
  """

  props = {
    'name': key,
    'index': key,
    'resizable': True,
  }

  if key == 'key' or key in hidden:
    props['hidden'] = True

  if key in hidden:
    props['searchoptions'] = {"searchhidden": True}

  extra_props = col_props.get(key, {})
  props.update(extra_props)

  return props


def getKeyOrderAndColNames(params, visibility):
  """Retrieve key order and col names
  """

  key_order = ["key"] + params.get('%s_field_keys' % visibility)
  col_names = ["Key"] + params.get('%s_field_names' % visibility)
  ignore = params.get('%s_field_ignore' % visibility, [])

  for field in ignore:
    if field not in key_order:
      continue

    pos = key_order.index(field)
    key_order = key_order[:pos] + key_order[pos+1:]
    col_names = col_names[:pos] + col_names[pos+1:]

  if not (key_order and col_names):
    key_order = col_names = ['kind']

  return key_order, col_names


def isJsonRequest(request):
  """Returns true iff the request is a JSON request.
  """

  if request.GET.get('fmt') == 'json':
    return True

  return False


def isDataRequest(request):
  """Returns true iff the request is a data request.
  """

  if isJsonRequest(request):
    return True

  return False


def getListIndex(request):
  """Returns the index of the requested list.
  """

  # get index
  idx = request.GET.get('idx', '')
  idx = int(idx) if idx.isdigit() else -1

  return idx


def getErrorResponse(request, msg):
  """Returns an error appropriate for the request type.
  """

  from soc.views.helper import responses

  if isJsonRequest(request):
    return responses.jsonErrorResponse(request, msg)

  raise Exception(msg)


def getResponse(request, contents):
  """Returns a response appropriate for the request type.
  """

  from soc.views.helper import responses
  from django.utils import simplejson

  if isJsonRequest(request):
    json = simplejson.dumps(contents)
    return responses.jsonResponse(request, json)

  # TODO(SRabbelier): this is probably the best way to handle this
  return contents


def getListConfiguration(request, params, visibility, order):
  """Returns the list data for the specified params.

  Args:
    visibility: determines which list will be used
    order: the order the data should be sorted in
  """

  key_order, col_names = getKeyOrderAndColNames(params, visibility)

  conf_extra = params.get('%s_conf_extra' % visibility, {})
  conf_min_num = params.get('%s_conf_min_num' % visibility, 0)
  button_global = params.get('%s_button_global' % visibility, [])
  row_action = params.get('%s_row_action' % visibility, {})
  col_props = params.get('%s_field_props' % visibility, {})
{'fraction_non_alphanumeric': 0.12450824029771398, 'fraction_numerical': 0.0028708133971291866, 'mean_word_length': 3.922030350601779, 'pattern_counts': {'":': 10, '<': 33, '<?xml version=': 0, '>': 33, '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': '3194775', 'n_tokens_mistral': 3077, 'n_tokens_neox': 2959, 'n_words': 905}