Exemplo n.º 1
0
    def filter_post(self, level=0, tag=None, catalog=None, user=None):
        query = Post.query \
            .filter(Post.secret <= level)

        if isinstance(user, (str, int)):
            user = UserManager.find_user(user)

        if user:
            query = query.filter(
                Post.owner == User.user_id).filter((~Post.hidden) | (
                    Post.owner == user.user_id) | (User.role < user.role))
        else:
            query = query.filter(~Post.hidden)

        if tag:
            tag = self.find_tag(tag)
            if tag is None:
                return empty_query(Post)
            query = query \
                .join(post_tag) \
                .filter(post_tag.c.post_id == Post.post_id) \
                .filter(post_tag.c.tag_id == tag.tag_id)

        if catalog:
            catalog = self.find_catalog(catalog) or self.find_catalog_by_link(
                catalog)
            if not catalog:
                return empty_query(Post)
            query = query \
                .join(post_catalog) \
                .filter(post_catalog.c.post_id == Post.post_id) \
                .filter(post_catalog.c.catalog_id == catalog.catalog_id)

        return query
Exemplo n.º 2
0
def refresh(username, role):
    success = False
    if UserManager.is_master(username):
        success = True
        refresh_salt()

    return {'success': success}
Exemplo n.º 3
0
def get_dynamic_info(username, role):
    if UserManager.is_master(username):
        return {
            'success': True,
            'result': {
                'cpu': RoseliaSysInfo.cpu_usage(),
                'memory': RoseliaSysInfo.memory_usage()
            }
        }
Exemplo n.º 4
0
 def on_connect(self):
     username = self.username
     username = UserManager.find_user_option(username).map(attrgetter('username')).get_or(username)
     if username:
         self.session.add_user(username, self.sid)
         self.enter_room(self.sid, username)
     else:
         self.emit('reject', 'You should login first.')
         raise ConnectionRefusedError('Not Authorized', 'Missing token or token expired.')
Exemplo n.º 5
0
def get_basic_info(username, role):
    if UserManager.is_master(username):
        return {
            'success': True,
            'result': {
                'os': RoseliaSysInfo.os_full_name(),
                'cpu': RoseliaSysInfo.processor_info()
            }
        }
    return {'success': False, 'msg': 'Not master'}
Exemplo n.º 6
0
    def delete_comment(cls, comment_id, by_user):
        user = UserManager.find_user(by_user)
        if user is None:
            return False

        comment = Comment.query.get(comment_id)
        post = comment.post
        author = comment.author

        if not cls.can_delete_comment(user, author):
            return False

        db.session.delete(comment)
        db.session.commit()
        return True
Exemplo n.º 7
0
 def get_posts_from_author(self,
                           user,
                           offset,
                           count=None,
                           level=0,
                           by_user=None):
     user = UserManager.find_user(user)
     if not user:
         return 0, []
     posts = self.filter_post(
         level, user=by_user).filter(Post.owner == user.user_id)
     return posts.count(), [
         x.brief_dict for x in posts.order_by(Post.post_id.desc()).limit(
             count).offset(offset).all()
     ]
Exemplo n.º 8
0
    def delete_by_index(cls,
                        application: str,
                        index_key: str,
                        all_user: bool,
                        username: str = None):
        query = PluginStorage.query \
            .filter(PluginStorage.application == application) \
            .filter(PluginStorage.index_key == index_key)

        if all_user:
            if not UserManager.is_master(username):
                return False
        if not all_user:
            query = query.filter(
                PluginStorage.user == cls.get_user_id(username))

        db.session.delete(query)
        db.session.commit()
        return True
Exemplo n.º 9
0
    def get_next(self, pid, level=0, user=None):
        data = Post.query \
            .filter(sql_ops.and_(Post.post_id > pid, Post.secret <= level))

        if isinstance(user, (str, int)):
            user = UserManager.find_user(user)

        if user:
            data = data.filter(
                Post.owner == User.user_id).filter((~Post.hidden) | (
                    Post.owner == user.user_id) | (User.role < user.role))
        else:
            data = data.filter(~Post.hidden)

        data = data \
            .order_by(Post.post_id.asc()).limit(1).first()
        if not data:
            return -1
        return data.post_id
Exemplo n.º 10
0
def add_comment(username, role, content, to_post, raw_payload):
    nickname = raw_payload.get('nickname')
    name = username or nickname
    if not name:
        return {
            'success': False,
            'msg': 'Missing nickname or token expired.'
        }

    to_post = Option.from_call(int, to_post).get_or(None)

    if not to_post:
        return {
            'success': False,
            'msg': 'to_post should be a number'
        }

    state, msg = CommentManager.add_comment(to_post, content, raw_payload.get('to_comment'), username, nickname)
    if state:
        token = dct_processor.iss_remove_token(msg)
        emit('comment_added', {
            'post_id': to_post,
            'comment_id': msg
        })
        raw_post = PostManager().get_db_post(to_post)
        author = raw_post.author.username
        if not nickname:
            name = UserManager.find_user(username).nickname
        emit('post_commented', {
            'post_id': raw_post.post_id,
            'title': raw_post.title,
            'by_name': name
        }, room=author)

    else:
        token = ''
    return {
        'success': state,
        'msg': 'Success' if state else msg,
        'comment_id': msg,
        'remove_token': token
    }
Exemplo n.º 11
0
 def add_comment(cls,
                 to_post,
                 content,
                 to_comment,
                 from_user=None,
                 nickname=None):
     user = None
     if from_user:
         user = UserManager.find_user(from_user)
         if not user:
             return False, 'Invalid user.'
     post = post_manager.get_db_post(to_post)
     if post is None:
         return False, 'Invalid post.'
     if not post.enable_comment and user is None:
         return False, 'You are not allowed to comment.'
     comment = Comment(to_article=post.post_id,
                       reply_to=to_comment,
                       content=content,
                       from_user=user and user.user_id,
                       nickname=nickname)
     db.session.add(comment)
     db.session.commit()
     return True, comment.comment_id
Exemplo n.º 12
0
    def add_post(self, post, owner, fmt_md=False):
        if not isinstance(post, dict):
            return False
        # if not all(i in keys for i in post.keys()): return False
        # post = {
        #     k: v for k, v in post.items() if k in self.keys
        # }
        secret = int(post.get('secret', 0))
        author = UserManager.find_user(owner)  # type: User
        if not author or author.role + 1 < secret:
            return False
        if fmt_md:
            post['md_content'] = post['content']
            post['content'] = markdown(post['content'])
        tags = list(map(self.ensure_tag, post.get('tags', [])))
        catalogs = list(
            filter(truth, map(self.find_catalog, post.get('catalog', []))))
        cnv_dict = {
            'title': post.get('title', 'Untitled'),
            'subtitle': post.get('subtitle', ''),
            'display_id': post.get('display_id', post.get('title')),
            'content': post.get('content', ''),
            'author': author,
            'cover': post.get('img', ''),
            'tags': tags,
            'secret': secret,
            'catalogs': catalogs,
            'md_content': post.get('md_content', ''),
            'hidden': post.get('hidden', False),
            'enable_comment': post.get('enable_comment', True)
        }
        insert_post = Post(**cnv_dict)
        db.session.add(insert_post)
        db.session.commit()

        return True
Exemplo n.º 13
0
def remove_application(application, username, role):
    result = UserManager.is_master(username)
    if result:
        result = PluginStorageManager.remove_application(application)
    return {'success': not not result}