def delete_articles(id): article = Article.query.get(id) with db_session() as session: session.delete(article) return redirect(url_for('index_articles'))
def delete_authors(id): author = Author.query.get(id) with db_session() as session: session.delete(author) return redirect(url_for('index_authors'))
def update_articles(): values = request.form.to_dict(flat=False) if request.method == 'POST': article_id = values['id'][0] # get the article to be updated article = Article.query.get(article_id) article.title = values['title'][0] article.content = values['content'][0] article.regions = [ Region.query.get(region) for region in values['regions'] ] # print('***********regions:', regions) article.authors = [ Author.query.get(author) for author in values['authors'] ] # print('***********authors:', authors) with db_session() as session: session.commit() return redirect(url_for('index_articles'))
def delete_regions(id): region = Region.query.get(id) with db_session() as session: session.delete(region) return redirect(url_for('index_regions'))
def insert_authors(): values = request.form.to_dict(flat=False) if request.method == 'POST': first_name = values['first_name'][0] last_name = values['last_name'][0] author = Author(first_name=first_name, last_name=last_name) with db_session() as session: session.add(author) return redirect(url_for('index_authors'))
def insert_regions(): values = request.form.to_dict(flat=False) if request.method == 'POST': code = values['code'][0] name = values['name'][0] region = Region(code=code, name=name) with db_session() as session: session.add(region) return redirect(url_for('index_regions'))
def update_authors(): values = request.form.to_dict(flat=False) if request.method == 'POST': author_id = values['id'][0] # get the article to be updated author = Author.query.get(author_id) author.first_name = values['first_name'][0] author.last_name = values['last_name'][0] with db_session() as session: session.commit() return redirect(url_for('index_authors'))
def update_regions(): values = request.form.to_dict(flat=False) if request.method == 'POST': region_id = values['id'][0] # get the article to be updated region = Region.query.get(region_id) region.code = values['code'][0] region.name = values['name'][0] with db_session() as session: session.commit() return redirect(url_for('index_regions'))
def insert_articles(): values = request.form.to_dict(flat=False) if request.method == 'POST': title = values['title'][0] content = values['content'][0] regions = [Region.query.get(region) for region in values['regions']] # print('***********regions:', regions) authors = [Author.query.get(author) for author in values['authors']] # print('***********authors:', authors) article = Article(title=title, content=content, regions=regions, authors=authors) with db_session() as session: session.add(article) return redirect(url_for('index_articles'))
from techtest.models.article import Article from techtest.models.region import Region from techtest.models.author import Author from techtest.connector import engine, BaseModel, db_session BaseModel.metadata.create_all(engine) with db_session() as session: au = Region(code='AU', name='Australia') uk = Region(code='UK', name='United Kingdom') us = Region(code='US', name='United States of America') author1 = Author(first_name='Agatha', last_name='Christie') author2 = Author(first_name='J.R.R', last_name='Tolkien') session.add_all([ au, uk, us, Article(title='Post 1', content='This is a post body', regions=[au, uk], authors=[author1]), Article(title='Post 2', content='This is the second post body', regions=[au, us], authors=[author2, author1]) ])