Exemplo n.º 1
0
def register():
    #创建随机体验用户
    username = fake.user_name()
    while User.query.filter_by(username=username).first() is not None:
        username = fake.user_name()

    password = fake.word()
    user = User(username=username)
    user.set_password(password)
    db.session.add(user)
    db.session.commit()

    item = Item(body='Witness someting truly majestic', author=user)
    item2 = Item(body='Help a complete stranger', author=user)
    item3 = Item(body='Drive a motorcycle on the Cteat Wall of China',
                 author=user)
    item4 = Item(body='Sit on the Cteat Egyptian Pyramids',
                 done=True,
                 author=user)
    db.session.add_all([item, item2, item3, item4])
    db.session.commit()

    return jsonify(username=username,
                   password=password,
                   message='Generate success.')
Exemplo n.º 2
0
def register():
    # generate a random account for demo use
    username = fake.user_name()
    # make sure the generated username was not in database
    while User.query.filter_by(username=username).first() is not None:
        username = fake.user_name()
    password = fake.word()
    user = User(username=username)
    user.set_password(password)
    db.session.add(user)
    db.session.commit()

    item = Item(body=_('Witness something truly majestic'), author=user)
    item2 = Item(body=_('Help a complete stranger'), author=user)
    item3 = Item(body=_('Drive a motorcycle on the Great Wall of China'),
                 author=user)
    item4 = Item(body=_('Sit on the Great Egyptian Pyramids'),
                 done=True,
                 author=user)
    db.session.add_all([item, item2, item3, item4])
    db.session.commit()

    return jsonify(username=username,
                   password=password,
                   message=_('Generate success.'))
Exemplo n.º 3
0
def register():
    # generate a random account for demo use
    username = fake.user_name()  # 调用fake实例的user_name()方法,生成一个虚拟用户名
    # make sure the generated username was not in database(确保生成的用户名不在数据库中)
    while User.query.filter_by(username=username).first() is not None:
        username = fake.user_name()
    password = fake.word()  # 生成一个虚拟的用户密码
    user = User(username=username)
    user.set_password(password)
    db.session.add(user)
    db.session.commit()
    # 添加几个待办条目作为示例
    item = Item(body=_('Witness something truly majestic'), author=user)
    item2 = Item(body=_('Help a complete stranger'), author=user)
    item3 = Item(body=_('Drive a motorcycle on the Great Wall of China'),
                 author=user)
    item4 = Item(body=_('Sit on the Great Egyptian Pyramids'),
                 done=True,
                 author=user)  # done=true 标记为完成状态
    db.session.add_all([item, item2, item3, item4])
    db.session.commit()
    # 使用jsonify()函数返回JSON格式的用户名、密码和提示消息。
    return jsonify(username=username,
                   password=password,
                   message=_('Generate success.'))
Exemplo n.º 4
0
def new_item():
    data = request.get_json()
    if data is None or data['body'].strip() == '':
        jsonify(message='Invalid item body.'), 400
    item = Item(body=data['body'], author=current_user._get_current_object())
    db.session.add(item)
    db.session.commit()
    return jsonify(html=render_template('_item.html', item=item), message='+1')
Exemplo n.º 5
0
def register():
    username = fake.user_name()
    while User.query.filter_by(username=username).first() is not None:
        username = fake.user_name()
    password = fake.word()
    user = User(username=username)
    user.generate_password(password)
    db.session.add(user)
    db.session.commit()

    item1 = Item(body=fake.word(), author=user)
    item2 = Item(body=fake.word(), author=user)
    item3 = Item(body=fake.word(), author=user)
    item4 = Item(body=fake.word(), author=user, done=True)
    db.session.add_all([item1, item2, item3, item4])
    db.session.commit()
    return jsonify(username=username, password=password, message='Generate success')
Exemplo n.º 6
0
    def setUp(self):
        app = create_app('testing')
        self.app_context = app.test_request_context()
        self.app_context.push()
        db.create_all()
        self.client = app.test_client()

        user = User(username='******')
        user.set_password('123')
        item = Item(body='Test Item', author=user)

        user2 = User(username='******')
        user2.set_password('123')
        item2 = Item(body='Test Item 2', author=user2)

        db.session.add_all([user, item, user2, item2])
        db.session.commit()
Exemplo n.º 7
0
def new_item():
    data = request.get_json()  # 获取输入
    if data is None or data['body'].strip(
    ) == '':  # 如果data的body内容去除首尾的空格后返回的结果是空的
        return jsonify(message='无效的内容.'), 400
    item = Item(body=data['body'], author=current_user._get_current_object())
    db.session.add(item)
    db.session.commit()
    return jsonify(html=render_template('_item.html', item=item), message='+1')
Exemplo n.º 8
0
 def post(self):
     """Create new item."""
     item = Item(body=get_item_body(), author=g.current_user)
     db.session.add(item)
     db.session.commit()
     response = jsonify(item_schema(item))
     response.status_code = 201
     response.headers['Location'] = url_for('.item', item_id=item.id, _external=True)
     return response
Exemplo n.º 9
0
def register():  # 注册页面
    # 生成一个一个随机账户作为测试用户
    username = fake.user_name()
    # 确定生成的虚拟用户不在数据库表中
    while User.query.filter_by(username=username).first() is not None:
        username = fake.user_name()
    password = fake.word()
    user = User(username=username)
    user.set_password(password)
    db.session.add(user)
    db.session.commit()
    item = Item(body='明天晚上吃烤肉!', author=user)
    item2 = Item(body='帮助一个不认识的人!', author=user)
    item3 = Item(body='今天晚上跑五公里!', author=user)
    item4 = Item(body='明天下午打篮球!', done=True, author=user)
    db.session.add_all([item, item2, item3, item4])
    db.session.commit()
    # 使用jsonify函数返回json格式的数据。

    return jsonify(username=username, password=password, message='生成成功!')
Exemplo n.º 10
0
    def setUp(self):
        app = create_app('testing')
        self.app_context = app.test_request_context()
        self.app_context.push()
        self.client = app.test_client()

        user = User(username='******')
        user.set_password('123')
        item1 = Item(body='Item 1', author=user)
        item2 = Item(body='Item 2', author=user)
        item3 = Item(body='Item 3', author=user)

        user2 = User(username='******')
        user2.set_password('123')
        item = Item(body='Item', author=user2)

        db.session.add_all(user, item1, item2, item3, user2, item)
        db.session.commit()

        self.client.post(url_for('auth.login'),
                         json=dict(username='******', password='******'))
Exemplo n.º 11
0
def register():
    username = fake.user_name()

    # 确保生成的随机用户名不重复
    while User.query.filter_by(username=username).first() is not None:
        username = fake.user_name()

    password = fake.word()
    user = User(username=username)
    user.set_password(password)
    db.session.add(user)
    db.session.commit()

    # 添加几条 todo item
    item1 = Item(body='学习 Flask', author=user)
    item2 = Item(body='学习 Python', author=user)
    item3 = Item(body='学习 Redis、RabbitMQ', author=user)
    db.session.add_all([item1, item2, item3])
    db.session.commit()

    return jsonify(username=username, password=password, message='成功。')
Exemplo n.º 12
0
def register():
    # 由于是demo, 用 faker 生成随机账户
    username = fake.user_name()
    # 为防止随机账户名跟数据库中的冲突, 循环检测
    while User.query.filter_by(username=username).first() is not None:
        username = fake.user_name()
    password = fake.word()

    # 创建随机用户到数据库
    user = User(username=username)
    user.set_password(password)
    db.session.add(user)
    db.session.commit()

    # 创建随机Todo Items到数据库
    item1 = Item(body='Witness something truly majestic', author=user)
    item2 = Item(body='Help a complete stranger', author=user)
    item3 = Item(body='Drive a motorcycle on the Great Wall of China', author=user)
    item4 = Item(body='Sit on the Great Egyptian Pyramids', done=True, author=user)
    db.session.add_all([item1, item2, item3, item4])
    db.session.commit()

    return jsonify(username=username, password=password, message='Generate success.')
Exemplo n.º 13
0
    def setUp(self):
        app = create_app('testing')
        self.app_context = app.test_request_context()
        self.app_context.push()
        db.create_all()
        self.client = app.test_client()

        user = User(username='******')
        user.set_password('123')
        item1 = Item(body='Item 1', author=user)
        item2 = Item(body='Item 2', author=user)
        item3 = Item(body='Item 3', author=user)

        user2 = User(username='******')
        user2.set_password('456')
        item = Item(body='Item', author=user2)

        db.session.add(user)
        db.session.add(user2)
        db.session.commit()
        # Log the test user in
        self.client.post(url_for('auth.login'),
                         json=dict(username='******', password='******'))
Exemplo n.º 14
0
    def test_get_items(self):
        user = User.query.get(1)
        item2 = Item(body='Test Item 2', author=user)
        item3 = Item(body='Test Item 3', author=user)
        item4 = Item(body='Test Item 4', author=user, done=True)
        db.session.commit()

        token = self.get_oauth_token()
        response = self.client.get(url_for('api_v1.items'),
                                   headers=self.set_auth_headers(token))
        data = response.get_json()
        self.assertEqual(response.status_code, 200)
        self.assertIn('self', data)
        self.assertIn('items', data)
        self.assertIn('prev', data)
        self.assertIn('next', data)
        self.assertEqual(data['count'], 4)
        # get active items
        response = self.client.get(url_for('api_v1.active_items'),
                                   headers=self.set_auth_headers(token))
        data = response.get_json()
        self.assertEqual(response.status_code, 200)
        self.assertIn('self', data)
        self.assertIn('items', data)
        self.assertIn('prev', data)
        self.assertIn('next', data)
        self.assertEqual(data['count'], 3)
        # get completed items
        response = self.client.get(url_for('api_v1.completed_items'),
                                   headers=self.set_auth_headers(token))
        data = response.get_json()
        self.assertEqual(response.status_code, 200)
        self.assertIn('self', data)
        self.assertIn('items', data)
        self.assertIn('prev', data)
        self.assertIn('next', data)
        self.assertEqual(data['count'], 1)
Exemplo n.º 15
0
def new_item():
    # 通过调用resquest对象的json属性的get_json()函数,获取解析后的JSON数据,通过字典的方式获取键值
    data = request.get_json()
    # 判断data变量的值是否为空或者data变量所对应的字典中,'body'键对用的值是否为空
    if data is None or data['body'].strip() == '':
        # 如果为空,返回JSON格式的错误提示消息,以及错误状态码400
        return jsonify(message=_('Invalid item body.')), 400
    # 如果不为空,则实例化Item模型类,并传入data['body']键对应的值,以及通过_get_current_object()方法获取的当前对象的信息作为参数
    # 存储到item对象中
    item = Item(body=data['body'], author=current_user._get_current_object())
    db.session.add(item)  # 将item对象以参数的形式提交到数据库会话中
    db.session.commit()  # 提交数据库会话到数据库中
    # 通过render_template()函数渲染_item_html模板,并将此模板以参数的形式传递给jsonify()函数,同时该函数中的message参数,用来表示新条目的数量
    # 最后返回JSON格式的模板以及新条目的数量
    return jsonify(html=render_template('_item.html', item=item), message='+1')
Exemplo n.º 16
0
 def post(self):
     """Create new item."""
     # 调用get_item_body()函数获取请求JSON中的body值,
     # 调用flask的全局变量g,获取当前用户
     # print('----', '断点3')
     item = Item(body=get_item_body(), author=g.current_user)
     # 将新创建的条目存储到数据库中
     db.session.add(item)
     db.session.commit()
     # 调用item_chema()模式函数,并传入item实例作为参数,返回按照预定模式创建的字典对象,
     # 调用jsonify()函数生成JSON数据,存储到response变量中
     response = jsonify(item_schema(item))
     response.status_code = 201  # 设置状态码201,表示已创建
     # 在响应头部的Location字段设置新创建条目的URL
     response.headers['Location'] = url_for('.item',
                                            item_id=item.id,
                                            _external=True)
     return response  # 返回新创建条目资源作为响应
Exemplo n.º 17
0
# -*- coding: utf-8 -*-
"""
    :author: Grey Li (李辉)
    :url: http://greyli.com
    :copyright: © 2018 Grey Li <*****@*****.**>
    :license: MIT, see LICENSE for more details.
"""
from todoism import create_app, db
from todoism.models import User, Item

app = create_app('testing')

with app.app_context():
    db.create_all()

    user = User(username='******')
    user.set_password('123')
    db.session.add(user)

    item1 = Item(body='test item 1')
    item2 = Item(body='test item 2')
    item3 = Item(body='test item 3')
    item4 = Item(body='test item 4', done=True)
    user.items = [item1, item2, item3, item4]

    db.session.commit()