Exemplo n.º 1
0
    def process_item(self, item, spider):
        if item['table_type'] == 'customer_order':
            try:
                cust = Customer.objects.get(source_id=item['source_id'],
                                            source_type=item['source_type'])
            except Customer.DoesNotExist:
                cust = Customer()
                cust.source_type = item['source_type']
                cust.source_id = item['source_id']
                cust.name = item['name']
                cust.save()
                logger.info(u'Added {}'.format(str(cust)))

            cust_ord = CustomerOrder()
            cust_ord.customer = cust
            cust_ord.order_id = item['order_id']
            if item['member'].lower().strip() == 'yes':
                cust_ord.member = True
            elif item['member'].lower().strip() == 'no':
                cust_ord.member = False
            cust_ord.caregiver = item['caregiver']
            cust_ord.date = datetime.strptime(item['date'], '%m/%d/%y').date()
            cust_ord.order_total = item['order_total'].lstrip('$')
            cust_ord.save()
            logger.info(u'Added {}'.format(str(cust_ord)))

        elif item['table_type'] == 'transaction':
            try:
                cust_ord = CustomerOrder.objects.get(
                    order_id=item['order_id'],
                    customer__source_type=item['source_type'])
            except Customer.DoesNotExist:
                logger.error(u'There is no customer order #{}!'.format(
                    item['order_id']))

            else:
                trans = Transaction()
                trans.order = cust_ord
                trans.description = item['description']
                trans.qty_dispensed = item['qty_dispensed']
                trans.qty_sold = item['qty_sold']
                trans.price = item['price'].lstrip('$')
                trans.subtotal = item['subtotal'].lstrip('$')
                trans.discount = item['discount']
                trans.tax = item['tax']
                trans.cashier = item['cashier']
                trans.save()
                logger.info(u'Added {}'.format(str(trans)))

        return item
Exemplo n.º 2
0
# PW=os.environ["DB_PWD"]
# DB="cattalax"

# con = MySQLdb.connect(HOST,USER,PW,DB)
# cur = con.cursor()

# cur.execute("SELECT id FROM enters WHERE obs>2 INTO OUTFILE '/tmp/enter2.csv' fields terminated by ',' ENCLOSED BY '\"' LINES TERMINATED BY '\\n'")

file_of_addrs = open("/tmp/enter2.csv")
addrs = file_of_addrs.read().split()

count0 = 0
for addr in addrs:
	if len(Customer.objects.filter(mac_addr=eval(addr)))==0:
		c = Customer(mac_addr=eval(addr))
		c.save()
	count0 += 1
	filename = '/tmp/detail' + str(count0) + '.csv'
#	sql_command = "SELECT DISTINCT * FROM attendance WHERE id="+addr+" ORDER BY timestamp, -rssi INTO OUTFILE '"+filename+"' FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\\n'"
#	print sql_command
#	cur.execute(sql_command)
	print "Getting duration, and inserting to customer_log table."
	g_d = robjects.r('get_duration(\"' + filename + '\")')
	visits = len(g_d)/4
	count = 0
	for i in range(visits):
#		cur.execute("INSERT INTO customer_log(Id, arrival_time, duration, leave_time, instantiated) VALUES('%s', %d, %d, %d, %d)" % (g_d[count], int(g_d[count+1]), int(g_d[count+2]), int(g_d[count+3]), 0))
		customer = Customer.objects.get(mac_addr=g_d[count])
		outlet = Outlet.objects.get(sensor_no=1)
		v = Visit(patron=customer, vendor=outlet, arrival_time=int(g_d[count+1]), duration=int(g_d[count+2]))
		print v.patron