<commit_before>"""Django admin configuration for the bot objects.
"""

from django.contrib import admin
from django.db import transaction
from django.forms.models import BaseInlineFormSet
import redis

from . import models
from botbot.apps.plugins.models import Plugin

class PluginFormset(BaseInlineFormSet):
    def __init__(self, *args, **kwargs):
        if not kwargs['instance'].pk:
            defaults = Plugin.objects.filter(
                slug__in=models.Channel.DEFAULT_PLUGINS)

            kwargs['initial'] = [{"plugin": obj.pk} for obj in defaults]
        super(PluginFormset, self).__init__(*args, **kwargs)


class ActivePluginInline(admin.StackedInline):
    model = models.Channel.plugins.through
    formset = PluginFormset

    def get_extra(self, request, obj=None, **kwargs):
        if obj is None:
            return len(models.Channel.DEFAULT_PLUGINS)
        return 0


class MembershipInline(admin.TabularInline):
    model = models.Channel.users.through
    extra = 0


class ChatBotAdmin(admin.ModelAdmin):
    list_display = ('__unicode__', 'is_active')
    list_editable = ('is_active',)
    list_filter = ('is_active',)
    readonly_fields = ('connection', 'server_identifier')

    # Disable bulk delete, because it doesn't call delete, so skips REFRESH
    actions = None

    def save_related(self, request, form, formsets, change):
        super(ChatBotAdmin, self).save_related(request, form, formsets, change)
        daemon_refresh()

    def delete_model(self, request, obj):
        super(ChatBotAdmin, self).delete_model(request, obj)
        daemon_refresh()


class ChannelAdmin(admin.ModelAdmin):
    list_display = ('__unicode__', 'name', 'chatbot', 'is_active',
                    'is_public', 'is_featured', 'is_pending', 'updated')
    list_filter = ('chatbot', 'is_active', 'is_public', 'is_featured', 'is_pending')
    list_editable = ('is_active',)
    readonly_fields = ('fingerprint', 'created', 'updated')
    search_fields = ('name', 'chatbot__server')
    inlines = [
            ActivePluginInline,
            MembershipInline
        ]

    def save_related(self, request, form, formsets, change):
        super(ChannelAdmin, self).save_related(request, form, formsets, change)
        daemon_refresh()

    def delete_model(self, request, obj):
        super(ChannelAdmin, self).delete_model(request, obj)
        daemon_refresh()

def daemon_refresh():
    """
    Ask daemon to reload configuration
    """
    # Wait until the db transaction is done and then send the REFRESH command
    # so the Go bot part will update it.

    # THIS IS NO-OP SINCE DJANGO1.6 TRANSACTION REDESIGN
    # See https://django-transaction-hooks.readthedocs.org/ for a better
    # implementation.
    if transaction.is_managed():
        transaction.commit()

    from time import sleep
    sleep(10)

    queue = redis.Redis(db=0)
    queue.lpush('bot', 'REFRESH')


admin.site.register(models.ChatBot, ChatBotAdmin)
admin.site.register(models.Channel, ChannelAdmin)
admin.site.register(models.UserCount)<commit_msg>Commit the transaction before sending the REFRESH signal to botbot-bot<commit_after>"""Django admin configuration for the bot objects.
"""
from django.contrib import admin
from django.db import transaction
from django.forms.models import BaseInlineFormSet
import redis

from . import models
from botbot.apps.plugins.models import Plugin

class PluginFormset(BaseInlineFormSet):
    def __init__(self, *args, **kwargs):
        if not kwargs['instance'].pk:
            defaults = Plugin.objects.filter(
                slug__in=models.Channel.DEFAULT_PLUGINS)

            kwargs['initial'] = [{"plugin": obj.pk} for obj in defaults]
        super(PluginFormset, self).__init__(*args, **kwargs)


class ActivePluginInline(admin.StackedInline):
    model = models.Channel.plugins.through
    formset = PluginFormset

    def get_extra(self, request, obj=None, **kwargs):
        if obj is None:
            return len(models.Channel.DEFAULT_PLUGINS)
        return 0


class MembershipInline(admin.TabularInline):
    model = models.Channel.users.through
    extra = 0


class ChatBotAdmin(admin.ModelAdmin):
    list_display = ('__unicode__', 'is_active')
    list_editable = ('is_active',)
    list_filter = ('is_active',)
    readonly_fields = ('connection', 'server_identifier')

    # Disable bulk delete, because it doesn't call delete, so skips REFRESH
    actions = None

    def save_model(self, request, obj, form, change):
        super(ChatBotAdmin, self).save_model(request, form, form, change)
        transaction.commit()
        daemon_refresh()

    def delete_model(self, request, obj):
        super(ChatBotAdmin, self).delete_model(request, obj)
        transaction.commit()
        daemon_refresh()


class ChannelAdmin(admin.ModelAdmin):
    list_display = ('__unicode__', 'name', 'chatbot', 'is_active',
                    'is_public', 'is_featured', 'is_pending', 'updated')
    list_filter = ('chatbot', 'is_active', 'is_public', 'is_featured', 'is_pending')
    list_editable = ('is_active',)
    readonly_fields = ('fingerprint', 'created', 'updated')
    search_fields = ('name', 'chatbot__server')
    inlines = [
            ActivePluginInline,
            MembershipInline
        ]

    def save_model(self, request, obj, form, change):
        """
        Given a model instance save it to the database.
        """
        super(ChannelAdmin, self).save_model(request, obj, form, change)
        transaction.commit()
        daemon_refresh()

    def delete_model(self, request, obj):
        """
        Given a model instance delete it from the database.
        """
        super(ChannelAdmin, self).delete_model(request, obj)
        transaction.commit()
        daemon_refresh()
        

def daemon_refresh():
    """
    Ask daemon to reload configuration
    """
    queue = redis.Redis(db=0)
    queue.lpush('bot', 'REFRESH')


admin.site.register(models.ChatBot, ChatBotAdmin)
admin.site.register(models.Channel, ChannelAdmin)
admin.site.register(models.UserCount)
{'fraction_non_alphanumeric': 0.11209876543209876, 'fraction_numerical': 0.0016460905349794238, 'mean_word_length': 3.35243553008596, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 1, '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': '6057090', 'n_tokens_mistral': 1803, 'n_tokens_neox': 1761, 'n_words': 442}