def take_job(self, job_id): from Items import Job user_jobs = Job.get_by_user_id(self.user_id) if any([job_id == job.job_id for job in user_jobs]): return False job = Job.get_by_id(job_id) if job is None: return False query("INSERT INTO user_jobs (user_id, job_id) VALUES (?, ?)", [self.user_id, job_id]) return True
def __init__(self): self.username = session.get("username", None) self.tasks_progress = None self.data = None if self.username is not None: self.data = query("SELECT * FROM users WHERE username = ?", [self.username], one=True) self.user_id = self.data['user_id']
def get_user_status_by_task_id(task_id): user = User() props = query( "SELECT * FROM user_status WHERE task_id = ? AND user_id = ?", [task_id, user.user_id], one=True) if props is None: return False return Status(props)
def get_by_id(task_id): props = query("SELECT * FROM tasks WHERE task_id = ?", [task_id], one=True) return Task(props)
def get_by_job_id(job_id): props = query("SELECT * FROM tasks WHERE job_id = ?", [job_id]) return map(Task, props)
def get_by_user_id(user_id): all_props = query( "SELECT j.* FROM jobs as j INNER JOIN user_jobs as uj ON (j.job_id = uj.job_id) WHERE uj.user_id=?", [user_id]) return [Job(prop) for prop in all_props]
def get_all_jobs(): all_props = query("SELECT * FROM jobs") return map(Job, all_props)
def get_by_id(job_id): props = query("SELECT * FROM jobs WHERE job_id = ?", [job_id], one=True) return Job(props)
def get_all_store_items(): props = query("SELECT * FROM store_items") return map(Store, props)
def get_examples_by_task_id(task_id): props = query("SELECT * FROM examples WHERE task_id = ?", [task_id]) return map(Example, props)
def drop_job(self, job_id): query("DELETE FROM user_jobs WHERE user_id = ? AND job_id = ?", [self.user_id, job_id])
def add_user(username, password): query("INSERT INTO users (username, password) VALUES (?, ?)", [username, password])
def username_exists(username): return len(query("SELECT * FROM users WHERE username= ?", [username])) > 0
def verify_login(username, password): return len( query("SELECT * FROM users WHERE username= ? AND password= ?", [username, password])) == 1