Пример #1
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
Пример #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}
Пример #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))
Пример #4
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