Example #1
0
    def testGroupby(self):
        """Tests if a list of dictionaries is grouped by a group_key.
    """
        target = [{"a": 1, "b": 2}, {"a": 3, "b": 4}, {"a": 1, "c": 4}]

        group_key = "a"
        expected = {1: [{"a": 1, "b": 2}, {"a": 1, "c": 4}], 3: [{"a": 3, "b": 4}]}
        self.assertEqual(dicts.groupby(target, group_key), expected)

        group_key = ""
        expected = {}
        self.assertRaises(KeyError, dicts.groupby, target, group_key)

        group_key = "f"
        self.assertRaises(KeyError, dicts.groupby, target, group_key)

        target = []
        group_key = ""
        expected = {}
        self.assertEqual(dicts.groupby(target, group_key), expected)

        target = []
        group_key = "a"
        expected = {}
        self.assertEqual(dicts.groupby(target, group_key), expected)
Example #2
0
  def testGroupby(self):
    """Tests if a list of dictionaries is grouped by a group_key.
    """
    target = [{'a':1, 'b': 2}, {'a':3, 'b': 4}, {'a':1, 'c': 4}]

    group_key = 'a'
    expected = {1: [{'a':1, 'b': 2}, {'a':1, 'c': 4}], 3: [{'a':3, 'b': 4}]}
    self.assertEqual(dicts.groupby(target, group_key), expected)

    group_key = ''
    expected = {}
    self.assertRaises(KeyError, dicts.groupby, target, group_key)

    group_key = 'f'
    self.assertRaises(KeyError, dicts.groupby, target, group_key)

    target = []
    group_key = ''
    expected = {}
    self.assertEqual(dicts.groupby(target, group_key), expected)

    target = []
    group_key = 'a'
    expected = {}
    self.assertEqual(dicts.groupby(target, group_key), expected)
Example #3
0
    def testGroupby(self):
        """Tests if a list of dictionaries is grouped by a group_key.
    """
        target = [{'a': 1, 'b': 2}, {'a': 3, 'b': 4}, {'a': 1, 'c': 4}]

        group_key = 'a'
        expected = {
            1: [{
                'a': 1,
                'b': 2
            }, {
                'a': 1,
                'c': 4
            }],
            3: [{
                'a': 3,
                'b': 4
            }]
        }
        self.assertEqual(dicts.groupby(target, group_key), expected)

        group_key = ''
        expected = {}
        self.assertRaises(KeyError, dicts.groupby, target, group_key)

        group_key = 'f'
        self.assertRaises(KeyError, dicts.groupby, target, group_key)

        target = []
        group_key = ''
        expected = {}
        self.assertEqual(dicts.groupby(target, group_key), expected)

        target = []
        group_key = 'a'
        expected = {}
        self.assertEqual(dicts.groupby(target, group_key), expected)
Example #4
0
def orgStats(target, orgs):
  """Retrieves org stats.
  """

  from soc.logic import dicts

  orgs = [(v.key(), v) for k, v in orgs.iteritems()]
  orgs = dict(orgs)

  grouped = dicts.groupby(target.values(), '_org')

  grouped = [(orgs[k], v) for k, v in grouped.iteritems()]
  popularity = [(k.link_id, len(v)) for k, v in grouped]

  return dict(grouped), dict(popularity)
Example #5
0
def orgStats(target, orgs):
  """Retrieves org stats.
  """

  from soc.logic import dicts

  orgs = [(v.key(), v) for k, v in orgs.iteritems()]
  orgs = dict(orgs)

  grouped = dicts.groupby(target.values(), '_org')

  grouped = [(orgs[k], v) for k, v in grouped.iteritems()]
  popularity = [(k.link_id, len(v)) for k, v in grouped]

  return dict(grouped), dict(popularity)
Example #6
0
def as_table_helper(context, form):
  """See as_table().
  """
  
  fields = []
  hidden_fields = []
  hidden_fields_errors = []
  errors = False
  
  # entity = context['entity']

  # Iterate over all fields and prepare it for adding 
  for name, field in form.fields.items():
    if not field:
      continue

    bf = forms_in.BoundField(form, field, name)
    attrs = {}

    if isinstance(field, widgets.ReferenceField):
      attrs = field.rf

    # If the field is hidden we display it elsewhere
    if not bf.is_hidden:
      if bf.errors:
        errors = True

      example_text = ''
      group = '0. '

      if hasattr(field, 'group'):
        group = field.group

      if hasattr(field, 'example_text'):
        example_text = force_unicode(field.example_text)

      item = {
          'field': bf,
          'required': field.required,
          'example_text': example_text,
          'group': group,
          }

      item.update(attrs)
      fields.append(item)
    else:
      hidden_fields.append(unicode(bf))

      for error in bf.errors:
        item = (name, force_unicode(error))
        hidden_fields_errors.append(item)

  grouped = dicts.groupby(fields, 'group')
  rexp = re.compile(r"\d+. ")
  fields = [(rexp.sub('', key), grouped[key]) for key in sorted(grouped)]

  context.update({
      'top_errors': form.non_field_errors() or '',
      'hidden_field_errors': hidden_fields_errors or '',
      'errors': errors,
      'groups': fields if fields else '',
      'hidden_fields': hidden_fields or '',
      'form': form,
      })

  return context
Example #7
0
def as_table_helper(context, form):
    """See as_table().
  """

    fields = []
    hidden_fields = []
    hidden_fields_errors = []
    errors = False

    # entity = context['entity']

    # support defining output order like in Django
    a = form.Meta
    if hasattr(form, 'Meta') and form.Meta.fields:
        items = [(i, form.fields[i]) for i in form.Meta.fields]
    else:
        items = form.fields.items()

    # Iterate over all fields and prepare it for adding
    for name, field in items:
        if not field:
            continue

        bf = forms_in.BoundField(form, field, name)
        attrs = {}

        if isinstance(field, widgets.ReferenceField):
            attrs = field.rf

        # If the field is hidden we display it elsewhere
        if not bf.is_hidden:
            if bf.errors:
                errors = True

            example_text = ''
            group = '0. '

            if hasattr(field, 'group'):
                group = field.group

            if hasattr(field, 'example_text'):
                example_text = force_unicode(field.example_text)

            item = {
                'field': bf,
                'required': field.required,
                'example_text': example_text,
                'group': group,
            }

            item.update(attrs)
            fields.append(item)
        else:
            hidden_fields.append(unicode(bf))

            for error in bf.errors:
                item = (name, force_unicode(error))
                hidden_fields_errors.append(item)

    grouped = dicts.groupby(fields, 'group')
    rexp = re.compile(r"\d+. ")
    fields = [(rexp.sub('', key), grouped[key]) for key in sorted(grouped)]

    context.update({
        'top_errors': form.non_field_errors() or '',
        'hidden_field_errors': hidden_fields_errors or '',
        'errors': errors,
        'groups': fields if fields else '',
        'hidden_fields': hidden_fields or '',
        'form': form,
        'json_for_js': {},
    })

    return context
Example #8
0
def as_table_helper(context, form):
    """See as_table().
  """

    fields = []
    hidden_fields = []
    hidden_fields_errors = []
    errors = False

    # entity = context['entity']

    # support defining output order like in Django
    a = form.Meta
    if hasattr(form, "Meta") and form.Meta.fields:
        items = [(i, form.fields[i]) for i in form.Meta.fields]
    else:
        items = form.fields.items()

    # Iterate over all fields and prepare it for adding
    for name, field in items:
        if not field:
            continue

        bf = forms_in.BoundField(form, field, name)
        attrs = {}

        if isinstance(field, widgets.ReferenceField):
            attrs = field.rf

        # If the field is hidden we display it elsewhere
        if not bf.is_hidden:
            if bf.errors:
                errors = True

            example_text = ""
            group = "0. "

            if hasattr(field, "group"):
                group = field.group

            if hasattr(field, "example_text"):
                example_text = force_unicode(field.example_text)

            item = {"field": bf, "required": field.required, "example_text": example_text, "group": group}

            item.update(attrs)
            fields.append(item)
        else:
            hidden_fields.append(unicode(bf))

            for error in bf.errors:
                item = (name, force_unicode(error))
                hidden_fields_errors.append(item)

    grouped = dicts.groupby(fields, "group")
    rexp = re.compile(r"\d+. ")
    fields = [(rexp.sub("", key), grouped[key]) for key in sorted(grouped)]

    context.update(
        {
            "top_errors": form.non_field_errors() or "",
            "hidden_field_errors": hidden_fields_errors or "",
            "errors": errors,
            "groups": fields if fields else "",
            "hidden_fields": hidden_fields or "",
            "form": form,
            "json_for_js": {},
        }
    )

    return context