def _create_job_at_townflix(job):
	lead = job.lead.get()
	property_owner = lead.property_owner.get()
	listing = lead.listing.get()

	schedules = [option.strftime(DATE_FORMAT) for option in lead.schedule_options]
	job_title = '%s - %s' % (listing.listing_type, listing.location_neighbourhood)

	phone = ''
	if property_owner.phone is not None and property_owner.phone != '':
		phone = property_owner.phone
	else:
		phone = property_owner.cellphone

	description = lead.description if lead.description is not None else '-'

	params = {
		'contact': {
			'contact_email': property_owner.email,
			'contact_name': property_owner.name,
			'contact_phone': phone
		},
		'address': listing.full_address,
		'title': job_title,
		'description': description,
		'schedules': schedules,
		'origin_id': str(job.key.id())
	}

	result = tf_create_job(params)

	if result is not None:
		pprint.pprint('=====')
		pprint.pprint('Job created at Townflix: %s' % result['job_id'])

		return int(result['job_id'])
	else:
		return None
def update(id):
	post_data = get_post_data()
	job = _get_job(id)
	has_patched_flixer = False
	should_update_lead = False
	should_send_reschedule_mail = False
	is_flixer_already_filled = True if job.flixer is not None else False

	# if flixer is sent
	if 'flixer' in post_data:
		# if the job status allows you to ASSIGN the job. RESCHEDULE is allowed just for development purposes.
		if job.status == 'CREATED' or job.status == 'ASSIGNED' or job.status == 'RESCHEDULE':
			job.flixer = post_data['flixer']
			job.status = 'ASSIGNED'
			has_patched_flixer = True

	# if scheduled_date is sent
	if 'scheduled_date' in post_data:
		try:
			# if scheduled_date is not empty, patches to job
			if post_data['scheduled_date'] is not None:
				job.scheduled_date = datetime.datetime.strptime(post_data['scheduled_date'], DATE_FORMAT)
			else:
				# check if the user is trying to cancel an assigned job
				if job.status == 'ASSIGNED':
					job.scheduled_date = None
					should_update_lead = True
					schedule_options = []

					cancelled_by_flixer = False

					pprint.pprint(post_data)
					if 'f' in post_data:
						cancelled_by_flixer = True

					_cancel_assigned_job(job, cancelled_by_flixer=cancelled_by_flixer)

					if cancelled_by_flixer:
						create_activity('JOB_CANCELLED_BY_FLIXER', job=job)
					else:
						create_activity('JOB_CANCELLED_BY_PROPERTY_OWNER', job=job)

		except ValueError:
			abort(500, {'message': DATE_FORMAT_INVALID_MESSAGE })

	# if schedule_options is sent
	if 'schedule_options' in post_data:
		# build an array with the schedule_options sent
		schedule_options = []
		for o in post_data['schedule_options']:
			schedule_options.append(datetime.datetime.strptime(o, DATE_FORMAT))
		should_update_lead = True

		# if the current status is to RESCHEDULE
		if job.status == 'RESCHEDULE':
			# mark the job as reopened and clean scheduled_date
			job.status = 'CREATED'
			job.scheduled_date = None

			# create a new job in townflix
			job_json = job.to_json()
			listing = job_json['listing']
			property_owner = job_json['property_owner']

			job_title = '%s - %s' % (listing['reference'], listing['title'])
			job_description = job_json['description'] if job_json['description'] is not None else '-'

			params = {
				'contact': {
					'contact_email': property_owner['email'],
					'contact_name': property_owner['name'],
					'contact_phone': property_owner['phone']
				},
				'address': listing['full_address'],
				'title': job_title,
				'description': job_description,
				'schedules': post_data['schedule_options'],
				'origin_id': str(job.key.id())
			}

			result = tf_create_job(params)
			if result is not None:
				job.tf_job_id = int(result['job_id'])

			# send a message to property owner telling him that his job has been rescheduled
			should_send_reschedule_mail = True

			create_activity('JOB_RESCHEDULED', job=job)


	# if something inside the lead has been modified
	if should_update_lead:
		# updates the lead
		lead = job.lead.get()
		lead.schedule_options = schedule_options
		lead.put()

	# if job is correctly updated
	if job.put():
		# if the flixer has been updated
		if has_patched_flixer:
			# send a message to property owner telling that his job has been assigned
			first_time = not is_flixer_already_filled
			_enqueue_assign_message(job, first_time=first_time)
			create_activity('JOB_ASSIGNED', job=job)

		if should_send_reschedule_mail:
			_enqueue_job_reschedule_message(job)

		return jsonify(data=job.to_json()), 201
	else:
		return jsonify(data=job.error_messages()), 500