Example #1
0
class WarehouseTransport:
	def __init__(self): 
		self.email = Email()

	# Даём положительный ответ
	def warehouse_re_ok(self, id):
		content = 'type: 0; id: %s' % (id.__str__()) 
		subject = 'warehouse_re_ok %s' % (id.__str__())
		self.email.sendmail(content, subject, [catalogue_email])

	# Подтверждаем резервирование
	def warehouse_re_reserved(self, id):
		content = 'type: 0; id: %s' % (id.__str__()) 
		subject = 'warehouse_re_reserved %s' % (id.__str__())
		self.email.sendmail(content, subject, [catalogue_email])
		
	# Невозможно укомплектовать заказ
	def warehouse_re_null(self, id):
		content = 'type: 0; id: %s' % (id.__str__()) 
		subject = 'warehouse_re_null %s' % (id.__str__())
		self.email.sendmail(content, subject, [catalogue_email])

	def __new_mail__(self, email):
		if email['from'] == "From: %s" % catalogue_email:
			subject = email['subject']
			content = email['text']

			dictionary = self.__get__(content)
			print "have got dictionary:"
			if (subject.startswith('Subject: warehouse_query')):
				self.__reserve_order__(dictionary['id'], dictionary['json_description'], dictionary['date'])  
			elif (subject.startswith('Subject: accept_warehouse_query')):
				order = Session.query(OrdersOperator).filter(OrdersOperator.id==dictionary['id']).first()
				order.status = "reserved"
				transaction.commit()
				self.warehouse_re_reserved(dictionary['id'])
			elif (subject.startswith('Subject: cancel')):
				self.__cancel_order__(dictionary[id])
			else:
				print "Invalid request '%s'." % subject
				
	def fetchmail(self):
		mails = self.email.fetchmail()
		if mails:
			for mail in mails:
				#print "EMAIL"
				#print mail
				self.__new_mail__(mail)

	# Резверв
	def __reserve_order__(self, id, json_description, date):
		description = json.loads(json_description)

		date = date.replace('T', '')
		for order in description:
			check = Session.query(CatalogueOperator).filter((CatalogueOperator.idCatalogue==order['idCatalogue']) & (CatalogueOperator.count>=int(order['count']))).first()
			if check == None:
				print "Tak"
				# Engine.rollback()
				self.warehouse_re_null(id)
				return
			
			newOrder = Orders_CatalogueOperator()
			newOrder.count = order['count']
			newOrder.idCatalogue = order['idCatalogue']
			newOrder.idOrder = id
			
			check.count -= order['count']
			Session.add(newOrder)
		
		order = OrdersOperator()
		order.id = id
		order.status = "prereserved"
		order.date = date
		Session.add(order)
		transaction.commit()
		
		self.warehouse_re_ok(id)

	# Отмена заказа
	def __cancel_order__(self, id):
		orders = Session.query(Orders_CatalogueOperator).filter(Orders_CatalogueOperator.idOrder==id).all()
		for order in orders:
			Session.delete(order)
			transaction.commit()
			
		order = Session.query(OrdersOperator).filter(OrdersOperator.id==id).first()
		order.status = "canceled"
		transaction.commit()

	def __get__(self, source):
		result = {}
		alist = source.split(';')

		for element in alist:
			element = element.strip()
			deuce = element.split('=')

			key = deuce[0].strip()
			value = deuce[1].strip()

			result[key] = value

		return result
Example #2
0
class PostmanTransport:
	def __init__(self): 
		self.email = Email()

	# Даём положительный ответ
	def postman_re_ok(self, id):
		content = 'type: 1; id: %s' % (id.__str__()) 
		subject = 'postman_re_ok %s' % (id.__str__())
		self.email.sendmail(content, subject, [catalogue_email])

	# Подтверждаем доставку
	def postman_re_delivering(self, id):
		content = 'type: 1; id: %s' % (id.__str__()) 
		subject = 'postman_re_delivering %s' % (id.__str__())
		self.email.sendmail(content, subject, [catalogue_email])
		
	# Отказ в доставке
	def postman_re_canceled(self, id):
		content = 'type: 1; id: %s' % (id.__str__()) 
		subject = 'postman_re_canceled %s' % (id.__str__())
		self.email.sendmail(content, subject, [catalogue_email])

	def __new_mail__(self, email):
		if email['from'] == "From: %s" % catalogue_email:
			subject = email['subject']
			content = email['text']

			dictionary = self.__get__(content)
			print "have got dictionary:"
			if (subject.startswith('Subject: postman_query')):
				self.__reserve_order__(dictionary['id'], dictionary['address'], dictionary['date'])
			elif (subject.startswith('Subject: accept_postman_query')):
				postman = Session.query(Postman).filter(Postman.idExternal==dictionary['id']).first()
				postman.status = "delivering"
				transaction.commit()
			elif (subject.startswith('Subject: cancel')):
				self.__cancel_order__(dictionary[id])
			else:
				print "Invalid request '%s'." % subject
				
	def __reserve_order__(self, id, address, date):
		postman = Postman()
		postman.idExternal = id
		postman.address = address
		postman.date = date
		postman.status = "recieved"
		Session.add(postman)
		transaction.commit()
		
	def fetchmail(self):
		mails = self.email.fetchmail()
		if mails:
			for mail in mails:
				#print "EMAIL"
				#print mail
				self.__new_mail__(mail)
				
	def __get__(self, source):
		result = {}
		alist = source.split(';')

		for element in alist:
			element = element.strip()
			deuce = element.split('=')

			key = deuce[0].strip()
			value = deuce[1].strip()

			result[key] = value

		return result
Example #3
0
class CatalogueTransport:
	def __init__(self):
		self.email = Email()

	# Добавление сервиса (склад/курьерская служба) в БД
	def add_service(self, name, type, host, port, email):
		session = Session()
		service = Services(name, type, host, port, email)
		session.add(service)
		session.flush()
		transaction.commit()

	# Отправка запроса на формирование заказа складам
	def warehouse_query(self, type, id, description, date):
		services = Session.query(Services).filter(Services.type==type).all()
		description = []
		orders_catalogue = Session.query(Orders_Catalogue).filter(Orders_Catalogue.idOrder==id).all()
		for order_catalogue in orders_catalogue:
			description.append({'idCatalogue':order_catalogue.idCatalogue, 'count':order_catalogue.count})

		json_description = json.dumps(description)
		i = 0
		for service in services:
			print "Sending mail to %s: %s..." % (service.name, service.email)
			content = 'id= %d; json_description= %s; date= %s' % (id, json_description, date)
			subject = 'warehouse_query %d' % id 
			self.email.sendmail(content, subject, [service.email])
			i += 1
		
		print
		print
		print
		print
		print
		print
		print
		print i
			
		order = Session.query(Orders).filter(Orders.id==id).first()
		order.status = 'accepted'
		order.warehouses = i
		transaction.commit()
		
	# Окончательный резерв заказа
	def accept_warehouse_query(self, type, id, service):
		content = 'id= %d' % id
		subject = 'accept_warehouse_query %d' % id 
		self.email.sendmail(content, subject, [service.email])
		
		order = Session.query(Orders).filter(Orders.id==id).first()
		order.status = 'reserverd'
		transaction.commit()

	# Отправка запроса на доставку заказа курьерской службе
	def postman_query(self, type, id, address, date):
		services = Session.query(Services).filter(Services.type==type).all()
		
		for service in services:
			print "Sending mail to %s: %s..." % (service.name, service.email)
			content = 'id= %d; address= %s; date= %s' % (id, address, date)
			subject = 'postman_query %d' % id 
			self.email.sendmail(content, subject, [service.email])

	# Окончательный резерв доставки
	def accept_postman_query(self, type, id, service):
		content = 'id= %d' % id
		subject = 'accept_query %d' % id 
		self.email.sendmail(content, subject, [service.email])
		
		order = Session.query(Orders).filter(Orders.id==id).first()
		order.status = 'delivering'
		transaction.commit()

	# Отмена заказа
	def cancel(self, type, id, service):
		content = 'id= %d' % id
		subject = 'cancel %d' % id
		self.email.sendmail(content, subject, [service.email])
		
		order = Session.query(Orders).filter(Orders.id==id).first()
		order.status = 'canceled'
		transaction.commit()

	def fetchmail(self):
		print "Fetching mail"
		mails = self.email.fetchmail()
		if mails:
			for mail in mails:
				print "EMAIL"
				print mail
				self.__new_mail__(mail)
				
	def __parse_email__(self, source):
		alist = source.split(' ')
		return alist[1]

	def __new_mail__(self, email):
		subject = email['subject']
		source = email['from']
		content = email['text']

		email = self.__parse_email__(source)
		print "MESSAGE from %s" % email
		
		dictionary = self.__get__(content)
	
		# Произведён пререзерв
		if (subject.startswith('Subject: warehouse_re_ok')):
			type = int(dictionary['type'])
			id = int(dictionary['id'])
			service = Session.query(Services).filter(Services.email==email).first()
			self.accept_warehouse_query(type, id, service)
		# Произведён резерв
		elif (subject.startswith('Subject: warehouse_re_reserved')):
			type = int(dictionary['type'])
			id = int(dictionary['id'])
			order = Session.query(Orders).filter(Orders.id==id).first()
			self.postman_query(1, id, order.address, order.date)
		elif (subject.startswith('Subject: warehouse_re_null')):
			type = int(dictionary['type'])
			id = int(dictionary['id'])
			order = Session.query(Orders).filter(Orders.id==id).first()
			order.warehouses = order.warehouses - 1
			if order.warehouses == 0:
				order.status = 'canceled'
			transaction.commit()
		elif (subject.startswith('Subject: postman_re_ok')):
			type = int(dictionary['type'])
			id = int(dictionary['id'])
			service = Session.query(Services).filter(Services.email==email).first()
			self.accept_postman_query(1, id, service)
		elif (subject.startswith('Subject: postman_re_delivering')):
			type = int(dictionary['type'])
			id = int(dictionary['id'])
			order = Session.query(Orders).filter(Orders.id==id).first()
			order.status = 'delivering'
			transaction.commit()
		elif (subject.startswith('Subject: postman_re_canceled')):
			type = int(dictionary['type'])
			id = int(dictionary['id'])
			order = Session.query(Orders).filter(Orders.id==id).first()
			order.status = 'canceled'
			transaction.commit()
		else:
			print "Invalid request '%s'." % subject
			
	def __get__(self, source):
		result = {}
		alist = source.split(';')

		for element in alist:
			element = element.strip()
			deuce = element.split(':')

			key = deuce[0].strip()
			value = deuce[1].strip()

			result[key] = value

		return result