2018-06-07 10:30:50 +01:00
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
from flask import Blueprint, jsonify, request
|
2018-06-05 14:25:24 +01:00
|
|
|
|
2018-06-07 10:30:50 +01:00
|
|
|
from app.complaint.complaint_schema import complaint_count_request
|
2018-07-02 17:19:23 +01:00
|
|
|
from app.dao.complaint_dao import fetch_count_of_complaints, fetch_paginated_complaints
|
2018-06-05 14:25:24 +01:00
|
|
|
from app.errors import register_errors
|
2018-06-07 10:30:50 +01:00
|
|
|
from app.schema_validation import validate
|
2018-07-02 17:19:23 +01:00
|
|
|
from app.utils import pagination_links
|
2018-06-05 14:25:24 +01:00
|
|
|
|
|
|
|
|
complaint_blueprint = Blueprint('complaint', __name__, url_prefix='/complaint')
|
|
|
|
|
|
|
|
|
|
register_errors(complaint_blueprint)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@complaint_blueprint.route('', methods=['GET'])
|
|
|
|
|
def get_all_complaints():
|
2018-07-02 17:19:23 +01:00
|
|
|
page = int(request.args.get('page', 1))
|
|
|
|
|
pagination = fetch_paginated_complaints(page=page)
|
|
|
|
|
|
|
|
|
|
return jsonify(
|
|
|
|
|
complaints=[x.serialize() for x in pagination.items],
|
|
|
|
|
links=pagination_links(
|
|
|
|
|
pagination,
|
|
|
|
|
'.get_all_complaints',
|
|
|
|
|
**request.args.to_dict()
|
|
|
|
|
)
|
|
|
|
|
), 200
|
2018-06-07 10:30:50 +01:00
|
|
|
|
|
|
|
|
|
2018-06-07 16:01:41 +01:00
|
|
|
@complaint_blueprint.route('/count-by-date-range', methods=['GET'])
|
2018-06-07 10:30:50 +01:00
|
|
|
def get_complaint_count():
|
2018-06-12 12:10:58 +01:00
|
|
|
if request.args:
|
|
|
|
|
validate(request.args, complaint_count_request)
|
2018-06-07 10:30:50 +01:00
|
|
|
|
2018-06-12 12:10:58 +01:00
|
|
|
# If start and end date are not set, we are expecting today's stats.
|
|
|
|
|
today = str(datetime.utcnow().date())
|
|
|
|
|
|
|
|
|
|
start_date = datetime.strptime(request.args.get('start_date', today), '%Y-%m-%d').date()
|
|
|
|
|
end_date = datetime.strptime(request.args.get('end_date', today), '%Y-%m-%d').date()
|
2018-06-07 10:30:50 +01:00
|
|
|
count_of_complaints = fetch_count_of_complaints(start_date=start_date, end_date=end_date)
|
|
|
|
|
|
|
|
|
|
return jsonify(count_of_complaints), 200
|