def db_add_dashevent(self): """ Saves the current dashevent to the database. """ x = checkFields(self.content, fields=[ 'dashevent_category', 'dashevent_name', 'dashevent_instructor', 'extra_info' ]) if (x): return make_response("Missing required field: " + x, 400) if (self.dashevent.objects( dashevent_name=self.content['dashevent_name']).count() > 0): return make_response( "There already exists a dashevent with the same name.", 400) self.dashevent( dashevent_category=self.content['dashevent_category'], dashevent_name=self.content['dashevent_name'], dashevent_instructor=self.content['dashevent_instructor'], students=[], extra_info=self.content['extra_info']).save() return make_response("", 200)
def db_update_event(self): """ Updates the event in the database for the corresponding email """ x = checkFields(self.content, fields=[ '_id', 'name', 'start_time', 'event_type', 'email', 'date', 'event_info' ]) if (x): return make_response("Missing required field: " + x, 400) # Checks if the event exists for the user event = self.Event.objects(class_name=self.content['_id'], email=self.content['email']).first() if event: event.update(pk=self.content['_id'], name=self.content['name'], start_time=self.content['start_time'], event_type=self.content['event_type'], event_info=self.content['event_info'], email=self.content['email'], date=self.content['date']) return make_response("", 200) else: return make_response("Event not found", 400)
def db_get_messages(self): """ Gets all the messages between two users """ x = checkFields(self.content, fields=['username1', 'username2']) if (x): return make_response("Missing required field: " + x, 400) messages = [] raw = self.Message.objects(username1=self.content['username1'], username2=self.content['username2']).all() for message in raw: messages.append(message.to_json()) raw = self.Message.objects(username2=self.content['username1'], username1=self.content['username2']).all() for message in raw: messages.append(message.to_json()) if len(message) == 0: return make_response("Messages between users does not exist", 404) else: messages = sorted(messages, key=lambda k: k['time'], reverse=False) return make_response(jsonify(messages), 200)
def db_get_user_type(self): """ Gets the user_type based on username """ x = checkFields(self.content, fields=['username']) if (x): return make_response("Missing required field: " + x, 400) user_obj = self.User.objects(username=self.content['username']).first() if user_obj: return make_response(jsonify(user_obj.user_type), 200) else: return make_response("", 404)
def db_get_user_email(self): """ Gets email based off of the username passed in. """ x = checkFields(self.content, fields=['username']) if (x): return make_response("Missing required field: " + x, 400) user_obj = self.User.objects(username=self.content['username']).first() if user_obj: return make_response(jsonify(user_obj.email), 200) else: return make_response("", 404)
def db_get_user(self): """ Creates the user based off of the email passed in. """ x = checkFields(self.content, fields=['email']) if (x): return make_response("Missing required field: " + x, 400) user_obj = self.User.objects(email=self.content['email']).first() if user_obj: return make_response(jsonify(user_obj.to_json()), 200) else: return make_response("", 404)
def db_last_login(self): """ Gets the last login time """ x = checkFields(self.content, fields=['username']) if (x): return make_response("Missing required field: " + x, 400) user_obj = self.User.objects(username=self.content['username']).first() if user_obj: return make_response(jsonify(user_obj.last_login), 200) else: return make_response("", 404)
def db_get_dashevents_of_instructor(self): """ Returns all of the dashevents thought by the instructor passed in. """ x = checkFields(self.content, fields=['dashevent_instructor']) if (x): return make_response("Missing required field: " + x, 400) dashevents = [] for dashevent_obj in self.dashevent.objects( dashevent_instructor=self.content['dashevent_instructor']): dashevents.append(dashevent_obj.dashevent_name) return make_response(jsonify(dashevents), 200)
def db_get_course(self): """ Returns a course based off of the course name passed in. """ x = checkFields(self.content, fields=['course_name']) if (x): return make_response("Missing required field: " + x, 400) course_obj = self.Course.objects( course_name=self.content['course_name']).first() if course_obj: return make_response(jsonify(course_obj.to_json()), 200) else: return make_response("", 404)
def db_get_profile(self): """ Gets the profile given a username """ x = checkFields(self.content, fields=['username']) if (x): return make_response("Missing required field: " + x, 400) prof_obj = self.Profile.objects( username=self.content['username']).first() if prof_obj: return make_response(jsonify(prof_obj.to_json()), 200) else: return make_response("Username does not exist", 404)
def db_delete_event(self): """ Delete a single event in the database """ x = checkFields(self.content, fields=['_id']) if (x): return make_response("Missing required field: " + x, 400) event = self.Event.objects(pk=self.content['_id']).first() if event: event.delete() return make_response("", 200) else: return make_response("Event does not exist.", 404)
def db_get_dashevent(self): """ Returns a dashevent based off of the dashevent name passed in. """ x = checkFields(self.content, fields=['dashevent_name']) if (x): return make_response("Missing required field: " + x, 400) dashevent_obj = self.dashevent.objects( dashevent_name=self.content['dashevent_name']).first() if dashevent_obj: return make_response(jsonify(dashevent_obj.to_json()), 200) else: return make_response("", 404)
def db_delete_single_user(self): """ Delete a single user in the database """ x = checkFields(self.content, fields=['email']) if (x): return make_response("Missing required field: " + x, 400) user_obj = self.User.objects(email=self.content['email']).first() if user_obj: user_obj.delete() return make_response("", 200) else: return make_response("User does not exist.", 404)
def db_update_user_email(self): """ Updates the email in the database for the corresponding email """ x = checkFields(self.content, fields=['old_email', 'new_email']) if (x): return make_response("Missing required field: " + x, 400) user_obj = self.User.objects(email=self.content['old_email']).first() if user_obj: user_obj.update(email=self.content['new_email']) return make_response("", 200) else: return make_response("User does not exist.", 404)
def db_get_courses_of_instructor(self): """ Returns all of the courses thought by the instructor passed in. """ x = checkFields(self.content, fields=['course_instructor']) if (x): return make_response("Missing required field: " + x, 400) courses = [] for course_obj in self.Course.objects( course_instructor=self.content['course_instructor']): courses.append(course_obj.course_name) return make_response(jsonify(courses), 200)
def db_update_profile_phone_number(self): """ Updates the phone number in the database for the corresponding username """ x = checkFields(self.content, fields=['phone_number', 'username']) if (x): return make_response("Missing required field: " + x, 400) prof_obj = self.Profile.objects( username=self.content['username']).first() if prof_obj: prof_obj.update(phone_number=self.content['phone_number']) return make_response("", 200) else: return make_response("User does not exist.", 404)
def db_delete_profile_education(self): """ deleteds a profile educations in the database for the corresponding username """ x = checkFields(self.content, fields=['education', 'username']) if (x): return make_response("Missing required field: " + x, 400) prof_obj = self.Profile.objects( username=self.content['username']).first() if prof_obj: prof_obj.update(pull__educations=self.content['education']) return make_response("", 200) else: return make_response("User does not exist.", 404)
def db_add_profile_skill(self): """ Adds a profile skill in the database for the corresponding username """ x = checkFields(self.content, fields=['skill', 'username']) if (x): return make_response("Missing required field: " + x, 400) prof_obj = self.Profile.objects( username=self.content['username']).first() if prof_obj: prof_obj.update(add_to_set__skills=self.content['skill']) return make_response("", 200) else: return make_response("User does not exist.", 404)
def db_delete_single_dashevent(self): """ Delete a single dashevent in the database """ x = checkFields(self.content, fields=['dashevent_name']) if (x): return make_response("Missing required field: " + x, 400) dashevent_obj = self.dashevent.objects( dashevent_name=self.content['dashevent_name']).first() if dashevent_obj: dashevent_obj.delete() return make_response("", 200) else: return make_response("dashevent does not exist.", 404)
def db_get_messaged_users(self): x = checkFields(self.content, fields=['username']) if (x): return make_response("Missing required field: " + x, 400) messages = [] raw = self.Message.objects(username1=self.content['username']).all() for message in raw: messages.append(message.username2) raw = self.Message.objects(username2=self.content['username']).all() for message in raw: messages.append(message.username1) return make_response(jsonify(messages), 200)
def db_logout(self): """ Updates the last logout date and time """ x = checkFields(self.content, fields=['username']) if (x): return make_response("Missing required field: " + x, 400) user_obj = self.User.objects(username=self.content['username']).first() if user_obj: user_obj.update( last_logout=datetime.now().strftime("%m/%d/%Y, %H:%M:%S")) return make_response("", 200) else: return make_response("", 401)
def db_delete_single_course(self): """ Delete a single course in the database """ x = checkFields(self.content, fields=['course_name']) if (x): return make_response("Missing required field: " + x, 400) course_obj = self.Course.objects( course_name=self.content['course_name']).first() if course_obj: course_obj.delete() return make_response("", 200) else: return make_response("Course does not exist.", 404)
def db_get_dashevents_of_student(self): """ Returns all of the dashevents taken by the student passed in. """ x = checkFields(self.content, fields=['student']) if (x): return make_response("Missing required field: " + x, 400) student = self.content['student'] dashevents_taken = [] for dashevent_obj in self.dashevent.objects(): students = dashevent_obj.students if student in students: dashevents_taken.append(dashevent_obj.dashevent_name) return make_response(jsonify(dashevents_taken), 200)
def db_update_course_name(self): """ Updates the name of the course in the database for the corresponding course name """ x = checkFields(self.content, fields=['old_course_name', 'new_course_name']) if (x): return make_response("Missing required field: " + x, 400) course_obj = self.Course.objects( course_name=self.content['old_course_name']).first() if course_obj: course_obj.update(course_name=self.content['new_course_name']) return make_response("", 200) else: return make_response("Course does not exist.", 404)
def db_get_courses_of_student(self): """ Returns all of the courses taken by the student passed in. """ x = checkFields(self.content, fields=['student']) if (x): return make_response("Missing required field: " + x, 400) student = self.content['student'] courses_taken = [] for course_obj in self.Course.objects(): students = course_obj.students if student in students: courses_taken.append(course_obj.course_name) return make_response(jsonify(courses_taken), 200)
def db_remove_student_from_course(self): ''' Removes the student passed in to the provided course ''' x = checkFields(self.content, fields=['course_name', 'student']) if (x): return make_response("Missing required field: " + x, 400) course_obj = self.Course.objects( course_name=self.content['course_name']).first() if course_obj: self.Course.objects( course_name=self.content['course_name']).update( pull__students=self.content['student']) return make_response("", 200) else: return make_response("Course does not exist.", 404)
def db_remove_student_from_dashevent(self): ''' Removes the student passed in to the provided dashevent ''' x = checkFields(self.content, fields=['dashevent_name', 'student']) if (x): return make_response("Missing required field: " + x, 400) dashevent_obj = self.dashevent.objects( dashevent_name=self.content['dashevent_name']).first() if dashevent_obj: self.dashevent.objects( dashevent_name=self.content['dashevent_name']).update( pull__students=self.content['student']) return make_response("", 200) else: return make_response("dashevent does not exist.", 404)
def db_update_user_name(self): """ Updates the username in the database for the corresponding email """ x = checkFields(self.content, fields=['username', 'email']) if (x): return make_response("Missing required field: " + x, 400) user_obj = self.User.objects(email=self.content['email']).first() if user_obj: prof_obj = ProfileObj.Profile.objects( username=user_obj.username).first() prof_obj.update(username=self.content['username']) user_obj.update(username=self.content['username']) return make_response("", 200) else: return make_response("User does not exist.", 404)
def db_login(self): """ Logs in using the username and password given """ x = checkFields(self.content, fields=['password', 'username']) if (x): return make_response("Missing required field: " + x, 400) user_obj = self.User.objects( username=self.content['username'], password=self.content['password']).first() if user_obj: user_obj.update( last_login=datetime.now().strftime("%m/%d/%Y, %H:%M:%S")) return make_response(jsonify("true"), 200) else: return make_response(jsonify("false"), 401)
def db_update_dashevent_instructor(self): """ Updates the instructor of the dashevent corresponding to the dashevent name in the database """ x = checkFields(self.content, fields=['dashevent_name', 'dashevent_instructor']) if (x): return make_response("Missing required field: " + x, 400) dashevent_obj = self.dashevent.objects( dashevent_name=self.content['dashevent_name']).first() if dashevent_obj: dashevent_obj.update( dashevent_instructor=self.content['dashevent_instructor']) return make_response("", 200) else: return make_response("dashevent does not exist.", 404)