Ejemplo n.º 1
0
def manage():
    if request.method == 'POST':
        # Handle the primary targets first
        primary_file = request.files['primary_file']
        alt_file = request.files['alt_file']
        name = request.form['name']
        if not name or name == '':
            flash('You must specify a unique target set name.')
            return render_template('manage_targets.html')

        primary_type = request.form['primary_type']
        alt_type  = 'text' if request.form['alt_type']=='None' else request.form['alt_type']
        
        target_set = TargetSet(name=name)
        target_list = launch_experiment.generate_target_blob(current_user.aws_bucket_name,
                                                             current_user.access_key_id,
                                                             current_user.secret_access_key,
                                                             str(datetime.date.today()),
                                                             primary_file,
                                                             primary_type,
                                                             alt_file,
                                                             alt_type)['target_blob']
        for target in target_list:
            target = Target(target_id = target['target_id'],
                            primary_type = target['primary_type'],
                            primary_description = target['primary_description'],
                            alt_type = target['alt_type'],
                            alt_description = target['alt_description'])
            target.save()
            target_set.targets.append(target)
        # Save the target_set
        target_set.save()
        current_project.add_target_set(target_set)
    # Always return this page
    return render_template('manage_targets.html')
Ejemplo n.º 2
0
def manage():
    if request.method == 'POST':
        # Handle the zip file case first
        has_zip_file = False
        hosted_zip_file = request.files['hosted_zip_file']
        hosted_zip_file.seek(0, os.SEEK_END)
        if "hosted_zip_file" in request.files.keys() and hosted_zip_file.tell()!=0:
            has_zip_file = True
            hosted_zip_file = request.files['hosted_zip_file']        

            # Dictionary mapping filename to object
            target_file_dict = zipfile_to_dictionary(hosted_zip_file)
            # Very basic consistency check
            #if len(target_list) != len(target_file_dict):
            #    flash("Inconsistent number of targets to images")
            #    return redirect(url_for('project.manage_targets'))


        hosted_csv_file = request.files['hosted_csv_file']
        
        # Parse the csv file into a list of dictionaries, one for each row
        target_list = csv_to_dict(hosted_csv_file, ["target_id", "primary_type", "alt_description"])

        # Create target set
        target_set = TargetSet(name=hosted_csv_file.filename)    

        for target in target_list:
                # Get file object
            if has_zip_file == True:
                target_file = target_file_dict[target["target_id"]]
                bucket = get_AWS_bucket()
                target_url = upload_to_S3(bucket, str(current_project.id)+"_"+target["target_id"], StringIO(target_file))
                target = Target( target_id = target["target_id"],
                                 primary_type = target["primary_type"],
                                 primary_description = target_url,
                                 alt_type = "text",
                                 alt_description = target["alt_description"])
            else:
                target = Target( target_id = target["target_id"],
                                 primary_type = target["primary_type"],
                                 primary_description = target["target_id"],
                                 alt_type = "text",
                                 alt_description = target["alt_description"])
            target.save()
            target_set.targets.append(target)
        # Save the target_set
        target_set.save()
        current_project.add_target_set(target_set)
            
    # Always return this page
    return render_template('manage_targets.html')