def business_reviews(bid): """ Provides logic to write reviews and retrieve a businesses reviews """ business_found = Business.get_business_by_id(business_list, bid) if not business_found: return not_found('That business was not found in our server please \ check again later') business = Business(business_found['Name'], business_found['Description'], business_found['Category'], business_found['Location'], business_found['Address'], business_found['Owner']) if request.method == 'POST': name = str(request.data.get('Email', '')) comment = str(request.data.get('Comment', '')) if name and comment: status = business.write_review(business_list, comment, name) response = jsonify({ "message":status['message'] }) response.status_code = 200 return response else: return bad_request("Invalid data provided") else: # get reviews reviews = business_found['Reviews'] if reviews: response = jsonify({ "Reviews":business_found['Reviews'] }) response.status_code = 200 return response else: return not_found('No reviews for this business')
def get_comment(id): try: comment = g.db.get_comment(id) except ModelNotFound as e: return not_found(e.str(e)) else: return jsonify(comment.get_attrs())
def get_article_comments(id): article = Article.query.get(id) if not article: return not_found(_('The article not exists')) page = request.args.get('page', default=1, type=int) pagination = article.comments.order_by(Comment.id.asc())\ .paginate(page, per_page=current_app.config['COMMENTS_PER_PAGE']) comments = pagination.items prev = None if pagination.has_prev: prev = url_for('api.get_article_comments', id=id, page=page - 1, _external=True) next = None if pagination.has_next: next = url_for('api.get_article_comments', id=id, page=page + 1, _external=True) return jsonify({ 'comments': [comment.to_dict() for comment in comments], 'prev': prev, 'next': next, 'count': pagination.total })
def edit_article(id): article = Article.query.get(id) if not article: return not_found(_('The article not exists')) if g.current_user != article.author and \ not g.current_user.can(Permission.MODERATE_ARTICLE): return forbidden('permission denied') article = Article.from_dict(request.json) db.session.add(article) db.session.commit() return jsonify(article.to_dict())
def retrieve_businesses(): """ Retrieves all the business """ businesses = business_list if businesses: response =jsonify(businesses) response.status_code = 200 return response else: return not_found("There are no businesss in our database")
def get_ticket(id): try: ticket = Ticket.from_db(g.db.get_ticket(id)) except ModelNotFound as e: return not_found(str(e)) ticket_attrs = ticket.get_attrs() ticket_attrs.update({ 'comments': [comment.get_attrs() for comment in g.db.get_comments(id)] }) return jsonify(ticket_attrs)
def new_article_comments(id): article = Article.query.get(id) if not article: return not_found(_('The article not exists')) comment = Comment.from_dict(request.json) comment.author = g.current_user comment.article = article db.session.add(comment) db.session.commit() return (jsonify(comment.to_dict()), 201, { 'Location': url_for('api.get_comment', id=comment.id, _external=True) })
def new_ticket_comment(id): try: ticket = Ticket.from_db(g.db.get_ticket(id)) except ModelNotFound as e: return not_found(str(e)) else: if ticket.state == TicketState.CLOSED: return ticket_closed(ticket_id=id) else: comment = Comment.from_json(request.get_json(), ticket_id=id) g.db.add_comment(comment) return jsonify(comment.get_attrs()), 201, { 'Location': url_for('api.get_comment', id=comment.id, _external=True) }
def manipulate_business(current_user, bid, **kwargs): """ Enables edit, and deletion of a business """ # if type(bid) != int: # return bad_request("Wrong data type") #get business by id business_found = Business.get_business_by_id(business_list, bid) if not business_found: return not_found('That business was not found in our server please check again later') business = Business(business_found['Name'], business_found['Description'], business_found['Category'], business_found['Location'], business_found['Address'], business_found['Owner']) if request.method == 'DELETE': Business.delete_business(business_list, bid) return{ 'Message': "business {} has been successfully\ deleted".format(business_found['Name']) }, 200 elif request.method == 'GET': response = jsonify(business_found) response.status_code = 200 return response else: # update business name = str(request.data.get('Name', '')) description = str(request.data.get('Description', '')) category = str(request.data.get('Category', '')) location = str(request.data.get('Location', '')) address = str(request.data.get('Address', '')) keys = {} if name: keys['Name'] = name if location: keys['Location'] = location if category: keys['Category'] = category if description: keys['Description'] = description if address: keys['Address'] = address for key, value in keys.items(): kwargs = {key:value} business.edit_business(business_list, **kwargs) response = jsonify({"message":"Business edited successfuly"}) response.status_code = 200 return response
def delete_business_review(current_user,bid, rid): """ Provides logic to write reviews and retrieve a businesses reviews """ business_found = Business.get_business_by_id(business_list, bid) if not business_found: return not_found('That business was not found in our server please \ check again later') business = Business(business_found['Name'], business_found['Description'], business_found['Category'], business_found['Location'], business_found['Address'], business_found['Owner']) status = business.delete_review(business_list, rid) response = jsonify({ 'message':status['message'] }) response.status_code = 200 return response
def get_categories_articles(id): category = Category.query.get(id) if not category: return not_found(_('The category not exists')) page = request.args.get('page', default=1, type=int) pagination = category.articles.order_by(Article.create_datetime.desc())\ .paginate(page=page, per_page=current_app.config['ARTICLES_PER_PAGE']) articles = pagination.items prev = None if pagination.has_prev: prev = url_for('api.get_categories_articles', id=id, page=page-1, _external=True) next = None if pagination.has_next: next = url_for('api.get_categories_articles', id=id, page=page+1, _external=True) return jsonify({ 'articles': [article.to_dict() for article in articles], 'prev': prev, 'next': next, 'count': pagination.total })
def get_user_articles(id): user = User.query.get(id) if not user: return not_found(_('The user not exists')) page = request.args.get('page', default=1, type=int) pagination = user.articles.order_by(Article.id.desc())\ .paginate(page, per_page=current_app.config['ARTICLES_PER_PAGE']) articles = pagination.items prev = None if pagination.has_prev: prev = url_for('api.get_user_articles', page=page - 1, _external=True) next = None if pagination.has_next: next = url_for('api.get_user_articles', page=page - 1, _external=True) return jsonify({ 'articles': [article.to_dict() for article in articles], 'prev': prev, 'next': next, 'count': pagination.total })
def get_category(id): category = Category.query.get(id) if not category: return not_found(_('The category not exists')) return jsonify(category.to_dict())
def get_user(id): user = User.query.get(id) if not user: return not_found(_('The user not exists')) return jsonify(user.to_dict())
def get_article(id): article = Article.query.get(id) if not article: return not_found(_('The article not exists')) return jsonify(article.to_dict())
def page_not_found(e): if request.accept_mimetypes.accept_json and \ not request.accept_mimetypes.accept_html: return not_found() return render_template('404.html'), 404