def new(self, taggable_id, **kwargs): """Display a NEW form.""" session = session_get() taggable = taggable_get(taggable_id) f_new.value = dict(taggable_id=taggable.id, current_tags_=', '.join([t.id for t in taggable.tags]), ) tags = session.query(Tag).order_by('id') choices = [t.id for t in tags if t not in taggable.tags] f_new.child.children.tagids.options = choices tmpl_context.form = f_new return dict(title='%s %s' % (_('Add tags to:'), taggable.tagged.path))
def post(self, taggable_id, tagids=[], new_tags=None): """Add tags to a ``taggable`` obect.""" session = session_get() user = tmpl_context.user taggable = taggable_get(taggable_id) if isinstance(tagids, list): tags = [tag_get(i) for i in tagids] else: tags = [tag_get(tagids)] if new_tags: tags.extend([tag_get(name) for name in new_tags.split(', ')]) added_tags = [] updates = [] for tag in tags: if tag not in taggable.tags: taggable.tags.append(tag) added_tags.append(tag) # prepare updates to notify clients updates.append(dict(item=tag, type='added', topic=TOPIC_TAGS, filter=taggable_id)) if added_tags: added = ', '.join([t.id for t in added_tags]) msg = '%s %s %s' % (added, n_('tag added to:', 'tags added to:', len(added_tags)), taggable_id) status = 'ok' # notify clients notify.send(updates) # log into Journal journal.add(user, '%s - %s' % (msg, taggable.tagged)) else: msg = _('No new tag applied') status = 'info' return dict(msg=msg, status=status, updates=updates)
def remove(self, taggable_id, tagids=[]): """Remove tags from an object.""" session = session_get() user = tmpl_context.user taggable = taggable_get(taggable_id) if isinstance(tagids, list): tags = [tag_get(i) for i in tagids] else: tags = [tag_get(tagids)] removed_tags = [] updates = [] for tag in tags: if tag in taggable.tags: taggable.tags.remove(tag) removed_tags.append(tag) # prepare updates updates.append(dict(item=tag, type='deleted', topic=TOPIC_TAGS, filter=taggable_id)) if removed_tags: removed = ', '.join([t.id for t in removed_tags]) msg = '%s %s %s' % (removed, n_('tag removed from:', 'tags removed from:', len(removed_tags)), taggable_id) status = 'ok' # notify clients notify.send(updates) # log into Journal journal.add(user, '%s - %s' % (msg, taggable.tagged)) else: msg = _('No tag removed') status = 'info' return dict(msg=msg, status=status, updates=updates)
def get_all(self, taggable_id): """Return a html fragment with a list of tags for this object.""" # tmpl_context.b_tags = b_tags taggable = taggable_get(taggable_id) return dict(tags=taggable.tags)