diff --git a/project/api/models.py b/project/api/models.py index a100e56..b1b1e40 100644 --- a/project/api/models.py +++ b/project/api/models.py @@ -7,7 +7,7 @@ class Report(db.Model): company_id = db.Column(db.Integer, nullable=True) date_from = db.Column(db.DateTime, nullable=False) date_to = db.Column(db.DateTime, nullable=False) - total_cost = db.Column(db.String, nullable=False) + total_cost = db.Column(db.String, nullable=True) total_tax_cost = db.Column(db.Float, nullable=True) #tagUseReports = db.relationship('tagUseReport', backref='report', lazy=True) @@ -17,6 +17,16 @@ def __init__(self, company_id, date_from, date_to, total_cost, total_tax_cost): self.date_to = date_to self.total_cost = total_cost self.total_tax_cost = total_tax_cost + + def to_json(self): + return { + 'id': self.id, + 'company_id': self.company_id, + 'date_from': self.date_from, + 'date_to': self.date_to, + 'total_cost': self.total_cost, + 'total_tax_cost': self.total_tax_cost + } class TagUseReport(db.Model): diff --git a/project/api/views.py b/project/api/views.py index 52837d2..e7ecf34 100644 --- a/project/api/views.py +++ b/project/api/views.py @@ -7,6 +7,16 @@ reports_blueprint = Blueprint('/report', __name__) +@reports_blueprint.route('/get_reports', methods=['GET']) +def get_all_reports(): + response = { + 'status': 'success', + 'data': { + 'reports': [reports.to_json() for reports in Report.query.all()] + } + } + + return jsonify(response) @reports_blueprint.route('/report', methods=['POST']) def reports(): @@ -18,40 +28,40 @@ def reports(): 'error': 'empty json' }), 400 - reports = data.get('receipts') + receipts = data.get('receipts') sum = 0 - for report in reports: - if not report.get('total_price'): + for receipt in receipts: + if not receipt.get('total_price'): return jsonify({ 'error': 'empty total_price' }), 400 - sum += report.get('total_price') + sum += receipt.get('total_price') sum = str(sum) return jsonify({ - 'receipts': reports, + 'receipts': receipts, 'total_cost': sum }), 200 + + @reports_blueprint.route('/add_report', methods=['POST']) def add_report(): data = request.get_json() - + if not data: return jsonify({ 'error': 'Report can not be saved' }), 400 - reports = data.get('receipts') - company_id = None data_from = data.get('date_from') data_to = data.get('date_to') - total_cost = data.get('total_cost') + total_cost = None total_tax_cost = None diff --git a/project/tests/test_reports.py b/project/tests/test_reports.py index de845e2..f566e93 100644 --- a/project/tests/test_reports.py +++ b/project/tests/test_reports.py @@ -1,7 +1,15 @@ import json import unittest from project.tests.base import BaseTestCase +from project.api.models import Report +from datetime import datetime +from project import db +def add_report(company_id, data_from, data_to, total_cost, total_tax_cost): + report = Report(company_id, data_from, data_to, total_cost, total_tax_cost) + db.session.add(report) + db.session.commit() + return report class TestReportService(BaseTestCase): @@ -126,20 +134,27 @@ def test_missing_date_from(self): self.assertEqual(response.status_code, 400) - def test_missing_total_cost(self): - with self.client: - response = self.client.post( - '/add_report', - data = json.dumps({ - "date_to": "2018-02-12", - "date_from": "2018-02-22" - }), - content_type = 'application/json', - ) + def test_get_all_receipts(self): + start = "22-09-2018" + end = "22-11-2018" + dateStart = datetime.strptime(start, '%d-%m-%Y').date() + dateEnd = datetime.strptime(end, '%d-%m-%Y').date() + + start = datetime.strptime(start, '%d-%m-%Y').strftime('%a, %d %b %Y %H:%M:%S GMT') + end = datetime.strptime(end, '%d-%m-%Y').strftime('%a, %d %b %Y %H:%M:%S GMT') + + add_report(None, dateStart, dateEnd, None, None) + + with self.client: + response = self.client.get('/get_reports') data = json.loads(response.data.decode()) - self.assertEqual(response.status_code, 400) - + self.assertEqual(response.status_code, 200) + self.assertEqual(len(data['data']['reports']), 1) + + self.assertEqual(start, data['data']['reports'][0]['date_from']) + self.assertEqual(end, data['data']['reports'][0]['date_to']) + if __name__ == '__main__': unittest.main() \ No newline at end of file