Exemple #1
0
 def yaml_check(self):
     """
     Checks for existence of YAML file in Cloud storage.
     Returns: True iff YAML with workout_type exists
     """
     if self.workout_type:
         workout_exists = False
         yaml_bucket = storage_client.get_bucket(
             workout_globals.yaml_bucket)
         bucket = storage_client.get_bucket(yaml_bucket)
         for blob in bucket.list_blobs():
             if blob.name == ("yaml-build-files/" + self.workout_type +
                              ".yaml"):
                 workout_exists = True
         return workout_exists
def store_student_upload(workout_id, upload, index):
    upload_bucket = str(project) + "_assessment_upload"
    bucket = storage_client.get_bucket(upload_bucket)
    new_blob = bucket.blob(
        str(workout_id) + '/' + secure_filename(str(index)) + '.png')
    new_blob.upload_from_file(upload, content_type=upload.content_type)
    return new_blob.public_url
def parse_workout_yaml(yaml_filename):
    """
    Pull and validate yaml contents from a designated project cloud bucket
    :param yaml_filename: Both the work type and filename of the yaml file in the cloud bucket
    :return: The yaml string from the cloud bucket
    """
    bucket = storage_client.get_bucket(workout_globals.yaml_bucket)
    blob = bucket.get_blob(workout_globals.yaml_folder + yaml_filename +
                           ".yaml")
    if not blob:
        cloud_log(LogIDs.MAIN_APP, f"YAML file not found: {yaml_filename}",
                  LOG_LEVELS.ERROR)
        raise FileNotFoundError
    yaml_string = blob.download_as_string()
    try:
        y = yaml.safe_load(yaml_string)
    except yaml.YAMLError as err:
        cloud_log(LogIDs.MAIN_APP,
                  f"Error parsing specified yaml file: {yaml_filename}",
                  LOG_LEVELS.ERROR)
        if hasattr(err, 'problem_mark'):
            mark = err.problem_mark
            cloud_log(LogIDs.MAIN_APP,
                      f"Error position: ({mark.line + 1}:{mark.column + 1})",
                      LOG_LEVELS.ERROR)
        raise
    return y
Exemple #4
0
def get_custom_base():
    bucket = storage_client.get_bucket(workout_globals.yaml_bucket)
    folder_path = './templates/{}-base.html'.format(project)
    blob = bucket.get_blob('base_template/{}-base.html'.format(project))
    if blob:
        blob.download_to_filename(folder_path)
        return '{}-base.html'.format(project)
    else:
        return False
Exemple #5
0
def get_custom_logo():
    bucket = storage_client.get_bucket(workout_globals.yaml_bucket)
    folder_path = './static/logo/'
    blob = bucket.get_blob('logo/logo.png')
    if blob:
        if not path.exists(folder_path):
            makedirs(folder_path)
        destination_path = './static/{}'.format(blob.name)

        blob.download_to_filename(destination_path)
        return destination_path
    else:
        return False
def store_background_color(color_code):
    css_string = ':root{--main_color: %s}' % (color_code)
    temp_css = open('temp.css', 'w')
    temp_css.write(css_string)
    temp_css.close()

    bucket = storage_client.get_bucket(workout_globals.yaml_bucket)
    new_blob = bucket.blob('color/{}-base.css'.format(project))
    #Prevent GCP from serving a cached version of this file
    new_blob.cache_control = 'private'
    new_blob.upload_from_string(css_string, content_type='text/css')

    remove('temp.css')
def parse_workout_yaml(workout_type):
    """
    Pull and validate yaml contents from a designated project cloud bucket
    :param workout_type: Both the work type and filename of the yaml file in the cloud bucket
    :return: The yaml string from the cloud bucket
    """
    # Open and read YAML file
    print('Loading config file')
    # get bucket with name
    bucket = storage_client.get_bucket(workout_globals.yaml_bucket)
    blob = bucket.get_blob(workout_globals.yaml_folder + workout_type +
                           ".yaml")
    if not blob:
        return False
    # convert to string
    yaml_string = blob.download_as_string()
    return yaml_string
def save_yaml_file(json_data):
    workout_name = json_data['workout']['name']
    workout_name.replace(" ", "")
    workout_name = workout_name.lower()
    workout_name += ".yaml"
    with open(workout_name, 'w') as temp:
        test = yaml.dump(json_data, temp)
        temp.close()

    bucket = storage_client.get_bucket(workout_globals.yaml_bucket)
    new_blob = bucket.blob(workout_globals.yaml_folder + workout_name)
    with open(workout_name, 'rb') as temp:
        new_blob.upload_from_file(temp,
                                  content_type='application/octet-stream')

        temp.close()
    if (path.exists(workout_name)):
        remove(workout_name)

    return True
Exemple #9
0
def store_custom_base(base):
    bucket = storage_client.get_bucket(workout_globals.yaml_bucket)
    new_blob = bucket.blob('base_template/{}-base.html'.format(project))
    new_blob.upload_from_file(base)
Exemple #10
0
def store_custom_logo(logo):
    bucket = storage_client.get_bucket(workout_globals.yaml_bucket)
    new_blob = bucket.blob('logo/logo.png')
    new_blob.upload_from_file(logo)
Exemple #11
0
def store_student_uploads(workout_id, uploads):
    bucket = storage_client.get_bucket('assessment-upload')
    for index, blob in enumerate(uploads):
        new_blob = bucket.blob(
            str(workout_id) + '/' + secure_filename(str(index)))
        new_blob.upload_from_file(blob, content_type=blob.content_type)
def upload_instruction_file(uploaded_file, folder, filename):
    bucket = storage_client.get_bucket(folder)
    new_blob = bucket.blob(filename)
    new_blob.cache_control = 'private'
    new_blob.upload_from_file(uploaded_file)