<commit_before>'''
Created on 12.11.2014

@author: mvi
'''

import logging
import pylons.config as config

import ckan.lib.base as base
import ckan.lib.helpers as h
import ckan.lib.plugins
import ckan.logic as logic
import ckan.model as model

from ckan.common import _, request, c
from ckanext.model.external_catalog import ExternalCatalog, STATUS
from urllib2 import URLError
from odn_ckancommons.ckan_helper import CkanAPIWrapper
from ckanext.publishing.ckan_sync import CkanSync
from datetime import datetime
from ckanext.publishing.plugin import sync_ext_catalog

NotFound = logic.NotFound
NotAuthorized = logic.NotAuthorized

render = base.render
abort = base.abort
lookup_package_plugin = ckan.lib.plugins.lookup_package_plugin
check_access = logic.check_access
get_action = logic.get_action

log = logging.getLogger('ckanext')

src_ckan = config.get('odn.ic2pc.src.ckan.url', None)
dst_ckan = config.get('odn.ic2pc.dst.ckan.url', None)
dst_api_key = config.get('odn.ic2pc.dst.ckan.api.key', None)
package_extras_whitelist = config.get('odn.ic2pc.package.extras.whitelist', '').split(' ')
resource_extras_whitelist = config.get('odn.ic2pc.resource.extras.whitelist', '').split(' ')



class PublishingController(base.BaseController):

    
    def _get_package_type(self, id):
        """
        Given the id of a package it determines the plugin to load
        based on the package's type name (type). The plugin found
        will be returned, or None if there is no plugin associated with
        the type.
        """
        pkg = model.Package.get(id)
        if pkg:
            return pkg.type or 'dataset'
        return None


    def _load(self, id):
        package_type = self._get_package_type(id.split('@')[0])
        data_dict = {'id': id}
        context = {'model': model, 'session': model.Session,
                   'user': c.user or c.author, 'for_view': True,
                   'auth_user_obj': c.userobj}
        
        try:
            check_access('package_update', context, data_dict)
        except NotAuthorized, e:
            abort(401, _('User {user} not authorized to edit {id}').format(user=c.user, id=id))
        # check if package exists
        try:
            c.pkg_dict = get_action('package_show')(context, data_dict)
            c.pkg = context['package']
        except NotFound:
            abort(404, _('Dataset not found'))
        except NotAuthorized:
            abort(401, _('Unauthorized to read package {id}').format(id=id))
        
        lookup_package_plugin(package_type).setup_template_variables(context, {'id': id})
    
    
    def show(self, id):
        self._load(id)
        vars = {
                'catalogs': ExternalCatalog.by_dataset_id(c.pkg_dict['id'])
        }
        return render('package/publishing.html', extra_vars = vars)
    
    
    def create(self, id):
        self._load(id)
        vars = {
                'form_action': 'save',
        }
        return render('publishing/edit.html', extra_vars = vars)
    
    
    def edit(self, id, cat_id):
        self._load(id)
        vars = {
                'form_action': 'save',
                'catalog': ExternalCatalog.by_id(cat_id)
        }
        return render('publishing/edit.html', extra_vars = vars)
    
    
    def delete(self, id, cat_id):
        # this is just the confirmation page
        self._load(id)
        vars = {
                'catalog': ExternalCatalog.by_id(cat_id)
        }
        return render('publishing/delete.html', extra_vars = vars)


    def delete_cat(self, id, cat_id):
        # this is the actual deletion
        log.debug("deleting ext catalog {0}".format(cat_id))
        
        try:
            catalog = ExternalCatalog.by_id(cat_id)
            catalog.delete()
            catalog.commit()
            h.flash_success(_('Catalog deleted successfully.'))
        except Exception, e:
            err_msg = str(e)
            h.flash_error(_("Error: couldn't delete catalog, cause: {err_msg}"\
                            .format(err_msg=err_msg)))
        
        base.redirect(h.url_for('dataset_publishing', id=id))


    def save(self, id):
        data = request.POST
        type = data[u'type']
        url = data['url']
        auth_req = False
        if u'authorization_required' in data:
            auth_req = True<commit_msg>fix: url of external catalog ends with slash #5<commit_after>'''
Created on 12.11.2014

@author: mvi
'''

import logging
import pylons.config as config

import ckan.lib.base as base
import ckan.lib.helpers as h
import ckan.lib.plugins
import ckan.logic as logic
import ckan.model as model

from ckan.common import _, request, c
from ckanext.model.external_catalog import ExternalCatalog, STATUS
from urllib2 import URLError
from odn_ckancommons.ckan_helper import CkanAPIWrapper
from ckanext.publishing.ckan_sync import CkanSync
from datetime import datetime
from ckanext.publishing.plugin import sync_ext_catalog

NotFound = logic.NotFound
NotAuthorized = logic.NotAuthorized

render = base.render
abort = base.abort
lookup_package_plugin = ckan.lib.plugins.lookup_package_plugin
check_access = logic.check_access
get_action = logic.get_action

log = logging.getLogger('ckanext')

src_ckan = config.get('odn.ic2pc.src.ckan.url', None)
dst_ckan = config.get('odn.ic2pc.dst.ckan.url', None)
dst_api_key = config.get('odn.ic2pc.dst.ckan.api.key', None)
package_extras_whitelist = config.get('odn.ic2pc.package.extras.whitelist', '').split(' ')
resource_extras_whitelist = config.get('odn.ic2pc.resource.extras.whitelist', '').split(' ')


def get_url_without_slash_at_the_end(url):
    if url and url.endswith("/"):
        return url[:-1]
    else:
        return url


class PublishingController(base.BaseController):

    
    def _get_package_type(self, id):
        """
        Given the id of a package it determines the plugin to load
        based on the package's type name (type). The plugin found
        will be returned, or None if there is no plugin associated with
        the type.
        """
        pkg = model.Package.get(id)
        if pkg:
            return pkg.type or 'dataset'
        return None


    def _load(self, id):
        package_type = self._get_package_type(id.split('@')[0])
        data_dict = {'id': id}
        context = {'model': model, 'session': model.Session,
                   'user': c.user or c.author, 'for_view': True,
                   'auth_user_obj': c.userobj}
        
        try:
            check_access('package_update', context, data_dict)
        except NotAuthorized, e:
            abort(401, _('User {user} not authorized to edit {id}').format(user=c.user, id=id))
        # check if package exists
        try:
            c.pkg_dict = get_action('package_show')(context, data_dict)
            c.pkg = context['package']
        except NotFound:
            abort(404, _('Dataset not found'))
        except NotAuthorized:
            abort(401, _('Unauthorized to read package {id}').format(id=id))
        
        lookup_package_plugin(package_type).setup_template_variables(context, {'id': id})
    
    
    def show(self, id):
        self._load(id)
        vars = {
                'catalogs': ExternalCatalog.by_dataset_id(c.pkg_dict['id'])
        }
        return render('package/publishing.html', extra_vars = vars)
    
    
    def create(self, id):
        self._load(id)
        vars = {
                'form_action': 'save',
        }
        return render('publishing/edit.html', extra_vars = vars)
    
    
    def edit(self, id, cat_id):
        self._load(id)
        vars = {
                'form_action': 'save',
                'catalog': ExternalCatalog.by_id(cat_id)
        }
        return render('publishing/edit.html', extra_vars = vars)
    
    
    def delete(self, id, cat_id):
        # this is just the confirmation page
        self._load(id)
        vars = {
                'catalog': ExternalCatalog.by_id(cat_id)
        }
        return render('publishing/delete.html', extra_vars = vars)


    def delete_cat(self, id, cat_id):
        # this is the actual deletion
        log.debug("deleting ext catalog {0}".format(cat_id))
        
        try:
            catalog = ExternalCatalog.by_id(cat_id)
            catalog.delete()
            catalog.commit()
            h.flash_success(_('Catalog deleted successfully.'))
        except Exception, e:
            err_msg = str(e)
            h.flash_error(_("Error: couldn't delete catalog, cause: {err_msg}"\
                            .format(err_msg=err_msg)))
        
        base.redirect(h.url_for('dataset_publishing', id=id))


    def save(self, id):
        data = request.POST
        type = data.get(u'type', '')
        url = get_url_without_slash_at_the_end(data.get(u'url', ''))
        auth_req = False
        if u'authorization_required' in data:
            auth_req = True
{'fraction_non_alphanumeric': 0.11047426387952348, 'fraction_numerical': 0.005844009889862891, 'mean_word_length': 2.409578544061303, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1665332', 'n_tokens_mistral': 2790, 'n_tokens_neox': 2734, 'n_words': 642}