Example #1
0
def create_post():
    form = PasteForm(request.POST)
    if form.validate():
        user = login.get_user()
        paste = Paste(title=form.title.data,
                      user=user,
                      is_private=form.is_private.data)
        tags = []
        for i, c in enumerate(form.codes):
            tag_name = c.tag.data.lower()
            if not c.title.data:
                c.title.data = '代码片段%s' % (i + 1)
            code = Code(title=c.title.data,
                        content=c.content.data,
                        tag=tag_name,
                        user=user)
            code.save()
            tags.append(tag_name)
            tag = Tag.objects(name=tag_name).first()
            if tag:
                tag.popularity += 1
            else:
                tag = Tag(name=tag_name)
            tag.save()
            paste.codes.append(code)
        paste.tags = list(set(tags))
        paste.save()
        return redirect('/paste/%s' % paste.hash_id)
    return {'form': form, 'token': request.csrf_token}
Example #2
0
def create_post():
    form = PasteForm(request.POST)
    if form.validate():
        user = login.get_user()
        paste = Paste(title=form.title.data, user=user, is_private=form.is_private.data)
        tags = []
        for i, c in enumerate(form.codes):
            tag_name = c.tag.data.lower()
            if not c.title.data:
                c.title.data = '代码片段%s' % (i + 1)
            code = Code(title=c.title.data,
                        content=c.content.data,
                        tag=tag_name,
                        user=user)
            code.save()
            tags.append(tag_name)
            tag = Tag.objects(name=tag_name).first()
            if tag:
                tag.popularity += 1
            else:
                tag = Tag(name=tag_name)
            tag.save()
            paste.codes.append(code)
        paste.tags = list(set(tags))
        paste.save()
        return redirect('/paste/%s' % paste.hash_id)
    return {'form': form, 'token': request.csrf_token}
Example #3
0
def fork(hash_id):
    paste = Paste.objects.get_or_404(hash_id=hash_id)
    new_paste = Paste(user=current_user.user,
                      title=paste.title,
                      tags=paste.tags,
                      codes=paste.codes)
    new_paste.save()

    return redirect(url_for('paste_app.view_paste', hash_id=new_paste.hash_id))
Example #4
0
def fork(hash_id):
    paste = Paste.objects.get_or_404(hash_id=hash_id)
    new_paste = Paste(user=current_user.user,
                      title=paste.title,
                      tags=paste.tags,
                      codes=paste.codes)
    new_paste.save()

    return redirect(url_for('paste_app.view_paste', hash_id=new_paste.hash_id))
Example #5
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]
Example #6
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])
Example #7
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])
Example #8
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))
Example #9
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])
Example #10
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]}
Example #11
0
def save_paste_and_codes(form, paste=None):
    if not paste:
        paste = Paste(user=current_user.user)

    paste.title = form.title.data
    paste.is_private = form.is_private.data
    paste.codes = []
    tags = {}

    for tag in form.tags.data.split():
        tags[tag.lower()] = tag.lower()
    for i, c in enumerate(form.codes):
        syntax = Syntax.objects(key=c.syntax.data).get_or_404()
        if not c.title.data:
            c.title.data = '代码片段%s' % (i + 1)
        code = Code(title=c.title.data, content=c.content.data, syntax=syntax)
        tags[syntax.key] = syntax.name
        paste.codes.append(code)
    for key in tags:
        syntax = Syntax.objects(name__iexact=tags[key]).first()
        tag = Tag.objects(key__iexact=key).first()
        if not tag and syntax:
            tag = Tag(key=syntax.key, name=syntax.name)
            tag.save()
        elif not tag and not syntax:
            tag = Tag(key=key, name=tags[key])
        else:
            tag.popularity += 1
        tag.save()
        if tag not in paste.tags:
            paste.tags.append(tag)
    paste.save()
    return paste
Example #12
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])
Example #13
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]
    }
Example #14
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)
Example #15
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)
Example #16
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)
Example #17
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]
    }
Example #18
0
def save_paste_and_codes(form, paste=None):
    if not paste:
        paste = Paste(user=current_user.user)

    paste.title = form.title.data
    paste.is_private = form.is_private.data
    paste.codes = []
    tags = {}

    for tag in form.tags.data.split():
        tags[tag.lower()] = tag.lower()
    for i, c in enumerate(form.codes):
        syntax = Syntax.objects(key=c.syntax.data).get_or_404()
        if not c.title.data:
            c.title.data = '代码片段%s' % (i + 1)
        code = Code(title=c.title.data,
                    content=c.content.data,
                    syntax=syntax)
        tags[syntax.key] = syntax.name
        paste.codes.append(code)
    for key in tags:
        syntax = Syntax.objects(name__iexact=tags[key]).first()
        tag = Tag.objects(key__iexact=key).first()
        if not tag and syntax:
            tag = Tag(key=syntax.key, name=syntax.name)
            tag.save()
        elif not tag and not syntax:
            tag = Tag(key=key, name=tags[key])
        else:
            tag.popularity += 1
        tag.save()
        if tag not in paste.tags:
            paste.tags.append(tag)
    paste.save()
    return paste
Example #19
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)
Example #20
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])
Example #21
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)
Example #22
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))
Example #23
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))
Example #24
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)
Example #25
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))
Example #26
0
def save_paste_and_codes(form, paste=None):
    if not paste:
        paste = Paste(user=current_user.user)

    paste.title = form.title.data
    paste.is_private = form.is_private.data
    paste.codes = []
    paste.tags = []
    tags = []
    if form.tags.data:
        form.tags.data.split(' ')

    syntaxes = []
    for i, c in enumerate(form.codes):
        syntax = Syntax.objects(key=c.syntax.data).get_or_404()
        syntaxes.append(syntax)
        if not c.title.data:
            c.title.data = '代码片段%s' % (i + 1)
        code = Code(title=c.title.data, content=c.content.data, syntax=syntax)
        paste.codes.append(code)

    for syntax in syntaxes:
        tag = Tag.objects(key=syntax.key).first()
        if not tag:
            tag = Tag(key=syntax.key, name=syntax.name)
            tag.save()

    for key in tags:
        tag = Tag.objects(name__iexact=key).first()
        if not tag:
            tag = Tag(key=key.lower(), name=key.lower())
        else:
            tag.popularity += 1
        tag.save()
        if tag not in paste.tags:
            paste.tags.append(tag)

    paste.save()
    return paste
Example #27
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))
Example #28
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)
Example #29
0
def save_paste_and_codes(form, paste=None):
    if not paste:
        paste = Paste(user=current_user.user)

    paste.title = form.title.data
    paste.is_private = form.is_private.data
    paste.codes = []
    paste.tags = []
    tags = []
    if form.tags.data:
        form.tags.data.split(' ')

    syntaxes = []
    for i, c in enumerate(form.codes):
        syntax = Syntax.objects(key=c.syntax.data).get_or_404()
        syntaxes.append(syntax)
        if not c.title.data:
            c.title.data = '代码片段%s' % (i + 1)
        code = Code(title=c.title.data,
                    content=c.content.data,
                    syntax=syntax)
        paste.codes.append(code)

    for syntax in syntaxes:
        tag = Tag.objects(key=syntax.key).first()
        if not tag:
            tag = Tag(key=syntax.key, name=syntax.name)
            tag.save()

    for key in tags:
        tag = Tag.objects(name__iexact=key).first()
        if not tag:
            tag = Tag(key=key.lower(), name=key.lower())
        else:
            tag.popularity += 1
        tag.save()
        if tag not in paste.tags:
            paste.tags.append(tag)

    paste.save()
    return paste
Example #30
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'))
Example #31
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'))
Example #32
0
 def pastes(self):
     return Paste.objects(codes__syntax=self.key)
Example #33
0
def status():
    return {'pastes_count': Paste.objects().count(),
            'codes_count': Code.objects().count(),
            'users_count': User.objects().count()}
Example #34
0
def status():
    return {'pastes_count': Paste.objects().count(),
            'codes_count': Code.objects().count(),
            'users_count': User.objects().count()}
Example #35
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]}
Example #36
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())
Example #37
0
 def pastes(self):
     return Paste.objects(tags=self)
Example #38
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())