def create(): data = request.get_json() form = TopicForm(**data) if form.validate(): form_data = form.data form_data['ip'] = request.remote_addr try: topic = g.user.create_topic(**form_data) alert = dict( type='success', messages=_("Your topic has been created successfully. You will be redirected to it shortly") ) redirect = topic.get_url('view') cache.update_sorted_topics(topic, 'date_created') return jsonify({"data": topic.json_data(), "alert": alert, "redirect": redirect}) except ModelException, me: db_session.rollback() return json_error(type=me.type, messages=me.message) except Exception, e: logging.error(e) db_session.rollback() return json_error_database()
def create(): data = request.get_json() form = TopicForm(**data) if form.validate(): form_data = form.data form_data['ip'] = request.remote_addr try: topic = g.user.create_topic(**form_data) alert = dict( type='success', messages= _("Your topic has been created successfully. You will be redirected to it shortly" )) redirect = topic.get_url('view') cache.update_sorted_topics(topic, 'date_created') return jsonify({ "data": topic.json_data(), "alert": alert, "redirect": redirect }) except ModelException, me: db_session.rollback() return json_error(type=me.type, messages=me.message) except Exception, e: logging.error(e) db_session.rollback() return json_error_database()
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 post(self): node_id = force_int(self.get_argument('node_id', 0), 0) node = Node.get(id=node_id) user = self.current_user form = TopicForm(self.request.arguments) if form.validate(): topic = form.save(user=user) topic.put_notifier() result = {'status': 'success', 'message': '主题创建成功', 'topic_url': topic.url} if self.is_ajax: return self.write(result) self.flash_message(**result) return self.redirect(topic.url) if self.is_ajax: return self.write(form.result) return self.render("topic/create.html", form=form, node=node)
def private_chat(username): form = TopicForm() topics = Topic.query.all() users = User.query.all() messages = PrivateMessage.query.all() chat_partner = User.query.filter_by(email=username).first() if chat_partner is None: return redirect(url_for('chat')) session['room'] = convertToNumber(chat_partner.email) + convertToNumber( session['email']) session['private'] = chat_partner.email if 'email' not in session: return redirect(url_for('signin')) user = User.query.filter_by(email=session['email']).first() if user is None: return redirect(url_for('signin')) else: if request.method == 'POST': if form.validate() == False: return render_template('private.html', form=form, topics=topics, users=users, messages=messages, private=username) else: uid = user.uid newtopic = Topic(form.topicname.data, uid) db.session.add(newtopic) db.session.commit() session['topic'] = newtopic.topicname return redirect('/chat/' + newtopic.topicname) if request.method == 'GET': return render_template('private.html', form=form, topics=topics, users=users, messages=messages, private=username)
def bbs_add_topic(): node_str = urllib.unquote(request.args.get('node', "").strip()) node = None if node_str: node = Node.query.filter(Node.name == node_str).first() if not node: return render_template("node_not_found.html", next=url_for('.bbs_add_topic'), name=node_str) form = TopicForm(request.form) form.node.choices = Section.query.get_all_nodes() if node_str: form.node.data = node.title if request.method == 'POST' and form.validate(): if not node: node = Node.query.filter( Node.title == form.data["node"].strip()).first() if not node: form.node.errors.append(u"节点不存在!") return render_template("add_topic.html", form=form) section = node.section topic = Topic(user=current_user, title=form.data['title'].strip(), content=form.data["content"], tags=form.data["tags"], created=datetime.now(), last_modified=datetime.now(), last_reply_at=datetime.now()) topic.section = section topic.node = node #db.session.add(topic) node.topic_count += 1 #db.session.add(node) section.topic_count += 1 #db.session.add(section) current_user.profile.topics += 1 db.session.add_all([topic, node, section, current_user.profile]) db.session.commit() return redirect(url_for(".bbs_topic", id=topic.id)) if node_str: action_url = url_for(".bbs_add_topic", node=node_str) else: action_url = url_for(".bbs_add_topic") return render_template("add_topic.html", form=form, action_url=action_url)
def post(self, topic_id): topic = Topic.get(id=topic_id) if not topic or (topic.author != self.current_user and not self.current_user.is_admin): return self.redirect_next_url() user = self.current_user form = TopicForm(self.request.arguments) if form.validate(): topic = form.save(user=user, topic=topic) topic.put_notifier() result = {'status': 'success', 'message': '主题修改成功', 'topic_url': topic.url} if self.is_ajax: return self.write(result) self.flash_message(**result) return self.redirect(topic.url) if self.is_ajax: return self.write(form.result) return self.render("topic/create.html", form=form, node=topic.node)
def post(self): if not self.has_permission: return node_id = force_int(self.get_argument('node_id', 0), 0) node = Node.get(id=node_id) user = self.current_user form = TopicForm(self.request.arguments) if form.validate(): topic = form.save(user=user) result = {'status': 'success', 'message': '主题创建成功', 'topic_url': topic.url} if self.is_ajax: return self.write(result) self.flash_message(result) return self.redirect(topic.url) if self.is_ajax: return self.write(form.result) return self.render("topic/create.html", form=form, node=node)
def chat(user): # Creating new topics. if request.method == 'POST': if user.topics_full(): return render_template('/index.html', topics=get_topics(), full=True, user=user.user) form = TopicForm(request.form) if form.validate(): form.add(user.user) return render_template('/index.html', topics=get_topics(), form=form, user=user.user) # Deleting topics. if request.method == 'GET' and request.args.get('del_topic', False): user.del_topic(request.args['del_topic']) form = TopicForm() return render_template('/index.html', topics=get_topics(), form=form, user=user.user)
def post(self, topic_id): if not self.has_permission: return topic = Topic.get(id=topic_id) if not topic: return self.redirect_next_url() user = self.current_user form = TopicForm(self.request.arguments) if form.validate(): topic = form.save(user=user, topic=topic) result = {'status': 'success', 'message': '主题修改成功', 'topic_url': topic.url} if self.is_ajax: return self.write(result) self.flash_message(result) return self.redirect(topic.url) if self.is_ajax: return self.write(form.result) return self.render("topic/create.html", form=form, node=topic.node)
def post(self, topic_id): if not self.has_permission: return topic = Topic.get(id=topic_id) if not topic or (topic.author != self.current_user and not self.current_user.is_admin): return self.redirect_next_url() user = self.current_user form = TopicForm(self.request.arguments) if form.validate(): topic = form.save(user=user, topic=topic) result = {'status': 'success', 'message': '主题修改成功', 'topic_url': topic.url} if self.is_ajax: return self.write(result) self.flash_message(result) return self.redirect(topic.url) if self.is_ajax: return self.write(form.result) return self.render("topic/create.html", form=form, node=topic.node)
async def _topic(request, topic_id=None): form = TopicForm(request) form.status.data = int(form.status.data) topic = None if topic_id is not None: topic = await SpecialTopic.get_or_404(topic_id) if request.method in ('POST', 'PUT') and form.validate(): dct = defaultdict(dict) for k in copy.copy(request.form): if k.startswith('posts'): match = FORM_REGEX.search(k) if match: key = match['key'] val = request.form[k][0] dct[match['index']][key] = int(val) if key == 'id' else val del request.form[k] title = form.title.data if topic_id is None: topic = await SpecialTopic.filter(title=title).first() if not topic: topic = SpecialTopic() form.populate_obj(topic) if dct: indexes = [ (i['id'], int(index)) for index, i in sorted(dct.items(), key=lambda i: i[0]) ] else: indexes = [] if topic_id is not None: await topic.set_indexes(indexes) await topic.save() if topic_id is None: await topic.set_indexes(indexes) ok = True else: ok = False topic = await topic.to_sync_dict() return json({'topic': topic if topic else None, 'ok': ok})
def Topic_fu(): from models import Topic from forms import TopicForm if request.method == "POST": form = TopicForm(request.form) if form.validate(): topicsql = Topic(**form.data) db.session.add(topicsql) db.session.commit() else: return render_template("errors.html", errors_text=str(form.errors)), 400 try: quotes = Topic.query.all() except Exception as e: return render_template("errors.html", errors_text=str(e)), 500 else: return render_template('_take_topic_template.html', quotes=quotes, link_to=url_for('Topic_fu') + "/")
async def _topic(request: Request, topic_id: Optional[Any] = None): form = TopicForm(request) form.status.data = int(form.status.data) topic = None if topic_id is not None: topic = await SpecialTopic.get_or_404(topic_id) if request.method in ('POST', 'PUT') and form.validate(): dct: DefaultDict[str, Dict[str, int]] = defaultdict(dict) for k in copy.copy(request.form): if k.startswith('posts'): match = FORM_REGEX.search(k) if (match := FORM_REGEX.search(k)): key = match['key'] # type: ignore val = request.form[k][0] dct[match['index']][key] = ( # type: ignore int(val) if key == 'id' else val) del request.form[k] title = form.title.data if topic_id is None: topic = await SpecialTopic.filter(title=title).first() if not topic: topic = SpecialTopic() form.populate_obj(topic) if dct: indexes = [ (i['id'], int(index)) for index, i in sorted(dct.items(), key=lambda i: i[0]) ] else: indexes = [] if topic_id is not None: await topic.set_indexes(indexes) await topic.save() if topic_id is None: await topic.set_indexes(indexes) ok = True
def bbs_add_topic(): node_str = urllib.unquote(request.args.get('node', "").strip()) node = None if node_str: node = Node.query.filter(Node.name==node_str).first() if not node: return render_template("node_not_found.html", next=url_for('.bbs_add_topic'), name=node_str) form = TopicForm(request.form) form.node.choices = Section.query.get_all_nodes() if node_str: form.node.data=node.title if request.method == 'POST' and form.validate(): if not node: node = Node.query.filter(Node.title==form.data["node"].strip()).first() if not node: form.node.errors.append(u"节点不存在!") return render_template("add_topic.html", form=form) section = node.section topic = Topic(user=current_user, title=form.data['title'].strip(), content=form.data["content"], tags=form.data["tags"], created=datetime.now(), last_modified=datetime.now(), last_reply_at=datetime.now()) topic.section = section topic.node = node #db.session.add(topic) node.topic_count += 1 #db.session.add(node) section.topic_count += 1 #db.session.add(section) current_user.profile.topics += 1 db.session.add_all([topic, node, section, current_user.profile]) db.session.commit() return redirect(url_for(".bbs_topic", id=topic.id)) if node_str: action_url = url_for(".bbs_add_topic", node=node_str) else: action_url = url_for(".bbs_add_topic") return render_template("add_topic.html", form=form, action_url=action_url)
def show_chatroom(chatroom_title): form = TopicForm() topics = Topic.query.all() users = User.query.all() messages = Message.query.all() banned_from = [] moderated_rooms = [] banned_users = [] topic = Topic.query.filter_by(topicname=chatroom_title).first() user = User.query.filter_by(email=session['email']).first() if BannedUser.query.filter_by(user_id=user.uid).first() is not None: flagged_rooms = BannedUser.query.filter_by(user_id=user.uid).all() for room in flagged_rooms: if room.times_flagged >= 5 and room.topic_id != 1: print("banned from:") print(room.topic_id) banned_from.append(room.topic_id) if BannedUser.query.filter_by(topic_id=topic.uid).first() is not None: users = BannedUser.query.filter_by(topic_id=topic.uid).all() for u in users: if u.times_flagged >= 5 and u.topic_id != 1: print(u.topic_id) banned_users.append( User.query.filter_by(uid=u.user_id).first().username) if Moderator.query.filter_by(user_id=user.uid).first() is not None: print("mod exists") mod_for = Moderator.query.filter_by(user_id=user.uid).all() for room in mod_for: print("mod") print(room.topic_id) if room.topic_id != 1: moderated_rooms.append( Topic.query.filter_by(uid=room.topic_id).first().topicname) if topic is None: return redirect(url_for('chat')) if topic.uid in banned_from: return redirect(url_for('chat')) session['room'] = topic.topicname if 'email' not in session: return redirect(url_for('signin')) user = User.query.filter_by(email=session['email']).first() if user is None: return redirect(url_for('signin')) else: if request.method == 'POST': if form.validate() == False: return render_template('chat.html', form=form, topics=topics, users=users, messages=messages, banned_from=banned_from, moderated_rooms=moderated_rooms, banned_users=banned_users) else: uid = user.uid newtopic = Topic(form.topicname.data, uid) db.session.add(newtopic) db.session.commit() session['topic'] = newtopic.topicname newtopic = Topic.query.filter_by( topicname=form.topicname.data).first() mod = Moderator(user.uid, newtopic.uid) db.session.add(mod) db.session.commit() return redirect('/chat/' + newtopic.topicname) if request.method == 'GET': return render_template('chat.html', form=form, topics=topics, users=users, messages=messages, banned_from=banned_from, moderated_rooms=moderated_rooms, banned_users=banned_users)