<commit_before>import yampy
import db as db
import json
import ckan.lib.helpers as h
import ckan.model.package as package
import ckan.plugins as plugins
import ckan.plugins.toolkit as toolkit
import ckanext.yammer.model.yammer_user as yammer_user
from pylons import config
from routes.mapper import SubMapper
from ckan.common import c
from sqlalchemy import exc, inspect
import ckan.lib.helpers as h


def yammer_config(id):

    try:
        context = {'for_view': True}
        yammer_config_options = yammer_user.Yammer_user().get(id)
        form = db.table_dictize(yammer_config_options, context)
        jsonform = json.dumps(form)
        print yammer_config_options
        return str(jsonform)
    except exc.SQLAlchemyError:
        return 'failure'

group_type = u'grup'
group_type_utf8 = group_type.encode('utf8')


def get_yammer_clientid():
    id = config['ckan.yammer.id']
    return id


class YammerPlugin(plugins.SingletonPlugin, toolkit.DefaultGroupForm):
    plugins.implements(plugins.IGroupForm, inherit=False)
    plugins.implements(plugins.IConfigurer, inherit=True)
    plugins.implements(plugins.IMapper)
    plugins.implements(plugins.IRoutes, inherit=True)
    plugins.implements(plugins.ITemplateHelpers)

    # Tell CKAN what custom template helper functions this plugin provides,
    def get_helpers(self):
        return {'yammer_config': yammer_config,
                'get_yammer_clientid': get_yammer_clientid }

    def get_edit_type(self, p):
        types = []
        if p.owner_org is not None:
            yammer_poster = yammer_user.Yammer_user().get(c.userobj.id + "." + p.owner_org)

            if p is not None:
                yammer_poster = yammer_user.Yammer_user().get(c.userobj.id + "." + p.owner_org)

                if yammer_poster is not None and yammer_poster.create_dataset is True:
                    types.append('create')
                if yammer_poster is not None and yammer_poster.update_dataset is True:
                    types.append('update')
                if yammer_poster is not None and yammer_poster.delete_dataset is True:
                    types.append('delete')

        return types

    def yammer_post(self, edit_type, p, org_name):
        if p.owner_org is not None:
            yammer_poster = yammer_user.Yammer_user().get(c.userobj.id + "." + p.owner_org)
            access_token = yammer_poster.token
            groups = yammer_poster.groups
            url_base = h.get_site_protocol_and_host()
            url = url_base[0] + '://' + url_base[1] + toolkit.url_for(controller='package', action='read', id=p.name)
            yammer = yampy.Yammer(access_token=access_token)
            for group_id in groups:
                if edit_type == 'deleted':
                    message = 'The {} dataset has been {} from {}'.format(p.name, edit_type,  org_name)
                    yammer.messages.create(message, group_id=group_id, topics=['Dataset', edit_type])
                elif edit_type == 'created':
                    message = 'The {} dataset has been {} for {}, you can see it here: {}'.format(p.name, edit_type, org_name,  url)
                    yammer.messages.create(message, group_id=group_id, topics=['Dataset', edit_type])
                elif edit_type == 'updated':
                    message = 'The {} dataset has been {} for {}, you can see the updates here: {}'.format(p.name, edit_type, org_name,  url)
                    yammer.messages.create(message, group_id=group_id, topics=['Dataset', edit_type])



    # IConfigurer
    def update_config(self, config):
        toolkit.add_template_directory(config, 'templates')
        toolkit.add_public_directory(config, 'public')
        toolkit.add_resource('fanstatic', 'yammer')

    # IMapper
    def before_update(self, mapper, connection, instance):
        pass

    def after_update(self, mapper, connection, instance):
        #get the package details from the mapper
        p = package.Package().get(instance.id)
        if p != None:
            edits = self.get_edit_type(p)
            org = h.get_organization(org=p.owner_org)
            for action in edits:
                if action == 'update' and p.state != 'deleted' and p.state != 'draft' and p.private is not True:
                    self.yammer_post('updated', p, org['display_name'])
                elif action == 'delete' and p.state == 'deleted':
                    self.yammer_post('deleted', p, org['display_name'])
                else:
                    pass


    def before_insert(self, mapper, connection, instance):
        pass

    def after_insert(self, mapper, connection, instance):
        p = package.Package().get(instance.id)
        if p is not None:
            edits = self.get_edit_type(p)
            org = h.get_organization(org=p.owner_org)<commit_msg>fixed inserts failing when table doesn't exist<commit_after>import yampy
import db as db
import json
import ckan.lib.helpers as h
import ckan.model.package as package
import ckan.plugins as plugins
import ckan.plugins.toolkit as toolkit
import ckanext.yammer.model.yammer_user as yammer_user
from pylons import config
from routes.mapper import SubMapper
from ckan.common import c
from sqlalchemy import exc, inspect
import ckan.lib.helpers as h


def yammer_config(id):

    try:
        context = {'for_view': True}
        yammer_config_options = yammer_user.Yammer_user().get(id)
        form = db.table_dictize(yammer_config_options, context)
        jsonform = json.dumps(form)
        print yammer_config_options
        return str(jsonform)
    except exc.SQLAlchemyError:
        return 'failure'

group_type = u'grup'
group_type_utf8 = group_type.encode('utf8')


def get_yammer_clientid():
    id = config['ckan.yammer.id']
    return id


class YammerPlugin(plugins.SingletonPlugin, toolkit.DefaultGroupForm):
    plugins.implements(plugins.IGroupForm, inherit=False)
    plugins.implements(plugins.IConfigurer, inherit=True)
    plugins.implements(plugins.IMapper)
    plugins.implements(plugins.IRoutes, inherit=True)
    plugins.implements(plugins.ITemplateHelpers)

    # Tell CKAN what custom template helper functions this plugin provides,
    def get_helpers(self):
        return {'yammer_config': yammer_config,
                'get_yammer_clientid': get_yammer_clientid }

    def get_edit_type(self, p):
        types = []
        if p.owner_org is not None:
            yammer_poster = yammer_user.Yammer_user().get(c.userobj.id + "." + p.owner_org)

            if p is not None:
                yammer_poster = yammer_user.Yammer_user().get(c.userobj.id + "." + p.owner_org)

                if yammer_poster is not None and yammer_poster.create_dataset is True:
                    types.append('create')
                if yammer_poster is not None and yammer_poster.update_dataset is True:
                    types.append('update')
                if yammer_poster is not None and yammer_poster.delete_dataset is True:
                    types.append('delete')

        return types

    def yammer_post(self, edit_type, p, org_name):
        if p.owner_org is not None:
            yammer_poster = yammer_user.Yammer_user().get(c.userobj.id + "." + p.owner_org)
            access_token = yammer_poster.token
            groups = yammer_poster.groups
            url_base = h.get_site_protocol_and_host()
            url = url_base[0] + '://' + url_base[1] + toolkit.url_for(controller='package', action='read', id=p.name)
            yammer = yampy.Yammer(access_token=access_token)
            for group_id in groups:
                if edit_type == 'deleted':
                    message = 'The {} dataset has been {} from {}'.format(p.name, edit_type,  org_name)
                    yammer.messages.create(message, group_id=group_id, topics=['Dataset', edit_type])
                elif edit_type == 'created':
                    message = 'The {} dataset has been {} for {}, you can see it here: {}'.format(p.name, edit_type, org_name,  url)
                    yammer.messages.create(message, group_id=group_id, topics=['Dataset', edit_type])
                elif edit_type == 'updated':
                    message = 'The {} dataset has been {} for {}, you can see the updates here: {}'.format(p.name, edit_type, org_name,  url)
                    yammer.messages.create(message, group_id=group_id, topics=['Dataset', edit_type])



    # IConfigurer
    def update_config(self, config):
        toolkit.add_template_directory(config, 'templates')
        toolkit.add_public_directory(config, 'public')
        toolkit.add_resource('fanstatic', 'yammer')

    # IMapper
    def before_update(self, mapper, connection, instance):
        pass

    def after_update(self, mapper, connection, instance):
        #get the package details from the mapper
        if db.yammer_user_table is not None:
            p = package.Package().get(instance.id)
        else:
            p = None

        if p != None:
            edits = self.get_edit_type(p)
            org = h.get_organization(org=p.owner_org)
            for action in edits:
                if action == 'update' and p.state != 'deleted' and p.state != 'draft' and p.private is not True:
                    self.yammer_post('updated', p, org['display_name'])
                elif action == 'delete' and p.state == 'deleted':
                    self.yammer_post('deleted', p, org['display_name'])
                else:
                    pass


    def before_insert(self, mapper, connection, instance):
        pass

    def after_insert(self, mapper, connection, instance):
        if db.yammer_user_table is not None:
            p = package.Package().get(instance.id)
        else:
            p = None


        if p is not None:
            edits = self.get_edit_type(p)
            org = h.get_organization(org=p.owner_org)
{'fraction_non_alphanumeric': 0.10016237061091943, 'fraction_numerical': 0.0008118530545971179, 'mean_word_length': 2.479872881355932, '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': '3360653', 'n_tokens_mistral': 3073, 'n_tokens_neox': 2994, 'n_words': 709}