def title_to_tag(context, event): """ Find keywords from the title of a content object. According to the configuration keywords may be added only, if they already exist, or new keywords may be generated from the title. :param context: The content object :return: Nothing """ # get the tagging configuration tagging_config = get_tagging_config() # Check if the title has to used at all. If not do nothing if tagging_config.scan_title or tagging_config.new_tags_from_title: # get the title title = context.title # check if the title should be scanned and if a regex for scanning is provided if tagging_config.scan_title and tagging_config.scan_title_regex: # do the scanning title_tags = tagging_config.scan_title_regex_compiled.findall(title) # get a list a already existing keyowrds. TODO Optimization (caching, lower case matching, use sets or dicts) existing_keywords = get_all_keywords(context) # match the keywords to the exiting ones for tag in title_tags: if tag in existing_keywords: # add the matching keywords add_tags(context, tags_to_add=[tag]) # check if the title should be scanned for new keywords and if a regex for the scanning is provided if tagging_config.new_tags_from_title and tagging_config.new_tags_from_title_regex: # do the scanning new_title_tags = tagging_config.new_tags_from_title_regex_compiled.findall(title) # TODO Optimization (caching, lower case matching, use sets or dicts) # add the keywords add_tags(context, tags_to_add=new_title_tags)
def meta_to_tag(context, event): tagging_config = get_tagging_config() meta = image_to_meta(context, use_exif=tagging_config.use_exif, use_iptc=tagging_config.use_iptc, use_xmp=tagging_config.use_xmp) allowed_exif = tagging_config.exif_fields allowed_iptc = tagging_config.iptc_fields allowed_xmp = tagging_config.xmp_fields if hasattr(meta['iptc'], 'data'): iptc = meta['iptc'].data else: iptc = meta['iptc'] exif = meta['exif'] xmp = meta['xmp'] tags = list(context.Subject()) if tagging_config.use_iptc: tags = tags + get_tags(iptc, allowed_iptc) if tagging_config.use_exif: tags = tags + get_tags(exif, allowed_exif) if tagging_config.use_xmp: tags = tags + get_tags(xmp, allowed_xmp) add_tags(context, tags_to_add=tags)
def applyChanges(self, data): config_store = get_tagging_config() for field in data: if field == 'test_image' and data['test_image']: config_store.test_image = data['test_image'] self.context.REQUEST.RESPONSE.redirect(self.request["ACTUAL_URL"]) elif data[field]: config_store.add_xmp_tag(field)
def test_get_ignored_tags_for_form(self): config = get_tagging_config() config._ignored_tags = ['test', 'hallo'] tags = get_ignored_tags_form() self.assertTrue(len(tags) == 2) self.assertTrue(tags[0] == {'tag': 'test'}) self.assertTrue(tags[1] == {'tag': 'hallo'})
def setUp(self): self.portal = self.layer['portal'] self.request = self.layer['request'] setRoles(self.portal, TEST_USER_ID, ['Manager']) self.config = get_tagging_config() self.config.new_tags_from_title = True self.config.title_regex = '(\w+)'
def handleApply(self, action): data, errors = self.extractData() if errors: self.status = self.formErrorsMessage return config_store = get_tagging_config() for field in data: setattr(config_store, field, data[field]) self.status = "Data was saved"
def updateFields(self): super(TagImportExifEditForm, self).updateFields() config_store = get_tagging_config() test_image = config_store.test_image if test_image and test_image.portal_type and test_image.portal_type == 'Image': exif = image_to_meta(test_image)['iptc'].data exif_keys = exif.keys() exif_keys.sort() for exif_key in exif_keys: exif_field = exif[exif_key] if str(exif_field) and len(str(exif_field)) < 100 : self.fields += field.Fields(schema.Bool( __name__=str(exif_key), title=unicode(exif_key), description=unicode("Example: " +str(exif_field)), default=False))
def updateFields(self): super(TagImportExifEditForm, self).updateFields() config_store = get_tagging_config() test_image = config_store.test_image if test_image and test_image.portal_type and test_image.portal_type == 'Image': xmp_data = image_to_meta(test_image)['xmp'] xmp_keys = xmp_data.keys() xmp_keys.sort() for xmp_key in xmp_keys: xmp_field = xmp_data[xmp_key] if str(xmp_field) and len(str(xmp_field)) < 100 : self.fields += field.Fields(schema.Bool( __name__=str(xmp_key), title=unicode(xmp_key), description=unicode("Example: " +str(xmp_field)), default=False))
def __call__(self): self.errors = [] self.protect() context = aq_inner(self.context) catalog = getToolByName(context, 'portal_catalog') mtool = getToolByName(context, 'portal_membership') config = get_tagging_config() missing = [] for key in self.request.form.keys(): if not key.startswith('UID_'): continue uid = self.request.form[key] brains = catalog(UID=uid) if len(brains) == 0: missing.append(uid) continue obj = brains[0].getObject() title = self.objectTitle(obj) if not mtool.checkPermission('Copy or Move', obj): self.errors(_(u'Permission denied to retag ${title}.', mapping={u'title': title})) continue sp = transaction.savepoint(optimistic=True) try: meta_to_tag(obj, None) title_to_tag(obj, None) except ConflictError: raise except Exception: sp.rollback() self.errors.append(_('Error retagging ${title}', mapping={ 'title': title})) return self.message(missing)
def test_get_config(self): config = get_tagging_config() self.assertIsInstance(config, TaggingConfig)
def __call__(self, *args, **kwargs): config_store = get_tagging_config() if config_store: return getattr(config_store, self.field, None) return None