def set_favorite_item(ctrl, request): """ """ corpora = [] main_size = None for i, c_id in enumerate(request.form.getlist('corpora')): corp = ctrl.cm.get_corpus( c_id, subcname=request.form['subcorpus_id'] if i == 0 else None) if i == 0: main_size = corp.search_size corpora.append(dict(id=c_id, name=corp.get_conf('NAME'))) subcorpus_id = request.form['subcorpus_id'] subcorpus_orig_id = request.form['subcorpus_orig_id'] item = FavoriteItem( dict( id=None, # will be updated after database insert (autoincrement) name=' || '.join(c['name'] for c in corpora) + (' / ' + subcorpus_orig_id if subcorpus_orig_id else ''), corpora=corpora, subcorpus_id=subcorpus_id, subcorpus_orig_id=subcorpus_orig_id, size=main_size)) with plugins.runtime.USER_ITEMS as uit: uit.add_user_item(ctrl._plugin_ctx, item) return item.to_dict()
def set_favorite_item(ctrl, request): """ """ corpora = [] main_size = None for i, c_id in enumerate(request.form.getlist('corpora')): corp = ctrl.cm.get_Corpus(c_id, subcname=request.form['subcorpus_id'] if i == 0 else None) if i == 0: main_size = corp.search_size() corpora.append(dict(id=c_id, name=l10n.import_string( corp.get_conf('NAME'), corp.get_conf('ENCODING')))) subcorpus_id = request.form['subcorpus_id'] subcorpus_orig_id = request.form['subcorpus_orig_id'] item = FavoriteItem(dict( name=u' || '.join(c['name'] for c in corpora) + (u' / ' + subcorpus_orig_id if subcorpus_orig_id else u''), corpora=corpora, subcorpus_id=subcorpus_id, subcorpus_orig_id=subcorpus_orig_id, size=main_size, size_info=l10n.simplify_num(main_size) )) with plugins.runtime.USER_ITEMS as uit: uit.add_user_item(ctrl._plugin_api, item) return item.to_dict()
def import_legacy_record(data): ans = FavoriteItem() ans.ident = data['id'] ans.name = data.get('name', '??') if data.get('corpora', None): ans.corpora = data.get('corpora') else: ans.corpora = [dict(id=data['corpus_id'], name=data['name'])] ans.subcorpus_id = data.get('subcorpus_id', None) ans.size = data.get('size', None) ans.size_info = data.get('size_info', None) return ans
def set_favorite_item(ctrl, request): """ """ corpora = [] main_size = None for i, c_id in enumerate(request.form.getlist('corpora')): corp = ctrl.cm.get_Corpus( c_id, request.form['subcorpus_id'] if i == 0 else None) if i == 0: main_size = corp.search_size() corpora.append( dict(id=c_id, canonical_id=ctrl._canonical_corpname(c_id), name=corp.get_conf('NAME'))) subcorpus_id = request.form['subcorpus_id'] item = FavoriteItem( dict(name=' + '.join(c['name'] for c in corpora) + (' : ' + subcorpus_id if subcorpus_id else ''), corpora=corpora, subcorpus_id=request.form['subcorpus_id'], size=main_size, size_info=l10n.simplify_num(main_size))) with plugins.runtime.USER_ITEMS as uit: uit.add_user_item(ctrl._plugin_api, item) return dict(id=item.ident)
def test_serialize(self): corpus = dict(name='intercorp_en', id='intercorp_en') f = FavoriteItem(data=dict(name='xxx', corpora=[ corpus], size=100, size_info='0.1k', subcorpus_id='foo')) ans = self.plugin.serialize(f) data = json.loads(ans) self.assertDictEqual(data['corpora'][0], corpus) self.assertEqual(data['name'], 'xxx') self.assertEqual(data['size'], 100) self.assertEqual(data['size_info'], '0.1k') self.assertEqual(data['subcorpus_id'], 'foo')
def import_legacy_record(data): ans = FavoriteItem() ans.ident = data['id'] ans.name = data.get('name', '??') ans.corpora = [dict(id=data['corpus_id'], name=data['name'])] if data.get('corpora', None): for item in data.get('corpora', []): try: ans.corpora.append(dict(id=item['canonical_id'], name=item['name'])) except Exception as ex: logging.getLogger(__name__).warning( 'Failed to import legacy fav. item record component: {0}'.format(ex)) ans.subcorpus_id = data.get('subcorpus_id', None) ans.subcorpus_orig_id = data.get('subcorpus_orig_id', ans.subcorpus_id) ans.size = data.get('size', None) ans.size_info = data.get('size_info', None) return ans
def import_record(obj): """ Provides a consistent decoding of JSON-encoded GeneralItem objects. If a new GeneralItem implementation occurs then this method must be updated accordingly. arguments: obj -- a dictionary as loaded from store """ if 'type' in obj: return import_legacy_record(obj) else: return FavoriteItem(data=obj)
def insert_favitem(self, user_id: int, item: FavoriteItem): with self._db.cursor() as cursor: cursor.execute( 'INSERT INTO kontext_user_fav_item (name, subcorpus_id, subcorpus_orig_id, user_id) ' 'VALUES (%s, %s, %s, %s) ', (item.name, item.subcorpus_id, item.subcorpus_orig_id, user_id)) favitem_id: int = cursor.lastrowid cursor.executemany( 'INSERT INTO kontext_corpus_user_fav_item (user_fav_corpus_id, corpus_name) ' 'VALUES (%s, %s) ', [(favitem_id, corp['id']) for corp in item.corpora]) self._db.commit() item.ident = str(favitem_id) # need to update new id
def import_legacy_record(data): ans = FavoriteItem() ans.ident = data['id'] ans.name = data.get('name', '??') ans.corpora = [dict(id=data['corpus_id'], name=data['name'])] if data.get('corpora', None): for item in data.get('corpora', []): try: ans.corpora.append(dict(id=item['canonical_id'], name=item['name'])) except Exception as ex: logging.getLogger(__name__).warning( u'Failed to import legacy fav. item record component: {0}'.format(ex)) ans.subcorpus_id = data.get('subcorpus_id', None) ans.subcorpus_orig_id = data.get('subcorpus_orig_id', ans.subcorpus_id) ans.size = data.get('size', None) ans.size_info = data.get('size_info', None) return ans
def get_favitems(self, user_id: int): with self._db.cursor() as cursor: cursor.execute( 'SELECT fav.id as id, fav.name, fav.subcorpus_id, fav.subcorpus_orig_id, ' " GROUP_CONCAT(t.corpus_name SEPARATOR ',') as corpora, " " GROUP_CONCAT(c.size SEPARATOR ',') as sizes " 'FROM kontext_user_fav_item as fav ' 'JOIN kontext_corpus_user_fav_item AS t ON fav.id = t.user_fav_corpus_id ' f'JOIN {self._corp_table} AS c ON t.corpus_name = c.name ' 'WHERE user_id = %s ' 'GROUP BY id ', (user_id,)) ans = [] for item in cursor: item['corpora'] = [{'name': corp, 'id': corp} for corp in item['corpora'].split(',')] item['size'] = int(item['sizes'].split(',')[0]) ans.append(FavoriteItem(item)) return ans
def create_corpus_obj(name='korpus syn 2010'): return FavoriteItem( dict(name=name, corpora=[dict(id='syn2010', name='Korpus SYN2010')]))