def create_project(project): user = utils.get_user(request, db_session) project["owned_by"] = user.id p = Model(**project) user.member_of.append(p) db_session.add(user) db_session.commit() db_session.refresh(p) return p.dump(), 201
def get_region(pid, region): user = utils.get_user(request, db_session) task = ( db_session.query(Task, Media) .outerjoin(Submission, Task.id == Submission.task_id) .join(Media, Media.source_id == Task.id) .filter(Task.info["SchoolState"].astext == user.info["region"]) .filter(Submission.user_id != user.id) .filter(Task.project_id == id) ) return
def delete_project(id): user = utils.get_user(request, db_session) project = db_session.query(Model).filter(Model.id == id).one_or_none() if project is None: return "Project was not found", 404 users = db_session.query(User).filter( User.member_of.any(Model.id == project.id)).all() for u in users: u.member_of.remove(project) db_session.commit() return model.delete(Model, id)
def delete_user(id): user = utils.get_user(request, db_session) user_projects = db_session.query(Project).filter( Project.owned_by == id).all() for p in user_projects: user.member_of.remove(p) p.owned_by = None # TODO handle the passing of this to a new user db_session.add(user) db_session.add(p) # users = db_session.query(User).filter(User.member_of.any(Model.id==p.id)).all() # for u in users: # print(u.dump()) # u.member_of.remove(project) # db_session.add(u) db_session.commit() # model.delete(Project, p.id) return model.delete(Model, id)
def get_random(pid, search): user = utils.get_user(request, db_session) task = ( db_session.query(Task, Media) .outerjoin(Submission, Task.id == Submission.task_id) .join(Media, Media.source_id == Task.id) .filter( (Task.info["SchoolState"].astext == search) | (Task.info["SchoolState"].astext == "") ) .filter((Submission.id == None) | (Submission.user_id != user.id)) .order_by(func.random()) .first() ) if task is None: return NoContent, 404 else: ret = {"task": task[0].dump(), "media": task[1].dump()} return ret, 200