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)
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)
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)
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)
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)
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)
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)