Exemplo n.º 1
0
def medicine(medicine_id):

    medicine = load_from_api('medicine', medicine_id)

    # find the maximum price, for making comparisons
    max_price = medicine['procurements'][-1]['unit_price_usd_fob']
    if medicine.get('benchmarks'):
        for benchmark in medicine['benchmarks']:
            if benchmark['price'] > max_price:
                max_price = benchmark['price']

    # add procurements and benchmarks to the same list, and sort
    tmp = list(medicine['procurements'])
    if medicine.get('benchmarks'):
        for benchmark in medicine['benchmarks']:
            benchmark['unit_price_usd_fob'] = benchmark['price']
            tmp.append(benchmark)
    procurements_and_benchmarks = sorted(tmp, key=lambda x: x["unit_price_usd_fob"])

    # find the best procurements
    best_procurements = medicine['procurements']
    if len(best_procurements) > 5:
        best_procurements = best_procurements[0:5]

    # calculate potential cost difference
    form_args = []
    if request.args.get("compare-quantity") and request.args.get("compare-pack-size") and request.args.get("compare-price"):
        form_args = request.args
        try:
            compare_quantity = int(form_args['compare-quantity'])
            compare_pack_size = int(form_args['compare-pack-size'])
            compare_price = float(form_args['compare-price'])
            compare_unit_price = float(compare_price/compare_pack_size)

            for procurement in best_procurements:
                unit_price = float(procurement['unit_price_usd_fob'])
                procurement['cost_difference'] = (unit_price - compare_unit_price) * compare_quantity * compare_pack_size
        except Exception as e:
            flash(gettext(u"There was a problem with your input."), "warning")
            logger.debug(e)
            pass

    return render_template(
        'medicine.html',
        API_HOST=API_HOST,
        medicine=medicine,
        active_nav_button="medicines",
        max_price = max_price,
        best_procurements = best_procurements,
        procurements_and_benchmarks = procurements_and_benchmarks,
        form_args = form_args,
        )
Exemplo n.º 2
0
 def handle(self, method, suffix=None, data=None):
     url = 'http://%s:%s' % (self._host, self._port)
     if self._index:
         url = '%s/%s' % (url, self._index)
         if self._type:
             url = '%s/%s' % (url, self._type)
     if suffix:
         url = '%s/%s' % (url, suffix)
     if isinstance(data, dict):
         data = json.dumps(data)
     logger.debug("[ REQUEST ] %s %s" % (url, data))
     request = urllib2.Request(url, data=data)
     request.get_method = lambda: method
     try:
         response = urllib2.urlopen(request)
         result = response.read()
         logger.debug("[ Response ] %s" % result)
         return json.loads(result)
     except urllib2.URLError as e:
         logger.exception(e)
         return None
Exemplo n.º 3
0
 def handle(self, method, suffix=None, data=None):
     url = 'http://%s:%s' % (self._host, self._port)
     if self._index:
         url = '%s/%s' % (url, self._index)
         if self._type:
             url = '%s/%s' % (url, self._type)
     if suffix:
         url = '%s/%s' % (url, suffix)
     if isinstance(data, dict):
         data = json.dumps(data)
     logger.debug("[ REQUEST ] %s %s" % (url, data))
     request = urllib2.Request(url, data=data)
     request.get_method = lambda: method
     try:
         response = urllib2.urlopen(request)
         result = response.read()
         logger.debug("[ Response ] %s" % result)
         return json.loads(result)
     except urllib2.URLError as e:
         logger.exception(e)
         return None