def new_assignment(): data = request.get_json() assignment = Assignment() for key, val in data: if hasattr(assignment, key): setattr(assignment, key, val) assignment.save() return jsonify({"assignment": assignment.json()})
def upload(user_id, course_title, post_id): user = User.get_or_none(User.id == user_id) course = Course.get_or_none(Course.title == course_title) thread = Thread.get_or_none(Thread.course_id == course.id) post = Post.get_or_none(Post.file_path != 'NULL', Post.thread_id == thread.id) params = request.form title = params.get("title") info = StudentCourse.get_or_none(StudentCourse.student_id == user_id, StudentCourse.course_name_id == course.id) if info: if current_user.id == user.id: # We check the request.files object for a user_file key. (user_file is the name of the file input on our form). If it's not there, we return an error message. if "assignment" not in request.files: flash("No file provided!", "danger") return redirect(url_for('posts.show', course_name=course_title, user_id=current_user.id, post_id = post.id)) # If the key is in the object, we save it in a variable called file. file = request.files["assignment"] # we sanitize the filename using the secure_filename helper function provided by the werkzeurg.security module. # file.filename = secure_filename(file.filename) # get path to image on S3 bucket using function in helper.py file_path = upload_file_to_s3(file, user.username) new_assignment = Assignment(title=title, info_id=info.id, file_path=file_path, post_id=post.id) if new_assignment.save(): flash("Successfully uploaded!","success") return redirect(url_for('posts.show', course_name=course_title, user_id=current_user.id, post_id = post.id)) # then redirect to profile page else: flash("Upload failed. Please try again!", "danger") return redirect(url_for('posts.show', course_name=course_title, user_id=current_user.id, post_id = post.id)) else: flash("Cannot upload assignments for other users", "danger") return redirect(url_for('posts.show', course_name=course_title, user_id=current_user.id, post_id = post.id)) else: flash("Failed to upload!", "danger") return redirect(url_for('posts.show', course_name=course_title, user_id=current_user.id, post_id = post.id))