示例#1
0
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 register_project():
    project_form = classes.ProjectForm()
    if project_form.validate_on_submit():
        Net_Wealth = project_form.Net_Wealth.data
        Annual_Income = project_form.Annual_Income.data
        Age = project_form.Age.data
        user_name = project_form.username.data
        project = classes.Project(Net_Wealth, Annual_Income, Age, user_name)
        db.session.add(project)
        db.session.commit()
        #return redirect(url_for('index'))

        if Age >= 22 and Net_Wealth >= 100000:
            return redirect(url_for('login'))
        else:
            return redirect(url_for('not_qualify'))
    return render_template('project_entry.html', form=project_form)
示例#3
0
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)})
示例#4
0
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
                                   )