<commit_before>from models import Plugin, Description, Rule, Cron, Intent
import json, git, os, shutil
from shutil import rmtree
from twisted.python.reflect import namedAny
import inspect
from django.template.loader import render_to_string
import datetime
from weblisa.settings import LISA_PATH


def install(plugin_url=None, plugin_sha=None, plugin_name=None):
    if plugin_url and plugin_sha:
        repo = git.Repo.clone_from(plugin_url, LISA_PATH + '/plugins/' + plugin_name)
        repo.git.checkout(plugin_sha)
    metadata = json.load(open(LISA_PATH + '/plugins/' + plugin_name + '/' + str(plugin_name).lower() + '.json'))

    if Plugin.objects(name=plugin_name):
        return {'status': 'fail', 'log': 'Plugin already installed'}
    else:
        plugin = Plugin()
        description_list = []
        for item in metadata:
            if item != 'cron' or item != 'rules':
                if item == 'description':
                    for description in metadata[item]:
                        oDescription = Description()
                        for k,v in description.iteritems():
                            setattr(oDescription, k, v)
                        description_list.append(oDescription)
                    setattr(plugin, item, description_list)
                elif item == 'enabled':
                    if metadata[item] == 0:
                        setattr(plugin, item, False)
                    else:
                        setattr(plugin, item, True)
                else:
                    setattr(plugin, item, metadata[item])
        plugin.save()

        for item in metadata:
            if item == 'rules':
                for rule_item in metadata['rules']:
                    rule = Rule()
                    for parameter in rule_item:
                        setattr(rule, parameter, rule_item[parameter])
                    rule.plugin = plugin
                    rule.save()
            if item == 'crons':
                for cron_item in metadata['crons']:
                    cron = Cron()
                    for parameter in cron_item:
                        setattr(cron, parameter, cron_item[parameter])
                    cron.plugin = plugin
                    cron.save()


        for intent, value in metadata['configuration']['intents'].iteritems():
            oIntent = Intent()
            oIntent.name = intent
            oIntent.function = value['method']
            oIntent.module = '.'.join(['plugins', plugin_name, 'modules', plugin_name.lower(), plugin_name])
            oIntent.enabled = True
            oIntent.plugin = plugin
            oIntent.save()

        return {'status': 'success', 'log': 'Plugin installed'}


def enable(plugin_name=None, plugin_pk=None):
    if plugin_pk:
        plugin_list = Plugin.objects(pk=plugin_pk)
    else:
        plugin_list = Plugin.objects(name=plugin_name)
    for plugin in plugin_list:
        if plugin.enabled:
            return {'status': 'fail', 'log': 'Plugin already enabled'}
        else:
            plugin.enabled = True
            plugin.save()
            for cron in Cron.objects(plugin=plugin):
                cron.enabled = True
                cron.save()
            for rule in Rule.objects(plugin=plugin):
                rule.enabled = True
                rule.save()

            intent_list = Intent.objects(plugin=plugin)
            for oIntent in intent_list:
                oIntent.enabled = True

            return {'status': 'success', 'log': 'Plugin enabled'}

def disable(plugin_name=None, plugin_pk=None):
    if plugin_pk:
        plugin_list = Plugin.objects(pk=plugin_pk)
    else:
        plugin_list = Plugin.objects(name=plugin_name)
    for plugin in plugin_list:
        if not plugin.enabled:
            return {'status': 'fail', 'log': 'Plugin already disabled'}
        else:
            plugin.enabled = False
            plugin.save()
            for cron in Cron.objects(plugin=plugin):
                cron.enabled = False
                cron.save()
            for rule in Rule.objects(plugin=plugin):
                rule.enabled = False
                rule.save()

            intent_list = Intent.objects(plugin=plugin)
            for oIntent in intent_list:
                oIntent.enabled = False

            return {'status': 'success', 'log': 'Plugin disabled'}

<commit_msg>Fixing the enable/disable of intents<commit_after>from models import Plugin, Description, Rule, Cron, Intent
import json, git, os, shutil
from shutil import rmtree
from twisted.python.reflect import namedAny
import inspect
from django.template.loader import render_to_string
import datetime
from weblisa.settings import LISA_PATH


def install(plugin_url=None, plugin_sha=None, plugin_name=None):
    if plugin_url and plugin_sha:
        repo = git.Repo.clone_from(plugin_url, LISA_PATH + '/plugins/' + plugin_name)
        repo.git.checkout(plugin_sha)
    metadata = json.load(open(LISA_PATH + '/plugins/' + plugin_name + '/' + str(plugin_name).lower() + '.json'))

    if Plugin.objects(name=plugin_name):
        return {'status': 'fail', 'log': 'Plugin already installed'}
    else:
        plugin = Plugin()
        description_list = []
        for item in metadata:
            if item != 'cron' or item != 'rules':
                if item == 'description':
                    for description in metadata[item]:
                        oDescription = Description()
                        for k,v in description.iteritems():
                            setattr(oDescription, k, v)
                        description_list.append(oDescription)
                    setattr(plugin, item, description_list)
                elif item == 'enabled':
                    if metadata[item] == 0:
                        setattr(plugin, item, False)
                    else:
                        setattr(plugin, item, True)
                else:
                    setattr(plugin, item, metadata[item])
        plugin.save()

        for item in metadata:
            if item == 'rules':
                for rule_item in metadata['rules']:
                    rule = Rule()
                    for parameter in rule_item:
                        setattr(rule, parameter, rule_item[parameter])
                    rule.plugin = plugin
                    rule.save()
            if item == 'crons':
                for cron_item in metadata['crons']:
                    cron = Cron()
                    for parameter in cron_item:
                        setattr(cron, parameter, cron_item[parameter])
                    cron.plugin = plugin
                    cron.save()


        for intent, value in metadata['configuration']['intents'].iteritems():
            oIntent = Intent()
            oIntent.name = intent
            oIntent.function = value['method']
            oIntent.module = '.'.join(['plugins', plugin_name, 'modules', plugin_name.lower(), plugin_name])
            oIntent.enabled = True
            oIntent.plugin = plugin
            oIntent.save()

        return {'status': 'success', 'log': 'Plugin installed'}


def enable(plugin_name=None, plugin_pk=None):
    if plugin_pk:
        plugin_list = Plugin.objects(pk=plugin_pk)
    else:
        plugin_list = Plugin.objects(name=plugin_name)
    for plugin in plugin_list:
        if plugin.enabled:
            return {'status': 'fail', 'log': 'Plugin already enabled'}
        else:
            plugin.enabled = True
            plugin.save()
            for cron in Cron.objects(plugin=plugin):
                cron.enabled = True
                cron.save()
            for rule in Rule.objects(plugin=plugin):
                rule.enabled = True
                rule.save()

            intent_list = Intent.objects(plugin=plugin)
            for oIntent in intent_list:
                oIntent.enabled = True
                oIntent.save()
            return {'status': 'success', 'log': 'Plugin enabled'}

def disable(plugin_name=None, plugin_pk=None):
    if plugin_pk:
        plugin_list = Plugin.objects(pk=plugin_pk)
    else:
        plugin_list = Plugin.objects(name=plugin_name)
    for plugin in plugin_list:
        if not plugin.enabled:
            return {'status': 'fail', 'log': 'Plugin already disabled'}
        else:
            plugin.enabled = False
            plugin.save()
            for cron in Cron.objects(plugin=plugin):
                cron.enabled = False
                cron.save()
            for rule in Rule.objects(plugin=plugin):
                rule.enabled = False
                rule.save()

            intent_list = Intent.objects(plugin=plugin)
            for oIntent in intent_list:
                oIntent.enabled = False
                oIntent.save()

            return {'status': 'success', 'log': 'Plugin disabled'}


{'fraction_non_alphanumeric': 0.09920814479638008, 'fraction_numerical': 0.00022624434389140272, 'mean_word_length': 1.7144611605772182, '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': '3611238', 'n_tokens_mistral': 2318, 'n_tokens_neox': 2185, 'n_words': 596}