Пример #1
0
def cron():
	logging.debug('Start cron')
	
	# Fetch page content
	current_data = helper.fetch_current_data()
	
	if current_data["year_month"] is not None and current_data["current_number"] is not None:
		logging.debug('Have year month and current number')
	
		# Initial house number model
		table = model.house_number()
		
		# Check current data is or not exists
		current_data_total = table.all().filter("subject = ", current_data["year_month"]).count()
		
		if current_data_total <= 0:
			# Insert new record
			table.subject = current_data["year_month"]
			table.current_number = current_data["current_number"]
			table.put()
			
			# Found out history
			history = []
			house_numbers = table.all().order("-create_at")
			for house_number in house_numbers:
				history.append(
					"<strong>%s</strong> at <strong>%s</strong><br />" % (
						house_number.subject, 
						house_number.current_number
					)
				)
			
			# Send mail
			helper.send_mail(
				year_month = current_data["year_month"].encode("utf8"),
				current_number = current_data["current_number"],
				history = (''.join(history)).encode("utf8"),
				to = config.mail_catcher
			)
			
			logging.info('Create new record success: %s, %s', current_data["year_month"].strip(), str(current_data["current_number"]))
		else:
			logging.warning('Create new record failed: not update content found in page')
		
		bottle.redirect('/')
	else:
		logging.error('current data may be None such as year_month and current_number field')
	
	logging.debug('End cron')
Пример #2
0
def create():
	current_data = helper.fetch_current_data()
	
	if current_data["year_month"] is not None and current_data["current_number"] is not None:
		table = model.house_number()
		
		# Check current data is or not exists
		current_data_total = table.all().filter("subject = ", current_data["year_month"]).count()
		
		if current_data_total <= 0:
			# Insert new record
			table.subject = current_data["year_month"]
			table.current_number = current_data["current_number"]
			table.put()
			
			bottle.redirect('/')
		else:
			return "Not create new record"
	else:
		bottle.redirect('/error/not-current-data')