예제 #1
0
파일: util.py 프로젝트: jachin/bom_data_viz
def update_groups_and_consumable():

    stream = open("beverage_data.yaml", 'r')
    beverages_data = yaml.load(stream)

    for (group_name, beverages) in beverages_data.items():
        query = BeverageGroup.query.filter_by(name=group_name)
        if query.count() == 0:
            group = BeverageGroup(name=group_name)
            db.session.add(group)
            db.session.commit()
            app.logger.debug("Adding beverage group: {0}".format(group_name))
        group = BeverageGroup.query.filter_by(name=group_name).first()
        for (beverage_name, upc) in beverages.items():

            if Consumable.query.filter_by(name=beverage_name).count() == 0:
                consumable = Consumable(
                    name=beverage_name,
                    beverage_group_id=group.id
                )
                db.session.add(consumable)
                db.session.commit()
                app.logger.debug("Adding consumable: {0}".format(beverage_name))

            consumable = Consumable.query.filter_by(name=beverage_name).first()

            if Barcode.query.filter_by(upc=upc).count() == 0:
                barcode = Barcode(
                    upc=upc,
                    consumable_id=consumable.id,
                )
                db.session.add(barcode)
                db.session.commit()
                app.logger.debug(
                    "Adding a new bar code '{0}' to the consumable '{1}'".format(
                        upc,
                        beverage_name
                    )
                )

            if consumable.beverage_group_id != group.id:
                consumable.beverage_group_id = group.id
                db.session.commit()
                app.logger.debug(
                    "Adding an existing consumable '{0}' to the beverage group '{1}'".format(
                        beverage_name,
                        group.name
                    )
                )
            if consumable.name != beverage_name:
                consumable.name = beverage_name
                db.session.commit()
                app.logger.debug(
                    "Updating the UPC '{0}' with the name '{1}'".format(
                        upc,
                        beverage_name
                    )
                )
예제 #2
0
파일: util.py 프로젝트: jachin/bom_data_viz
def update_bom(location):

    bad_upcs = get_bad_upcs()

    last_consumed = Consumed.query.filter_by(
        location=location.id
    ).order_by(
        desc(Consumed.datetime)
    ).first()

    url = "http://{0}".format(location.address)

    if last_consumed is None:
        req = urllib2.Request(url)
    else:
        req = urllib2.Request("{0}/after/{1}".format(url, last_consumed.uuid))

    opener = urllib2.build_opener()
    f = opener.open(req)
    scans = simplejson.load(f)

    stats = {
        'number_of_scans': 0,
        'number_of_new_consumables': 0,
        'number_of_new_consumed': 0,
    }

    for scan in scans:
        stats['number_of_scans'] += 1

        if scan['upc'] in bad_upcs:
            # skip any bad upcs
            continue

        consumable = Consumable.get_or_create_by_barcode(scan['upc'])
        barcode = Barcode.query.filter_by(upc=scan['upc']).first()

        if consumable.name == '':
            stats['number_of_new_consumables'] += 1

        if Consumed.query.filter_by(uuid=scan['uuid']).count() == 0:
            timestamp = datetime.strptime(
                scan['timestamp'],
                '%Y-%m-%dT%H:%M:%S'
            )

            timestamp = timestamp.replace(tzinfo=pytz.utc)

            consumed = Consumed(
                uuid=scan['uuid'],
                datetime=timestamp,
                barcode=barcode.id,
                location=location.id
            )
            db.session.add(consumed)
            db.session.commit()
            stats['number_of_new_consumed'] += 1

    return stats