Example #1
0
 def patch(self, id):
     try:
         measurement = Measurement.objects(id=id).first()
         if measurement is not None:
             if get_formatted_date(
                     measurement.created) != get_formatted_date(
                         get_today_date()):
                 raise BadRequest(
                     f'Cannot update a measurement for {get_formatted_date(measurement.created)}'
                 )
             data = self.reqparse.parse_args()
             data = normalize_data(data)
             measurement.update(**data)
             measurement.reload()
             return measurement.to_dict(), 200
         abort(404, message=f'Measurement ID={id} was not found')
     except BadRequest as e:
         app.logger.error(e)
         raise e
     except NotFound as e:
         app.logger.error(e)
         raise e
     except Exception as e:
         app.logger.error(e)
         abort(500, message=str(e))
Example #2
0
 def put(self, id):
     """
     Updates a measurement given its ID
     """
     measurement = Measurement.objects(id=id).first()
     if measurement is not None:
         if get_formatted_date(get_today_date()) != get_formatted_date(measurement.created):
             self.abort_with_http_code_error(
                 400, f'Cannot update a measurement for {get_formatted_date(measurement.created)}'
             )
         data = self.reqparse.parse_args()
         data = normalize_data(data)
         if not data:
             self.abort_with_http_code_error(
                 400, 'Payload cannot be empty'
             )
         measurement.update(**data)
         measurement.reload()
         return measurement.to_dict(), 200
     self.abort_with_http_code_error(404, f'Measurement ID={id} was not found')
Example #3
0
 def patch(self, id):
     try:
         import pdb
         pdb.set_trace()
         measurement = Measurement.objects(id=id).first()
         if measurement is not None:
             if get_formatted_date(
                     measurement.created) != get_formatted_date(
                         get_today_date()):
                 raise BadRequest(
                     f'Cannot update a measurement for {measurement.created}'
                 )
             data = self.reqparse.parse_args()
             data = normalize_data(data)  # eliminamos los valores None
             measurement.update(**data)
             measurement.reload()
             return measurement.to_dic(), 200
         abort(404, message=f'Measurement ID={id} was not found')
     except BadRequest as e:
         raise e
     except NotFound as e:
         raise e
     except Exception as e:
         abort(500, message=str(e))
Example #4
0
def handle_history_command(message_from_user, pre_message):
    message_text = utils.replace_and_split_by_regex(r"(/history|\s{2,})",
                                                    message_from_user.text)
    base, target = message_text[0].upper().split('/')
    days = int(message_text[-2])

    start_at = utils.get_given_days_ago(days)
    end_at = utils.get_formatted_date()

    err, path_or_message = exchange_rate_helper.get_history_image(
        base, target, start_at, end_at, days)
    if err:
        bot.edit_message_text(chat_id=message_from_user.chat.id,
                              message_id=pre_message.message_id,
                              text=path_or_message,
                              parse_mode='markdown')
    else:
        with open(path_or_message, 'rb') as f:
            bot.send_photo(message_from_user.chat.id, f)
            bot.delete_message(chat_id=message_from_user.chat.id,
                               message_id=pre_message.message_id)
        os.remove(path_or_message)
Example #5
0
def main():
    # Get current day - 1
    logger.info('Calculating yesterday\'s day')
    yesterday = get_yesterday_date()
    # Get yesterday's measurement
    logger.info(f'Getting measurement for {get_formatted_date(yesterday)}')
    measurement = Measurement.objects(
        created=get_formatted_date(yesterday)).first()
    if measurement is None:
        raise Exception(
            f'Measurement for date={get_formatted_date(yesterday)} was not found'
        )

    # Compute the period for that measurment
    logger.info(f'Calculating period for {get_formatted_date(yesterday)}')
    period = get_period(yesterday)
    # Get the report by period
    logger.info(f'Getting report for {period}')
    report = Report.objects(period=period).first()
    if report is None:
        logger.info(f'Report not found, creating a new report for {period}')
        report = Report(
            period=period,
            values=[],
            user=User.objects(email='*****@*****.**').first())
        report.save()

    logger.info(
        f'Adding a new measurement for {measurement.created} in period {period}'
    )
    report_item = MeasurementValue(sys=measurement.sys,
                                   dia=measurement.dia,
                                   pul=measurement.pul,
                                   date=measurement.created,
                                   ok=measurement.is_ok())
    report.values.append(report_item)
    report.save()
    logger.info('Done')