def create_task(request, group_id): ''' INPUT Takes in a submission of the create task form, gathering the Title, Description, Deadline, Assignee, and optional Points arguments. Also takes the Group ID and Assigner ID implicitly from the page request. OUTPUT Creates a new Task entry in the database associated with all relevant parties, adding it to the Tasks for each Member assigned. ''' # Grab the information of the creator group & member # Grab the basic stuff associated with the Task. When's it due, what is it, what has to be returned, # any special comments. name = request.form['name'] deadline = request.form['deadline'] if request.form['deadline'] != None else None description = request.form['description'] if request.form['description'] != None else None deliverable = request.form['deliverable'] if request.form['deliverable'] != None else None comments = request.form['comments'] if request.form['comments'] != None else None # Create the task using the information we picked out of the form. new_task = Task(name=name, group_id=group_id, description=description, deadline=deadline, deliverable=deliverable, comments=comments) # Now we need to set all our ForeignKey associations. First, make lists of all the Members and Roles # which are supposed to be true now. new_approving_members = [Member.query.filter_by(group_id=group_id, codename=membername) for membername in request.form['approving_members']] new_delivering_members = [Member.query.filter_by(group_id, codename=membername) for membername in request.form['delivering_members']] new_approving_roles = [Role.query.get(int(role_id)) for role_id in request.form['approving_roles']] new_delivering_roles = [Role.query.get(int(role_id)) for role_id in request.form['delivering_roles']] # Then, set all of these relations to be set as described by the form. new_task.approving_members = new_approving_members new_task.approving_roles = new_approving_roles new_task.delivering_members = new_delivering_members new_task.delivering_roles = new_delivering_roles # Finally, correct the approving_members and delivering_members relations to only contain Members # who have one of the Roles specified in the approving_roles and delivering_roles relations. new_task.update_approvers_by_roles() new_task.update_deliverers_by_roles() # Add and save our work. db_session.add(new_task) db_session.commit() return new_task