Last active
March 16, 2019 19:16
-
-
Save satyamkapoor/98992c613a8941731ee4d7a795e933a3 to your computer and use it in GitHub Desktop.
flask rest API Hackthon
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, jsonify, request, render_template, Response | |
from datetime import datetime | |
app = Flask(__name__) | |
def compute_total_cost(prices,quantities,startDate,endDate,country,reduction): | |
number_of_days = compute_number_of_days(startDate,endDate) | |
packages_sum = compute_sum(prices,quantities) | |
#if reduction == "STANDARD": | |
# return | |
#else: | |
reduction = apply_reduction(packages_sum) | |
print(number_of_days) | |
print(packages_sum) | |
print(reduction) | |
taxes = apply_taxes(country, packages_sum) | |
total_cost = compute_cost(packages_sum,number_of_days,reduction,taxes) | |
print(taxes) | |
print(total_cost) | |
return total_cost | |
def compute_sum(prices,quantities): | |
return sum([prices[i]*quantities[i] for i in range(len(prices))]) | |
def compute_cost(package_sum,number_of_days,reduction,taxes): | |
return (package_sum * number_of_days * taxes * reduction)/100 | |
def compute_number_of_days(start_date,end_date): | |
start_date = datetime.strptime(start_date,"%Y-%m-%dT%H:%M:%S.000Z") | |
end_date = datetime.strptime(end_date,"%Y-%m-%dT%H:%M:%S.000Z" ) | |
delta = end_date-start_date | |
return delta.days | |
def apply_reduction(packages_sum): | |
if 100 < packages_sum < 500: | |
return 0.97 | |
elif 500 <packages_sum < 700: | |
return 0.95 | |
elif 700 < packages_sum < 1000: | |
return 0.93 | |
elif 1000 < packages_sum < 5000: | |
return 0.90 | |
elif 5000 < packages_sum: | |
return 0.85 | |
# reduction_dic = {100:0.03,500:0.05,700:0.07,1000:0.1,5000:0.15} | |
# if packages_sum in reduction_dic.keys(): | |
# return reduction_dic[packages_sum] | |
#else: | |
# return 1 | |
def apply_taxes(country, packages_sum): | |
"""tax_percentage = 0 | |
if country == "FR" or country == "DE": | |
if packages_sum < 2000: | |
tax_percentage = 18 | |
else if packages_sum > 2000: | |
tax_percentage = 1.5 | |
""" | |
taxes_dic = {"DE":1.2,"UK":1.21,"FR":1.2,"IT":1.25,"ES":1.19,"PL":1.21,"RO":1.2,"NL":1.2,"BE":1.24,"EL":1.2,"CZ":1.19,"PT":1.23,"HU":1.27,"SE":1.23,"AT":1.22,"BG":1.21,"DK":1.21,"F!":1.17,"SK":0.00,"IE":1.21,"HR":1.23,"LT":1.23,"SI":1.24,"LV":1.2,"EE":1.22,"CY":1.21,"LU":1.25,"MT":1.2} | |
return taxes_dic[country] | |
@app.route('/order',methods=['POST']) | |
def fortest(): | |
bus_data = request.get_json() | |
#print(bus_data) | |
startDate = bus_data['startDate'] | |
quantities = bus_data['quantities'] | |
endDate = bus_data['endDate'] | |
country = bus_data['country'] | |
reduction = bus_data['reduction'] | |
prices = bus_data['prices'] | |
totalc = compute_total_cost(prices,quantities,startDate,endDate,country,reduction) | |
res = {'total': totalc} | |
print(bus_data) | |
return jsonify(res) | |
#return Response(status=404) | |
#print(type(startDate)) | |
@app.route('/feedback', methods=['POST']) | |
def feedback(): | |
feedback_data = request.get_json() | |
print(feedback_data) | |
return Response(status=404) | |
if __name__ == '__main__': | |
app.run(debug=True,host= '0.0.0.0') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment