Пример #1
0
def index():
    page = get_page()
    pagination = Paste.objects(is_private=False).order_by('-updated_at').paginate(page=page, per_page=20)

    return render_template('index.html',
                           pagination=pagination,
                           tags=Tag.objects().order_by('-popularity')[:10])
Пример #2
0
def add_paste():
    if 'paste_hash_id' not in request.form or 'bookmark_id' not in request.form:
        abort(404)

    bookmark = Bookmark.objects(hash_id=request.form['bookmark_id']).get_or_404()
    paste = Paste.objects(hash_id=request.form['paste_hash_id']).get_or_404()
    if paste in bookmark.pastes:
        return render_template('error.html',
                               title=u'该代码集合已经在收藏夹中',
                               message=u'该代码集合已经在收藏夹中')
    if len(bookmark.pastes) >= 10:
        return render_template('error.html',
                               title=u'一个收藏夹最多只能有10个代码集合',
                               message=u'一个收藏夹最多只能有10个代码集合')
    if paste not in bookmark.pastes:
        bookmark.pastes.append(paste)
        bookmark.save()

        if bookmark.user != paste.user and not bookmark.is_private:
            content = BOOKMARK.format(user_username=current_user.user.username,
                                      user_url=url_for('user_app.view', username=current_user.user.username),
                                      paste_title=paste.title,
                                      paste_url=url_for('paste_app.view_paste', hash_id=paste.hash_id),
                                      bookmark_title=bookmark.title,
                                      bookmark_url=url_for('bookmark_app.view', hash_id=bookmark.hash_id))

            message = Message(user=paste.user,
                              who=bookmark.user,
                              content=content)
            message.save()

    return redirect(url_for('bookmark_app.view', hash_id=bookmark.hash_id))
Пример #3
0
def get_pastes_from_search(p=1):
    query_string = request.query.q

    def get_string_by_keyword(keyword, query_string):
        string = ''
        result = re.search('\s*%s:([a-zA-Z+-_#]+)\s*' % keyword, query_string)
        if result:
            if len(result.groups()) == 1:
                string = result.groups()[0]
        query_string = query_string.replace('%s:%s' % (keyword, string), '')
        return string, query_string

    tag, query_string = get_string_by_keyword('tag', query_string)
    user, query_string = get_string_by_keyword('user', query_string)
    keyword = query_string.strip()

    criteria = {'title__contains': keyword, 'is_private': False}
    if tag:
        criteria['tags'] = tag
    if user:
        user_object = User.objects(username=user).first()
        criteria['user'] = user_object

    return keyword, Paste.objects(
        **criteria).order_by('-updated_at')[(p - 1) * ITEMS_PER_PAGE:p *
                                            ITEMS_PER_PAGE]
Пример #4
0
def index():
    page = get_page()
    pagination = Paste.objects(is_private=False).order_by('-updated_at').paginate(page=page, per_page=20)

    return render_template('index.html',
                           pagination=pagination,
                           tags=Tag.objects().order_by('-popularity')[:10])
Пример #5
0
def view(username):
    page = get_page()
    user = User.objects.get_or_404(username=username)

    pastes = user.pastes.order_by('-updated_at')
    if not (current_user.is_authenticated and current_user.user == user):
        pastes = pastes(is_private=False)

    pagination = pastes.paginate(page, per_page=20)

    pastes = Paste.objects(user=user)
    syntax = {}
    for paste in pastes:
        for code in paste.codes:
            if code.syntax.name not in syntax:
                syntax[code.syntax.name] = 1
            else:
                syntax[code.syntax.name] += 1

    if len(syntax.keys()) > 3:
        most_syntax = [get_most_syntax(syntax) for i in range(3)]
    else:
        most_syntax = [Syntax.objects(name=key).first() for key in syntax]

    return render_template('users/user.html',
                           user=user,
                           pagination=pagination,
                           most_syntax=most_syntax,
                           tags=Tag.objects().order_by('-popularity')[:10])
Пример #6
0
def index():
    page = get_page()
    pastes = Paste.objects(is_private=False).order_by('-updated_at')
    pastes, summary = paginate(pastes, page)

    return {'pastes': pastes,
            'page_summary': summary,
            'tags': Tag.objects().order_by('-popularity')[:10]}
Пример #7
0
def view_likes(username):
    user = User.objects.get_or_404(username=username)

    page = get_page()
    likes = Paste.objects(id__in=[str(i.id) for i in user.likes]).order_by('-updated_at')
    pagination = likes.paginate(page, per_page=20)

    return render_template('users/likes.html',
                           user=user,
                           pagination=pagination)
Пример #8
0
def index():
    page = get_page()
    pastes = Paste.objects(is_private=False).order_by('-updated_at')
    pastes, summary = paginate(pastes, page)

    return {
        'pastes': pastes,
        'page_summary': summary,
        'tags': Tag.objects().order_by('-popularity')[:10]
    }
Пример #9
0
def index():
    page = get_page()
    pagination = Paste.objects(is_private=False).order_by('-updated_at').paginate(page=page, per_page=20)

    print datetime.today()

    return render_template('index.html',
                           pagination=pagination,
                           hot_pastes=Paste.objects(is_private=False).order_by('-views')[:10],
                           pastes_count=Paste.objects().count(),
                           comments_count=Comment.objects().count(),
                           users_count=User.objects().count(),
                           syntax_count=Syntax.objects().count(),
                           bookmarks_count=Bookmark.objects().count(),
                           users_increased=User.objects(created_at__gt=date.today()).count(),
                           pastes_increased=Paste.objects(created_at__gt=date.today()).count(),
                           comments_increased=Comment.objects(created_at__gt=date.today()).count(),
                           bookmarks_increased=Bookmark.objects(created_at__gt=date.today()).count(),
                           tags=Tag.objects().order_by('-popularity')[:10])
Пример #10
0
def view(tag_name):
    criteria = {}
    tag = Tag.objects.get_or_404(key=tag_name)
    if request.args.get('filter', None) != 'mine' or not current_user.is_authenticated:
        criteria['is_private'] = False
    syntax = Syntax.objects(key=tag_name).first()
    if syntax:
        criteria['codes__syntax'] = syntax
    else:
        criteria['tags'] = tag
    pastes = Paste.objects(**criteria).order_by('-updated_at')
    page = get_page()

    pagination = pastes.paginate(page, per_page=20)

    return render_template('tags/view.html',
                           tag=tag,
                           hot_pastes=Paste.objects(is_private=False).order_by('-views')[:10],
                           pagination=pagination)
Пример #11
0
def tag_more(tag_name):
    p = int(request.query.p)
    if not p:
        return {}
    return {
        'pastes':
        Paste.objects(
            tags=tag_name,
            is_private=False).order_by('-updated_at')[(p - 1) *
                                                      ITEMS_PER_PAGE:p *
                                                      ITEMS_PER_PAGE]
    }
Пример #12
0
def view(tag_name):
    criteria = {}
    tag = Tag.objects.get_or_404(key=tag_name)
    if request.args.get('filter',
                        None) != 'mine' or not current_user.is_authenticated:
        criteria['is_private'] = False
    syntax = Syntax.objects(key=tag_name).first()
    if syntax:
        criteria['codes__syntax'] = syntax
    else:
        criteria['tags'] = tag
    pastes = Paste.objects(**criteria).order_by('-updated_at')
    page = get_page()

    pagination = pastes.paginate(page, per_page=20)

    return render_template(
        'tags/view.html',
        tag=tag,
        hot_pastes=Paste.objects(is_private=False).order_by('-views')[:10],
        pagination=pagination)
Пример #13
0
def view(tag_name):
    tag = Tag.objects.get_or_404(key=tag_name)
    page = get_page()

    if request.args.get('filter', None) == 'mine' and current_user.is_authenticated:
        pastes = tag.pastes(user=current_user.user).order_by('-updated_at')
    else:
        pastes = tag.pastes(is_private=False).order_by('-updated_at')
    pagination = pastes.paginate(page, per_page=20)

    return render_template('tags/view.html',
                           tag=tag,
                           hot_pastes=Paste.objects(is_private=False).order_by('-views')[:10],
                           pagination=pagination)
Пример #14
0
def view_paste(hash_id):
    paste = Paste.objects.get_or_404(hash_id=hash_id)
    paste.increase_views()

    paste_lists = []
    if current_user.is_authenticated:
        paste_lists = Bookmark.objects(user=current_user.user)

    syntax_list = [code.syntax for code in paste.codes]
    related_pastes = Paste.objects(codes__syntax__in=syntax_list).order_by('-created_at')[:10]

    return render_template('pastes/view.html',
                           paste=paste,
                           related_pastes=related_pastes,
                           paste_lists=paste_lists)
Пример #15
0
def remove_paste():
    if 'paste_hash_id' not in request.form or 'bookmark_id' not in request.form:
        abort(404)

    bookmark = Bookmark.objects(hash_id=request.form['bookmark_id']).get_or_404()
    paste = Paste.objects(hash_id=request.form['paste_hash_id']).get_or_404()
    if paste not in bookmark.pastes:
        return render_template('error.html',
                               title=u'该代码集合已经收藏夹中移除',
                               message=u'该代码集合已经在收藏夹中移除')

    bookmark.pastes.remove(paste)
    bookmark.save()

    return redirect(url_for('bookmark_app.view', hash_id=bookmark.hash_id))
Пример #16
0
def index():
    page = get_page()
    pagination = Paste.objects(
        is_private=False).order_by('-updated_at').paginate(page=page,
                                                           per_page=20)

    print datetime.today()

    return render_template(
        'index.html',
        pagination=pagination,
        hot_pastes=Paste.objects(is_private=False).order_by('-views')[:10],
        pastes_count=Paste.objects().count(),
        comments_count=Comment.objects().count(),
        users_count=User.objects().count(),
        syntax_count=Syntax.objects().count(),
        bookmarks_count=Bookmark.objects().count(),
        users_increased=User.objects(created_at__gt=date.today()).count(),
        pastes_increased=Paste.objects(created_at__gt=date.today()).count(),
        comments_increased=Comment.objects(
            created_at__gt=date.today()).count(),
        bookmarks_increased=Bookmark.objects(
            created_at__gt=date.today()).count(),
        tags=Tag.objects().order_by('-popularity')[:10])
Пример #17
0
def view_paste(hash_id):
    paste = Paste.objects.get_or_404(hash_id=hash_id)
    paste.increase_views()

    paste_lists = []
    if current_user.is_authenticated:
        paste_lists = Bookmark.objects(user=current_user.user)

    syntax_list = [code.syntax for code in paste.codes]
    related_pastes = Paste.objects(
        codes__syntax__in=syntax_list).order_by('-created_at')[:10]

    return render_template('pastes/view.html',
                           paste=paste,
                           related_pastes=related_pastes,
                           paste_lists=paste_lists)
Пример #18
0
def remove_paste():
    if 'paste_hash_id' not in request.form or 'bookmark_id' not in request.form:
        abort(404)

    bookmark = Bookmark.objects(
        hash_id=request.form['bookmark_id']).get_or_404()
    paste = Paste.objects(hash_id=request.form['paste_hash_id']).get_or_404()
    if paste not in bookmark.pastes:
        return render_template('error.html',
                               title=u'该代码集合已经收藏夹中移除',
                               message=u'该代码集合已经在收藏夹中移除')

    bookmark.pastes.remove(paste)
    bookmark.save()

    return redirect(url_for('bookmark_app.view', hash_id=bookmark.hash_id))
Пример #19
0
def add_paste():
    if 'paste_hash_id' not in request.form or 'paste_list_id' not in request.form:
        abort(404)
    paste_list = Bookmark.objects(hash_id=request.form['paste_list_id']).get_or_404()
    paste = Paste.objects(hash_id=request.form['paste_hash_id']).get_or_404()
    if paste in paste_list.pastes:
        return render_template('error.html',
                               title=u'该代码集合已经在收藏夹中',
                               message=u'该代码集合已经在收藏夹中')
    if len(paste_list.pastes) >= 10:
        return render_template('error.html',
                               title=u'一个收藏夹最多只能有10个代码集合',
                               message=u'一个收藏夹最多只能有10个代码集合')
    if paste not in paste_list.pastes:
        paste_list.pastes.append(paste)
        paste_list.save()

    return redirect(url_for('bookmark_app.view_list', hash_id=paste_list.hash_id))
Пример #20
0
def add_paste():
    if 'paste_hash_id' not in request.form or 'bookmark_id' not in request.form:
        abort(404)

    bookmark = Bookmark.objects(
        hash_id=request.form['bookmark_id']).get_or_404()
    paste = Paste.objects(hash_id=request.form['paste_hash_id']).get_or_404()
    if paste.is_private and not bookmark.is_private:
        return render_template('error.html',
                               title=u'公开的收藏夹不能添加私有的代码集合',
                               message=u'公开的收藏夹不能添加私有的代码集合')
    if paste in bookmark.pastes:
        return render_template('error.html',
                               title=u'该代码集合已经在收藏夹中',
                               message=u'该代码集合已经在收藏夹中')
    if len(bookmark.pastes) >= 10:
        return render_template('error.html',
                               title=u'一个收藏夹最多只能有10个代码集合',
                               message=u'一个收藏夹最多只能有10个代码集合')
    if paste not in bookmark.pastes:
        bookmark.pastes.append(paste)
        bookmark.save()

        if bookmark.user != paste.user and not bookmark.is_private:
            content = BOOKMARK.format(
                user_username=current_user.user.username,
                user_url=url_for('user_app.view',
                                 username=current_user.user.username),
                paste_title=paste.title,
                paste_url=url_for('paste_app.view_paste',
                                  hash_id=paste.hash_id),
                bookmark_title=bookmark.title,
                bookmark_url=url_for('bookmark_app.view',
                                     hash_id=bookmark.hash_id))

            message = Message(user=paste.user,
                              who=bookmark.user,
                              content=content)
            message.save()

    return redirect(url_for('bookmark_app.view', hash_id=bookmark.hash_id))
Пример #21
0
def get_pastes_from_search(query_string, p=1):
    def get_string_by_keyword(keyword, query_string):
        string = ''
        result = re.search('\s*%s:([a-zA-Z+-_#]+)\s*' % keyword, query_string)
        if result:
            if len(result.groups()) == 1:
                string = result.groups()[0]
        return string, query_string.replace('%s:%s' % (keyword, string), '')

    tag, query_string = get_string_by_keyword('tag', query_string)
    user, query_string = get_string_by_keyword('user', query_string)
    keyword = query_string.strip()

    criteria = {'title__contains': keyword, 'is_private': False}
    if tag:
        criteria['tags'] = tag
    if user:
        user_object = User.objects(username=user).first()
        if user_object:
            criteria['user'] = user_object

    return keyword, Paste.objects(**criteria).order_by('-updated_at').paginate(p, per_page=2)
Пример #22
0
 def pastes(self):
     return Paste.objects(codes__syntax=self.key)
Пример #23
0
def status():
    return {'pastes_count': Paste.objects().count(),
            'codes_count': Code.objects().count(),
            'users_count': User.objects().count()}
Пример #24
0
def tags():
    return render_template('tags/index.html',
                           hot_pastes=Paste.objects(is_private=False).order_by('-views')[:10],
                           tags=Tag.objects().order_by('-popularity'))
Пример #25
0
def status():
    return {'pastes_count': Paste.objects().count(),
            'codes_count': Code.objects().count(),
            'users_count': User.objects().count()}
Пример #26
0
def tag_more(tag_name):
    p = int(request.query.p)
    if not p:
        return {}
    return {'pastes': Paste.objects(tags=tag_name, is_private=False).order_by('-updated_at')[(p - 1) * ITEMS_PER_PAGE:p * ITEMS_PER_PAGE]}
Пример #27
0
def status():
    return jsonify(version=current_app.config['VERSION'],
                   pastes=Paste.objects().count(),
                   bookmarks=Bookmark.objects().count(),
                   tags=Tag.objects().count(),
                   users=User.objects().count())
Пример #28
0
def status():
    return jsonify(version=current_app.config['VERSION'],
                   pastes=Paste.objects().count(),
                   bookmarks=Bookmark.objects().count(),
                   tags=Tag.objects().count(),
                   users=User.objects().count())
Пример #29
0
def tags():
    return render_template(
        'tags/index.html',
        hot_pastes=Paste.objects(is_private=False).order_by('-views')[:10],
        tags=Tag.objects().order_by('-popularity'))
Пример #30
0
 def pastes(self):
     return Paste.objects(tags=self)