class DateRegistry(object):
    """Maintains a collection of important changeable date fields.
    """
    def __init__(self):
        self._registry = OrderedRegistry()

    class DateItem(object):
        def __init__(self, date_field, name_generator, model):
            self.date_field = date_field
            self.name_generator = name_generator
            self.model = model

    def register(self,
                 date_field,
                 name_generator=None,
                 model=None,
                 order=sys.maxint):
        """Registers a new date item.

           :param date_field: the date's field in the model
           :param name_generator: function taking model's object and returning
                        the name to be displayed with the date.
           :param model: the date's model. If the model is not provided the
                        method returns a decorator for a model.
           :param order: the date's order. The lower the order, the higher the
                        priority of the date.
        """
        def decorator(original_class):
            self.register(date_field, name_generator, original_class, order)
            return original_class

        if model is None:
            return decorator

        if name_generator is None:
            name_generator = lambda obj: \
                unicode(model._meta.verbose_name) + " " + unicode(model._meta.
                                get_field_by_name(date_field)[0].verbose_name)

        date_item = self.DateItem(date_field, name_generator, model)
        self._registry.register(date_item, order)

    def tolist(self, contest_id):
        """Returns a list of items to pass to a template for rendering."""
        context_items = []
        for idx, item in enumerate(self._registry):
            model = item.model
            data = model.objects.filter(contest=contest_id).values()
            for record in data:
                context_items.append(
                    dict(text=item.name_generator(record),
                         date=record[item.date_field],
                         date_field=item.date_field,
                         model=model,
                         id=record['id'],
                         order=self._registry.keys[idx]))
        return context_items
Beispiel #2
0
 def test_ordered_registry(self):
     reg = OrderedRegistry()
     reg.register(1, 12)
     reg.register(3)
     reg.register(2, 3)
     reg.register(4)
     self.assertListEqual([2, 1, 3, 4], list(reg))
     reg.unregister(3)
     self.assertListEqual([2, 1, 4], list(reg))
     self.assertEqual(len(reg), 3)
Beispiel #3
0
 def test_ordered_registry(self):
     reg = OrderedRegistry()
     reg.register(1, 12)
     reg.register(3)
     reg.register(2, 3)
     reg.register(4)
     self.assertListEqual([2, 1, 3, 4], list(reg))
     reg.unregister(3)
     self.assertListEqual([2, 1, 4], list(reg))
     self.assertEqual(len(reg), 3)
Beispiel #4
0
class AttachmentRegistry(object):
    """Maintains a collection of functions that
       return attachments for 'Downloads' view.
    """
    def __init__(self):
        self._registry = OrderedRegistry()

    def register(self, attachment_generator=None, order=sys.maxsize):
        """Register function generating attachments.

            :Parameters:
                Function that takes elements from `**kwargs` as arguments and
                returns dictionary containing following keys: `category`,
                `name`, `description`, `link`, `pub_date`.
        """
        if attachment_generator is not None:
            self._registry.register(attachment_generator, order)

    def to_list(self, **kwargs):
        attachments = []
        for idx, gen in enumerate(self._registry):
            attachments.extend(gen(**kwargs))
        return attachments
Beispiel #5
0
class DateRegistry(object):
    """Maintains a collection of important changeable date fields."""
    def __init__(self):
        self._registry = OrderedRegistry()

    class DateItem(object):
        def __init__(self, date_field, name_generator, round_chooser,
                     qs_filter, model):
            self.date_field = date_field
            self.name_generator = name_generator
            self.round_chooser = round_chooser
            self.qs_filter = qs_filter
            self.model = model

    def register(
        self,
        date_field,
        name_generator=None,
        round_chooser=None,
        qs_filter=None,
        model=None,
        order=sys.maxsize,
    ):
        """Registers a new date item.

        :param date_field: the date's field in the model
        :param name_generator: function taking model's object and returning
                     the name to be displayed with the date.
        :param round_chooser: function taking model's object and returning
                     the round it belongs to.
        :param qs_filter: function taking a (queryset, contest id)
                     pair and returning a queryset limited to
                     instances related to the contest.
        :param model: the date's model. If the model is not provided the
                     method returns a decorator for a model.
        :param order: the date's order. The lower the order, the higher the
                     priority of the date.
        """
        def decorator(original_class):
            self.register(
                date_field,
                name_generator,
                round_chooser,
                qs_filter,
                original_class,
                order,
            )
            return original_class

        if model is None:
            return decorator

        if name_generator is None:
            name_generator = (
                lambda obj: six.text_type(model._meta.verbose_name) + " " + six
                .text_type(model._meta.get_field(date_field).verbose_name))

        if round_chooser is None:
            round_chooser = lambda obj: None

        if qs_filter is None:
            qs_filter = lambda qs, contest_id: qs.filter(contest=contest_id)

        date_item = self.DateItem(date_field, name_generator, round_chooser,
                                  qs_filter, model)
        self._registry.register(date_item, order)

    def tolist(self, contest_id):
        """Returns a list of items to pass to a template for rendering."""
        context_items = []
        for idx, item in enumerate(self._registry):
            model = item.model
            instances = item.qs_filter(model.objects.all(), contest_id)
            for instance in instances:
                context_items.append(
                    dict(
                        text=item.name_generator(instance),
                        date=getattr(instance, item.date_field),
                        date_field=item.date_field,
                        model=model,
                        id=instance.id,
                        round=item.round_chooser(instance),
                        order=self._registry.keys[idx],
                    ))
        return context_items
Beispiel #6
0
class DateRegistry(object):
    """Maintains a collection of important changeable date fields.
    """
    def __init__(self):
        self._registry = OrderedRegistry()

    class DateItem(object):
        def __init__(self, date_field, name_generator, round_chooser,
                qs_filter, model):
            self.date_field = date_field
            self.name_generator = name_generator
            self.round_chooser = round_chooser
            self.qs_filter = qs_filter
            self.model = model

    def register(self, date_field, name_generator=None, round_chooser=None,
                qs_filter=None, model=None, order=sys.maxint):
        """Registers a new date item.

           :param date_field: the date's field in the model
           :param name_generator: function taking model's object and returning
                        the name to be displayed with the date.
           :param round_chooser: function taking model's object and returning
                        the round it belongs to.
           :param qs_filter: function taking a (queryset, contest id)
                        pair and returning a queryset limited to
                        instances related to the contest.
           :param model: the date's model. If the model is not provided the
                        method returns a decorator for a model.
           :param order: the date's order. The lower the order, the higher the
                        priority of the date.
        """
        def decorator(original_class):
            self.register(date_field, name_generator, round_chooser,
                    qs_filter, original_class, order)
            return original_class

        if model is None:
            return decorator

        if name_generator is None:
            name_generator = lambda obj: \
                unicode(model._meta.verbose_name) + " " + unicode(model._meta.
                                get_field_by_name(date_field)[0].verbose_name)

        if round_chooser is None:
            round_chooser = lambda obj: None

        if qs_filter is None:
            qs_filter = lambda qs, contest_id: qs.filter(contest=contest_id)

        date_item = self.DateItem(date_field, name_generator, round_chooser,
                qs_filter, model)
        self._registry.register(date_item, order)

    def tolist(self, contest_id):
        """Returns a list of items to pass to a template for rendering."""
        context_items = []
        for idx, item in enumerate(self._registry):
            model = item.model
            instances = item.qs_filter(model.objects.all(), contest_id)
            for instance in instances:
                context_items.append(dict(
                    text=item.name_generator(instance),
                    date=getattr(instance, item.date_field),
                    date_field=item.date_field,
                    model=model,
                    id=instance.id,
                    round=item.round_chooser(instance),
                    order=self._registry.keys[idx]))
        return context_items