Example #1
0
    def setUp(self):
        app = create_app('testing')
        self.context = app.test_request_context()
        self.context.push()
        self.client = app.test_client()
        self.runner = app.test_cli_runner()

        db.create_all()
        Role.init_role()

        admin_user = User(email='*****@*****.**',
                          name='Admin',
                          username='******',
                          confirmed=True)
        admin_user.set_password('123')
        normal_user = User(email='*****@*****.**',
                           name='Normal User',
                           username='******',
                           confirmed=True)
        normal_user.set_password('123')
        unconfirmed_user = User(email='*****@*****.**',
                                name='unconfirmed',
                                username='******',
                                confirmed=False)
        unconfirmed_user.set_password('123')
        locked_user = User(email='*****@*****.**',
                           name='Locked User',
                           username='******',
                           confirmed=True,
                           locked=True)
        locked_user.set_password('123')
        locked_user.lock()
        blocked_user = User(email='*****@*****.**',
                            name='Blocked User',
                            username='******',
                            confirmed=True,
                            active=False)
        blocked_user.set_password('123')

        photo = Photo(filename='test.jpg',
                      filename_s='test_s.jpg',
                      filename_m='test_m.jpg',
                      description='Photo 1',
                      author=admin_user)
        photo2 = Photo(filename='test2.jpg',
                       filename_s='test_s2.jpg',
                       filename_m='test_m2.jpg',
                       description='Photo 2',
                       author=normal_user)

        comment = Comment(body='test comment body',
                          photo=photo,
                          author=normal_user)
        tag = Tag(name='test tag')
        photo.tags.append(tag)
        db.session.add_all([
            admin_user, normal_user, unconfirmed_user, locked_user,
            blocked_user
        ])
        db.session.commit()
Example #2
0
def fake_photo(count=30):
    # 生成随机图片,并将随机图片名存库
    upload_path = current_app.config['ALBUMY_UPLOAD_PATH']
    for i in range(count):
        print(i)
        filename = f'random_{i}.jpg'
        r = lambda: random.randint(128, 255)
        img = Image.new(mode='RGB', size=(800, 800), color=(r(), r(), r()))
        img.save(os.path.join(upload_path, filename))

        photo = Photo(description=fake.text(),
                      filename=filename,
                      filename_m=filename,
                      filename_s=filename,
                      author=User.query.get(
                          random.randint(1, User.query.count())),
                      timestamp=fake.date_time_this_year())

        # 生成假tag
        for j in range(random.randint(1, 5)):
            tag = Tag.query.get(random.randint(1, Tag.query.count()))
            if tag not in photo.tags:
                photo.tags.append(tag)

        db.session.add(photo)
    db.session.commit()
Example #3
0
def upload():
    if request.method == 'POST' and 'file' in request.files:
        f = request.files.get('file')  # 获取文件对象

        # Dropzone.js 是通过 Ajax 发送请求的,
        # 每个文件一个请求。为此,无法返回重定向响应,
        # 使用 flash() 函数或是操作 session。
        # 假设我们使用一个 check_image() 函数来检查文件的有效性:
        # if not check_image(f):
        #     return '无效的图片', 400
        # 客户端会把接收到的错误响应显示出来。

        filename = random_filename(f.filename)  # 生成随机文件名
        f.save(os.path.join(current_app.config['ALBUMY_UPLOAD_PATH'],
                            filename))  # 保存文件
        filename_s = resize_image(f, filename, 400)
        filename_m = resize_image(f, filename, 800)

        photo = Photo(  # 创建图片记录
            filename=filename,
            filename_s=filename_s,
            filename_m=filename_m,
            author=current_user._get_current_object()  # 获取真实的用户对象,而不是代理的用户对象
        )
        db.session.add(photo)
        db.session.commit()
    return render_template('main/upload.html')
Example #4
0
def upload():
	"""
	上传图片
	"""
	logger.info('url = ' + str(request.url))
	if request.method == "POST" and "file" in request.files:
		# 文件对象
		f = request.files.get("file")
		# 文件名
		filename = rename_image(f.filename)
		# 保存
		f.save(os.path.join(current_app.config["ALBUMY_UPLOAD_PATH"], filename))
		# 小图
		# 在resize_image函数中,会保存图片
		filename_s = resize_image(
			f, filename, current_app.config["ALBUMY_PHOTO_SIZE"]["small"]
		)
		# 中图
		filename_m = resize_image(
			f, filename, current_app.config["ALBUMY_PHOTO_SIZE"]["medium"]
		)
		# 保存图片对象
		photo = Photo(
			filename=filename,
			filename_s=filename_s,
			filename_m=filename_m,
			author=current_user._get_current_object(),
		)
		logger.info('上传文件,{},{},{}'.format(filename, filename_m, filename_s))
		# 提交
		db.session.add(photo)
		db.session.commit()
	return render_template("main/upload.html")
Example #5
0
def upload():
    if request.method == 'POST' and 'file' in request.files:
        f = request.files.get('file')
        filename = rename_image(f.filename)
        f.save(os.path.join(current_app.config['ALBUMY_UPLOAD_PATH'], filename))
        filename_s = resize_image(f, filename, current_app.config['ALBUMY_PHOTO_SIZE']['small'])
        filename_m = resize_image(f, filename, current_app.config['ALBUMY_PHOTO_SIZE']['medium'])
        photo = Photo(filename=filename, filename_s=filename_s, filename_m=filename_m,
                      author=current_user._get_current_object())
        db.session.add(photo)
        db.session.commit()
    return render_template('main/upload.html')
    def test_photo_prev(self):
        user = User.query.get(1)
        photo2 = Photo(filename='test.jpg',
                       filename_s='test_s.jpg',
                       filename_m='test_m.jpg',
                       description='Photo 2',
                       author=user)
        photo3 = Photo(filename='test.jpg',
                       filename_s='test_s.jpg',
                       filename_m='test_m.jpg',
                       description='Photo 3',
                       author=user)
        photo4 = Photo(filename='test.jpg',
                       filename_s='test_s.jpg',
                       filename_m='test_m.jpg',
                       description='Photo 4',
                       author=user)
        db.session.add_all([photo2, photo3, photo4])
        db.session.commit()

        response = self.client.get(url_for('main.photo_previous', photo_id=1),
                                   follow_redirects=True)
        data = response.get_data(as_text=True)
        self.assertIn('Photo 2', data)

        response = self.client.get(url_for('main.photo_previous', photo_id=3),
                                   follow_redirects=True)
        data = response.get_data(as_text=True)
        self.assertIn('Photo 3', data)

        response = self.client.get(url_for('main.photo_previous', photo_id=4),
                                   follow_redirects=True)
        data = response.get_data(as_text=True)
        self.assertIn('Photo 4', data)

        response = self.client.get(url_for('main.photo_previous', photo_id=5),
                                   follow_redirects=True)
        data = response.get_data(as_text=True)
        self.assertIn('This is already the first one.', data)
Example #7
0
    def test_photo_next(self):
        user = User.query.get(1)
        photo3 = Photo(filename='test.jpg',
                       filename_s='test_s.jpg',
                       filename_m='test_m.jpg',
                       description='Photo 3',
                       author=user)
        photo4 = Photo(filename='test.jpg',
                       filename_s='test_s.jpg',
                       filename_m='test_m.jpg',
                       description='Photo 4',
                       author=user)
        photo5 = Photo(filename='test.jpg',
                       filename_s='test_s.jpg',
                       filename_m='test_m.jpg',
                       description='Photo 5',
                       author=user)
        db.session.add_all([photo3, photo4, photo5])
        db.session.commit()

        response = self.client.get(url_for('main.photo_next', photo_id=5),
                                   follow_redirects=True)
        data = response.get_data(as_text=True)
        self.assertIn('Photo 4', data)

        response = self.client.get(url_for('main.photo_next', photo_id=4),
                                   follow_redirects=True)
        data = response.get_data(as_text=True)
        self.assertIn('Photo 3', data)

        response = self.client.get(url_for('main.photo_next', photo_id=3),
                                   follow_redirects=True)
        data = response.get_data(as_text=True)
        self.assertIn('Photo 1', data)

        response = self.client.get(url_for('main.photo_next', photo_id=1),
                                   follow_redirects=True)
        data = response.get_data(as_text=True)
        self.assertIn('已经是最后一个了', data)
Example #8
0
def upload2():
    form = PhotoForm()
    if form.validate_on_submit():
        filename_s = form.url_s.data
        filename_m = form.url_m.data
        filename = form.url_l.data

        photo = Photo(filename=filename,
                      filename_s=filename_s,
                      filename_m=filename_m,
                      author=current_user._get_current_object())
        db.session.add(photo)
        db.session.commit()
    return render_template('main/upload2.html', form=form)
Example #9
0
def upload():
    if request.method == "POST" and "file" in request.files:
        f = request.files.get("file")
        filename = rename_image(f.filename)
        f.save(os.path.join(current_app.config["ALBUMY_UPLOAD_PATH"],
                            filename))
        filename_s = resize_image(
            f, filename, current_app.config["ALBUMY_PHOTO_SIZE"]["small"])
        filename_m = resize_image(
            f, filename, current_app.config["ALBUMY_PHOTO_SIZE"]["medium"])
        photo = Photo(filename=filename,
                      filename_s=filename_s,
                      filename_m=filename_m,
                      author=current_user._get_current_object())
        db.session.add(photo)
        db.session.commit()
    return render_template("main/upload.html")
Example #10
0
def fake_photo(count=30):
	"""
	照片
	"""
	# 上传路径,从配置文件中读取
	upload_path = current_app.config["ALBUMY_UPLOAD_PATH"]
	for i in range(count):
		print(i)

		filename = "random_%d.jpg" % i
		# 随机颜色
		r = lambda: random.randint(128, 255)
		# 根据颜色得到图片对象
		img = Image.new(mode="RGB", size=(800, 800), color=(r(), r(), r()))
		# 保存图片
		img.save(os.path.join(upload_path, filename))

		# 创建Photo对象,并保存到数据库
		photo = Photo(
			# 随机文字
			description=fake.text(),
			filename=filename,
			filename_m=filename,
			filename_s=filename,
			# 随机User对象
			author=User.query.get(random.randint(1, User.query.count())),
			# 时间
			timestamp=fake.date_time_this_year(),
		)

		# 给图片添加随机1-5个Tag
		for j in range(random.randint(1, 5)):
			tag = Tag.query.get(random.randint(1, Tag.query.count()))
			if tag not in photo.tags:
				photo.tags.append(tag)

		db.session.add(photo)
	db.session.commit()
    def test_collect(self):
        photo = Photo(filename='test.jpg',
                      filename_s='test_s.jpg',
                      filename_m='test_m.jpg',
                      description='Photo 3',
                      author=User.query.get(2))
        db.session.add(photo)
        db.session.commit()
        self.assertEqual(Photo.query.get(3).collectors, [])

        self.login()
        response = self.client.post(url_for('main.collect', photo_id=3),
                                    follow_redirects=True)
        data = response.get_data(as_text=True)
        self.assertIn('Photo collected.', data)

        self.assertEqual(
            Photo.query.get(3).collectors[0].collector.name, 'Normal User')

        response = self.client.post(url_for('main.collect', photo_id=3),
                                    follow_redirects=True)
        data = response.get_data(as_text=True)
        self.assertIn('Already collected.', data)
Example #12
0
def upload():
    if request.method == 'POST' and 'file' in request.files:
        try:
            f = request.files.get('file')
            filename = random_filename(f.filename)
            # save方法应该放在前面,因为resize_image会将文件流读取
            f.save(os.path.join(current_app.config['ALBUMY_UPLOAD_PATH'], filename))
            # PIL.Image.open方法使用fp.seek(0)可以从头读取文件流
            filename_s = resize_image(f, filename, current_app.config['ALBUMY_PHOTO_SIZE']['small'])
            filename_m = resize_image(f, filename, current_app.config['ALBUMY_PHOTO_SIZE']['medium'])

            photo = Photo(
                filename=filename,
                filename_s=filename_s,
                filename_m=filename_m,
                author=current_user._get_current_object()
            )
            db.session.add(photo)
            db.session.commit()
            return 'Success', 200
        except Exception as e:
            # print(e)
            return 'Invalid image or server error', 415
    return render_template('main/upload.html')
Example #13
0
    def setUp(self) -> None:
        app = create_app('testing')
        self.context = app.test_request_context()
        self.context.push()
        self.client = app.test_client()
        self.runner = app.test_cli_runner()

        db.create_all()
        Role.init_role()

        admin_user = User(email='*****@*****.**',
                          name='Admin',
                          username='******',
                          confirmed=True)
        admin_user.password = '******'

        normal_user = User(email='*****@*****.**',
                           name='Normal',
                           username='******',
                           confirmed=True)
        normal_user.password = '******'

        unconfirm_user = User(email='*****@*****.**',
                              name='Unconfirm',
                              username='******',
                              confirmed=False)
        unconfirm_user.password = '******'

        locked_user = User(email='*****@*****.**',
                           name='Locked',
                           username='******',
                           confirmed=True,
                           locked=True)
        locked_role = Role.query.filter_by(name='Locked').first()
        locked_user.role = locked_role
        locked_user.password = '******'

        block_user = User(email='*****@*****.**',
                          name='Block',
                          username='******',
                          confirmed=True,
                          active=False)
        block_user.password = '******'

        photo = Photo(filename='test.jpg',
                      filename_s='test_s.jpg',
                      filename_m='test_m.jpg',
                      author=admin_user,
                      description='Photo 1')
        photo2 = Photo(filename='test2.jpg',
                       filename_s='test2_s.jpg',
                       filename_m='test2_m.jpg',
                       author=normal_user,
                       description='Photo 2')

        comment = Comment(body='test comment body',
                          photo=photo,
                          author=normal_user)
        tag = Tag(name='test tag')
        photo.tags.append(tag)
        db.session.add_all([
            admin_user, normal_user, unconfirm_user, locked_user, block_user,
            photo, photo2, comment, tag
        ])
        db.session.commit()