def create_topic(): """Docstring.""" if only_check(): tform = TopicForm() tform.validate_on_submit() # to get error messages to the browser if request.method == 'POST': if tform.validate() is False: flash('All fields are required.', 'critical') return check_and_render('topics_create.html', form=tform) else: if validate_topic(tform.topic_name.data) is False: create_topic_entry(tform.topic_name.data, tform.partition_count.data, tform.replication_factor.data) flash('Added Topic: ' + tform.topic_name.data) return redirect(url_for('topics')) else: flash('This topic name exists already.', 'critical') return check_and_render('topics_create.html', form=tform) elif request.method == 'GET': return check_and_render('topics_create.html', form=tform) else: return check_and_render('index.html')
def new_post(): form = TopicForm() if form.validate_on_submit(): post = Post(title=form.title.data) db.session.add(post) db.session.commit() return redirect(url_for('index')) return render_template('create_topic.html', form=form, legend='New Topic')
def update_post(post_id): post = Post.query.get_or_404(post_id) form = TopicForm() if form.validate_on_submit(): post.title = form.title.data db.session.commit() return redirect(url_for('post', post_id=post.id)) elif request.method == 'GET': form.title.data = post.title return render_template('create_post.html', form=form)
def topic_create(project_id): project = Project.query.get(project_id) if not current_user.in_team(project.team_id): abort(404) form = TopicForm(current_user, project, request.form.getlist('attachment')) if form.validate_on_submit(): topic = form.saveTopic() if topic is not None: flash(u'保存成功') return redirect(url_for('topic_detail', topic_id=topic.id)) else: flash(u'保存失败,请联系管理员', 'error') return render_template('project/topic_create.html', project=project, form=form)
def new(): form = TopicForm() flash(form.board_id.data) if form.validate_on_submit(): u = User.query.filter_by(id = g.user.id).first() topic = Topic(board_id=form.board_id.data, category=form.category.data, author=u, title=form.title.data, content=form.content.data) db.session.add(topic) db.session.commit() flash("发表成功") return redirect(request.args.get("next") or url_for("index")) form.board_id.data = request.args.get('board_id', '') return render_template('new.html', form = form, title = '新建')
def forum(page=1): # Нужная страница if request.args.get('page'): page = int(request.args.get('page')) # Разбиение на страницы pagination = Pagination(page, TOPIC_PER_PAGE, ForumTopic.query.count()) # Форма для постинга сообщений form_topic = TopicForm() form_message = MessageForm() # Если отправлена форма постинга if request.method == 'POST' and form_topic.validate_on_submit() and form_message.validate_on_submit(): # Данные из формы c применением форматирования data_topic = form_topic.topic.data data_message = message_format(form_message.message.data, True) # Создание темы и обновление счётчиков у пользователя new_topic = ForumTopic(name=data_topic, author_id=current_user.id) current_user.message_count += 1 current_user.topic_count += 1 db.session.add(new_topic) # Коммит в этом месте нужен, чтобы появился ID db.session.commit() # Создание сообщения new_mes = ForumMessage(topic_id=new_topic.id, author_id=current_user.id, text=data_message) db.session.add(new_mes) db.session.commit() return(redirect(url_for('topic', topic_id=str(new_topic.id)))) # Выборка всех тем с счётчиком сообщений для каждой # Подзапрос all_topics_subq = db.session.query( ForumMessage.topic_id, func.count(ForumMessage.id).label('mes_count')).\ group_by(ForumMessage.topic_id).\ subquery() # Основной запрос all_topics = db.session.query( ForumTopic, all_topics_subq.c.mes_count).\ join(all_topics_subq, ForumTopic.id == all_topics_subq.c.topic_id).\ order_by(ForumTopic.time_last.desc()).all() # Вернуть страницу return(render_template('forum.html', user=current_user, all_topics=all_topics, form_topic=form_topic, form_message=form_message, pagination=pagination))
def new_topic(): """ Starts a new thread creating a new Topic and corresponding first Post in the database. """ form = TopicForm(request.form) if form.validate_on_submit(): topic = Topic(subject=form.subject.data) post = Post(message=form.message.data, ip_address=request.remote_addr, name=form.name.data, email=form.email.data) topic.first_post = post post.topic = topic db.session.add(topic) db.session.commit() return redirect(url_for('view_topic', topic_id=topic.id)) return render_template('new_topic.html', form=form)
def mail_write(reply=None): # Форма для нового сообщения form_recepient = RecepientForm() form_subject = TopicForm() form_message = MessageForm() # Если отправлена форма с новым сообщением if request.method == 'POST' and \ form_recepient.validate_on_submit() and \ form_subject.validate_on_submit() and \ form_message.validate_on_submit(): # Данные из формы c применением форматирования data_recipient = form_recepient.recepient.data data_subject = form_subject.topic.data data_message = message_format(form_message.message.data, True) # От кого и кому отправлено сообщение sender_id = current_user.id recipient_id = User.query.filter_by(login=data_recipient).first() if recipient_id: recipient_id = recipient_id.id else: return(render_template('info.html', user=current_user, text='No such user')) # Создание сообщений в отправленных у посылающего и # во входящих у того, кому адресовано письмо new_message = Mailbox(sender_id=current_user.id, owner_id=sender_id, directory=1, recipient_id=recipient_id, subject=data_subject, text=data_message, read=True) db.session.add(new_message) new_message = Mailbox(sender_id=current_user.id, owner_id=recipient_id, directory=0, recipient_id=recipient_id, subject=data_subject, text=data_message) db.session.add(new_message) db.session.commit() # Перейти в отправленные return(redirect(url_for('mailbox', box='sent'))) # Заполнение полей надо делать после проверки отправки, иначе # form_message.message.data останется =quote и не перезапишется # теми данными, которые ввёл пользователь # Если это ответное сообщение reply = request.args.get('reply') recepient = '' subject = '' if reply: previous_message = Mailbox.query.get(reply) recepient = previous_message.sender.login subject = 'Re: ' + previous_message.subject text = message_format(previous_message.text, False) quote = '' for line in text.split('\n'): if line[:3] == '>> ': quote += '>> ' + line + '\n' else: for i in range(0, len(line), 50): quote += '>> ' + line[i:i+50] + '\n' quote += '\n' form_message.message.data = quote # Все пользователей для выбора в качестве адреса all_users = User.query.all() return(render_template('mail_write.html', user=current_user, recepient=recepient, subject=subject, form_recepient=form_recepient, form_subject=form_subject, form_message=form_message, all_users=all_users))