def delete(ctx, uid): """Delete an article""" article_service = ArticleService(ctx.obj['table_name']) status = article_service.delete_article(uid) if status: click.echo('Successful article delete') else: click.echo(f'Error: the article with uid {uid} doesnt exists')
def create(ctx, name, brand, price, content): """Create a new article""" article = Article(name, brand, price, content, 0) article_service = ArticleService(ctx.obj['table_name']) status = article_service.create_article(article) if status: click.echo(f'Successful register of {article}') else: click.echo(f'Error: the article {article} already exists')
def get_ranking_list(cls, request): """ 展示首页排行榜 """ response = dict( traffic_list=ArticleService().get_traffic_list(), latest_list=ArticleService().get_latest_articles(), recommended_list=ArticleService().get_recommended_list()) return render(request, 'index/ranking_list.html', response)
def list(ctx): """List all the articles""" article_service = ArticleService(ctx.obj['table_name']) articles = article_service.articles_list() click.echo( ' uid | name | brand | price | content | sales' ) click.echo('*' * 80) for article in articles: click.echo( f"{article['uid']} | {article['name']} | {article['brand']} | {article['price']} | {article['content']} | {article['sales']}" )
def buy(ctx, uid, quantity): """Buy an article""" if quantity > 0: article_service = ArticleService(ctx.obj['table_name']) article = article_service.get_article(uid) article.increment_sales(quantity) status = article_service.update_article(article) if status: click.echo('Article bought') else: click.echo('Error: something went wrong') elif quantity < 0: click.echo('Invalid quantity')
def update(ctx, uid): """Update an article""" article_service = ArticleService(ctx.obj['table_name']) article = article_service.get_article(uid) if article: article = _update_article_flow(article) status = article_service.update_article(article) if status: click.echo('Successful article update') else: click.echo('Error: something went wrong') else: click.echo(f'Error: the article with uid {uid} doesnt exists')
def get_latest_article(cls, request): """ 展示最新的 6 条记录 """ response = dict(articles=ArticleService().get_latest_articles()) return render(request, 'article/blog.html', response)
def get_correlation(cls, request): response = dict(correlations=ArticleService().get_correlation_list()) return render(request, 'article/correlation.html', response)
def get_by_id(cls, request, article_id): article = ArticleService().get_by_id(article_id) return render(request, 'article/article_content.html', dict(article=article))