def reschedule(id):
	job = Job.get_by_id(int(id))
	if job is not None and job.lead is not None:
		post_data = get_post_data()

		if 'schedule_options' in post_data:
			options = []
			for o in post_data['schedule_options']:
				options.append(datetime.datetime.strptime(o, DATE_FORMAT))

			# updates the lead schedule options
			lead = job.lead.get()
			lead.schedule_options = options
			lead.put()

			# mark the job as reopened and clean scheduled_date
			job.status = 'REOPENED'
			job.scheduled_date = None
			job.put()

			# reschedule the job in townflix
			tf_reschedule_job(job.tf_job_id, job.key.id(), options)

			# send a message to property owner telling him that his job has been rescheduled
			_enqueue_job_reschedule_message(job)

		return jsonify(data=job.to_json()), 201
	else:
		return jsonify(data={}), 404
def create_at_townflix():
	pprint.pprint('###### BACKGROUND PROCESSING!')

	# find a job by id
	job_id = request.form['job_id']
	job = Job.get_by_id(int(job_id))

	# if job is found
	if job is not None:
		# creates a job in townflix
		tf_job_id = _create_job_at_townflix(job)

		# updates the job with the id returned by townflix
		if tf_job_id is not None:
			job.tf_job_id = tf_job_id
			job.put()

			lead = job.lead.get()
			lead.status = 'CONVERTED'
			lead.put()

			# pprint.pprint('/jobs/create_at_townflix')
			# pprint.pprint('=== LEAD UPDATED')
			# pprint.pprint('Lead Id: ' % lead.key.id())
			# pprint.pprint('Lead Status: ' % lead.status)

			return '', 200

	return '', 500
def cancel(id):
	job = Job.get_by_id(int(id))

	if job.status == 'CREATED':
		return _cancel_created_job(job)
	if job.status == 'ASSIGNED':
		return _cancel_assigned_job(job, cancelled_by_flixer=True)
def _get_job(id):
	job = Job.get_by_id(int(id))
	if job is not None:
		return job
	else:
		abort(404, {'message': 'Job not found'})
def get(id):
	job = Job.get_by_id(int(id))
	if job is not None:
		return jsonify(data=job.to_json()), 200
	else:
		return jsonify(data={}), 404
Exemplo n.º 6
0
    def run(self):
        """Function that will upload a new file, slice it if needed,
        and delete stl files from the octopi. Should be used whenever
        a new job is uploaded to the hub
        :returns: boolean of success

        """
        id      = self.id
        log     = self.log
        job_id  = self.job_id
        webapi  = self.webapi
        loc     = octopi.local
        printer = Printer.get_by_id(id, fresh=True)
        ip      = printer.ip
        port    = printer.port
        key     = printer.key
        job     = Job.get_by_id(job_id)
        log.log("Starting job upload to " + str(id)
                + " for job " + str(job.id))
        fpath = job.file.path
        job.state("processing")
        url = ip + ":" + str(port)
        r = octopi.upload_file(url, key, fpath, loc)
        if r == None:
            log.log("ERROR: Did not have a response from " + str(id)
                    + ". File upload canceled for " + fpath + ".")
            self.success = False
            return False
        if r.status_code != 201:
            log.log("ERROR: Could not upload file " + fpath
                    + ". Return code from printer " + str(r.status_code))
            return False
        data = r.json()
        fname = data['files']['local']['name']
        ext = get_extension(fname)
        if ext in ['stl']:
            job.state("slicing")
            webapi.patch_job(job.to_web())
            r = octopi.slice(url, key, fname, loc)
            if r == None:
                log.log("ERROR: Did not have a response from " + str(id)
                        + ". Job upload canceled for job "
                        + str(job_id) + ".")
                self.success = False
                return False
            if r.status_code != 202:
                log.log("ERROR: Job upload failed for " + str(job_id)
                        + ". Return code from printer "
                        + str(r.status_code))
                self.success = False
                return False
            j = r.json()
            rname = j.get('name')
            r = octopi.get_one_file_info(url, key, rname, loc)
            while r == None or r.status_code != 200:
                #This is really f*****g hacky
                log.log("Could not retrieve file info for "
                        + str(job.id))
                sleep(10)
                r = octopi.get_one_file_info(url, key, rname, loc)
            job.set_remote_name(rname)
            r = octopi.delete_file(url, key, fname, loc)
            while r == None:
                sleep(10)
                r = octopi.delete_file(url, key, fname, loc)
        elif ext in ['gcode', 'gco']:
            r = octopi.get_one_file_info(url, key, fname, loc)
            while r == None or r.status_code != 200:
                #This is really f*****g hacky
                log.log("Could not retrieve file info for "
                        + str(job.id))
                sleep(10)
                r = octopi.get_one_file_info(url, key, fname, loc)
            job.set_remote_name(fname)
        else:
            self.success = False
            return False
        log.log("Completed job upload to " + str(id)
                + " for job " + str(job.id))
        self.success = True
        return True