Пример #1
0
class Message(db.Document):
    """ 消息回复 """

    TYPE_ARTICLE = "article"
    TYPE_TEXT = "text"
    TYPE_MUSIC = "music"
    TYPE_CHOICES = (
        (TYPE_ARTICLE, '文章'),
        (TYPE_TEXT, '文本'),
        (TYPE_MUSIC, '音乐'),
    )

    keyword = db.XListField(db.StringField(), verbose_name='关键字')
    type = db.StringField(default=TYPE_TEXT,
                          verbose_name='类型',
                          choices=TYPE_CHOICES)
    content = db.StringField(verbose_name='文本回复')
    music = db.EmbeddedDocumentField((MusicItem), verbose_name='音乐')
    article = db.XListField(db.EmbeddedDocumentField(ArticleItem),
                            verbose_name='文章')
    default = db.BooleanField(default=False, verbose_name='默认回复')
    follow = db.BooleanField(default=False, verbose_name='关注回复')
    enable = db.StringField(default=Enable.ENABLED,
                            verbose_name='状态',
                            choices=Enable.CHOICES)
    created = db.DateTimeField(default=datetime.now, verbose_name='创建时间')

    meta = dict(indexes=[
        'default',
        'keyword',
    ])

    def reply(self, message, **kwargs):
        if self.type == 'text':
            return TextReply(
                message=message,
                content=self.content if self.content else '欢迎关注花胶!')
        elif self.type == 'music':
            if self.music.url:
                return MusicReply(message=message,
                                  title=self.music.music_title,
                                  description=self.music.music_description,
                                  url=self.music.url.link,
                                  hq_url=self.music.hq_url.link)
        elif self.type == 'article':
            reply = ArticlesReply(message=message)
            if self.article:
                for art in self.article:
                    article = Article(
                        title=art.title,
                        description=art.description,
                        img=art.img.get_link(),
                        url=art.url,
                    )
                    reply.add_article(article)
                return reply
        return ''
Пример #2
0
class Choices(db.Document):
    """ 选项模型 """

    MENU_ICON = 'plus-circle'

    fields = dict()
    key = db.StringField(verbose_name='键名')
    name = db.StringField(verbose_name='名称')
    default = db.StringField(verbose_name='默认值')
    choices = db.XListField(db.EmbeddedDocumentField(Choice),
                            verbose_name='选项')
    enable = db.BooleanField(default=True, verbose_name='启用')
    modified = db.DateTimeField(default=datetime.now, verbose_name='修改时间')
    created = db.DateTimeField(default=datetime.now, verbose_name='创建时间')

    meta = dict(indexes=['key'])

    def __unicode__(self):
        return '%s - %s' % (self.key, self.name)

    @staticmethod
    def init():
        for key, field in Choices.fields.iteritems():
            Choices.init_field(key, field[1], field[0])

    @staticmethod
    def init_field(key, name, field, choices=None):
        choices = choices or Choices.objects(key=key).first()
        if choices:
            if choices.enable:
                if choices.default:
                    field.default = choices.default
                field.choices = [(x.key, x.name) for x in choices.choices]
            else:
                field.default = None
                field.choices = None
        else:
            Choices(key=key, name=name).save()
        Choices.refresh(field.owner_document)

    @staticmethod
    def refresh(model):
        if model:
            for admin in current_app.extensions.get('admin', []):
                for view in admin._views:
                    if model == getattr(view, 'model', None):
                        view._refresh_cache()

    def save(self):
        super(Choices, self).save()
        field = self.fields.get(self.key)
        if field:
            self.init_field(self.key, field[1], field[0], self)
Пример #3
0
class Model(db.Document):
    """ 建模 """

    MENU_ICON = 'database'

    name = db.StringField(max_length=100, verbose_name='名称')
    desc = db.StringField(max_length=100, verbose_name='描述')
    fields = db.XListField(db.EmbeddedDocumentField(Field), verbose_name='字段')
    modified_field = db.BooleanField(default=True, verbose_name='修改')
    created_field = db.BooleanField(default=True, verbose_name='创建')
    modified = db.DateTimeField(default=datetime.now, verbose_name='修改时间')
    created = db.DateTimeField(default=datetime.now, verbose_name='创建时间')

    def __unicode__(self):
        return self.name