def post_sole(course_id): """Create a new sole. """ # authenticate the user user, err = auth.get_user_from_request(db, request) if err: return json_error(err) # validate and format the POST data user_id = str(user.get('id')) data = json.loads(request.data) s = Sole.format_params_for_create(user_id, data) err = Sole.validate_params_for_create(s) if err: return json_error(err) # create the sole sole_id = Sole.create_new_sole(db, s) # format response s = Sole.find_by_id(db, sole_id) ss = User.update_sole_with_students(db, s) # enqueue an email to be sent to the creator resq.enqueue(EmailCreator, str(sole_id)) return json.dumps(ss)
def get_soles_for_course(course_id): """ Get a list of soles for the course, within a certain radius of the provided lat, lon """ lat, lon = get_lat_lon() r = Sole.find_by_course_id(db, course_id, lat, lon) nr = User.update_soles_with_students(db, r) return json.dumps(nr)
def patch_sole(course_id, sole_id): """ Join or leave a sole """ user, err = auth.get_user_from_request(db, request) if not user: return json_error("User not found") sole = Sole.find_by_id(db, sole_id) if not sole: return json_error("that study group doesn't exist") # if the new set of student ids makes sense, use them data = json.loads(request.data) current_sids = sole.get(Sole.A_STUDENT_IDS) new_sids = data.get(Sole.A_STUDENT_IDS) user_id = user.get('id') resp = None action = None if (set(current_sids) - set(new_sids)) == set([user_id]): resp = Sole.leave_sole_by_id(db, sole_id, user_id) action = 'left' elif (set(new_sids) - set(current_sids)) == set([user_id]): resp = Sole.join_sole_by_id(db, sole_id, user_id) action = 'joined' else: return json_error("Invalid new student_ids") # return the new model if resp: s = Sole.find_by_id(db, sole_id) ns = User.update_sole_with_students(db, s) resq.enqueue(EmailGroup, action, sole_id, user_id) return json.dumps(ns) else: return json_error("Some other error")
def get_sole_course_and_user_information(sole_id, user_id=None): db = MongoClient() # find the sole sole = Sole.find_by_id(db, sole_id) if not sole: None, None, None # find the course cid = sole.get(Sole.A_COURSE_ID) course = Course.find_by_id(db, cid) if not course: sole, None, None # find the user who did the action user_ids = set(sole.get(Sole.A_STUDENT_IDS)) if not user_ids: # everyone has left the group! sole, course, None if user_id: user_ids.add(user_id) users = User.find_by_ids(db, user_ids, as_dict=True) return sole, course, users
def get_soles_for_home(): """ Get all the soles close to the current location """ lat, lon = get_lat_lon() r = Sole.find(db, lat, lon) return json.dumps(r)