Commit 41a0a950 authored by Denis Laxalde's avatar Denis Laxalde
Browse files

[views] Explicitly pass parameters to underlying import method in ImportSchemeConceptsView

As a consequence, always display the tab even if empty (otherwise one would
not see the import link).
parent 0f269891a2ed
......@@ -146,12 +146,12 @@ class SKOSParser(datafeed.DataFeedParser):
raise_on_error=raise_on_error, **kwargs)
def _skos_import_lcsv(cnx, stream, import_log, scheme_uri, delimiter, encoding, default_lang=None,
**kwargs):
def _skos_import_lcsv(cnx, stream, import_log, scheme_uri, delimiter,
encoding, language_code=None, **kwargs):
"""import SKOS from LCSV stream or URL (by transforming it to RDF first)"""
graph = rdfio.RDFLibRDFGraph()
# add LCSV statements to the RDF graph
lcsv2rdf = lcsv.LCSV2RDF(stream, delimiter, encoding, default_lang=default_lang,
lcsv2rdf = lcsv.LCSV2RDF(stream, delimiter, encoding, default_lang=language_code,
uri_cls=graph.uri, uri_generator=lambda x: str(uuid4())+x)
for (subj, pred, obj) in lcsv2rdf.triples():
graph.add(subj, pred, obj)
......
......@@ -135,15 +135,13 @@ class LCSVImportTC(CubicWebTC):
self.scheme_uri = scheme.cwuri
def test_import_lcsv(self):
"""Test import lcsv file"""
with self.admin_access.repo_cnx() as cnx:
cnx.call_service('lcsv.skos.import', scheme_uri=self.scheme_uri,
stream=open(self.datapath('lcsv_example_shortened.csv')),
delimiter='\t', encoding='utf-8', default_lang='es')
delimiter='\t', encoding='utf-8', language_code='es')
self._check_imported_lcsv(cnx, 'es')
def test_import_lcsv_without_default_lang(self):
"""Test import lcsv file without default language"""
def test_import_lcsv_without_language_code(self):
with self.admin_access.repo_cnx() as cnx:
cnx.call_service('lcsv.skos.import', scheme_uri=self.scheme_uri,
stream=open(self.datapath('lcsv_example_shortened.csv')),
......
......@@ -32,9 +32,9 @@ class ViewsTC(CubicWebTC):
# simply test the form properly render and is well formed
self.view('skos.scheme.import', rset=scheme.as_rset(), req=req, template=None)
req.form = self.fake_form('skos.scheme.import', {
'file': ('filename.txt', StringIO('\n\nélectro\nhip-hop\nrap\njazz\nclassique\n')),
'stream': ('filename.txt', StringIO('\n\nélectro\nhip-hop\nrap\njazz\nclassique\n')),
'encoding': u'utf-8',
'language': u'fr',
'language_code': u'fr',
'format': u'simple',
'delimiter': u'tab',
}, [(scheme, {})])
......@@ -56,9 +56,9 @@ class ViewsTC(CubicWebTC):
self.view('skos.scheme.import', rset=scheme.as_rset(), req=req,
template=None)
fname = 'lcsv_example_shortened.csv'
posted = {'file': (fname, open(self.datapath(fname))),
posted = {'stream': (fname, open(self.datapath(fname))),
'encoding': u'utf-8',
'language': u'fr',
'language_code': u'fr',
'delimiter': u'tab',
'format': u'lcsv'}
req.form = self.fake_form('skos.scheme.import', posted, [(scheme, {})])
......
......@@ -109,8 +109,7 @@ class ConceptSchemePrimaryView(tabs.TabbedPrimaryView):
class ConceptSchemeConceptsTab(tabs.TabsMixin, EntityView):
"""display a SKOS concept scheme tree"""
__regid__ = 'skos_top_concepts_tab' # don't use '.' in tab's regid
__select__ = (is_instance('ConceptScheme')
& has_related_entities('in_scheme', role='object'))
__select__ = is_instance('ConceptScheme')
def entity_call(self, entity):
rschema = self._cw.vreg.schema.rschema('in_scheme')
......@@ -118,9 +117,11 @@ class ConceptSchemeConceptsTab(tabs.TabsMixin, EntityView):
self.w(tags.a(self._cw._('import concepts'),
href=entity.absolute_url(vid='skos.scheme.import'),
klass='btn btn-success pull-right'))
self.w(tags.div(klass='clearfix'))
rset = entity.top_concepts_rset
treeid = 'skos_tree_%s' % entity.eid
self._cw.view('treeview', rset=rset, treeid=treeid, initial_thru_ajax=True, w=self.w)
if rset:
treeid = 'skos_tree_%s' % entity.eid
self._cw.view('treeview', rset=rset, treeid=treeid, initial_thru_ajax=True, w=self.w)
class ConceptPrimaryView(tabs.TabbedPrimaryView):
......@@ -222,7 +223,7 @@ class ImportSchemeConceptsMixIn(object):
class ImportSchemeConceptsForm(ImportSchemeConceptsMixIn, forms.EntityFieldsForm):
"""ask for a file to import"""
filefield = ff.FileField(name='file', label=_('Concepts file'), required=True)
filefield = ff.FileField(name='stream', label=_('Concepts file'), required=True)
formatfield = ff.StringField(name='format', label=_('file format'),
required=True, internationalizable=True,
choices=[(_('simple'), u'simple'),
......@@ -231,7 +232,7 @@ class ImportSchemeConceptsForm(ImportSchemeConceptsMixIn, forms.EntityFieldsForm
internationalizable=True,
choices=[(_('utf-8'), u'utf-8'),
(_('latin 1'), u'latin1')], sort=False)
language = ff.StringField(name='language',
language = ff.StringField(name='language_code',
label=_('language of concepts in the file'),
internationalizable=True,
choices=[(_('french'), u'fr'),
......@@ -258,21 +259,20 @@ class ImportSchemeConceptsView(ImportSchemeConceptsMixIn, EntityView):
form = self._cw.vreg['forms'].select(self.__regid__, self._cw, entity=entity)
if form.posting:
posted = form.process_posted()
if 'file' not in posted: # https://www.cubicweb.org/ticket/5245936
raise ValidationError(None, {'file': self._cw.__("required field")})
if 'stream' not in posted: # https://www.cubicweb.org/ticket/5245936
raise ValidationError(None, {'stream': self._cw.__("required field")})
posted['delimiter'] = self.delimiters[posted['delimiter']]
if posted.pop('format') == 'simple':
self._simple_import(entity, posted)
self._simple_import(entity, **posted)
else:
self._lcsv_import(entity, posted)
self._lcsv_import(entity, **posted)
raise Redirect(entity.absolute_url(__message=self._cw._('Import completed')))
else:
form.render(w=self.w)
def _simple_import(self, entity, posted):
delimiter = self.delimiters[posted['delimiter']]
def _simple_import(self, entity, **kwargs):
try:
entity.add_concepts_from_file(posted['file'], posted['encoding'],
posted['language'], delimiter)
entity.add_concepts_from_file(**kwargs)
except CSVIndentationError as exc:
raise ValidationError(entity.eid,
{None: self._cw._("Indentation error line %s") % exc.line})
......@@ -280,15 +280,10 @@ class ImportSchemeConceptsView(ImportSchemeConceptsMixIn, EntityView):
raise ValidationError(entity.eid,
{None: self._cw._("Encoding error line %s") % exc.line})
def _lcsv_import(self, entity, posted):
delimiter = self.delimiters[posted['delimiter']]
def _lcsv_import(self, entity, **kwargs):
try:
self._cw.cnx.call_service('lcsv.skos.import',
scheme_uri=entity.cwuri,
stream=posted['file'],
encoding=posted['encoding'],
delimiter=delimiter,
default_lang=posted['language'])
scheme_uri=entity.cwuri, **kwargs)
except lcsv.InvalidLCSVFile as exc:
if exc.column:
msg = self._cw._(to_unicode(exc)) % exc.column
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment