diff options
Diffstat (limited to 'chimere/utils.py')
-rw-r--r-- | chimere/utils.py | 27 |
1 files changed, 22 insertions, 5 deletions
diff --git a/chimere/utils.py b/chimere/utils.py index f3ec751..73e38ba 100644 --- a/chimere/utils.py +++ b/chimere/utils.py @@ -73,7 +73,7 @@ class ImportManager(object): raise NotImplementedError def create_or_update_item(self, cls, values, import_key, version=None, - key='', pk=None): + key='', pk=None, category=None): updated, created, item = False, False, None import_key = unicode(import_key).replace(':', '^') if not values.get('name'): @@ -138,8 +138,11 @@ class ImportManager(object): if import_key: item.set_key(key, import_key) item.categories.clear() - for cat in self.importer_instance.categories.all(): - item.categories.add(cat) + if category: + item.categories.add(category) + else: + for cat in self.importer_instance.categories.all(): + item.categories.add(cat) return item, updated, created @classmethod @@ -964,6 +967,8 @@ class HtmlXsltManager(ImportManager): val = re.sub(r, replaced % {'base_url':base_url}, val) item[k] = html_unescape(val) updated_item, new_item = 0, 0 + key_categories = self.importer_instance.get_key_category_dict() + missing_cats = set() for item in items: if not self.importer_instance.default_localisation and \ not "point" in item and not ("lat" in item and item['lat']): @@ -973,6 +978,12 @@ class HtmlXsltManager(ImportManager): self.importer_instance.origin), 'license':self.importer_instance.license, 'name':item['name']} + category = None + if 'category' in item and item['category']: + if item['category'] in key_categories: + category = key_categories[item['category']] + else: + missing_cats.add(item['category']) cls = Marker if 'point' in item: x, y = item['point'].split(",") @@ -1008,12 +1019,18 @@ class HtmlXsltManager(ImportManager): int(values['day2'])) break key = item['key'] - it, updated, created = self.create_or_update_item(cls, dct, key) + it, updated, created = self.create_or_update_item(cls, dct, key, + category=category) if updated: updated_item += 1 if created: new_item += 1 - return (new_item, updated_item, '') + msg = '' + if missing_cats: + msg = _(u"Names \"%s\" doesn't match existing categories. " + u"Modify the import to match theses names with categories.") % ( + u'", "'.join(missing_cats)) + return (new_item, updated_item, msg) class XMLXsltManager(HtmlXsltManager): PARSER = 'XMLParser' |