Exemplo n.º 1
0
    def tags(self):
        directory = self.context.aq_inner.aq_parent

        labels = directory.labels()
        descriptions = directory.descriptions()

        tags = []

        Tag = namedtuple('ItemTag', ['label', 'value', 'description'])
        categorized = IDirectoryCategorized(self.context)

        for category in sorted(descriptions):
            values = getattr(categorized, category) or []

            for value in sorted(utils.flatten(values)):
                if value in descriptions[category]:
                    desc = descriptions[category][value]
                else:
                    desc = None

                tags.append(Tag(labels[category], value, desc))

        # why u no accept generators zpt???
        result = {}

        for group, values in groupby(tags, attrgetter('label')):
            result[group] = list(values)

        return result
Exemplo n.º 2
0
    def possible_values(self, items=None, categories=None):
        """Returns a dictionary with the keys being the categories of the
        directory, filled with a list of all possible values for each category.
        If an item contains a list of values (as opposed to a single string)
        those values flattened. In other words, there is no hierarchy in the
        resulting list.

        """
        items = items or self.query()
        categories = categories or self.directory.all_categories()

        values = dict([(c, list()) for c in categories])
        for item in items:
            for key, label, value in item.categories:
                if not key in categories:
                    continue

                if not value:
                    continue

                if isinstance(value, basestring):
                    values[key].append(value)
                else:
                    map(values[key].append, utils.flatten(value))

        return values
Exemplo n.º 3
0
    def possible_values(self, items=None, categories=None):
        """Returns a dictionary with the keys being the categories of the
        directory, filled with a list of all possible values for each category.
        If an item contains a list of values (as opposed to a single string)
        those values flattened. In other words, there is no hierarchy in the
        resulting list.

        """
        items = items or self.query()
        categories = categories or self.directory.all_categories()

        values = dict([(c, list()) for c in categories])
        for item in items:
            for key, label, value in item.categories:
                if not key in categories:
                    continue

                if not value:
                    continue

                if isinstance(value, basestring):
                    values[key].append(value)
                else:
                    map(values[key].append, utils.flatten(value))

        return values
Exemplo n.º 4
0
    def generate_tags(self, item):

        url = self.directory.absolute_url() + '?filter&%s=%s'

        for cat, label, value in item.categories:

            for value in utils.flatten(value):
                yield DirectoryTag(
                    url=url % (cat, value), name=value, category=cat
                )
Exemplo n.º 5
0
    def keywords(self, categories=None):
        """Returns a flat list of all categories, wheter they are actually
        visible in the directory or not, unless a list of categories is
        specifically passed.

        """
        categories = categories or self.get_parent().all_categories()
        values = []
        for cat in categories:
            values.append(getattr(self, cat))

        return list(utils.flatten(values))
Exemplo n.º 6
0
    def keywords(self, categories=None):
        """Returns a flat list of all categories, wheter they are actually
        visible in the directory or not, unless a list of categories is
        specifically passed.

        """
        categories = categories or self.get_parent().all_categories()
        values = []
        for cat in categories:
            values.append(getattr(self, cat))

        return list(utils.flatten(values))
def run_import(app, site_name, folder_path, records):
    # setup request and get plone site
    app = makerequest(app)
    site = app.unrestrictedTraverse(site_name)

    # setup user context
    admin = app.acl_users.getUserById("admin")
    newSecurityManager(app, admin)

    #setup site
    setSite(site)

    folder = site.unrestrictedTraverse(folder_path)
    assert folder.portal_type == 'seantis.dir.facility.directory'

    # clear folder
    for id in folder.contentIds():
        del folder[id]

    suggested_values = dict(cat1=set(), cat2=set(), cat3=set(), cat4=set())

    for record in records:
        facility = createContentInContainer(
            folder,
            'seantis.dir.facility.item',
            title=record['name'],
            description=''
        )

        facility.opening_hours = record['opening_hours']

        facility.cat1 = record['types']
        facility.cat2 = [record['kita_location']]
        facility.cat3 = record['subsidized'] and [u'Ja'] or [u'Nein']
        facility.cat4 = record['languages']

        map(suggested_values['cat1'].add, utils.flatten(facility.cat1))
        map(suggested_values['cat2'].add, utils.flatten(facility.cat2))
        map(suggested_values['cat3'].add, utils.flatten(facility.cat3))
        map(suggested_values['cat4'].add, utils.flatten(facility.cat4))

        facility.affix = record['affix']
        facility.spots = record['spots']
        facility.address = record['address']
        facility.zipcode = record['zipcode']
        facility.location = record['location']
        facility.phone = record['phone']
        facility.email = record['email']
        facility.url = record['url']
        facility.fax = record['fax']
        facility.age = record['age']
        facility.holidays = record['holidays']
        facility.contact_first_name = record['contact']['first_name']
        facility.contact_last_name = record['contact']['last_name']
        facility.contact_phone = record['contact']['phone']
        facility.contact_email = record['contact']['email']
        facility.correspondence_first_name = record['correspondence'][
            'first_name'
        ]
        facility.correspondence_last_name = record['correspondence'][
            'last_name'
        ]
        facility.correspondence_address = record['correspondence'][
            'address'
        ]
        facility.correspondence_zipcode = record['correspondence'][
            'zipcode'
        ]
        facility.correspondence_town = record['correspondence'][
            'town'
        ]
        facility.notes = record['notes']

        if record['lon'] and record['lat']:
            facility.set_coordinates('Point', map(
                float, (record['lon'], record['lat'])
            ))

        facility.image = None
        facility.contact = ''
        facility.infrastructure = ''
        facility.terms_of_use = ''

        facility.reindexObject()

    folder.cat1_suggestions = sorted(list(suggested_values['cat1']))
    folder.cat2_suggestions = sorted(list(suggested_values['cat2']))
    folder.cat3_suggestions = sorted(list(suggested_values['cat3']))
    folder.cat4_suggestions = sorted(list(suggested_values['cat4']))

    transaction.commit()