Exemple #1
0
    def post(self, post_id=None):
        if post_id:
            abort(400)
        else:
            args = post_post_parser.parse_args(strict=True)

            user = User.verify_auth_token(args['token'])
            if not user:
                abort(401)

            new_post = Post(args['title'])
            new_post.user = user
            new_post.date = datetime.datetime.now()
            new_post.text = args['text']

            if args['tags']:
                for item in args['tags']:
                    tag = Tag.query.filter_by(title=item).first()

                    # Add the tag if it exists. If not, make a new tag
                    if tag:
                        new_post.tags.append(tag)
                    else:
                        new_tag = Tag(item)
                        new_post.tags.append(new_tag)

            db.session.add(new_post)
            db.session.commit()
            return new_post.id, 201
def populate_default_data(db, app):
    db.app = app
    db.create_all()

    user = User()
    user.username = "******"
    user.set_password("jim")
    db.session.add(user)
    db.session.commit()

    tag_one = Tag(title="Python")
    tag_two = Tag(title="Flask")
    tag_three = Tag(title="SQLAlchemy")
    tag_four = Tag(title="Jinja")
    tag_list = [tag_one, tag_two, tag_three, tag_four]
    s = "Example text"

    for i in range(100):
        new_post = Post(title="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 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()
Exemple #4
0
	def post(self,post_id=None):
		if post_id:
			abort(405)
		else:
			args = post_post_parser.parse_args(strict=True)

			user = User.verify_auth_token(args['token'])
			if not user:
				abort(401)

			new_post = Post(args['title'])
			new_post.user = user
			new_post.publish_date=datetime.datetime.now()
			new_post.text = args['text']
			if args['tags']:
				for item in args['tags']:
					tag = Tag.query.filter_by(
						title=item
					).first()

					if tag:
						new_post.tags.append(tag)
					else:
						new_tag = Tag(item)
						new_post.tags.append(new_tag)


			db.session.add(new_post)
			db.session.commit()

			return new_post.id,201
Exemple #5
0
def new_post():
    form = PostForm()
    if form.validate_on_submit():
        new_post = Post(form.title.data)
        new_post.text = form.text.data
        new_post.publish_date = datetime.datetime.now()
        new_post.user = User.query.filter_by(
            username=current_user.username).one()
        db.session.add(new_post)
        db.session.commit()
    return render_template('new.html', form=form)
Exemple #6
0
def new_post():
    form = PostForm()

    if form.validate_on_submit():
        new_post = Post(form.title.data)
        new_post.body = form.text.data
        new_post.publish_time = datetime.datetime.now()
        new_post.user = current_user
        db.session.add(new_post)
        db.session.commit()

    return render_template('new.html', form=form)
Exemple #7
0
def new_post():
    form = PostForm()
    print("new_post")
    if form.validate_on_submit():
        new_post = Post()
        new_post.title = form.title.data
        new_post.text = form.text.data
        new_post.publish_date = datetime.datetime.now()
        user = User.objects(id=current_user.id).first()
        new_post.user = user
        new_post.save()

    return render_template('new.html', form=form)
Exemple #8
0
def new_post():
    form = PostForm()

    if form.validate_on_submit():
        new_post = Post(form.title.data)
        new_post.text = form.text.data
        new_post.publish_date = datetime.datetime.now()
        new_post.user = User.query.filter_by(
            username=current_user.username
        ).one()

        db.session.add(new_post)
        db.session.commit()

    return render_template('new.html', form=form)
Exemple #9
0
def add_post():
    if not g.current_user:
        flash('发表新想法前请先登录!', category='error')
        return redirect(url_for('main.login'))
    form = PostForm()
    if form.validate_on_submit():
        new_post = Post(form.title.data, form.content.data)
        new_post.publish_date = datetime.datetime.now()
        new_post.user = g.current_user
        new_post.category = PostCategory(form.category.data)

        db.session.add(new_post)
        db.session.commit()
        return redirect(url_for('.get_post', post_id=new_post.id))

    return render_template('add.html', form=form)
Exemple #10
0
def new_post():
    # if not g.current_user:
    # 	return redirect(url_for('main.login'))

    form = PostForm()

    if form.validate_on_submit():
        new_post = Post(form.title.data)
        new_post.text = form.text.data
        new_post.publish_date = datetime.datetime.now()
        # new_post.user = g.current_user
        new_post.user = current_user

        db.session.add(new_post)
        db.session.commit()

    return render_template('blog/new.html', form=form)
Exemple #11
0
def new_post():
    # 此处验证用login_required装饰器代替
    '''
    if not g.current_user:
        return redirect(url_for('main.login'))
    '''
    form = PostForm()

    if form.validate_on_submit():
        new_post = Post(form.title.data)
        new_post.text = form.text.data
        new_post.publish_date = datetime.datetime.now()
        new_post.user = current_user

        db.session.add(new_post)
        db.session.commit()
        return redirect(url_for('.post', post_id=new_post.id))
    return render_template('new.html', form=form)
Exemple #12
0
def setup_db():
    db.create_all()

    admin_role = Role()
    admin_role.name = "admin"
    admin_role.description = "admin"
    db.session.add(admin_role)

    default_role = Role()
    default_role.name = "default"
    default_role.description = "default"
    db.session.add(default_role)

    admin = User()
    admin.username = "******"
    admin.set_password("password")
    admin.roles.append(admin_role)
    admin.roles.append(default_role)
    db.session.add(admin)

    tag_one = Tag('Python')
    tag_two = Tag('Flask')
    tag_three = Tag('SQLAlechemy')
    tag_four = Tag('Jinja')
    tag_list = [tag_one, tag_two, tag_three, tag_four]

    s = "Body text"

    for i in xrange(100):
        new_post = Post("Post " + str(i))
        new_post.user = admin
        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()
Exemple #13
0
def setup_db():
    db.create_all()

    admin_role = Role()
    admin_role.name = "admin"
    admin_role.description = "admin"
    db.session.add(admin_role)

    default_role = Role()
    default_role.name = "default"
    default_role.description = "default"
    db.session.add(default_role)

    admin = User()
    admin.username = "******"
    admin.set_password("password")
    admin.roles.append(admin_role)
    admin.roles.append(default_role)
    db.session.add(admin)

    tag_one = Tag('Python')
    tag_two = Tag('Flask')
    tag_three = Tag('SQLAlechemy')
    tag_four = Tag('Jinja')
    tag_list = [tag_one, tag_two, tag_three, tag_four]

    s = "Body text"

    for i in xrange(100):
        new_post = Post("Post " + str(i))
        new_post.user = admin
        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()
Exemple #14
0
def new_post():
    if request.method == 'POST':
        jsonStr = request.get_data()
        data = json.loads(jsonStr)
        if hasattr(data, 'key') or data['key'] != "wysj3910":
            return "密钥错误!"
        new_post = Post(data['title'])  # 创建帖子
        new_post.text = data['text']  # 内容
        new_post.cover = data['cover']  # 封面
        new_post.publish_date = time.strftime(
            "%Y-%m-%d %H:%M:%S", time.localtime(data['publish_date']))  # 发布日期
        new_post.update_date = datetime.datetime.now()  #更新的日期
        user = User.query.filter_by(username=data['username']).first()  # 获取用户
        if not user:
            user = User(data['username'])
            if hasattr(data, 'nickname'):
                user.nickname = data['nickname']
        new_post.user = user  # 用户
        new_post.type = data['type']  # 类型
        new_post.summary = data['summary']  # 简介
        new_post.video = data['video']  # 视频地址
        new_post.read = data['read']  # 阅读量
        new_post.post_hash = data['post_hash']  #贴的hash值
        for photo_url in data['photos']:  # 图片列表
            if photo_url != '':
                photo = Photo(photo_url)
                new_post.photos.append(photo)

        for tagStr in data['tags']:  # 标签
            tagStr = tagStr.strip()
            tag = Tag.query.filter_by(title=tagStr).first()
            if not tag:
                tag = Tag(tagStr)
            new_post.tags.append(tag)
        db.session.add(new_post)
        db.session.commit()
        return json.dumps({'status': 0})
Exemple #15
0
def setup_db():
    db.create_all()

    # 创建管理员角色
    admin_role = Role('admin')
    admin_role.description = 'admin'
    db.session.add(admin_role)
    # 创建默认角色
    default_role = Role('default')
    default_role.description = 'default'
    db.session.add(default_role)

    # 创建管理员用户信息
    admin = User('admin')
    admin.set_password('password')
    admin.roles.append(admin_role)
    admin.roles.append(default_role)
    db.session.add(admin)

    tag_one = Tag('Python')
    tag_two = Tag('Flask')
    tag_three = Tag('SQLAlchemy')
    tag_four = Tag('Jinja')
    tag_list = [tag_one, tag_two, tag_three, tag_four]

    s = 'Body text'

    for i in range(100):
        new_post = Post('Post %s' % i, s)
        new_post.user = admin
        new_post.publish_date = datetime.datetime.now()
        new_post.tags = random.sample(tag_list, random.randint(1, 3))

        db.session.add(new_post)

    db.session.commit()
def insert_data():
    with app.app_context():
        # 不需要在这里创建库,应该使用数据库升级命令`db upgrade`来创建库
        # db.create_all()

        # 这里设定了3种角色
        role_admin = Role(name='admin')
        role_admin.description = "administrator role"
        role_poster = Role(name='poster')
        role_poster.description = "the registered user role"
        role_default = Role(name='default')
        role_default.description = 'the unregistered user role'
        db.session.add(role_admin)
        db.session.add(role_poster)
        db.session.add(role_default)

        # add User
        admin = User(username='******')
        admin.email = '*****@*****.**'
        admin.password = '******'
        admin.confirmed = True
        admin.roles.append(role_admin)
        admin.roles.append(role_poster)
        admin.roles.append(role_default)
        db.session.add(admin)

        user01 = User(username='******')
        user01.email = '*****@*****.**'
        user01.password = '******'
        user01.confirmed = True
        user01.roles.append(role_poster)
        user01.roles.append(role_default)
        db.session.add(user01)

        user02 = User(username='******')
        user02.email = '*****@*****.**'
        user02.password = '******'
        user02.confirmed = True
        user02.roles.append(role_poster)
        user02.roles.append(role_default)
        db.session.add(user02)

        # add Tag and Post
        tag_one = Tag('Python')
        tag_two = Tag('Flask')
        tag_three = Tag('SQLAlechemy')
        tag_four = Tag('Jinja')
        tag_list = [tag_one, tag_two, tag_three, tag_four]

        s = "Example Text"

        for i in xrange(1, 101):
            new_post = Post("Post {}".format(i))
            if i % 2:
                new_post.user = user01
            else:
                new_post.user = user02
            new_post.publish_date = datetime.datetime.utcnow()
            new_post.text = s
            new_post.tags = random.sample(tag_list, random.randint(1, 3))
            db.session.add(new_post)

        # add comment
        comment01 = Comment()
        comment01.name = 'comment01'
        comment01.text = 'comment text'
        comment01.post_id = 99
        comment01.date = datetime.datetime.utcnow()
        db.session.add(comment01)

        comment02 = Comment()
        comment02.name = 'comment02'
        comment02.text = 'comment text'
        comment02.post_id = 100
        comment02.date = datetime.datetime.utcnow()
        db.session.add(comment02)

        db.session.commit()
Exemple #17
0
import random
import datetime
from webapp.models import db, User, Post, tags, Tag, Comment

user = User.query.get(1)

tag_one = Tag('Python')
tag_two = Tag('Flask')
tag_three = Tag('SQLAlechemy')
tag_four = Tag('Jinja')
tag_list = [tag_one, tag_two, tag_three, tag_four]

s = "Example text"

for i in range(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()
Exemple #18
0
def setup():
    db.create_all()

    admin_role = Role("admin")
    admin_role.description = "admin"
    db.session.add(admin_role)

    default_role = Role("default")
    default_role.description = "default"
    db.session.add(default_role)

    guest_role = Role("guest")
    guest_role.description = "guest"
    db.session.add(guest_role)

    db.session.commit()

    admin = User("Admin")
    admin.nickname = "管理员"
    admin.email = "*****@*****.**"
    admin.set_password('admin')
    admin.roles.append(admin_role)
    db.session.add(admin)

    official = User("PublicOfficial")
    official.nickname = "公共库用户"
    official.email = "*****@*****.**"
    official.set_password('official')
    admin.roles.append(admin_role)
    db.session.add(official)

    db.session.commit()

    # 创建原始公共库目录(共5个)
    script1 = Script('always_work.sg')
    script1.content = """Strategy T1:
    return 1"""
    script1.description = "永远合作"
    script1.user = official
    script1.is_private = False
    db.session.add(script1)

    script2 = Script('random_example.sg')
    script2.content = "Strategy T2:i = RANDOM(1);if(i==0){return 0}else{return 1}"
    script2.description = "`听天由命`,背叛与合作的几率为 50% (RANDOM 为系统范围内真随机)"
    script2.user = official
    script2.is_private = False
    db.session.add(script2)

    script3 = Script('slow_work.sg')
    script3.content = "Strategy T3:if(COUNT == 1){return 1}else{return CUR}"
    script3.description = "`慢半拍`:开局合作,随后一直跟随对方上一轮的选择"
    script3.user = official
    script3.is_private = False
    db.session.add(script3)

    script4 = Script('elusive_work.sg')
    script4.content = "Strategy T4:if (COUNT == 1){return 1}else {i = RANDOM(9);if (i == 9){return 0}else{return CUR}}"
    script4.description = "改进版`慢半拍`,开局合作,随后随机自我背叛,大几率跟随对方上一轮的选择"
    script4.user = official
    script4.is_private = False
    db.session.add(script4)

    script5 = Script('never_forgive.sg')
    script5.content = "Strategy T5:if(FLAG){return 0}else{return 1}"
    script5.description = "永不原谅,只要对方存在过背叛行为,就一直背叛"
    script5.user = official
    script5.is_private = False
    db.session.add(script5)

    db.session.commit()

    # 创建文章评论
    comment1 = Comment('comment1')
    comment1.user = admin
    comment2 = Comment('comment2')
    comment2.user = official
    comment3 = Comment('comment3')
    comment3.user = admin
    db.session.add(comment1)
    db.session.add(comment2)
    db.session.add(comment3)
    db.session.commit()

    post1 = Post('Post1', 'Content1')
    post1.user = official
    post1.category = PostCategory.General
    post1.comments.append(comment1)
    post1.comments.append(comment2)

    post2 = Post('Post2', 'Content2')
    post2.user = admin
    post2.category = PostCategory.Feedback
    post2.comments.append(comment3)

    db.session.add(post1)
    db.session.add(post2)

    db.session.commit()
Exemple #19
0
    def test_comments(self):
        # add two users
        r = Role.query.filter_by(name='poster').first()
        self.assertIsNotNone(r)
        u1 = User('john')
        u1.email = '*****@*****.**'
        u1.password = '******'
        u1.confirmed = True
        u1.roles.append(r)
        u2 = User('susan')
        u2.email = '*****@*****.**'
        u2.password = '******'
        u2.confirmed = True
        u2.roles.append(r)
        db.session.add_all([u1, u2])
        db.session.commit()

        # add a post
        post = Post(title='title of the post')
        post.text = 'body of the post'
        post.user = u1
        db.session.add(post)
        db.session.commit()

        # write a comment
        response = self.client.post(url_for('api.new_post_comment',
                                            id=post.id),
                                    headers=self.get_api_headers(
                                        'susan', 'dog'),
                                    data=json.dumps({
                                        'name':
                                        'comment name',
                                        'text':
                                        'Good [post](http://example.com)!'
                                    }))
        self.assertTrue(response.status_code == 201)
        json_response = json.loads(response.data.decode('utf-8'))
        url = response.headers.get('Location')
        self.assertIsNotNone(url)
        self.assertTrue(json_response['name'] == 'comment name')
        self.assertTrue(
            json_response['text'] == 'Good [post](http://example.com)!')

        # get the new comment
        response = self.client.get(url,
                                   headers=self.get_api_headers('john', 'cat'))
        self.assertTrue(response.status_code == 200)
        json_response = json.loads(response.data.decode('utf-8'))
        self.assertTrue(json_response['url'] == url)
        self.assertTrue(json_response['name'] == 'comment name')
        self.assertTrue(
            json_response['text'] == 'Good [post](http://example.com)!')

        # add another comment
        comment = Comment(name='another comment name')
        comment.text = 'Thank you!'
        comment.user = u1
        comment.post = post
        db.session.add(comment)
        db.session.commit()

        # get the two comments from the post
        response = self.client.get(url_for('api.get_post_comments',
                                           id=post.id),
                                   headers=self.get_api_headers(
                                       'susan', 'dog'))
        self.assertTrue(response.status_code == 200)
        json_response = json.loads(response.data.decode('utf-8'))
        self.assertIsNotNone(json_response.get('comments'))
        self.assertTrue(json_response.get('count', 0) == 2)

        # get all the comments
        response = self.client.get(url_for('api.get_comments', id=post.id),
                                   headers=self.get_api_headers(
                                       'susan', 'dog'))
        self.assertTrue(response.status_code == 200)
        json_response = json.loads(response.data.decode('utf-8'))
        self.assertIsNotNone(json_response.get('comments'))
        self.assertTrue(json_response.get('count', 0) == 2)