Exemplo n.º 1
0
def login():
    """通过将用户id添加到会话来登录注册用户。"""
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        db = get_db()
        error = None
        user = db.execute('SELECT * FROM user WHERE username = ?',
                          (username, )).fetchone()  # 查询数据保存在变量中

        if user is None:
            error = 'Incorrect username.'
        elif not check_password_hash(user['password'],
                                     password):  # 哈希 提交的密码 并比较哈希值
            error = 'Incorrect password.'

        # session 是一个 dict ,它用于储存横跨请求的值。当验证 成功后,用户的 id 被储存于一个新的会话中。会话数据被储存到一个 向浏览器发送的 cookie 中,在后继请求中,浏览器会返回它。 Flask 会安全对数据进行 签名 以防数据被篡改。
        if error is None:
            session.clear()
            session['user_id'] = user['id']
            return redirect(url_for('index'))

        flash(error)

    return render_template('/login.html')
Exemplo n.º 2
0
def test_image_repository_get_not_found(client):
    col: Collection = get_db().images

    with pytest.raises(NoImageFound):
        image2 = ImageRepository.get('test_3')

    close_db()
Exemplo n.º 3
0
class ImageRepository:
    db: Database = get_db()
    collection: Collection = db.images
    image_schema = ImageSchema()

    @staticmethod
    def create(filename: str, extension: str) -> Image:
        image: Image = ImageRepository.image_schema.load({
            'filename': filename,
            'extension': extension,
        })
        ImageRepository.collection.insert_one(image.as_dict())
        return image

    @staticmethod
    def get(image_id: str) -> Image:
        res: dict = ImageRepository.collection.find_one({'_id': image_id})
        if res is None:
            raise NoImageFound(f"Image with id: <{image_id}> not found.")
        return ImageRepository.image_schema.load(res)

    @staticmethod
    def update(image: Image) -> bool:
        # noinspection PyProtectedMember
        res = ImageRepository.collection.update_one({'_id': image._id}, {'$set': image.as_dict()})
        return res.modified_count == res.matched_count

    @staticmethod
    def delete(image_id: str) -> bool:
        # noinspection PyProtectedMember
        res = ImageRepository.collection.delete_one({'_id': image_id})
        return res.deleted_count == 1
Exemplo n.º 4
0
def test_image_repository_delete(client):
    col: Collection = get_db().images

    image = ImageRepository.create('test_4', 'test_4')
    db_filter = {'_id': image._id}

    assert ImageRepository.delete(image._id)

    s_image = col.find_one(db_filter)
    assert s_image is None

    close_db()
Exemplo n.º 5
0
def result():
    db = get_db()
    # page = 1
    # pagesize = 10
    # if request.method == 'POST':
    #     data = request.form['dataArray']
    #     print(data)
    #     page = data['page']
    #     pagesize = data['pagesize']
    # # shorts = query_db('SELECT id, n_star, short, sentiment FROM douban').paginate(page=page,per_page=pagesize)
    shorts = query_db('SELECT id, n_star, short, sentiment FROM douban')
    return render_template('/douban.html', shorts=shorts)
Exemplo n.º 6
0
def test_image_repository_create(client):
    col: Collection = get_db().images

    image = ImageRepository.create('test_1', 'test_1')
    db_filter = {'_id': image._id}
    s_image = col.find_one(db_filter)

    assert image.filename == s_image['filename']
    assert image.extension == s_image['extension']
    assert image.created == s_image['created']

    assert col.delete_one(db_filter).deleted_count == 1

    close_db()
Exemplo n.º 7
0
def test_image_repository_update(client):
    col: Collection = get_db().images

    image = ImageRepository.create('test_3', 'test_3')
    db_filter = {'_id': image._id}
    image.filename = 'new_test_3'

    assert ImageRepository.update(image)

    s_image = col.find_one(db_filter)
    assert image.filename == s_image['filename'] == 'new_test_3'

    assert col.delete_one(db_filter).deleted_count == 1

    close_db()
Exemplo n.º 8
0
def test_image_repository_get(client):
    col: Collection = get_db().images

    image = ImageRepository.create('test_2', 'test_2')

    s_image = col.find_one({'_id': image._id})
    image2 = ImageRepository.get(image._id)

    assert image.filename == s_image['filename'] == image2.filename
    assert image.extension == s_image['extension'] == image2.extension
    assert image.created == s_image['created'] == image2.created

    assert col.delete_one({'_id': image._id}).deleted_count == 1

    close_db()
Exemplo n.º 9
0
from datetime import datetime as dt, timezone as tz

from app.models import get_db

db = get_db()


def prepare():
    photos = [{
        '_id':
        '5fd27878a7a76140f9cedb60',
        'filename':
        'coruh-river-3003816_960_720',
        'extension':
        '.webp',
        'created':
        str(dt.utcnow().replace(microsecond=0).replace(
            tzinfo=tz.utc).isoformat())
    }, {
        '_id':
        '5fd2794ff7ccd7d7e7b78d51',
        'filename':
        'forest-floor-4700814_960_720',
        'extension':
        '.jpg',
        'created':
        str(dt.utcnow().replace(microsecond=0).replace(
            tzinfo=tz.utc).isoformat())
    }, {
        '_id':
        '5fd279882a6be6c7fa914418',
Exemplo n.º 10
0
def check_thumbnail(task_id):
    if task_id is not None:
        from app.models import get_db
        db = get_db()
        return db.celery_taskmeta.find_one({'_id': task_id})