def from_json(data): if type(data) != type({}): data = json.loads(str(data).strip()) job = Job() job.id = get_value(data, "id", int, optional = True) job.name = get_value(data, "name", str); # Optional attributes job.description = get_value(data, "description", str, optional = True); job.cron = None cron = get_value(data, "cron", dict, optional = True) if cron != None: for name in ["day", "month", "year", "hour", "minute", "second"]: if name in cron: if job.cron == None: job.cron = {} job.cron[name] = get_value(cron, name, str) for s in data["signals"]: job.add_signal(Signal.from_json(s)) return job
def save_signal(self, handler): try: signal = Signal.from_json(handler.get_post_data()) signal = signal.sql_save(self.sql()) self.sql_commit() except Exception, e: self.log_error(e) handler.send_json_response(str(e), 400) return
def sql_load(sql, job_id = None, order_by = None): Job.sql_create(sql) if job_id != None: sql.execute("SELECT id, name, description, cron " "FROM jobs WHERE id=? LIMIT 0,1", (job_id,)) else: if order_by == None: order_by = "name" sql.execute("SELECT id, name, description, cron " "FROM jobs ORDER BY ?", (order_by,)) result = sql.fetchall() if job_id != None and result == None: raise Exception("Could not find job id %i" % job_id) return None jobs = [] for data in result: job = Job() (job.id, job.name, job.description, job.cron) = data if job.cron != None: job.cron = json.loads(job.cron) sql.execute("SELECT signal_id FROM jobs_signals " "WHERE job_id=? ORDER BY Position ASC", (job.id,)) for (signal_id,) in sql.fetchall(): job.add_signal(Signal.sql_load(sql, signal_id=signal_id)) if job_id != None: return job jobs.append(job) return jobs
def load_signals(self, handler, order_by = "name"): signals = Signal.sql_load(self.sql(), order_by=order_by) handler.send_json_response(signals)