def create_page_content(request, library_code, page_id, managed_libraries=[]): library = org_utils.get_library(library_code, managed_libraries) if not library: return HttpResponseForbidden(u'Вы должны быть сотрудником этой организации') page = get_object_or_404(Page, id=page_id) if request.method == 'POST': content_form = ContentForm(request.POST, prefix='content_form') if content_form.is_valid(): content = content_form.save(commit=False) content.page = page content.save() save = request.POST.get('save', u'save_edit') if save == u'save': return redirect('participant_pages:administration:edit_page', library_code=library_code, id=page_id) else: return redirect('participant_pages:administration:edit_page_content', library_code=library_code, page_id=page_id, lang=content.lang) else: content_form = ContentForm(prefix='content_form') return render(request, 'participant_pages/administration/create_page_content.html', { 'library': library, 'page': page, 'content_form': content_form, 'content_type': 'participant_pages', 'content_id': str(library.id) + '_' + page.url_path.replace('/', '_') })
def add_news(): form = ContentForm() if form.validate_on_submit(): db_sess = db_session.create_session() all_news = db_sess.query(News).all() le = len(all_news) news_item = News() news_item.title = form.title.data news_item.content = form.content.data news_item.created_date = datetime.now().date() news_item.picture = '' current_user.news.append(news_item) img = Image.open(form.picture.data) img.save(f'static/img/news/back_news_item{le}.jpg') news_item.picture = f'back_news_item{le}.jpg' db_sess.merge(current_user) db_sess.commit() db_sess.close() return redirect('/') return render_template('content.html', title='Новая новость - Sputnik', form=form)
def add_art(): form = ContentForm() if form.validate_on_submit(): db_sess = db_session.create_session() all_art = db_sess.query(Arts).all() le = len(all_art) art = Arts() art.title = form.title.data art.content = form.content.data art.picture = '' art.created_date = datetime.now().date() current_user.arts.append(art) img = Image.open(form.picture.data) img.save(f'static/img/articles/back_art{le}.jpg') art.picture = f'back_art{le}.jpg' db_sess.merge(current_user) db_sess.commit() db_sess.close() return redirect('/') return render_template('content.html', title='Новая статья - Sputnik', form=form)
def create(): """ Creating new post """ form=ContentForm() if form.validate_on_submit(): new_content = Content(title=form.title.data, detail=form.detail.data,username=current_user.username,time=datetime.datetime.now()) db.session.add(new_content) db.session.commit() return redirect(url_for('index')) return render_template('create.html', form=form)
def submit_content(): content_form = ContentForm() if content_form.validate_on_submit() and content_form.content: NewText = PasteText(0, content_form.content.data, 1) db.session.add(NewText) db.session.flush() NewPostID = NewText.id db.session.commit() flash("Paste posted! Paste code: " + str(NewPostID)) return redirect((url_for("show_paste", pid=NewPostID))) else: flash(content_form.errors) return redirect(url_for("show_index"))
def show_index(): content_form = ContentForm() # code_form = CodeForm() if request.method == "GET": return render_template("index.html", content_form=content_form) else: if content_form.validate_on_submit() and content_form.content: NewText = PasteText(0, content_form.content.data, 1) db.session.add(NewText) db.session.flush() NewPostID = NewText.id db.session.commit() flash("Paste posted! Paste code: " + str(NewPostID)) return redirect((url_for("show_paste", pid=NewPostID))) else: flash(content_form.errors) return redirect(url_for("show_index"))
def edit(slug): """ Editing the clicked post from the index page """ data = Content.query.filter_by(id=slug).first() form=ContentForm() #I found best way to re-populate is to partion request btw GET and POST if request.method == 'GET': form.title.data= data.title form.detail.data= data.detail return render_template('edit.html', form=form,data=data) elif request.method == 'POST': if form.validate_on_submit(): data.title=form.title.data data.detail=form.detail.data db.session.commit() return redirect(url_for('index'))
def review(question_id): form = ContentForm() user_id = session.get('user.id') question_model = Question.query.filter(Question.id == question_id).first() answer = Answer.query.filter(Answer.question_id == question_id).all() return render_template('review.html', question=question_model, answer=answer, form=form)
def add_answer(): # content = request.form.get('answer_content') question_id = request.form.get('question_id') form = ContentForm() content = form.body.data answer = Answer(content=content) answer.author = g.user question = Question.query.filter(Question.id == question_id).first() answer.question = question db.session.add(answer) db.session.commit() return redirect(url_for('review', question_id=question_id, form=form))
def addcontentview(request,classslug): resp = requireclassperm(request,classslug) if resp: return resp classobj = get_object_or_404(Class, slug=classslug) school = classobj.school if request.method == 'POST': # If the form has been submitted... form = ContentForm(request.POST,request.FILES) # A form bound to the POST data if form.is_valid(): # All validation rules pass newcontent = form.save(commit=False) newcontent.classowner = classobj newcontent.schoolowner = school newcontent.userowner = request.user.ccuser newcontent.save() form.save_m2m() messages.success(request, "Content added") return HttpResponseRedirect(reverse('classcast:classview', args=(classobj.slug,))) # Redirect after POST else: messages.error(request, "Upload failed. See below.") else: form = ContentForm() # An unbound form return render(request, 'classcast/addcontent.html',{"school":school,"class":classobj,"form":form})
def edit_page_content(request, library_code, page_id, lang, managed_libraries=[]): library = org_utils.get_library(library_code, managed_libraries) if not library: return HttpResponseForbidden(u'Вы должны быть сотрудником этой организации') lang_form = LanguageForm({'lang': lang}) if not lang_form.is_valid(): return HttpResponse(_(u'Language is not registered in system.') + _(u" Language code: ") + lang) page = get_object_or_404(Page, id=page_id) try: content = Content.objects.get(page=page_id, lang=lang) except Content.DoesNotExist: content = Content(page=page, lang=lang) ContentForm = get_content_form(('page', 'lang')) if request.method == 'POST': content_form = ContentForm(request.POST, prefix='content_form', instance=content) if content_form.is_valid(): content = content_form.save(commit=False) content.page = page content.save() save = request.POST.get('save', u'save_edit') if save == u'save': return redirect('participant_pages:administration:edit_page', library_code=library_code, id=page_id) else: content_form = ContentForm(prefix='content_form', instance=content) return render(request, 'participant_pages/administration/edit_page_content.html', { 'library': library, 'page': page, 'content': content, 'content_form': content_form, 'content_type': 'participant_pages', 'content_id': str(library.id) + '_' + page.url_path.replace('/', '_') })
def edit_page_content(request, page_id, lang): lang_form = LanguageForm({'lang': lang}) if not lang_form.is_valid(): return HttpResponse(_(u'Language is not registered in system.') + _(u" Language code: ") + lang) page = get_object_or_404(Page, id=page_id) try: content = Content.objects.get(page=page_id, lang=lang) except Content.DoesNotExist: content = Content(page=page, lang=lang) ContentForm = get_content_form(('page', 'lang')) if request.method == 'POST': content_form = ContentForm(request.POST, prefix='content_form', instance=content) if content_form.is_valid(): content = content_form.save(commit=False) content.page = page content.save() save = request.POST.get('save', u'save_edit') if save == u'save': return redirect('professionals_pages:administration:edit_page', id=page_id) else: content_form = ContentForm(prefix='content_form', instance=content) return render(request, 'professionals_pages/administration/edit_page_content.html', { 'page': page, 'content': content, 'content_form': content_form, })
def add_content(): form = ContentForm() if request.method == 'POST': if form.validate_on_submit(): # get form data title = form.title.data content = form.content.data category = form.category.data # add to db info = { 'title': title, 'content': content, 'category': category, 'added': datetime.utcnow(), 'user': session['username'], } db.content.insert_one(info) # return to add content page message = '"' + title + '" added successfully' log_message = "%s added " + title app.logger.info(log_message, session["username"]) flash(message) return redirect("/add_content") return render_template('add_content.html', form=form)
def post(): form = ContentForm() # if form.validate_on_submit(): # text = form.pagedown.data # print(text) # # do something interesting with the Markdown text # # allowed_tags=['a','ul','strong','p','h1','h2','h3'] # # html_body=markdown(value,output_format='html') # # html_body=bleach.clean(html_body,tags=allowed_tags,strip=True) # # html_body=bleach.linkify(html_body) # # pagedown = html_body # print(pagedown) return render_template('markdown.html', form=form)
def edit_news(id): form = ContentForm() if request.method == "GET": db_sess = db_session.create_session() news_item = db_sess.query(News).filter( News.id == id, News.user == current_user).first() if news_item: form.title.data = news_item.title form.content.data = news_item.content form.picture.data = news_item.picture else: abort(404) db_sess.close() if form.validate_on_submit(): db_sess = db_session.create_session() news_item = db_sess.query(News).filter( News.id == id, News.user == current_user).first() if news_item: news_item.title = form.title.data news_item.content = form.content.data news_item.created_date = datetime.now().date() if form.picture.data == str: db_sess.commit() else: img = Image.open(form.picture.data) os.remove(f'static/img/news/{news_item.picture}') img.save(f'static/img/news/{news_item.picture}') news_item.picture = f'{news_item.picture}' db_sess.commit() return redirect('/') else: abort(404) db_sess.close() return render_template('content.html', title='Редактирование новости - Sputnik', form=form)
def edit_art(id): form = ContentForm() if request.method == "GET": db_sess = db_session.create_session() art = db_sess.query(Arts).filter(Arts.id == id, Arts.user == current_user).first() if art: form.title.data = art.title form.content.data = art.content form.picture.data = art.picture else: abort(404) db_sess.close() if form.validate_on_submit(): db_sess = db_session.create_session() art = db_sess.query(Arts).filter(Arts.id == id, Arts.user == current_user).first() if art: art.title = form.title.data art.content = form.content.data art.created_date = datetime.now().date() if form.picture.data == str: db_sess.commit() else: img = Image.open(form.picture.data) os.remove(f'static/img/articles/{art.picture}') img.save(f'static/img/articles/{art.picture}') art.picture = f'{art.picture}' db_sess.commit() return redirect('/') else: abort(404) db_sess.close() return render_template('content.html', title='Редактирование статьи - Sputnik', form=form)
def edit_page_content(request, library_code, page_id, lang, managed_libraries=[]): library = org_utils.get_library(library_code, managed_libraries) if not library: return HttpResponseForbidden( u'Вы должны быть сотрудником этой организации') lang_form = LanguageForm({'lang': lang}) if not lang_form.is_valid(): return HttpResponse( _(u'Language is not registered in system.') + _(u" Language code: ") + lang) page = get_object_or_404(Page, id=page_id) try: content = Content.objects.get(page=page_id, lang=lang) except Content.DoesNotExist: content = Content(page=page, lang=lang) ContentForm = get_content_form(('page', 'lang')) if request.method == 'POST': content_form = ContentForm(request.POST, prefix='content_form', instance=content) if content_form.is_valid(): content = content_form.save(commit=False) content.page = page content.save() save = request.POST.get('save', u'save_edit') if save == u'save': return redirect('participant_pages:administration:edit_page', library_code=library_code, id=page_id) else: content_form = ContentForm(prefix='content_form', instance=content) return render( request, 'participant_pages/administration/edit_page_content.html', { 'library': library, 'page': page, 'content': content, 'content_form': content_form, 'content_type': 'participant_pages', 'content_id': str(library.id) + '_' + page.url_path.replace('/', '_') })
def edit(request, pk): content = Content.objects.get(pk=pk) print content.customer_name if request.method == 'POST': print 'okokok!' form = ContentForm(request.POST, instance=content) if form.is_valid(): form.save() return redirect('index') else: print 'errors!!!!' new_content = ContentForm(instance=content) return render(request, 'jihua/edit.html', locals())
def create(request): if request.method == 'GET': new = ContentForm() if request.method == 'POST': new = ContentForm(request.POST) print new.is_valid() if new.is_valid( ): #如果is_valid()不对可以打印new.errors来看哪块有问题,可以搜form组件is_valid校验机制 date = new.cleaned_data['date'] customer_name = new.cleaned_data['customer_name'] dfe_name = new.cleaned_data['dfe_name'] question = new.cleaned_data['question'] solution = new.cleaned_data['solution'] solution_or_not = new.cleaned_data['solution_or_not'] update_or_not = new.cleaned_data['update_or_not'] change_or_not = new.cleaned_data['change_or_not'] share_or_not = new.cleaned_data['share_or_not'] # author = request.user.username print request.user # aa = {} # aa.update(new.cleaned_data) # aa['author'] = author aa = Content() aa.date = date aa.customer_name = customer_name aa.dfe_name = dfe_name aa.question = question aa.solution = solution aa.solution_or_not = solution_or_not aa.update_or_not = update_or_not aa.change_or_not = change_or_not aa.share_or_not = share_or_not aa.author = request.user aa.save() return redirect('index') else: print 'ccccc' for i in new.errors: print i return render(request, 'jihua/input.html', locals())
def create_page_content(request, page_id): page = get_object_or_404(Page, id=page_id) if request.method == 'POST': content_form = ContentForm(request.POST, prefix='content_form') if content_form.is_valid(): content = content_form.save(commit=False) content.page = page content.save() save = request.POST.get('save', u'save_edit') if save == u'save': return redirect('professionals_pages:administration:edit_page', id=page_id) else: return redirect('professionals_pages:administration:edit_page_content', page_id=page_id, lang=content.lang) else: content_form = ContentForm(prefix='content_form') return render(request, 'professionals_pages/administration/create_page_content.html', { 'page': page, 'content_form': content_form, })
def create_page_content(request, library_code, page_id, managed_libraries=[]): library = org_utils.get_library(library_code, managed_libraries) if not library: return HttpResponseForbidden( u'Вы должны быть сотрудником этой организации') page = get_object_or_404(Page, id=page_id) if request.method == 'POST': content_form = ContentForm(request.POST, prefix='content_form') if content_form.is_valid(): content = content_form.save(commit=False) content.page = page content.save() save = request.POST.get('save', u'save_edit') if save == u'save': return redirect('participant_pages:administration:edit_page', library_code=library_code, id=page_id) else: return redirect( 'participant_pages:administration:edit_page_content', library_code=library_code, page_id=page_id, lang=content.lang) else: content_form = ContentForm(prefix='content_form') return render( request, 'participant_pages/administration/create_page_content.html', { 'library': library, 'page': page, 'content_form': content_form, 'content_type': 'participant_pages', 'content_id': str(library.id) + '_' + page.url_path.replace('/', '_') })
def post(self): form = ContentForm(self.request.POST) if form.is_valid(): form.save() self.redirect(self.uri_for('admin_content')+'?success=1') return self.render_response(self.template, locals())
def show_paste_list(): form = ContentForm() pasteList = PasteText.query.all() return render_template("manage.html", pastelists=pasteList, form=form)
def show(): form_pass = ChangePassWord() user = User.query.filter_by(username=current_user.username).first() if form_pass.validate_on_submit(): if not user.is_correct_password(form_pass.lastpass.data): flash('初始密码错误') return redirect('/accounts/settings') else: if form_pass.confirpass.data != form_pass.newpass.data: flash('两次密码不一致') return redirect('/accounts/settings') else: user.password = form_pass.newpass.data db.session.commit() flash('修改成功') return redirect('/accounts/settings') username = current_user.username user_content = Content.query.filter_by(user_name=username).order_by(Content.pub_date.desc()).first() form = ContentForm() if form.validate_on_submit(): if user_content == None: new_content = Content( body = form.contentbox.data, user = current_user ) db.session.add(new_content) user.nickname = form.nickname.data db.session.commit() return redirect('accounts/settings') else: user.nickname = form.nickname.data db.session.commit() user_content.body = form.contentbox.data db.session.commit() return redirect('/accounts/settings') form_media = MediaForm() user_media = Media.query.filter_by(user_name=username).first() if form_media.validate_on_submit(): if user_media == None: media_setting = Media( user = current_user, weibo = form_media.weibo.data, weixin = form_media.weixin.data, douban = form_media.douban.data, twitter = form_media.twitter.data, github = form_media.github.data, zhihu = form_media.zhihu.data, ) db.session.add(media_setting) db.session.commit() if form_media.weibo.data != '' and user_media != None: user_media.weibo = form_media.weibo.data db.session.commit() if form_media.weixin.data != '' and user_media != None: user_media.weixin = form_media.weixin.data db.session.commit() if form_media.douban.data != '' and user_media != None: user_media.douban = form_media.douban.data db.session.commit() if form_media.twitter.data != '' and user_media != None: user_media.twitter = form_media.twitter.data db.session.commit() if form_media.github.data != '' and user_media != None: user_media.github = form_media.github.data db.session.commit() if form_media.zhihu.data != '' and user_media != None: user_media.zhihu = form_media.zhihu.data db.session.commit() return redirect('/accounts/settings') useravatar = User.query.filter_by(username=username).first() if request.method == 'POST': global file file = request.files['file'] useravatar.avatar = secure_filename(file.filename) db.session.commit() if file and allowed_file(file.filename): global filename filename = secure_filename(file.filename) file.save(os.path.join(UPLOAD_FOLDER, filename)) avatar_url = url_for('uploaded_file', filename=filename) return redirect('accounts/settings') return render_template('/accounts/settings.html', form=form, user_content=user_content, form_media=form_media, user_media=user_media, file=file, useravatar=useravatar, form_pass =form_pass, user = user )
def show(): form_pass = ChangePassWord() user = User.query.filter_by(username=current_user.username).first() if form_pass.validate_on_submit(): if not user.is_correct_password(form_pass.lastpass.data): flash('初始密码错误') return redirect('/accounts/settings') else: if form_pass.confirpass.data != form_pass.newpass.data: flash('两次密码不一致') return redirect('/accounts/settings') else: user.password = form_pass.newpass.data db.session.commit() flash('修改成功') return redirect('/accounts/settings') username = current_user.username user_content = Content.query.filter_by(user_name=username).order_by( Content.pub_date.desc()).first() form = ContentForm() if form.validate_on_submit(): if user_content == None: new_content = Content(body=form.contentbox.data, user=current_user) db.session.add(new_content) user.nickname = form.nickname.data db.session.commit() return redirect('accounts/settings') else: user.nickname = form.nickname.data db.session.commit() user_content.body = form.contentbox.data db.session.commit() return redirect('/accounts/settings') form_media = MediaForm() user_media = Media.query.filter_by(user_name=username).first() if form_media.validate_on_submit(): if user_media == None: media_setting = Media( user=current_user, weibo=form_media.weibo.data, weixin=form_media.weixin.data, douban=form_media.douban.data, twitter=form_media.twitter.data, github=form_media.github.data, zhihu=form_media.zhihu.data, ) db.session.add(media_setting) db.session.commit() if form_media.weibo.data != '' and user_media != None: user_media.weibo = form_media.weibo.data db.session.commit() if form_media.weixin.data != '' and user_media != None: user_media.weixin = form_media.weixin.data db.session.commit() if form_media.douban.data != '' and user_media != None: user_media.douban = form_media.douban.data db.session.commit() if form_media.twitter.data != '' and user_media != None: user_media.twitter = form_media.twitter.data db.session.commit() if form_media.github.data != '' and user_media != None: user_media.github = form_media.github.data db.session.commit() if form_media.zhihu.data != '' and user_media != None: user_media.zhihu = form_media.zhihu.data db.session.commit() return redirect('/accounts/settings') useravatar = User.query.filter_by(username=username).first() if request.method == 'POST': global file file = request.files['file'] useravatar.avatar = secure_filename(file.filename) db.session.commit() if file and allowed_file(file.filename): global filename filename = secure_filename(file.filename) file.save(os.path.join(UPLOAD_FOLDER, filename)) avatar_url = url_for('uploaded_file', filename=filename) return redirect('accounts/settings') return render_template('/accounts/settings.html', form=form, user_content=user_content, form_media=form_media, user_media=user_media, file=file, useravatar=useravatar, form_pass=form_pass, user=user)