# Path: ckanext/dcatapit/controllers/thesaurus.py
# class ThesaurusController(View):
# 
#     def get(self):
#         print('------------------')
#         print('thesaurus')
#         print('------------------')
#         return base.render(
#             u'admin/thesaurus.html',
#             extra_vars={
#                 'title': tk._('Thesaurus Data Update')
#             }
#         )
# 
# def get_thesaurus_admin_page():
#     return base.render(
#         u'admin/thesaurus.html',
#         extra_vars={}
#     )
# 
# def update_vocab_admin():
#     ALLOWED_EXTENSIONS = {'rdf'}
#     file = None
# 
#     def is_file_allowed(filename):
#         return '.' in filename and filename.rsplit('.', 1)[-1].lower() in ALLOWED_EXTENSIONS
# 
#     if 'thesaurus_file' not in request.files or not request.files['thesaurus_file']:
#         return abort(400, detail='Missing thesaurus file')
# 
#     thesaurus_file = request.files['thesaurus_file']
#     if not is_file_allowed(thesaurus_file.filename):
#         return abort(400, detail='File type not allowed')
# 
#     # Store file
#     storage_path = config.get(u'ckan.storage_path') or tempfile.gettempdir()
#     filename = secure_filename(thesaurus_file.filename)
#     upload_dir = os.path.join(storage_path, 'uploaded_vocabularies')
#     os.makedirs(upload_dir, exist_ok=True)  # make sure the directory is there
#     file_path = os.path.join(upload_dir, filename)
#     log.info(f'Storing vocabulary into {file_path}')
#     thesaurus_file.save(file_path)
# 
#     try:
#         g, name, uri, eurovoc = validate_vocabulary(file_path, url=None, eurovoc=None)
#     except DataException as e:
#         return abort(400, detail=str(e))
#     except ConfigException as e:
#         return abort(500, detail=str(e) + tk._(' Please contact the administrator.'))
#     finally:
#         os.remove(file_path)
# 
#     results = load(g, name, uri, eurovoc)
# 
#     return base.render(u'admin/thesaurus_result.html',
#                        extra_vars={'results': results, 'voc_name': name})


from flask import Blueprint
from ckanext.dcatapit.controllers.thesaurus import ThesaurusController, get_thesaurus_admin_page, update_vocab_admin
import logging
import ckan.plugins as plugins
import ckan.plugins.toolkit as tk



log = logging.getLogger(__file__)


class DCATAPITVocabularyPlugin(plugins.SingletonPlugin, tk.DefaultDatasetForm):

    plugins.implements(plugins.IBlueprint, inherit=True)
    plugins.implements(plugins.IConfigurer)

    def get_blueprint(self):
        blueprint = Blueprint('dcatapit_core', self.__module__)

        blueprint.add_url_rule(
            rule='/ckan-admin/thesaurus',
            endpoint='thesaurus',
            view_func=get_thesaurus_admin_page,
        )

# Path: ckanext/dcatapit/controllers/thesaurus.py
# class ThesaurusController(View):
# 
#     def get(self):
#         print('------------------')
#         print('thesaurus')
#         print('------------------')
#         return base.render(
#             u'admin/thesaurus.html',
#             extra_vars={
#                 'title': tk._('Thesaurus Data Update')
#             }
#         )
# 
# def get_thesaurus_admin_page():
#     return base.render(
#         u'admin/thesaurus.html',
#         extra_vars={}
#     )
# 
# def update_vocab_admin():
#     ALLOWED_EXTENSIONS = {'rdf'}
#     file = None
# 
#     def is_file_allowed(filename):
#         return '.' in filename and filename.rsplit('.', 1)[-1].lower() in ALLOWED_EXTENSIONS
# 
#     if 'thesaurus_file' not in request.files or not request.files['thesaurus_file']:
#         return abort(400, detail='Missing thesaurus file')
# 
#     thesaurus_file = request.files['thesaurus_file']
#     if not is_file_allowed(thesaurus_file.filename):
#         return abort(400, detail='File type not allowed')
# 
#     # Store file
#     storage_path = config.get(u'ckan.storage_path') or tempfile.gettempdir()
#     filename = secure_filename(thesaurus_file.filename)
#     upload_dir = os.path.join(storage_path, 'uploaded_vocabularies')
#     os.makedirs(upload_dir, exist_ok=True)  # make sure the directory is there
#     file_path = os.path.join(upload_dir, filename)
#     log.info(f'Storing vocabulary into {file_path}')
#     thesaurus_file.save(file_path)
# 
#     try:
#         g, name, uri, eurovoc = validate_vocabulary(file_path, url=None, eurovoc=None)
#     except DataException as e:
#         return abort(400, detail=str(e))
#     except ConfigException as e:
#         return abort(500, detail=str(e) + tk._(' Please contact the administrator.'))
#     finally:
#         os.remove(file_path)
# 
#     results = load(g, name, uri, eurovoc)
# 
#     return base.render(u'admin/thesaurus_result.html',
#                        extra_vars={'results': results, 'voc_name': name})


# Path: ckanext/dcatapit/plugins/vocabulary.py
from flask import Blueprint
from ckanext.dcatapit.controllers.thesaurus import ThesaurusController, get_thesaurus_admin_page, update_vocab_admin
import logging
import ckan.plugins as plugins
import ckan.plugins.toolkit as tk




log = logging.getLogger(__file__)


class DCATAPITVocabularyPlugin(plugins.SingletonPlugin, tk.DefaultDatasetForm):

    plugins.implements(plugins.IBlueprint, inherit=True)
    plugins.implements(plugins.IConfigurer)

    def get_blueprint(self):
        blueprint = Blueprint('dcatapit_core', self.__module__)

        blueprint.add_url_rule(
            rule='/ckan-admin/thesaurus',
            endpoint='thesaurus',
            view_func=get_thesaurus_admin_page,
        )

        blueprint.add_url_rule(