Exemple #1
0
class CommentMod(ClassTool):
    def __init__(self, user_info=None):
        ClassTool.__init__(self)
        self.__conn = MongoHelp('Comment')
        from user import User
        if isinstance(user_info, User):
            self.user = user_info

    @ModVerify
    def add(self, main_id, comment, level_reply=None, reviewed=False):
        comment_num = self.__conn.conn.find({'main_id': main_id}).count()
        comment_post = dict(
            main_id=main_id,
            level_num=comment_num+1,
            level_reply=level_reply,
            content=comment,
            username=self.user.user_data['username'],
            user_id=self.user.user_data['_id'],
            status='normal',
            reviewed=reviewed
        )
        is_add = self.__conn.insert(comment_post)
        return self._sys_msg(is_add, 'Grant Fail', {'_id': is_add})

    @ModVerify
    def modify(self, comment_id, comment):
        is_fix = self.__conn.fix_one({'_id': comment_id}, {'content': comment})
        return self._sys_msg(is_fix, 'Grant Fail', {'_id': comment_id})

    @ModVerify
    def remove(self, comment_id):
        is_del = self.__conn.remove({'_id': comment_id})
        return self._sys_msg(is_del, 'Grant Fail', {'_id': comment_id})

    @ModVerify
    def review(self, comment_id, is_pass=True):
        is_review = self.__conn.fix_one({'_id': comment_id}, {'reviewed': is_pass})
        return self._sys_msg(is_review, 'Grant Fail', {'_id': comment_id})

    @ModVerify
    def details(self, _id):
        found = self.__conn.find_one({'_id': _id})
        return self._sys_msg(found, 'Not Found')

    @ModVerify
    def get_list(self, ident=None, limit=10, skip=0):
        if ident is None:
            ident = {'reviewed': True}
        found = self.__conn.conn.find(ident).limit(limit).skip(skip)
        total = found.count()
        comment_data = self.__conn.id_format(found)
        return self._sys_msg(comment_data, 'Not Found', {'page': {'total': total, 'limit': limit, 'skip': skip}})
Exemple #2
0
class ContentMod(ClassTool):
    def __init__(self, user_info=None):
        ClassTool.__init__(self)
        self.__conn = MongoHelp('Content')
        from user import User
        if isinstance(user_info, User):
            self.user = user_info

    def __set_classid(self, class_id):
        self.req_permisssion = class_id
        self.__class_id = class_id
        self.__class_info = MongoHelp('Column').find_one({'class_id': class_id})
        if self.__class_info['type'] not in MongoHelp('Setting').find_one(
                {'set_type': 'content'})['content_default'].keys():
            raise Exception('Content set Error: %s' % self.__class_info['type'])
        self.__content_default = MongoHelp('Setting').find_one(
            {'set_type': 'content'})['content_default'][self.__class_info['type']]['data']

    @staticmethod
    def _set_type(default, input_data):
        if isinstance(default, unicode):
            if default.split(',')[0] == 'str':
                default = input_data
            elif default.split(',')[0] == 'int':
                default = int(input_data)
            elif default.split(',')[0] == 'datetime':
                default = datetime.datetime.strptime(input_data, "%Y-%m-%d %H:%M:%S").date()
                # default = input_data
            else:
                default = default.split(',')[2]
            return default
        elif isinstance(default, dict):
            new = {}
            for n in default.keys():
                new[n] = ContentMod._set_type(default[n], input_data[n])
            return new
        elif isinstance(default, list):
            new = []
            for n in input_data:
                new.append(ContentMod._set_type(default[0], n))
            return new

    def _data_format(self, json_data):
        format_data = dict(content_type=self.__class_info['type'],
                           status='normal',
                           reviewed=False,
                           class_id=self.__class_id,
                           statistics={})
        format_data['data'] = ContentMod._set_type(self.__content_default, json_data)
        return format_data

    @ModVerify
    def insert(self, class_id, content_json):
        self.__set_classid(class_id)
        content_json['author_name'] = self.user.user_data['username']
        content_json['author_id'] = self.user.user_data['_id']
        content_data = self._data_format(content_json)
        content_data['addtime'] = datetime.datetime.now()
        content_data['title'] = content_data['data'].get('title', '')
        content_data['author_id'] = self.user.user_data['_id']
        is_insert = self.__conn.insert(content_data)
        return self._sys_msg(is_insert, 'Grant Fail', {'_id': is_insert})

    @ModVerify
    def modify(self, content_id, content_json):
        content_json['author_name'] = self.user.user_data['username']
        content_json['author_id'] = self.user.user_data['_id']
        content_data = self._data_format(content_json)
        is_update = self.__conn.fix_one({'_id': content_id}, content_data)
        return self._sys_msg(is_update, 'Grant Fail', {'_id': content_id})

    @ModVerify
    def remove(self, content_id):
        is_remove = self.__conn.remove({'_id': content_id})
        return self._sys_msg(is_remove, 'Grant Fail', {'_id': content_id})

    @ModVerify
    def review(self, content_id, is_pass=True):
        is_reviewed = self.__conn.fix_one({'_id': content_id}, {'reviewed': is_pass})
        return self._sys_msg(is_reviewed, 'Grant Fail', {'_id': content_id})

    @ModVerify
    def details(self, _id):
        found = self.__conn.find_one({'_id': _id})
        return self._sys_msg(found, 'Not Found')

    @ModVerify
    def get_list(self, ident=None, limit=10, skip=0):
        ident_json = {}
        if isinstance(ident, dict):
            ident_json.update(ident)
        found = self.__conn.conn.find(ident_json, {'data': 0}).limit(limit).skip(skip)
        total = found.count()
        found = self.__conn.id_format(found)
        reviewed = self.__conn.conn.find({'reviewed': True}).count()
        return self._sys_msg(found, 'Grant Fail', {'page': {'total': total, 'limit': limit,
                                                            'skip': skip, 'reviewed': reviewed}})

    pass