Beispiel #1
0
def edit_post(hash_id):
    paste = get_paste(hash_id)
    if not paste.is_user_owned(request.user):
        abort(404)
    form = PasteForm(request.POST)
    if form.validate():
        user = login.get_user()
        paste.title = form.title.data
        paste.is_private = form.is_private.data
        tags = []
        codes = [code for code in paste.codes]
        paste.codes = []
        for code in codes:
            code.delete()
        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, 'paste': paste, 'token': request.csrf_token}
Beispiel #2
0
def create_paste():
    if request.method == 'GET':
        # missing csrf
        form = PasteForm(
            data={'codes': [{
                'title': '',
                'content': '',
                'syntax': 'text'
            }]})
        return render_template('pastes/create.html', form=form)
    else:
        form = PasteForm.from_json(data=request.json)
        if form.validate():
            user = current_user.user
            paste = save_paste_and_codes(form)
            if not paste.is_private:
                followers = User.objects(followings=user)
                content = NEW_PASTE.format(
                    user_username=user.username,
                    user_url=url_for('user_app.view', username=user.username),
                    paste_title=paste.title,
                    paste_url=url_for('paste_app.view_paste',
                                      hash_id=paste.hash_id))
                for follower in followers:
                    message = Message(user=follower, who=user, content=content)
                    message.save()
            return jsonify(success=True, hash_id=paste.hash_id)
        else:
            errors = form.errors
            errors['codes'] = [code.errors for code in form.codes]
            logger.info('Failed saving paste for reason: %s', errors)
            return jsonify(success=False, errors=errors)
Beispiel #3
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}
Beispiel #4
0
def create_paste():
    if request.method == 'GET':
        # missing csrf
        form = PasteForm(data={'codes': [{'title': '', 'content': ''}]})
        return render_template('pastes/create.html', form=form)
    else:
        form = PasteForm(request.form)
        if form.validate():
            user = current_user.user
            paste = save_paste_and_codes(form)
            if not paste.is_private:
                followers = User.objects(followings=user)
                content = NEW_PASTE.format(user_username=user.username,
                                           user_url=url_for('user_app.view', username=user.username),
                                           paste_title=paste.title,
                                           paste_url=url_for('paste_app.view_paste', hash_id=paste.hash_id))
                for follower in followers:
                    message = Message(user=follower,
                                      who=user,
                                      content=content)
                    message.save()
            return jsonify(success=True, hash_id=paste.hash_id)
        else:
            errors = form.errors
            errors['codes'] = [code.errors for code in form.codes]
            logger.info('Failed saving paste for reason: %s', errors)
            return jsonify(success=False, errors=errors)
Beispiel #5
0
def edit_paste(hash_id):
    paste = Paste.objects.get_or_404(hash_id=hash_id)

    if not paste.is_user_owned(current_user.user):
        abort(404)

    if request.method == 'GET':
        tags = [tag.name for tag in paste.tags]
        form = PasteForm(title=paste.title,
                         is_private=paste.is_private,
                         tags=' '.join(tags))

        form.codes.pop_entry()
        for code in paste.codes:
            form.codes.append_entry({
                'title': code.title,
                'content': code.content,
                'syntax': code.syntax.key
            })

        return render_template('pastes/edit.html', paste=paste, form=form)
    else:
        form = PasteForm.from_json(request.json)
        if form.validate():
            paste = save_paste_and_codes(form, paste=paste)
            flash(u"代码集合已成功修改")
            return jsonify(success=True, hash_id=paste.hash_id)
        else:
            errors = form.errors
            errors['codes'] = [code.errors for code in form.codes]
            logger.info('Failed saving paste for reason: %s', errors)
            return jsonify(success=False, errors=errors)
Beispiel #6
0
def edit_paste(hash_id):
    paste = Paste.objects.get_or_404(hash_id=hash_id)

    if not paste.is_user_owned(current_user.user):
        abort(404)

    if request.method == 'GET':
        tags = []
        syntaxes = [code.syntax.name for code in paste.codes]
        for tag in paste.tags:
            if tag.name not in syntaxes:
                tags.append(tag.name)
        data = {'hash_id': paste.hash_id,
                'title': paste.title,
                'is_private': paste.is_private,
                'tags': ' '.join(tags),
                'codes': [{'title': code.title, 'content': code.content, 'syntax': code.syntax.key} for code in paste.codes]}
        return render_template('pastes/edit.html',
                               paste=paste,
                               data=data)
    else:
        form = PasteForm(request.form)
        if form.validate():
            paste = save_paste_and_codes(form, paste=paste)
            return jsonify(success=True, hash_id=paste.hash_id)
        else:
            errors = form.errors
            errors['codes'] = [code.errors for code in form.codes]
            logger.info('Failed saving paste for reason: %s', errors)
            return jsonify(success=False, errors=errors)
Beispiel #7
0
def edit_paste(hash_id):
    paste = Paste.objects.get_or_404(hash_id=hash_id)

    if not paste.is_user_owned(current_user.user):
        abort(404)

    if request.method == 'GET':
        tags = [tag.name for tag in paste.tags]
        form = PasteForm(title=paste.title,
                         is_private=paste.is_private,
                         tags=' '.join(tags))

        form.codes.pop_entry()
        for code in paste.codes:
            form.codes.append_entry({'title': code.title,
                                     'content': code.content,
                                     'syntax': code.syntax.key})

        return render_template('pastes/edit.html',
                               paste=paste,
                               form=form)
    else:
        form = PasteForm.from_json(request.json)
        if form.validate():
            paste = save_paste_and_codes(form, paste=paste)
            flash(u"代码集合已成功修改")
            return jsonify(success=True, hash_id=paste.hash_id)
        else:
            errors = form.errors
            errors['codes'] = [code.errors for code in form.codes]
            logger.info('Failed saving paste for reason: %s', errors)
            return jsonify(success=False, errors=errors)
Beispiel #8
0
def edit_post(hash_id):
    paste = get_paste(hash_id)
    if not paste.is_user_owned(request.user):
        abort(404)
    form = PasteForm(request.POST)
    if form.validate():
        user = login.get_user()
        paste.title = form.title.data
        paste.is_private = form.is_private.data
        tags = []
        codes = [code for code in paste.codes]
        paste.codes = []
        for code in codes:
            code.delete()
        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, 'paste': paste, 'token': request.csrf_token}
Beispiel #9
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}
Beispiel #10
0
def create_paste():
    if request.method == 'GET':
        # missing csrf
        form = PasteForm(data={'codes': [{'title': '', 'content': ''}]})
        return render_template('pastes/create.html', form=form)
    else:
        form = PasteForm(request.form)
        if form.validate():
            user = current_user.user
            paste = save_paste_and_codes(form)
            followers = User.objects(followers=user)
            for follower in followers:
                create_message(follower, paste)
            return jsonify(success=True, hash_id=paste.hash_id)
        else:
            errors = form.errors
            errors['codes'] = [code.errors for code in form.codes]
            logger.info('Failed saving paste for reason: %s', errors)
            return jsonify(success=False, errors=errors)
Beispiel #11
0
def create_paste():
    if request.method == 'GET':
        # missing csrf
        form = PasteForm(data={'codes': [{'title': '', 'content': ''}]})
        return render_template('pastes/create.html', form=form)
    else:
        form = PasteForm(request.form)
        if form.validate():
            user = current_user.user
            paste = save_paste_and_codes(form)
            followers = User.objects(followers=user)
            for follower in followers:
                create_message(follower, paste)
            return jsonify(success=True, hash_id=paste.hash_id)
        else:
            errors = form.errors
            errors['codes'] = [code.errors for code in form.codes]
            logger.info('Failed saving paste for reason: %s', errors)
            return jsonify(success=False, errors=errors)
Beispiel #12
0
def edit_paste(hash_id):
    paste = Paste.objects.get_or_404(hash_id=hash_id)

    if not paste.is_user_owned(current_user.user):
        abort(404)

    if request.method == 'GET':
        tags = []
        syntaxes = [code.syntax.name for code in paste.codes]
        for tag in paste.tags:
            if tag.name not in syntaxes:
                tags.append(tag.name)
        data = {
            'hash_id':
            paste.hash_id,
            'title':
            paste.title,
            'is_private':
            paste.is_private,
            'tags':
            ' '.join(tags),
            'codes': [{
                'title': code.title,
                'content': code.content,
                'syntax': code.syntax.key
            } for code in paste.codes]
        }
        return render_template('pastes/edit.html', paste=paste, data=data)
    else:
        form = PasteForm(request.form)
        if form.validate():
            paste = save_paste_and_codes(form, paste=paste)
            return jsonify(success=True, hash_id=paste.hash_id)
        else:
            errors = form.errors
            errors['codes'] = [code.errors for code in form.codes]
            logger.info('Failed saving paste for reason: %s', errors)
            return jsonify(success=False, errors=errors)
Beispiel #13
0
def edit_get(hash_id):
    paste = get_paste(hash_id)
    if not paste.is_user_owned(request.user):
        abort(404)
    data = {
        'title':
        paste.title,
        'is_private':
        paste.is_private,
        'codes': [{
            'title': code.title,
            'content': code.content,
            'tag': code.tag
        } for code in paste.codes]
    }
    form = PasteForm(data=data)
    return {'form': form, 'paste': paste, 'token': request.csrf_token}
Beispiel #14
0
def create_get():
    form = PasteForm(data={'codes': [{'title': '', 'content': ''}]})
    return {'form': form, 'token': request.csrf_token}