Example #1
0
def add(id = 0):
	topic = Topic.query.filter(Topic.id == id).first()
	topic_form = TopicForm(obj = topic)

	if request.method == 'POST' and topic_form.validate():
		if topic:
			if topic.category_id != topic_form.category_id.data:
				new_category = Category.get(topic_form.category_id.data)
				new_category.num += 1
				old_category = Category.get(topic.category_id)
				old_category.num -= 1

			tag_list = topic_form.tag.data.split(" ")
			for tag_name in tag_list:
				if tag_name.strip():
					tag = TopicTag.query.filter(TopicTag.name == tag_name).first()
					if tag:
						topic_to_tag = TopicToTag.query.filter(TopicToTag.topic_id == id).filter(TopicToTag.tag_id == tag.id).first()
						if topic_to_tag is None:
							tag.num += 1
							topic_to_tag = TopicToTag(id, tag.id)
							db_session.add(topic_to_tag)
					else:
						tag = TopicTag(tag_name)
						db_session.add(tag)	
						db_session.flush()
						topic_to_tag = TopicToTag(topic.id, tag.id)
						db_session.add(topic_to_tag)
								
			topic_form.populate_obj(topic)
			db_session.commit()	
		else:
			topic = Topic(topic_form.title.data)
			topic.content = topic_form.content.data
			topic.category_id = topic_form.category_id.data
			topic.tag = topic_form.tag.data
			db_session.add(topic)
			db_session.flush()

			category = 	Category.get(topic_form.category_id.data)
			category.num += 1
			tag_list = topic_form.tag.data.split(" ")
			for tag_name in tag_list:
				if tag_name.strip():
					tag = TopicTag.query.filter(TopicTag.name == tag_name).first()
					if tag:
						tag.num += 1
					else:
						tag = TopicTag(tag_name)
						db_session.add(tag)
						db_session.flush()
					topic_to_tag = TopicToTag(topic.id, tag.id)
					db_session.add(topic_to_tag)	
			db_session.commit()	
		return redirect('/admin/topic/list')
	else:
		return render_template('/admin/addtopic.html',
			topic_form = topic_form,
			)
Example #2
0
def init_data():
	"""
	init data
	"""	
	from domain.model.user import User

	sha1 = hashlib.sha1()
	sha1.update('123456')

	user = User("*****@*****.**")
	user.nickname = 'fainle'
	user.password = sha1.hexdigest()

	db_session.add(user)
	db_session.commit()
Example #3
0
def init_data():
    """
    init data
    """
    from domain.model.user import User

    sha1 = hashlib.sha1()
    sha1.update('123456')

    user = User("*****@*****.**")
    user.nickname = 'fainle'
    user.password = sha1.hexdigest()

    db_session.add(user)
    db_session.commit()
Example #4
0
def register():
    register_form = RegisterForm(request.form)
    if request.method == 'POST':
        if register_form.validate():
            user = User(register_form.email.data)
            user.nickname = register_form.nickname.data
            sha1 = hashlib.sha1()
            sha1.update(register_form.password.data)
            user.password = sha1.hexdigest()
            db_session.add(user)
            db_session.commit()
            session['user_id'] = user.id
            session['nickname'] = user.nickname
            return redirect(request.args.get('next') or '/user')
    return render_template('/user/register.html', registerform=register_form)
Example #5
0
def topic_show(id=1):
    """
    main page
    """
    topic = Topic.query.filter(Topic.id == id).first()
    #reply_form = ReplyForm()
    if topic:
        category = Category.query.filter(
            Category.id == topic.category_id).first()
        #reply = TopicReply.query.filter(TopicReply.topic_id == topic.id).all()
        topic.count.views += 1
        db_session.commit()
    else:
        abort(404)

    return render_template('/topic/show.html', topic=topic, category=category)
Example #6
0
def register():
    register_form = RegisterForm(request.form)
    if request.method == 'POST':
        if register_form.validate():
            user = User(register_form.email.data)
            user.nickname = register_form.nickname.data
            sha1 = hashlib.sha1()
            sha1.update(register_form.password.data)
            user.password = sha1.hexdigest()
            db_session.add(user)
            db_session.commit()
            session['user_id'] = user.id
            session['nickname'] = user.nickname
            return redirect(request.args.get('next') or '/user')
    return render_template('/user/register.html',
                           registerform=register_form
    )
Example #7
0
def topic_show(id=1):
    """
    main page
    """
    topic = Topic.query.filter(Topic.id == id).first()
    #reply_form = ReplyForm()
    if topic:
        category = Category.query.filter(Category.id == topic.category_id).first()
        #reply = TopicReply.query.filter(TopicReply.topic_id == topic.id).all()
        topic.count.views += 1
        db_session.commit()
    else:
        abort(404)

    return render_template('/topic/show.html',
                           topic=topic,
                           category=category)
Example #8
0
def add_category():
	data = {}
	id = request.form['id']
	category = Category.query.filter(Category.id == id).first()
	category_form = CategoryForm(obj = category)
	if request.method == 'POST' and category_form.validate():
		if category:
			category_form.populate_obj(category)
			db_session.commit()
		else:
			category = Category(category_form.name.data)
			db_session.add(category)
			db_session.commit()
		data['code'] = 200
	else:				
		data['code'] = 401
		data['errors'] = category_form.errors	
	
	return jsonify(data)
Example #9
0
def add_category(id=0):
    """
    add category
    """
    category = Category.query.filter(Category.id == id).first()
    category_form = CategoryForm(obj=category)
    if request.method == 'POST' and category_form.validate():
        if category:
            category_form.populate_obj(category)
            db_session.flush()
            db_session.commit()
        else:
            category = Category(category_form.name.data)
            category.content = category_form.content.data
            db_session.add(category)
            db_session.flush()
            db_session.commit()
        return redirect("/c/%s" % category.id)
    else:
        return render_template('/topic/add_category.html',
                               category_form=category_form)
Example #10
0
def add_category(id=0):
    """
    add category
    """
    category = Category.query.filter(Category.id == id).first()
    category_form = CategoryForm(obj=category)
    if request.method == 'POST' and category_form.validate():
        if category:
            category_form.populate_obj(category)
            db_session.flush()
            db_session.commit()
        else:
            category = Category(category_form.name.data)
            category.content = category_form.content.data
            db_session.add(category)
            db_session.flush()
            db_session.commit()
        return redirect("/c/%s" % category.id)
    else:
        return render_template('/topic/add_category.html',
                               category_form=category_form)
Example #11
0
def topic_add(id=0):
    """
    add topic
    """
    topic = Topic.query.filter(Topic.id == id).first()
    topic_form = TopicForm(obj=topic)
    topic_form.category_id.choices = [
        (c.id, c.name)
        for c in Category.query.order_by(Category.id.asc()).all()
    ]
    if request.method == 'POST' and topic_form.validate():
        if topic:
            if topic.category_id != topic_form.category_id.data:
                new_category = Category.get(topic_form.category_id.data)
                new_category.num += 1
                old_category = Category.get(topic.category_id)
                old_category.num -= 1

            tag_list = topic_form.tag.data.split(" ")
            for tag_name in tag_list:
                if tag_name.strip():
                    tag = TopicTag.query.filter(
                        TopicTag.name == tag_name).first()
                    if tag:
                        topic_to_tag = TopicToTag.query.filter(
                            TopicToTag.topic_id == id).filter(
                                TopicToTag.tag_id == tag.id).first()
                        if topic_to_tag is None:
                            tag.num += 1
                            topic_to_tag = TopicToTag(id, tag.id)
                            db_session.add(topic_to_tag)
                    else:
                        tag = TopicTag(tag_name)
                        db_session.add(tag)
                        db_session.flush()
                        topic_to_tag = TopicToTag(topic.id, tag.id)
                        db_session.add(topic_to_tag)

            topic_form.populate_obj(topic)
            db_session.commit()
        else:
            topic = Topic(topic_form.title.data)
            topic.content = topic_form.content.data
            topic.category_id = topic_form.category_id.data
            topic.tag = topic_form.tag.data
            db_session.add(topic)
            db_session.flush()

            category = Category.get(topic_form.category_id.data)
            category.num += 1
            tag_list = topic_form.tag.data.split(" ")
            for tag_name in tag_list:
                if tag_name.strip():
                    tag = TopicTag.query.filter(
                        TopicTag.name == tag_name).first()
                    if tag:
                        tag.num += 1
                    else:
                        tag = TopicTag(tag_name)
                        db_session.add(tag)
                        db_session.flush()
                    topic_to_tag = TopicToTag(topic.id, tag.id)
                    db_session.add(topic_to_tag)
            db_session.commit()
        return redirect('/%s' % topic.id)
    else:
        return render_template('/topic/add.html', topic_form=topic_form)