I have to calculate costs based on country code and city(optional). There is also a condition that countries in Asia have separate pricing logic depending on both country and city and they are stored in a different table (for segregating regional price variances). For the rest, it's only dependent on country code. Let's assume we must have separate tables Asia & the rest.
Please suggest some improvements in the below code.
def get_cost_by_location(country_code: str, city=None) -> Optional[models.OperatorCost]: cost = None # check if the country is in Asia if models.OperatorCostAsia.objects.filter(country=country_code).exists(): cost = models.OperatorCostAsia.objects.filter(country=country_code, city__icontains=city).first() if cost is None: cost = models.OperatorCostAsia.objects.filter(country=country_code).first() else: cost = models.OperatorCost.objects.filter(country=country_code).first() return cost
I have two different tables to calculate the prices. The pricing table for Asia has three columns: Country code, city name and the price itself. The pricing table for rest of the countries just has country code and price columns.
I get a mandatory country code and an optional city name as parameters from the frontend. First I check these params against the Asia pricing table. if the selected country happens to be Asian, I query against country code and city name (if there is one). If the city param is empty, i just pick the first match against the country.
If the country is not an Asian country, I simply query the second (Rest of the world) table to find the pricing against the country code.