def create(): user = login_utils.get_current_user() course_id = request.form['course_id'] title = request.form['title'] content = request.form['content'] try: new_announcement = Announcement( user_id=user._id, course_id=course_id, title=title, content=content ) new_announcement.save() except ValidationError as e: logger.warning( 'Failed to create new announcement. Exception: {}'.format(e.message)) return jsonify({ 'success': 0, 'msg': '{}: {}'.format(error_msg.ILLEGAL_ARGUMENT, ','.join(list(e.message.keys()))) }), 400 return jsonify({ 'success': 1, 'survey_id': str(new_announcement._id) })
def get(): submission = Submission.objects.get( {'_id': ObjectId(request.args.get('submission_id'))}) user = login_utils.get_current_user() if user.type == 'student': with no_auto_dereference(Submission): if submission.user_id != user._id: return jsonify({ 'success': 0, 'msg': error_msg.REQUEST_FORBIDDEN }), 403 content = submission.content sbm = [] i = 0 n = len(content) while i < n: ctnt = {} ctnt['type'] = content[i] i += 1 if ctnt['type'] not in FILE_TYPES: ctnt['content'] = content[i] else: filename = content[i] i += 1 ctnt['file_id'] = content[i] sbm.append(ctnt) i += 1 return jsonify({ 'success': 1, 'content': sbm, 'after_deadline': submission.after_deadline, 'is_marked': submission.is_marked })
def get_self(): user = login_utils.get_current_user() submission = Submission.objects.raw({ 'course_id': ObjectId(request.args.get('course_id')), 'user_id': user._id }) submissions = [] for sbm in submission: submissions.append({ 'submission_id': str(sbm._id), 'after_deadline': sbm.after_deadline, 'is_marked': sbm.is_marked, 'homework': filter_homework_fields(sbm.homework_id), 'create_at': utils.get_timestamp(sbm.create_at) }) return jsonify({ 'success': 1, 'submissions': sorted(submissions, key=lambda x: x['create_at']) })
def update(): response_id = request.form['survey_response_id'] response = SurveyResponse.objects.get({'_id': ObjectId(response_id)}) user = login_utils.get_current_user() with no_auto_dereference(SurveyResponse): logger.warn('User {} updating others\'s survey response {}'.format( str(user._id), response_id)) if user._id != response.user_id: return jsonify({ 'success': 0, 'msg': error_msg.REQUEST_FORBIDDEN }), 403 try: response.content = request.form['content'] response.save() except ValidationError as e: logger.warning( 'Failed to update survey response {}. Exception: {}'.format( response_id, e.message)) return jsonify({ 'success': 0, 'msg': '{}: {}'.format(error_msg.ILLEGAL_ARGUMENT, ','.join(list(e.message.keys()))) }), 400 with no_auto_dereference(Survey): logger.info('Updated survey response. _id:{}, course_id:{}'.format( str(response._id), str(response.course_id))) return jsonify({'success': 1})
def create_comment(): try: comment = Comments(post_id=ObjectId(request.form['post_id']), comment=request.form['comment'], author_id=get_current_user()._id, author_name=get_current_user().username) comment.save() return jsonify({'success': 1, 'comment_id': str(comment._id)}) except ValidationError as e: logger.warning('Failed to Create new comment. Exception: {}'.format( e.message)) return jsonify({ 'success': 0, 'msg': '{}: {}'.format(error_msg.ILLEGAL_ARGUMENT, ','.join(list(e.message.keys()))) }), 400
def check(): user = get_current_user() query = {'course_id': ObjectId(get_val_from_req('course_id'))} if user.type == 'teacher': query['teacher_id'] = user._id model = CourseToTeacher e = error_msg.TEACHER_NOT_IN_CLASS elif user.type == 'student': query['student_id'] = user._id model = CourseToStudent e = error_msg.STUDENT_NOT_IN_CLASS if not dbutils.exists(model, query): return jsonify({'success': 0, 'msg': e}), 403 return f()
def get_file(): sbm_file = SubmissionFile.objects.get( {'_id': ObjectId(request.args.get('file_id'))}) user = login_utils.get_current_user() if user.type == 'student': with no_auto_dereference(SubmissionFile): if sbm_file.user_id != user._id: return jsonify({ 'success': 0, 'msg': error_msg.REQUEST_FORBIDDEN }), 403 file = sbm_file.file_id return send_file(file, mimetype=mimetypes.guess_type(sbm_file.file_name)[0])
def create_post(): try: post = Content(title=request.form['title'], content=request.form['content'], author=get_current_user().username, create_at=get_timestamp()) post.save() return jsonify({'success': 1, 'post_id': str(post._id)}) except ValidationError as e: logger.warning('Failed to create new post. Exception: {}'.format( e.message)) return jsonify({ 'success': 0, 'msg': '{} : {}'.format(error_msg.ILLEGAL_ARGUMENT, ','.join(list(e.message.keys()))) }), 400
def list_visitor(): user_id = get_current_user()._id post_id = request.args.get('post_id') logger.info('post_id: {}'.format(post_id)) qs = Comments.objects.raw({'post_id': ObjectId(post_id)}) logger.info('user_id: {}'.format(user_id)) return jsonify({ 'success': 1, 'msg': sorted([{ 'author_name': each.author_name, 'comment': each.comment, 'post_id': str(each.post_id._id), 'permission': 1 if user_id == each.author_id._id else 0, 'create_at': get_timestamp(each.create_at) } for each in qs], key=lambda x: x['create_at']) })
def create(): """teachers or administrators create a slide for a course, all the students in this course can see it.""" try: slides = Slides(author=get_current_user()._id, title=request.form['title'], content=request.form['content'], course_id=ObjectId(request.form['course_id'])) slides.save() return jsonify({'success': 1, 'slides_id': str(slides._id)}) except ValidationError as e: logger.warning('Failed to create new slides. Exception: {}'.format( e.message)) return jsonify({ 'success': 0, 'msg': '{}: {}'.format(error_msg.ILLEGAL_ARGUMENT, ','.join(list(e.message.keys()))) }), 400
def mark(): user = login_utils.get_current_user() course_id = request.form['course_id'] homework_id = request.form['homework_id'] submission_id = request.form['submission_id'] content = request.form['content'] score = int(request.form['score']) # check if the submission has already been marked sbm = Submission.objects.get({'_id': ObjectId(submission_id)}) if sbm.is_marked == 1: return jsonify({ 'success': 0, 'msg': error_msg.SUBMISSION_ALREADY_MARKED }), 409 try: new_mark = SubmissionMark(user_id=user._id, course_id=course_id, homework_id=homework_id, submission_id=submission_id, content=content, score=score) new_mark.save() except ValidationError as e: logger.warning( 'Failed to create new submission mark. Exception: {}'.format( e.message)) return jsonify({ 'success': 0, 'msg': '{}: {}'.format(error_msg.ILLEGAL_ARGUMENT, ','.join(list(e.message.keys()))) }), 400 sbm.is_marked = 1 sbm.save() return jsonify({'success': 1})
def submit(): user = login_utils.get_current_user() course_id = request.form['course_id'] survey_id = request.form['survey_id'] # check if already submitted old_response = SurveyResponse.objects.raw({ 'course_id': ObjectId(course_id), 'survey_id': ObjectId(survey_id), 'user_id': user._id }) if old_response.count() != 0: return jsonify({ 'success': 0, 'msg': error_msg.DUPLICATE_SURVEY_RESPONSE }), 409 content = request.form['content'] new_response = SurveyResponse(user_id=user._id, course_id=course_id, survey_id=survey_id, content=content) try: new_response.save() except ValidationError as e: logger.warning( 'Failed to create new survey response. Exception: {}'.format( e.message)) return jsonify({ 'success': 0, 'msg': '{}: {}'.format(error_msg.ILLEGAL_ARGUMENT, ','.join(list(e.message.keys()))) }), 400 return jsonify({'success': 1, 'survey_response_id': str(new_response._id)})
def get(): user = login_utils.get_current_user() course_id = request.args.get('course_id') survey_id = request.args.get('survey_id') survey_response_id = request.args.get('survey_response_id') q = {'course_id': ObjectId(course_id), 'survey_id': ObjectId(survey_id)} if user.type == 'student': if survey_response_id: return jsonify({ 'success': 0, 'msg': error_msg.REQUEST_FORBIDDEN }), 403 q['user_id'] = user._id else: q['_id'] = ObjectId(survey_response_id) try: response = SurveyResponse.objects.get(q) except Exception as e: return jsonify({ 'success': 0, 'msg': error_msg.SURVEY_RESPONSE_NOT_EXISTS }), 404 return jsonify({'success': 1, 'response': filter_public_fields(response)})
def submit(): user = login_utils.get_current_user() course_id = request.form['course_id'] homework_id = request.form['homework_id'] content = json.loads(request.form['content']) # check if the user has already submitted old_submission = Submission.objects.raw({ 'user_id': user._id, 'course_id': ObjectId(course_id), 'homework_id': ObjectId(homework_id) }) if old_submission.count() != 0: return jsonify({ 'success': 0, 'msg': error_msg.DUPLICATE_SUBMISSION }), 409 t = datetime.utcnow() hw = Homework.objects.get({'_id': ObjectId(homework_id)}) new_submission = Submission(user_id=user._id, course_id=course_id, homework_id=homework_id, after_deadline=t > hw.deadline) sbm_content = [] saved_files = [] # structure of content: multiline, one line of format, and one/two lines of content # text # {some text content} # audio, or some other file types # {name of this file} # {id of this file in SubmissionFile} # ... try: for c in content: sbm_content.append(c['type']) if c['type'] not in FILE_TYPES: sbm_content.append(c['content']) else: sbm_content.append(c['file_name']) new_file = SubmissionFile( user_id=user._id, course_id=course_id, homework_id=homework_id, file_name=c['file_name'], file_type=c['type'], file_id=request.files[c['file_name']]) new_file.save() saved_files.append(new_file) sbm_content.append(str(new_file._id)) new_submission.content = sbm_content new_submission.save() except ValidationError as e: # delete all saved files in this submission for f in saved_files: f.delete() logger.warning('Failed to create new submission. Exception: {}'.format( e.message)) return jsonify({ 'success': 0, 'msg': '{}: {}'.format(error_msg.ILLEGAL_ARGUMENT, ','.join(list(e.message.keys()))) }), 400 return jsonify({'success': 1, 'submission_id': str(new_submission._id)})