def setUp(self): db.app = test_app db.create_all() user = User() user.username = self.username user.set_password(self.password) db.session.add(user) comment = Comment() comment.name = self.comment_name comment.text = self.comment_text tag = Tag() tag.title = self.tag_title post = Post() post.title = self.post_title post.text = self.post_text post.publish_date = self.post_publish_date # add relationships to other tables post.user = user post.tags = [tag] post.comments = [comment] db.session.add(user) db.session.add(comment) db.session.add(tag) db.session.add(post) db.session.commit()
def post(self, post_id=None): if post_id: abort(400) else: args = post_post_parser.parse_args(strict=True) username = args['username'] password = args['password'] user = User.query.filter_by(username=username).first() if not user: abort(401) if not user.check_password(password): abort(401) new_post = Post() new_post.user_id = user.id new_post.title = args['title'] new_post.publish_date = datetime.datetime.now() new_post.text = args['text'] if args['tags']: for t_title in args['tags']: tag = Tag.query.filter_by(title=t_title).first() # Add the tag if it exists. If not make a new tag. if tag: new_post.tags.append(tag) else: new_tag = Tag() new_tag.title = t_title new_post.tags.append(new_tag) db.session.add(new_post) db.session.commit() return new_post.id, 201
def tag(uid,tid,tagname): """ tags the thread with given tagname - only creator of the thread can do it for now """ try: t = Thread.objects.get(pk = int(tid)) if t.creator.id != int(uid): return (False,'kendi postlarini etiketle bence') tagname = tagname.strip().lower() if tagname.startswith('#'): tagname = tagname[1:] if '#' in tagname: return (False,'boyle etiket olmaz ki') if ' ' in tagname: return (False,'etiketlerde bosluk olmuyo') tag = Tag.objects.filter(name = tagname) if len(tag) == 0: id = generateId() tag = Tag(id = id, name = tagname) tag.save() tag.threads.add(t) tag.save() return (True,'') else: tag = tag[0] if len(tag.threads.filter(id = t.id)) != 0: return (False,'bu etiket varmis ki bunda') else: tag.threads.add(t) tag.save() return (True,'') except: connection._rollback() return (False,str(traceback.format_exc()))
#!/usr/bin/env Python #encoding:utf-8 import random import datetime from webapp.models import db, User, Post, Tag from webapp import create_app app = create_app('webapp.config.ProdConfig') #获取ID为1的用户 #生成4类标签和标签列表 user = User.query.get(1) tag_one = Tag('Python') tag_two = Tag('Flask') tag_three = Tag('SQLAlechemy') tag_four = Tag('Jinjia') tag_list = [tag_one, tag_two, tag_three, tag_four] s = 'Example text' #Post为提交的标题 for i in xrange(100): new_post = Post("Post" + str(i)) new_post.user = user new_post.publish_date = datetime.datetime.now() new_post.text = s new_post.tags = random.sample(tag_list, random.randint(1, 3)) db.session.add(new_post) db.session.commit()
def get(self, tag_id=None): if not tag_id: return Tag.query.all() # return tag.blogs.all() return Tag.tag_blogs(tag_id)
def get_all_tags(): all_tags = [] tags = Tag.objects(father=None).all() for tag in tags: add_all_children_tags(tag, 0, all_tags) return all_tags
def add_all_children_tags(tag, level, all_tags): all_tags.append((tag.title, level)) children_tags = Tag.objects(father=tag.title).all() for child_tag in children_tags: add_all_children_tags(child_tag, level + 1, all_tags)