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}
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}
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
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
def status(): return {'pastes_count': Paste.objects().count(), 'codes_count': Code.objects().count(), 'users_count': User.objects().count()}