def projects(): """ This route displays the projects of a given user and allows them the ability to add a project. If a project using the same project_name already exists, this will display an error to tell the user to pick another project name. """ if request.method == 'GET': projects = db.session.query( classes.User_Project.project_name).filter_by( user_id=int(current_user.id)).all() # return render_template('projects.html', projects=list(projects)) return render_template( 'projects.html', projects=[proj[0].strip(",") for proj in projects]) elif request.method == 'POST': project_name = request.form['project_name'] labels = [label.strip() for label in request.form['labels'].split(',')] # query the Project table to see if the project already exists # if it does, tell the user to pick another project_name projects_with_same_name = classes.User_Project.query.filter_by( project_name=project_name).all() if len(projects_with_same_name) > 0: return f"<h1> A project with the name: {project_name}" + \ " already exists. Please choose another name for your project." else: # insert into the Project table db.session.add(classes.Project(project_name, int(current_user.id))) # get the project for the current user that was just added # (by using the creation date) most_recent_project = classes.Project.query.filter_by(project_owner_id=current_user.id)\ .order_by(classes.Project.project_creation_date.desc()).first() # insert into the User_Project table so that the user is associated with a project db.session.add( classes.User_Project(int(current_user.id), most_recent_project.project_id, project_name)) # TODO: find a way to bulk insert # insert all of the labels that the user entered for label in labels: db.session.add( classes.Label(most_recent_project.project_id, label)) # pass the list of projects (including the new project) to the page so it can be shown to the user projects = classes.User_Project.query.filter_by( user_id=int(current_user.id)).all() # only commit the transactions once everything has been entered successfully. db.session.commit() projects = db.session.query( classes.User_Project.project_name).filter_by( user_id=int(current_user.id)).all() return render_template( 'projects.html', projects=[proj[0].strip(",") for proj in projects])
def status(projid): """ This route provides project status, including the prediction results and users of this project. At this route, a user can also add other users to their project, one user at a time. Added users will also receive an email notifying them that they are added. """ proj = classes.Project.query.filter_by(project_id=projid) \ .first() project_predictions = classes.Pred_Results.query.filter_by(project_id=projid).all() for project in project_predictions: print(project.path_to_img) projnm = proj.project_name proj_owner = classes.User.query.filter_by(id=proj.project_owner_id).first().username userids_of_one_project = classes.User_Project.query \ .filter_by(project_id=projid).all() users = [] for user_proj in userids_of_one_project: users.append(classes.User.query.filter_by( id=user_proj.user_id).first().username) # add user functionality if request.method == "POST": added_username = request.form['username'] if len(added_username.split()) > 1: flash("You can only add one user at a time.") render_template('status.html', projnm=projnm, users=users, projid=projid) found_users = classes.User.query.filter_by(username=added_username).all() if len(found_users) == 0: flash('User does not exist.') render_template('status.html', projnm=projnm, users=users, projid=projid) elif added_username in users: flash(added_username + ' already has access to the project.') render_template('status.html', projnm=projnm, users=users, projid=projid) else: user = classes.User.query.filter_by(username=added_username).first() user_id = user.id user_proj = classes.User_Project(user_id, projid) db.session.add(user_proj) db.session.commit() #send notification to the added user user_email = user.email subject_line = f"You have been added to project {projnm}." email_body = f"You have just been added to project {projnm}. " \ f"Now you can start uploading data, training, and predicting." send_notification(added_username, user_email, subject_line, email_body) users_with_new = [] userids_with_new_of_one_project = classes.User_Project\ .query.filter_by(project_id=projid).all() for user_proj_new in userids_with_new_of_one_project: users_with_new.append( classes.User.query.filter_by( id=user_proj_new.user_id).first().username ) return render_template('status.html', projnm=projnm, proj_owner=proj_owner, users=users_with_new, projid=projid) return render_template('status.html', projnm=projnm, proj_owner=proj_owner, users=users, projid=projid, project_predictions=project_predictions)
def mobile_projects(): """ This route is the backend for the project menu of a given user in the mobile phone. It allows for projects to be added. If a project using the same project_name already exists, this will display an error to tell the user to pick another project name. :return: return dictionary with success "1"/"0" if success/failure and the list of projects """ if request.method == 'GET': projects = classes.User_Project.query.filter_by( user_id=int(current_user.id)).all() # return objects to easily call by attribute names, # rather than by indexes, please keep proj_labs = {} for proj in projects: proj_labs[proj.project_id] = classes.Label.query.filter_by( project_id=proj.project_id).all() # use dictionary to easily call by key which matches the ids, # more secure than by indexes, please keep # return render_template('projects.html', projects=projects, # proj_labs=proj_labs) return json.dumps( {"success": "1", "projects": json.dumps(projects), "proj_labs": json.dumps(proj_labs)}) elif request.method == 'POST': project_name = request.form['project_name'] labels = [label.strip() for label in request.form['labels'].split(',')] # TODO: verify label_names to be unique within one project, # right now can have same name but different labelid. # query the Project table to see if the project already exists # if it does, tell the user to pick another project_name projects_with_same_name = classes.User_Project.query.filter_by( project_name=project_name).all() if len(projects_with_same_name) > 0: # return f"<h1> A project with the name: {project_name}" + \ # " already exists. Please choose # another name for your project." return json.dumps({"success": "0"}) else: # insert into the Project table db.session.add(classes.Project(project_name, int(current_user.id))) # get the project for the current user that was just added # (by using the creation date) most_recent_project = classes.Project.query.filter_by( project_owner_id=current_user.id) \ .order_by(classes.Project.project_creation_date.desc()).first() # insert into the User_Project table so that the # user is associated with a project db.session.add(classes.User_Project(int(current_user.id), most_recent_project.project_id, project_name)) # TODO: find a way to bulk insert # insert all of the labels that the user entered for label in labels: db.session.add(classes.Label(most_recent_project.project_id, label)) # pass the list of projects (including the new project) # to the page so it can be shown to the user # only commit the transactions once everything # has been entered successfully. db.session.commit() projects = classes.User_Project.query.filter_by(user_id=int( current_user.id)).all() proj_labs = {} for proj in projects: proj_labs[proj.project_id] = classes.Label.query.filter_by( project_id=proj.project_id).all() # return render_template('projects.html', # projects=projects, proj_labs=proj_labs) return json.dumps( {"success": "1", "projects": json.dumps(projects), "proj_labs": json.dumps(proj_labs)})
def projects(): """ This route displays the projects of a given user and allows them the ability to add a project. If a project using the same project_name already exists, this will display an error to tell the user to pick another project name. Project details are listed in a table, allowing user to upload image data per project, per label, to initiate training process and to upload new image for prediction. """ if request.method == 'GET': users_projects = classes.User_Project.query.filter_by(user_id=current_user.id).all() project_ids = [user_project.project_id for user_project in users_projects] projects = classes.Project.query.filter(classes.Project.project_id.in_(project_ids)) # proj_owners = {} # for proj in projects: # proj_owners[proj.project_id] = classes.User.query.filter_by(id=proj.project_owner_id).first().username # return objects to easily call by attribute names, # rather than by indexes, please keep proj_labs = {} for proj in projects: proj_labs[proj.project_id] = classes.Label.query.filter_by( project_id=proj.project_id).all() # use dictionary to easily call by key which matches the ids, # more secure than by indexes, please keep return render_template('projects.html', projects=projects, proj_labs=proj_labs # , proj_owners=proj_owners ) elif request.method == 'POST': project_name = request.form['project_name'] labels = [label.strip() for label in request.form['labels'].split(',')] # this was updated if len(set(labels)) != len(labels): return f"<h1>There are duplicate labels. Please enter labels that are different.</h1>" # TODO: verify label_names to be unique within one project, # TODO: right now can have same name but different labelid. # query the Project table to see if the project already exists # if it does, tell the user to pick another project_name users_projects = classes.User_Project.query.filter_by(user_id=current_user.id).all() project_ids = [user_project.project_id for user_project in users_projects] # if a user has multiple projects # check if the project name with the same name already exists for them projects_with_same_name = [] if len(users_projects) > 0: projects = classes.Project.query.filter_by(project_name=project_name).all() projects_with_same_name = [project.project_id for project in projects if project.project_id in project_ids] if len(projects_with_same_name) > 0: return f"<h1> A project with the name: {project_name}" + \ " already exists. Please choose another " \ "name for your project.</h1>" # flash("A project with the same name already exists." # "Please choose another name.") # return url_for('projects') else: # insert into the Project table db.session.add(classes.Project(project_name, int(current_user.id))) # get the project for the current user that was just added # (by using the creation date) most_recent_project = classes.Project.query \ .filter_by(project_owner_id=current_user.id) \ .order_by(classes.Project.project_creation_date.desc()).first() print(most_recent_project.project_name) # insert into the User_Project table # so that the user is associated with a project db.session.add(classes.User_Project(int(current_user.id), most_recent_project.project_id)) # TODO: find a way to bulk insert # insert all of the labels that the user entered for label_idx, label in enumerate(labels): db.session.add(classes.Label(most_recent_project.project_id, label, label_idx)) most_recent_project_labels = classes.Label.query.filter_by(project_id=most_recent_project.project_id) # this was added # TODO: when creating the project, I need to create the model # and prediction folders for a given project in S3 bucket = get_deepVision_bucket() # bucket.set_acl('public-read') k = Key(bucket) for label in most_recent_project_labels: k.key = f'/{str(most_recent_project.project_id)}/{str(label.label_id)}/' k.set_contents_from_string('') k.key = f'/{str(most_recent_project.project_id)}/model/' k.set_contents_from_string('') k.key = f'/{str(most_recent_project.project_id)}/prediction/' k.set_contents_from_string('') k = Key(bucket) k.key = f'/{str(most_recent_project.project_id)}/' k.set_contents_from_string('') # pass the list of projects (including the new project) to the page # so it can be shown to the user # only commit the transactions once everything has been entered. db.session.commit() users_projects = classes.User_Project.query.filter_by(user_id=current_user.id).all() project_ids = [user_project.project_id for user_project in users_projects] projects = classes.Project.query.filter(classes.Project.project_id.in_(project_ids)) proj_owners = {} for proj in projects: proj_owners[proj.project_id] = classes.User.query.filter_by(id=proj.project_owner_id).first().username proj_labs = {} for proj in projects: proj_labs[proj.project_id] = classes.Label.query.filter_by( project_id=proj.project_id).all() return render_template('projects.html', projects=projects, proj_labs=proj_labs # , proj_owners=proj_owners )