예제 #1
0
def upload_profile_picture():

    user = User.query.get(g.user.id)
    user.profile_picture = upload_file(request.files['profile_picture'], 'static/images/profile_pictures')['filename']
    g.user = user
    db.session.commit()

    return url_for('static', filename='images/profile_pictures/' + user.profile_picture, _external=True)
예제 #2
0
def upload_profile_picture():

    user = User.query.get(g.user.id)
    user.profile_picture = upload_file(
        request.files['profile_picture'],
        'static/images/profile_pictures')['filename']
    g.user = user
    db.session.commit()

    return url_for('static',
                   filename='images/profile_pictures/' + user.profile_picture,
                   _external=True)
예제 #3
0
def add_building_model():
    errors = []
    if request.method == 'GET':
        return render_template("users/add_building_model.html", errors=errors)
    else:
        addition_information = ''
        name = request.form.get('name', '')

        if name.strip() == '':
            errors.append(gettext('Scenario name is required'))
        if request.files['data_file'].filename == '':
            errors.append(gettext('Model file is required'))
        if len(errors) > 0:
            return render_template("users/add_building_model.html", errors=errors), 400

        uploaded_file = upload_file(request.files['data_file'], 'static/models/building_models', file_type="model")
        data_file = uploaded_file['filename']
        if uploaded_file['extension'] not in config.ALLOWED_FILE_TYPES:
            errors.append('Unrecognized model file format')
            try:
                os.remove(os.path.join(app_dir, 'static/models/building_models', data_file))
            except:
                pass
            return render_template("users/add_building_model.html", errors=errors), 400
        if os.stat(os.path.join(app_dir, 'static/models/building_models', data_file)).st_size >= 1024*1024*10:
            errors.append('File too big, max file size is 10MB')
            os.remove(os.path.join(app_dir, 'static/models/building_models', data_file))
            return render_template("users/add_building_model.html", errors=errors), 400
        #check if file type is zip => unzip it
        if uploaded_file['extension'] == 'zip':

            tmp = data_file

            zip_ref = zipfile.ZipFile(os.path.join(app_dir, 'static/models/building_models', data_file), 'r')
            zip_ref.extractall(os.path.join(app_dir, 'static/models/building_models', uploaded_file['filename_without_extension']))
            zip_ref.close()
            #os.rename(os.path.join(app_dir, 'static/models/building_models', uploaded_file['original_filename']), os.path.join(app_dir, 'static/models/building_models', uploaded_file['filename_without_extension']))
            zipped_dir = os.path.join(app_dir, 'static/models/building_models', uploaded_file['filename_without_extension'])

            #check if inside of the zip is a dir of contain file, if contain only 1 dir => move file from inside that dir outside
            tmp_files = os.listdir(zipped_dir)
            if len(tmp_files) == 1 and not os.path.isfile(os.path.join(zipped_dir, tmp_files[0])): #contain only 1 dir
                inside_files = os.listdir(os.path.join(zipped_dir, tmp_files[0]))
                new_name = utilities.generate_random_string(50)
                os.rename(os.path.join(zipped_dir, tmp_files[0]), os.path.join(zipped_dir, new_name))
                for file in inside_files:
                    try:
                        shutil.move(os.path.join(zipped_dir, new_name, file), os.path.join(zipped_dir, file))
                    except:
                        pass
                try:
                    shutil.rmtree(os.path.join(zipped_dir, new_name)) #delete unzipped folder
                except:
                    pass


            files = [f for f in os.listdir(zipped_dir) if os.path.isfile(os.path.join(zipped_dir, f))]
            important_file_index = -1
            important_file_name = ''
            file_index = -1
            for file in files:
                filename_parts = file.split('.')
                current_filename = filename_parts[0]
                for i in range(1, len(filename_parts)-1):
                    current_filename += '.' + filename_parts[i]
                extension = filename_parts[len(filename_parts)-1].lower()
                os.rename(os.path.join(zipped_dir, file), os.path.join(zipped_dir, current_filename+'.'+extension))
                if important_file_index < 0:
                    file_index += 1
                    if extension in ['dae', 'obj']:
                        important_file_name = current_filename
                        important_file_index = file_index
                        file_type = extension

            if important_file_index < 0: # user uploaded unrecognized file
                errors.append(gettext('Unrecognized model file format'))
                os.remove(os.path.join(app_dir, 'static/models/building_models', data_file))
                try:
                    shutil.rmtree(os.path.join(app_dir, 'static/models/building_models', uploaded_file['filename_without_extension'])) #delete unzipped folder
                except:
                    pass
                return render_template("users/add_building_model.html", errors=errors), 400
            if file_type == 'obj': # important file is obj inside a zipped => objmtl
                file_type = 'objmtl'

            addition_information = {'original_filename': important_file_name,
                                    'directory': uploaded_file['filename_without_extension'],
                                    'camera_x': 30, 'camera_y': 250, 'camera_z': 350,
                                    "camera_lookat_x": 31, "camera_lookat_y": 222, "camera_lookat_z": 366}
        else:
            file_type = uploaded_file['extension']
            addition_information = {'original_filename': uploaded_file['filename_without_extension'],
                                    'directory': '',
                                    'camera_x': 30, 'camera_y': 250, 'camera_z': 350,
                                    "camera_lookat_x": 31, "camera_lookat_y": 222, "camera_lookat_z": 366}
                

        building_model = BuildingModel(name, data_file, g.user, addition_information=addition_information)
        building_model.file_type = file_type
        building_model.addition_information = json.dumps(addition_information, ensure_ascii=False)
        db.session.add(building_model)
        db.session.commit()
        return redirect(url_for('sketchup.view_building_model', id=building_model.id))
예제 #4
0
def add_building_model():
    errors = []
    if request.method == 'GET':
        return render_template("users/add_building_model.html", errors=errors)
    else:
        addition_information = ''
        name = request.form.get('name', '')

        if name.strip() == '':
            errors.append(gettext('Scenario name is required'))
        if request.files['data_file'].filename == '':
            errors.append(gettext('Model file is required'))
        if len(errors) > 0:
            return render_template("users/add_building_model.html",
                                   errors=errors), 400

        uploaded_file = upload_file(request.files['data_file'],
                                    'static/models/building_models',
                                    file_type="model")
        data_file = uploaded_file['filename']
        if uploaded_file['extension'] not in config.ALLOWED_FILE_TYPES:
            errors.append('Unrecognized model file format')
            try:
                os.remove(
                    os.path.join(app_dir, 'static/models/building_models',
                                 data_file))
            except:
                pass
            return render_template("users/add_building_model.html",
                                   errors=errors), 400
        if os.stat(
                os.path.join(app_dir, 'static/models/building_models',
                             data_file)).st_size >= 1024 * 1024 * 10:
            errors.append('File too big, max file size is 10MB')
            os.remove(
                os.path.join(app_dir, 'static/models/building_models',
                             data_file))
            return render_template("users/add_building_model.html",
                                   errors=errors), 400
        #check if file type is zip => unzip it
        if uploaded_file['extension'] == 'zip':

            tmp = data_file

            zip_ref = zipfile.ZipFile(
                os.path.join(app_dir, 'static/models/building_models',
                             data_file), 'r')
            zip_ref.extractall(
                os.path.join(app_dir, 'static/models/building_models',
                             uploaded_file['filename_without_extension']))
            zip_ref.close()
            #os.rename(os.path.join(app_dir, 'static/models/building_models', uploaded_file['original_filename']), os.path.join(app_dir, 'static/models/building_models', uploaded_file['filename_without_extension']))
            zipped_dir = os.path.join(
                app_dir, 'static/models/building_models',
                uploaded_file['filename_without_extension'])

            #check if inside of the zip is a dir of contain file, if contain only 1 dir => move file from inside that dir outside
            tmp_files = os.listdir(zipped_dir)
            if len(tmp_files) == 1 and not os.path.isfile(
                    os.path.join(zipped_dir,
                                 tmp_files[0])):  #contain only 1 dir
                inside_files = os.listdir(
                    os.path.join(zipped_dir, tmp_files[0]))
                new_name = utilities.generate_random_string(50)
                os.rename(os.path.join(zipped_dir, tmp_files[0]),
                          os.path.join(zipped_dir, new_name))
                for file in inside_files:
                    try:
                        shutil.move(os.path.join(zipped_dir, new_name, file),
                                    os.path.join(zipped_dir, file))
                    except:
                        pass
                try:
                    shutil.rmtree(os.path.join(
                        zipped_dir, new_name))  #delete unzipped folder
                except:
                    pass

            files = [
                f for f in os.listdir(zipped_dir)
                if os.path.isfile(os.path.join(zipped_dir, f))
            ]
            important_file_index = -1
            important_file_name = ''
            file_index = -1
            for file in files:
                filename_parts = file.split('.')
                current_filename = filename_parts[0]
                for i in range(1, len(filename_parts) - 1):
                    current_filename += '.' + filename_parts[i]
                extension = filename_parts[len(filename_parts) - 1].lower()
                os.rename(
                    os.path.join(zipped_dir, file),
                    os.path.join(zipped_dir,
                                 current_filename + '.' + extension))
                if important_file_index < 0:
                    file_index += 1
                    if extension in ['dae', 'obj']:
                        important_file_name = current_filename
                        important_file_index = file_index
                        file_type = extension

            if important_file_index < 0:  # user uploaded unrecognized file
                errors.append(gettext('Unrecognized model file format'))
                os.remove(
                    os.path.join(app_dir, 'static/models/building_models',
                                 data_file))
                try:
                    shutil.rmtree(
                        os.path.join(
                            app_dir, 'static/models/building_models',
                            uploaded_file['filename_without_extension'])
                    )  #delete unzipped folder
                except:
                    pass
                return render_template("users/add_building_model.html",
                                       errors=errors), 400
            if file_type == 'obj':  # important file is obj inside a zipped => objmtl
                file_type = 'objmtl'

            addition_information = {
                'original_filename': important_file_name,
                'directory': uploaded_file['filename_without_extension'],
                'camera_x': 30,
                'camera_y': 250,
                'camera_z': 350,
                "camera_lookat_x": 31,
                "camera_lookat_y": 222,
                "camera_lookat_z": 366
            }
        else:
            file_type = uploaded_file['extension']
            addition_information = {
                'original_filename':
                uploaded_file['filename_without_extension'],
                'directory': '',
                'camera_x': 30,
                'camera_y': 250,
                'camera_z': 350,
                "camera_lookat_x": 31,
                "camera_lookat_y": 222,
                "camera_lookat_z": 366
            }

        building_model = BuildingModel(
            name, data_file, g.user, addition_information=addition_information)
        building_model.file_type = file_type
        building_model.addition_information = json.dumps(addition_information,
                                                         ensure_ascii=False)
        db.session.add(building_model)
        db.session.commit()
        return redirect(
            url_for('sketchup.view_building_model', id=building_model.id))
def spaces():
    print("     list - To list all spaces")
    print("--------------------------------------")
    print("     sno - To list number of spaces")
    print("--------------------------------------")
    print("     create - To create a new space")
    print("--------------------------------------")
    print("     help - To view more")
    print("--------------------------------------")
    print("Advanced features will be added soon")
    print("Kindly send any suggestions or bugs to - [email protected] ")
    print("I will be very excited to add it")
    while True:
        ex = input("Spaces-> ")
        cmd = ex.lower()
        if cmd.strip() == "list":
            app.count()
        elif cmd == "sno":
            print("Available Spaces: " + str(app.amount_of_spaces))
        elif cmd == "create":
            name = input("Name: ")
            if len(name) > 0:
                region = input("Region: ")
                app.create(name, region)
        elif cmd == "help":
            app.help()
        elif cmd[:3] == "use":
                try:
                    use, space, region = cmd.split(" ")
                    if region in regions:
                        app.use(space, region)
                    else:
                        print("Invalid region")
                        for region in regions:
                            print("[+] " + region)
                        print("Usage: use space_name region_name [ex: use my-tes-space nyc3]")

                except:
                    print("Usage: use space_name region_name [ex: use my-tes-space nyc3]")
        elif cmd[:7] == 'destroy':

            print("Format: space_name region [Ex: my-test-space nyc3]")
            space_name = input("Please type the name. not the number -> ")
            print("We are still working on a security feature to make the user ...")
            print("... is 100% sure that they know what they are doing ")
            print("Sorry :( ")
        elif cmd[:6] == "upload":
            try:
                new = cmd[7:]
                a, b, c = new.split(":")
                space = a.strip()
                region, file = b.split("->")
                detect = os.path.isfile(str(file))
                if detect:
                    if len(c) > 0:
                        app.upload_file(file, space, region, c)
                    else:
                        print("Invalid FileName to put on the space")
                        print("Usage: upload space_name:region->file_on_local_disk:name_when_uploaded")
                        print("EX: upload myspace:nyc3->my-file-from-disk:output-name.txt")
                        print("Type 'list' to view spaces")
                else:
                    print("File Not Found")
                    print("Usage: upload space_name:region->file_on_local_disk:name_when_uploaded")
                    print("EX: upload myspace:nyc3->my-file-from-disk:output-name.txt")
                    print("Type 'list' to view spaces")
            except:
                print("Error")
                print("Usage: upload space_name:region->file_on_local_disk:name_when_uploaded")
                print("EX: upload myspace:nyc3->my-file-from-disk:output-name.txt")
                print("Type 'list' to view spaces")
        elif cmd[:7] == "list in":
            try:
                spaces_a = ['ams3', 'nyc3', 'sgp1', 'sfo2']
                l, a, space_name, region = cmd.split(" ")
                if region.strip() in spaces_a:
                    app.show(space_name, region)
                else:
                    print('Invalid Space_region')
                    print('Available Regions')
                    for region in spaces_a:
                        print("[+] " + region)
                    print("Usage: [ex: list in my-test-space nyc3]")
            except:
                print("Usage: list in space_name region_name [ex: list in my-test-space nyc3]")
        else:
            print("Unknow Command")