def submit(self, data):
        """
        Handle form submission; create or update models in the DB. Raises an
        Exception for any errors.

        :param data: submitted form data
        :type data: dict
        :return: message describing changes to DB (i.e. link to created record)
        :rtype: str
        """
        if 'id' in data and data['id'].strip() != '':
            # updating an existing budget
            trans = db_session.query(Transaction).get(int(data['id']))
            if trans is None:
                raise RuntimeError("Error: no Transaction with ID "
                                   "%s" % data['id'])
            if trans.reconcile is not None:
                raise RuntimeError(
                    "Transaction %d is already reconciled; cannot be edited."
                    "" % trans.id)
            action = 'updating Transaction ' + data['id']
        else:
            trans = Transaction()
            action = 'creating new Transaction'
        trans.description = data['description'].strip()
        trans.date = datetime.strptime(data['date'], '%Y-%m-%d').date()
        trans.account_id = int(data['account'])
        trans.notes = data['notes'].strip()
        budg_amts = {}
        for bid, budg_amt in data['budgets'].items():
            budg = db_session.query(Budget).get(int(bid))
            budg_amts[budg] = Decimal(budg_amt)
        trans.set_budget_amounts(budg_amts)
        logger.info('%s: %s', action, trans.as_dict)
        db_session.add(trans)
        db_session.commit()
        return {
            'success_message':
            'Successfully saved Transaction %d  in database.'
            '' % trans.id,
            'success':
            True,
            'trans_id':
            trans.id
        }
Exemple #2
0
    def submit(self, data):
        """
        Handle form submission; create or update models in the DB. Raises an
        Exception for any errors.

        :param data: submitted form data
        :type data: dict
        :return: message describing changes to DB (i.e. link to created record)
        :rtype: str
        """
        total = float(data['total_cost'])
        dt = datetime.strptime(data['date'], '%Y-%m-%d').date()
        veh_name = db_session.query(Vehicle).get(int(data['vehicle'])).name
        fill = FuelFill()
        fill.date = dt
        fill.vehicle_id = int(data['vehicle'])
        fill.odometer_miles = int(data['odometer_miles'])
        fill.reported_miles = int(data['reported_miles'])
        fill.level_before = int(data['level_before'])
        fill.level_after = int(data['level_after'])
        fill.fill_location = data['fill_location'].strip()
        fill.cost_per_gallon = float(data['cost_per_gallon'])
        fill.total_cost = total
        fill.gallons = float(data['gallons'])
        fill.reported_mpg = float(data['reported_mpg'])
        fill.notes = data['notes'].strip()
        logger.info('Creating new FuelFill: %s', fill.as_dict)
        db_session.add(fill)
        db_session.commit()
        fill.calculate_mpg()
        db_session.commit()
        if data['add_trans'] != 'true':
            return {
                'success_message':
                'Successfully saved FuelFill %d '
                'in database.' % fill.id,
                'success':
                True,
                'fill_id':
                fill.id,
                'calculated_mpg':
                fill.calculated_mpg
            }
        trans = Transaction()
        budg = db_session.query(Budget).get(int(data['budget']))
        trans.description = '%s - FuelFill #%d (%s)' % (
            data['fill_location'].strip(), fill.id, veh_name)
        trans.date = dt
        trans.actual_amount = total
        trans.account_id = int(data['account'])
        trans.budget = budg
        trans.notes = data['notes'].strip()
        logger.info('Creating new Transaction for FuelFill: %s', trans.as_dict)
        db_session.add(trans)
        db_session.commit()
        return {
            'success_message':
            'Successfully saved FuelFill %d '
            ' and Transaction %d in database.' % (fill.id, trans.id),
            'success':
            True,
            'trans_id':
            trans.id,
            'fill_id':
            fill.id,
            'calculated_mpg':
            fill.calculated_mpg
        }