Beispiel #1
0
 def simple_value(self):
     field_names = translation_utils.expand_model_fields(
         self, self.value_fields())
     return tuple((f, getattr(self, f)) for f in field_names)
Beispiel #2
0
 def simple_value(self):
     field_names = translation_utils.expand_model_fields(self, self.value_fields())
     return tuple((f, getattr(self, f)) for f in field_names)
Beispiel #3
0
    def _update_super_event(self, super_event):
        events = super_event.get_children()
        first_event = events.order_by('start_time').first()
        super_event.start_time = first_event.start_time
        super_event.has_start_time = first_event.has_start_time
        last_event = events.order_by('-end_time').first()
        super_event.end_time = last_event.end_time
        super_event.has_end_time = last_event.has_end_time

        # Functions which map related models into simple comparable values.
        def simple(field):
            return frozenset(map(lambda x: x.simple_value(), field.all()))
        value_mappers = {
            'offers': simple,
            'external_links': simple
        }
        fieldnames = expand_model_fields(
            super_event, [
                'info_url', 'description', 'short_description', 'headline',
                'secondary_headline', 'provider', 'publisher', 'location',
                'location_extra_info', 'audience', 'data_source',
                'image', 'offers', 'external_links'])

        # The set of fields which have common values for all events.
        common_fields = set(
            f for f in fieldnames
            if 1 == len(set(map(
                    value_mappers.get(f, lambda x: x),
                    (getattr(event, f) for event in events.all())))))

        for fieldname in common_fields:
            value = getattr(events.first(), fieldname)
            if hasattr(value, 'all'):
                manager = getattr(super_event, fieldname)
                simple = False
                if hasattr(value.first(), 'simple_value'):
                    # Simple related models can be deleted and copied.
                    manager.all().delete()
                    simple = True
                for m in value.all():
                    if simple:
                        m.id = None
                        m.event_id = super_event.id
                        m.save()
                    manager.add(m)
            else:
                setattr(super_event, fieldname, value)

        for lang in self.languages.keys():
            headline = getattr(
                super_event, 'headline_{}'.format(lang)
            )
            secondary_headline = getattr(
                super_event, 'secondary_headline_{}'.format(lang)
            )
            setattr(super_event, 'name_{}'.format(lang),
                    make_event_name(headline, secondary_headline)
            )

        # Gather common keywords present in *all* subevents
        common_keywords = functools.reduce(
            lambda x, y: x & y,
            (set(event.keywords.all()) for event in events.all())
        )
        super_event.keywords.clear()
        for k in common_keywords:
            super_event.keywords.add(k)

        super_event.save()
Beispiel #4
0
    def _update_super_event(self, super_event):
        events = super_event.get_children()
        first_event = events.order_by("start_time").first()
        super_event.start_time = first_event.start_time
        super_event.has_start_time = first_event.has_start_time
        last_event = events.order_by("-end_time").first()
        super_event.end_time = last_event.end_time
        super_event.has_end_time = last_event.has_end_time

        # Functions which map related models into simple comparable values.
        def simple(field):
            return frozenset(map(lambda x: x.simple_value(), field.all()))

        value_mappers = {"offers": simple, "external_links": simple}
        fieldnames = expand_model_fields(
            super_event,
            [
                "info_url",
                "description",
                "short_description",
                "headline",
                "secondary_headline",
                "provider",
                "publisher",
                "location",
                "location_extra_info",
                "data_source",
                "image",
                "offers",
                "external_links",
            ],
        )

        # The set of fields which have common values for all events.
        common_fields = set(
            f
            for f in fieldnames
            if 1 == len(set(map(value_mappers.get(f, lambda x: x), (getattr(event, f) for event in events.all()))))
        )

        for fieldname in common_fields:
            value = getattr(events.first(), fieldname)
            if hasattr(value, "all"):
                manager = getattr(super_event, fieldname)
                simple = False
                if hasattr(value.first(), "simple_value"):
                    # Simple related models can be deleted and copied.
                    manager.all().delete()
                    simple = True
                for m in value.all():
                    if simple:
                        m.id = None
                        m.event_id = super_event.id
                        m.save()
                    manager.add(m)
            else:
                setattr(super_event, fieldname, value)

        # The name may vary within a recurring event; hence, take the common part
        if expand_model_fields(super_event, ["headline"])[0] not in common_fields:
            words = getattr(events.first(), "headline").split(" ")
            name = ""
            is_common = lambda: all(
                headline.startswith(name + words[0]) for headline in [event.name for event in events]
            )
            while words and is_common():
                name += words.pop(0) + " "
                print(words)
                print(name)
            setattr(super_event, "name", name)

        for lang in self.languages.keys():
            headline = getattr(super_event, "headline_{}".format(lang))
            secondary_headline = getattr(super_event, "secondary_headline_{}".format(lang))
            setattr(super_event, "name_{}".format(lang), make_event_name(headline, secondary_headline))

        # Gather common keywords present in *all* subevents
        common_keywords = functools.reduce(lambda x, y: x & y, (set(event.keywords.all()) for event in events.all()))
        super_event.keywords.clear()
        for k in common_keywords:
            super_event.keywords.add(k)

        super_event.save()
Beispiel #5
0
    def _update_super_event(self, super_event):
        events = super_event.get_children()
        first_event = events.order_by('start_time').first()
        super_event.start_time = first_event.start_time
        super_event.has_start_time = first_event.has_start_time
        last_event = events.order_by('-end_time').first()
        super_event.end_time = last_event.end_time
        super_event.has_end_time = last_event.has_end_time

        # Functions which map related models into simple comparable values.
        def simple(field):
            return frozenset(map(lambda x: x.simple_value(), field.all()))

        value_mappers = {'offers': simple, 'external_links': simple}
        fieldnames = expand_model_fields(super_event, [
            'info_url', 'description', 'short_description', 'headline',
            'secondary_headline', 'provider', 'publisher', 'location',
            'location_extra_info', 'data_source', 'images', 'offers',
            'external_links'
        ])

        # The set of fields which have common values for all events.
        common_fields = set(f for f in fieldnames if 1 == len(
            set(
                map(value_mappers.get(f, lambda x: x), (
                    getattr(event, f) for event in events.all())))))

        for fieldname in common_fields:
            value = getattr(events.first(), fieldname)
            if hasattr(value, 'all'):
                manager = getattr(super_event, fieldname)
                simple = False
                if hasattr(value.first(), 'simple_value'):
                    # Simple related models can be deleted and copied.
                    manager.all().delete()
                    simple = True
                for m in value.all():
                    if simple:
                        m.id = None
                        m.event_id = super_event.id
                        m.save()
                    manager.add(m)
            else:
                setattr(super_event, fieldname, value)

        # The name may vary within a recurring event; hence, take the common part
        if expand_model_fields(super_event,
                               ['headline'])[0] not in common_fields:
            words = getattr(events.first(), 'headline').split(' ')
            name = ''
            while words and all(
                    headline.startswith(name + words[0])
                    for headline in [event.name for event in events]):
                name += words.pop(0) + ' '
                logger.warning(words)
                logger.warning(name)
            setattr(super_event, 'name', name)

        for lang in self.languages.keys():
            headline = getattr(super_event, 'headline_{}'.format(lang))
            secondary_headline = getattr(super_event,
                                         'secondary_headline_{}'.format(lang))
            setattr(super_event, 'name_{}'.format(lang),
                    make_event_name(headline, secondary_headline))

        # Gather common keywords present in *all* subevents
        common_keywords = functools.reduce(lambda x, y: x & y,
                                           (set(event.keywords.all())
                                            for event in events.all()))
        super_event.keywords.clear()
        for k in common_keywords:
            super_event.keywords.add(k)

        common_audience = functools.reduce(lambda x, y: x & y,
                                           (set(event.audience.all())
                                            for event in events.all()))
        super_event.audience.clear()
        for k in common_audience:
            super_event.audience.add(k)

        super_event.save()