Beispiel #1
0
def topic_add(nodesite):
	if not session.get('user_id'):
		return redirect(url_for('account.login'))
	node = Node.query.filter_by(site=nodesite).first()
	if request.method == 'POST':
		if g.user.time < 20:
			flash(u'时间不足20分钟!')
			return redirect(url_for('topic.index'))
		node = Node.query.filter_by(site=nodesite).first()
		if request.form['title'] == '':
			g.error = u'请输入主题标题!'
			render_template('topic_add.html', node=node)
		elif request.form['text'] == '':
			g.error = u'请输入主题内容!'
			render_template('topic_add.html', node=node)
		else:
			if '@' in request.form['text']:
				text = mentionfilter(request.form['text'])
			else:
				text = request.form['text']
			text = textformat(text)
			text = markdown(text)
			topic = Topic(author=g.user, title=request.form['title'], text=text, text_origin=request.form['text'], node=node, reply_count=0)
			bank = Bank.query.get(1)
			g.user.time -= 20
			bank.time +=20
			db.session.add(topic)
			db.session.commit()
			bill = Bill(author=g.user,time=20,type=2,date=int(time.time()),topic=topic,balance=g.user.time)
			db.session.add(bill)
			db.session.commit()
			flash(u'发布成功!')
			if '@' in request.form['title']:
				title = request.form['title'].replace('\n','')
				usernames = list(set(mentions(title)))
				for username in usernames:
					author = User.query.filter_by(name=username).first()
					if author.id != g.user.id:
						notify = Notify(author, topic, reply=None, type=3)
						db.session.add(notify)
						db.session.commit()
			if '@' in request.form['text']:
				text = request.form['text'].replace('\n','')
				usernames = list(set(mentions(text)))
				for username in usernames:
					author = User.query.filter_by(name=username).first()
					if author.id != g.user.id:
						notify = Notify(author, topic, reply=None, type=3)
						db.session.add(notify)
						db.session.commit()
			return redirect(url_for('topic.topic_view', topic_id=topic.id))
	return render_template('topic_add.html', node=node)
Beispiel #2
0
def add_reply(topic_id):
	if not session.get('user_id'):
		abort(401)
	if g.user.time < 5:
		g.error = u'抱歉,您的时间不足5分钟!'
		return redirect(url_for('topic.topic_view', topic_id=topic_id) + "#replyend")
	if request.form['reply[content]'] == '':
		g.error = u'抱歉,您的时间不足5分钟!'
		return redirect(url_for('topic.topic_view', topic_id=topic_id) + "#replyend")
	topic = Topic.query.get(topic_id)
	if topic.replys is not None:
		numbered = len(topic.replys.all())
	else:
		numbered = 0
	page_obj = topic.replys.paginate(1, per_page=config.RE_PER_PAGE)
	if page_obj.pages == 0:
		page = 1
	elif len(topic.replys.all()) % config.RE_PER_PAGE == 0:
		page = page_obj.pages + 1
	else:
		page = page_obj.pages
	if '@' in request.form['reply[content]']:
		reply_content = mentionfilter(request.form['reply[content]'])
	else:
		reply_content = request.form['reply[content]']
	reply_content = textformat(reply_content)
	reply_content = markdown(reply_content)
	reply = Reply(topic=topic, author=g.user, text=reply_content, text_origin=request.form['reply[content]'], number=numbered + 1)
	g.user.time -= 5
	topic.author.time += 5
	topic.last_reply_date = int(time.time())
	topic.reply_count += 1
	for reader in topic.readers:
		topic.readers.remove(reader)
	db.session.add(reply)
	db.session.commit()
	t = int(time.time())
	if session['user_id'] != topic.author.id:
		bill = Bill(author=g.user,time=5,type=3,date=t,reply=reply,user_id=topic.author.id,balance=g.user.time)
		bill2 = Bill(author=topic.author,time=5,type=5,date=t,reply=reply,user_id=g.user.id,balance=topic.author.time)
		db.session.add(bill)
		db.session.add(bill2)
		db.session.commit()
		notify = Notify(author=topic.author, topic=topic, reply=reply, type=1)
		db.session.add(notify)
		db.session.commit()
	if '@' in request.form['reply[content]']:
		content = request.form['reply[content]'].replace('\n','')
		usernames = list(set(mentions(content)))
		for username in usernames:
			author = User.query.filter_by(name=username).first()
			if author.id != topic.author.id and author.id != g.user.id:
				notify = Notify(author, topic, reply=reply, type=2)
				db.session.add(notify)
				db.session.commit()
	return redirect(url_for('topic.topic_view', topic_id=topic_id, page=page) + "#replyend")
Beispiel #3
0
def add_reply(topic_id):
    if not session.get('user_id'):
        abort(401)
    if g.user.time < 5:
        g.error = u'抱歉,您的时间不足5分钟!'
        return redirect(
            url_for('topic.topic_view', topic_id=topic_id) + "#replyend")
    if request.form['reply[content]'] == '':
        g.error = u'抱歉,您的时间不足5分钟!'
        return redirect(
            url_for('topic.topic_view', topic_id=topic_id) + "#replyend")
    topic = Topic.query.get(topic_id)
    if topic.replys is not None:
        numbered = len(topic.replys.all())
    else:
        numbered = 0
    page_obj = topic.replys.paginate(1, per_page=config.RE_PER_PAGE)
    if page_obj.pages == 0:
        page = 1
    elif len(topic.replys.all()) % config.RE_PER_PAGE == 0:
        page = page_obj.pages + 1
    else:
        page = page_obj.pages
    if '@' in request.form['reply[content]']:
        reply_content = mentionfilter(request.form['reply[content]'])
    else:
        reply_content = request.form['reply[content]']
    reply_content = textformat(reply_content)
    reply_content = markdown(reply_content)
    reply = Reply(topic=topic,
                  author=g.user,
                  text=reply_content,
                  text_origin=request.form['reply[content]'],
                  number=numbered + 1)
    g.user.time -= 5
    topic.author.time += 5
    topic.last_reply_date = int(time.time())
    topic.reply_count += 1
    for reader in topic.readers:
        topic.readers.remove(reader)
    db.session.add(reply)
    db.session.commit()
    t = int(time.time())
    if session['user_id'] != topic.author.id:
        bill = Bill(author=g.user,
                    time=5,
                    type=3,
                    date=t,
                    reply=reply,
                    user_id=topic.author.id,
                    balance=g.user.time)
        bill2 = Bill(author=topic.author,
                     time=5,
                     type=5,
                     date=t,
                     reply=reply,
                     user_id=g.user.id,
                     balance=topic.author.time)
        db.session.add(bill)
        db.session.add(bill2)
        db.session.commit()
        notify = Notify(author=topic.author, topic=topic, reply=reply, type=1)
        db.session.add(notify)
        db.session.commit()
    if '@' in request.form['reply[content]']:
        content = request.form['reply[content]'].replace('\n', '')
        usernames = list(set(mentions(content)))
        for username in usernames:
            author = User.query.filter_by(name=username).first()
            if author.id != topic.author.id and author.id != g.user.id:
                notify = Notify(author, topic, reply=reply, type=2)
                db.session.add(notify)
                db.session.commit()
    return redirect(
        url_for('topic.topic_view', topic_id=topic_id, page=page) +
        "#replyend")