Skip to content

Commit

Permalink
Merge pull request #7 from Kalkuli/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
MarianaPicolo authored Oct 23, 2018
2 parents 8596a70 + b4656e3 commit df7ac0b
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 22 deletions.
12 changes: 11 additions & 1 deletion project/api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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):
Expand Down
28 changes: 19 additions & 9 deletions project/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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


Expand Down
39 changes: 27 additions & 12 deletions project/tests/test_reports.py
Original file line number Diff line number Diff line change
@@ -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):

Expand Down Expand Up @@ -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()

0 comments on commit df7ac0b

Please sign in to comment.