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()
def new_tag(photo_id): """ 新标签 :param photo_id: 图片id """ logger.info('url = ' + str(request.url)) photo = Photo.query.get_or_404(photo_id) if current_user != photo.author and not current_user.can("MODERATE"): abort(403) form = TagForm() if form.validate_on_submit(): # 添加新标签时,如果有多个标签,会以空格隔开 for name in form.tag.data.split(): # 查询,判断标签是否已经存在 tag = Tag.query.filter_by(name=name).first() # 如果不存在,则先创建标签 if tag is None: tag = Tag(name=name) logger.debug('用户:{}添加了新标签:{}'.format(current_user.username, name)) db.session.add(tag) db.session.commit() # 将标签加入到photo的标签中 if tag not in photo.tags: photo.tags.append(tag) db.session.commit() flash("标签添加成功!", "success") flash_errors(form) return redirect(url_for(".show_photo", photo_id=photo_id))
def fake_tag(count=20): for i in range(count): tag = Tag(name=fake.word()) db.session.add(tag) try: db.session.commit() except IntegrityError: db.session.rollback()
def test_delete_tag(self): tag = Tag() db.session.add(tag) db.session.commit() self.assertIsNotNone(Tag.query.get(1)) response = self.client.post(url_for('admin.delete_tag', tag_id=1), follow_redirects=True) data = response.get_data(as_text=True) self.assertIn('Tag deleted.', data) self.assertEqual(Tag.query.get(1), None)
def test_delete_tag(self): photo = Photo.query.get(2) tag = Tag(name='test') photo.tags.append(tag) db.session.commit() self.login() response = self.client.post(url_for('main.delete_tag', photo_id=2, tag_id=2), follow_redirects=True) data = response.get_data(as_text=True) self.assertIn('Tag deleted.', data) self.assertEqual(photo.tags, []) self.assertIsNone(Tag.query.get(2))
def new_tag(photo_id): photo = Photo.query.get_or_404(photo_id) if current_user != photo.author: abort(403) form = TagForm() if form.validate_on_submit(): for name in form.tag.data.split(): tag = Tag.query.filter_by(name=name).first() if tag is None: tag = Tag(name=name) db.session.add(tag) if tag not in photo.tags: photo.tags.append(tag) db.session.commit() flash_errors(form) return redirect(url_for('.show_photo', photo_id=photo_id))
def new_tag(photo_id): photo = Photo.query.get_or_404(photo_id) if current_user != photo.author and not current_user.can('MODERATE'): abort(403) form = TagForm() if form.validate_on_submit(): for name in form.tag.data.split(): tag = Tag.query.filter_by(name=name).first() if tag is None: tag = Tag(name=name) photo.tags.append(tag) db.session.commit() flash('Tag added', 'success') flash_errors(form) return redirect(url_for('main.show_photo', photo_id=photo_id))
def new_tag(photo_id): photo = Photo.query.get_or_404(photo_id) if current_user != photo.author: abort(403) tag_form = TagForm() if tag_form.validate_on_submit(): tag_names = tag_form.tag.data.split() for tag_name in tag_names: tag = Tag.query.filter_by(name=tag_name).first() if not tag: tag = Tag(name=tag_name) db.session.add(tag) # db.session.commit() if tag not in photo.tags: photo.tags.append(tag) db.session.commit() flash("Tag added.", 'success') flash_errors(tag_form) return redirect(url_for('.show_photo', photo_id=photo_id))
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()