def test_reference_list_all(self): author = 'test_author' title = 'test_title' summary = 'this is a test summary' cite = Citation(author=author, title=title, summary=summary) cite.save() res = self.check_status_code_with_admin('/reference/items/all') data = res.data.decode() self.assertIn(cite.author, data) self.assertIn(cite.title, data)
def ref_item_register(): kwargs = { 'status': 'success', 'task': 'write', 'type': 'reference', } try: author = request.json.get('author') title = request.json.get('title') year = request.json.get('year') url = request.json.get('url') summary = request.json.get('summary') Citation(author=author, title=title, year=year, url=url, summary=summary).save() response = jsonify(**kwargs) response.status_code = 201 except Exception as e: kwargs['status'] = 'error' kwargs['error'] = str(e) response = jsonify(**kwargs) response.status_code = 400 return response
def test_reference_item_view(self): author = 'test_author' title = 'test_title' summary = 'this is a test summary' cite = Citation(author=author, title=title, summary=summary) cite.save() id_ = cite.id path = f'/reference/item/{id_}' # check if accessible only if user is admin res = self.check_status_code_with_admin(path) data = res.data.decode() self.assertIn(author, data) self.assertIn(title, data) self.assertIn(summary, data)
def note(): updates = [ update.to_dict() for update in Update.query().order_by(Update.id.desc()).all() ] references = [ref.to_dict() for ref in Citation.query().all()] return render_template('note.html', updates=updates, references=references)
def test_reference_edit_view(self): cite = Citation(author='tester', title='test') cite.save() id_ = cite.id path = f'/reference/edit/{id_}' # check if accessible only if user is admin self.check_status_code_with_admin(path) target_title = 'test title' self.check_status_code_with_admin( path, method='PUT', author='tester', title=target_title, ) # check if data is updated cite_after = Citation.get(id_) self.assertEqual(cite_after.title, target_title)
def fetch_ref_items_all(): items = Citation.query().all() kwargs = { 'status': 'success', 'task': 'read-only', 'type': 'reference', } response = jsonify(data=[item.to_dict() for item in items], **kwargs) response.status_code = 200 return response
def ref_register(): form = CitationForm() if form.validate_on_submit(): author = form.author.data title = form.title.data year = form.year.data url = form.url.data summary = form.summary.data Citation(author=author, title=title, year=year, url=url, summary=summary).save() return redirect(url_for('base.note')) return render_template('citation_form.html', form=form)
def test_reference_register_view(self): self.check_status_code_with_admin('/reference/register') self.auth_login() res = self.client.post( '/reference/register', data=dict( author='tester', title='test title', year=1990 ) ) self.assertEqual(res.status_code, 302) cit = Citation.query().filter_by(author='tester').first() self.assertEqual(cit.title, 'test title') self.assertEqual(cit.year, 1990)
def fetch_ref_item(item_id): item = Citation.get(item_id) kwargs = { 'status': 'success', 'task': 'read-only', 'type': 'reference', } if item is None: data = {} else: data = item.to_dict() response = jsonify(data=data, **kwargs) response.status_code = 200 return response
def ref_edit(item_id): cite = Citation.get(item_id) form = CitationForm() if form.validate_on_submit(): cite.author = form.author.data cite.title = form.title.data cite.year = form.year.data cite.url = form.url.data cite.save() return redirect(url_for('base.note')) # set initial values form.author.data = cite.author form.title.data = cite.title form.year.data = cite.year form.url.data = cite.url return render_template('citation_form.html', form=form)
def note(): kwargs = { 'status': 'success', 'task': 'read-only', 'type': 'note', } updates = [ update.to_dict(date=True) for update in Update.query().order_by(Update.id.desc()).all() ] references = [ref.to_dict() for ref in Citation.query().all()] data = {'updates': updates, 'references': references} response = jsonify(data=data, **kwargs) response.status_code = 200 return response
def test_save_and_get_data(self): author = 'tester' title = 'test content' invalid_year = 1980 with self.assertRaises(ValueError): cite = Citation(author='invalid', title='invalid', year=invalid_year) year = 1994 cite = Citation(author=author, title=title, year=year) cite.save() data = Citation.query().first() self.assertEqual(data.id, cite.id) self.assertEqual(data.author, author)
def ref_item_edit(item_id): cite = Citation.get(item_id) kwargs = { 'status': 'success', 'task': 'edit', 'type': 'reference', } if cite is None: kwargs['status'] = 'error' kwargs['message'] = 'item not found' response = jsonify(**kwargs) response.status_code = 400 return response try: author = request.json.get('author') if author is not None: cite.author = author title = request.json.get('title') if title is not None: cite.title = title year = request.json.get('year') if year is not None: cite.year = year url = request.json.get('url') if url is not None: cite.url = url cite.save() response = jsonify(**kwargs) response.status_code = 201 except Exception as e: kwargs['status'] = 'error' kwargs['error'] = str(e) response = jsonify(**kwargs) response.status_code = 400 return response
def list_ref_item(item_id): item = Citation.get(item_id) if item is None: return redirect(url_for('base.note')) return render_template('citation_item.html', item=item)
def list_ref_items_all(): items = Citation.query().all() return render_template('citation_items_all.html', items=items)
def __call__(self): author = fake.name() title = fake.text(max_nb_chars=64) year = fake.random_int(min=1995, max=2019, step=1) url = fake.url() return Citation(author=author, title=title, year=year, url=url)