本文整理汇总了Python中models.Topic.topic方法的典型用法代码示例。如果您正苦于以下问题:Python Topic.topic方法的具体用法?Python Topic.topic怎么用?Python Topic.topic使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类models.Topic
的用法示例。
在下文中一共展示了Topic.topic方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: topic
# 需要导入模块: from models import Topic [as 别名]
# 或者: from models.Topic import topic [as 别名]
def topic(atopic):
if request.method == 'POST':
t = Topic(topic = atopic, author = g.user)
db.session.add(t)
db.session.commit()
reply = dict(id = t.id, name = t.topic, count = t.snippets.count())
return Response(json.dumps(reply), 200, mimetype="application/json")
elif request.method == 'PUT':
topic_id = atopic
topic_name = ""
topic_name = request.data
t = g.user.topics.filter_by(id=topic_id).first()
t.topic = topic_name
db.session.commit()
reply = dict(id = t.id, name = t.topic, count = t.snippets.count())
return Response(json.dumps(reply), 200, mimetype="application/json")
elif request.method == 'DELETE':
topic = g.user.topics.filter_by(id=atopic).first()
if topic is None:
return jsonify(error=404, text='Invalid topic id'), 404
# Get all snippets in the topic
snippets = topic.snippets.all()
# Move all snippets in the topic to be removed to the 'General' topic
general_topic = g.user.topics.filter_by(topic='General').first()
snippets_added_to_general = 0
for snippet in snippets:
snippets_added_to_general += 1
snippet.topic = general_topic
db.session.delete(topic)
db.session.commit()
# Return the number of topics added to the 'General' list
reply = dict(id = atopic, new_general_snippets = snippets_added_to_general)
return Response(json.dumps(reply), 200, mimetype="application/json")