# -*- coding: utf-8 -*-

from django.db import models

from system.core.exceptions import PluginDoesNotExist
from system.official_account.models import OfficialAccount


class PluginManager(models.Manager):
    def get(self, official_account, iden):
        """
        获取插件实例
        :param official_account: 所属公众号
        :param iden: 插件标识符
        :return: 如果插件存在, 返回插件实例 (Plugin)
        :raise PluginDoesNotExist: 当不存在该插件或该插件在所属公众号中没有开启则抛出此异常
        """
        plugin = super(PluginManager, self).get_queryset().filter(pk=iden)
        if not plugin:
            raise PluginDoesNotExist('the plugin iden does not exist')
        if official_account not in plugin[0].official_account.all():
            raise PluginDoesNotExist('the plugin is not enabled in the official account')
        return plugin[0]

    def add(self, iden, name, description=None, author=None, website=None, email=None, version=None):
        """
        添加一个新的插件
        :param iden: 插件标识符
        :param name: 插件名称
        :param description: 插件描述
        :param author: 插件作者
        :param website: 插件作者网站
        :param email: 插件作者邮箱
        :param version: 插件版本
        :return:
        """
        return super(PluginManager, self).create(
            iden=iden,
            name=name,
            description=description,
            author=author,
            website=website,
            email=email,
            version=version
        )


class Plugin(models.Model):
    iden = models.CharField(u'插件标识符', max_length=50, primary_key=True)
    name = models.CharField(u'插件名称', max_length=100)
    description = models.TextField(u'插件描述', blank=True, null=True)
    author = models.CharField(u'插件作者', max_length=50, blank=True, null=True)
    website = models.URLField(u'插件作者网站', blank=True, null=True)
    email = models.EmailField(u'插件作者邮箱', blank=True, null=True)
    version = models.CharField(u'插件版本', max_length=30, blank=True, null=True)
    official_account = models.ManyToManyField(OfficialAccount, verbose_name=u'开启该插件的公众号集合')

    objects = models.Manager()
    manager = PluginManager()

    class Meta:
        verbose_name = u'插件表'
        verbose_name_plural = u'插件表'
        db_table = 'wechat_plugin'

    def __unicode__(self):
        return self.name
@@ -18,16 +18,32 @@
 -8 -*-%0A%0A
+import logging%0A%0A
 from dja
@@ -178,16 +178,61 @@
 ccount%0A%0A
+logger_plugin = logging.getLogger(__name__)%0A%0A
 %0Aclass P
@@ -612,16 +612,153 @@
 plugin:%0A
+            logger_plugin.warning('%5BPlugin%5D The plugin iden %25s does not exist %5BOfficialAccount%5D %25s' %25 (iden, official_account.__dict__))%0A
         
@@ -803,16 +803,19 @@
 in iden 
+%25s 
 does not
@@ -821,16 +821,23 @@
 t exist'
+ %25 iden
 )%0A      
@@ -899,16 +899,195 @@
 .all():%0A
+            logger_plugin.warning('%5BPlugin%5D The plugin is not enabled in the official account %5BOfficialAccount%5D %25s %5BPlugin%5D %25s' %25 (official_account.__dict__, plugin%5B0%5D.__dict__))%0A
         
@@ -1565,22 +1565,24 @@
         
-return
+plugin =
  super(P
@@ -1811,16 +1811,126 @@
       )%0A
+        logger_plugin.info('%5BPlugin%5D New plugin created %5BDetail%5D %25s' %25 plugin.__dict__)%0A        return plugin%0A
 %0A%0Aclass 

add the log for plugin app