Skip to main content

A Python package utilizing the Cascade CMS 8 REST API to enforce file name rules, namely 1) all lowercase, 2) spaces replaced with hyphens, 3) no trailing spaces

Project description

Cascade CMS Filename Rule Enforcer

This package was developed to automate the enforcement of file naming conventions in an enterprise instance of Cascade CMS 8 for the College of Charleston, but has been re-written in a way that allows you to set the configuration for your own Cascade CMS environment. With over 100 sites to manage, and each site containing improperly named resources that were allowed in the previous Cascade Server 7 from which we migrated, we needed a way of automatically detecting improper file names (file names with spaces and capitalized letters) and fixing them dynamically. This package handles that automation in an object-oriented way.

How To Use

You can refer to the test() method here if you want a more extensive example of usage, but here is the basic flow of using the package.

  • I recommend first creating and activating a virtual environment:
  • python3.6 -m venv venv && source venv/bin/activate
  • Next, install the package using pip
  • pip install py-cascadecms-fname-enforcer
  • Create a python file, e.g. test.py
  • Inside the file, add the following content
from c8ruleenforcer import CascadeCMSFileNameRuleEnforcer
# specific to my example, I need quote to correctly parse 
# the password from the environment variable
from requests.utils import quote 
# Set your configuration variables
# I like to use os.environ.get() to pull from environment variables. 
# There is a start-dev-template.sh script you can use to set the values for these.
cpass = quote(os.environ.get('CASCADE_CMS_PASS',''))
cuser = os.environ.get('CASCADE_CMS_USER','')
# The base URL of the Cascade 8 REST API. THIS SHOULD END WITH /api/v1
restapi= os.environ.get('CASCADE_CMS_REST_API_BASE','') 

# Create a rule enforcer 
rule_enforcer = CascadeCMSFileNameRuleEnforcer(
    cuser=cuser, cpass=cpass, restapi=restapi
)
site_name = "name_of_your_site"
# Call the recursive traversal function that will also 
# fix your improperly named assets as well as return a list of 
# the ones it fixes in the form of: 
[
    {
        "old": "old BAD name.png",
        "new": "old-bad-name.png"
    },
    {
        "old": "old BAD name 2.png",
        "new": "old-bad-name-2.png"
    },... 
] 
assets_fixed = rule_enforcer.traverse(
    current_parent_folder=f'{site_name}',
    site_full_assets_list=[], # initialize empty list 
    # sites for which you want to skip enforcement
    skip_sites=["_Auto-Migrated Global_", "_skeleton.cofc.edu"] 
)
print(assets_fixed) 

The following is a more elaborate use of the package to iterate over many sites and keep a record of what was changed for each in JSON format.

def main(): 
    """ Production call - loop through all Cascade CMS sites and 
    fix improper filenames, keeping a record of the resources changed in 
    a local JSON file """ 
    # Create rule enforcer
    cpass = quote(os.environ.get('CASCADE_CMS_PASS',''))
    cuser = os.environ.get('CASCADE_CMS_USER','')
    restapi= os.environ.get('CASCADE_CMS_REST_API_BASE','')
    rule_enforcer = CascadeCMSFileNameRuleEnforcer(
        cpass=cpass, cuser=cuser, restapi=restapi
    ) 
    site_dicts = []
    for s in rule_enforcer.get_all_sites():
        site_name = s['path']['path']
        site_id = s['id'] 
        # Start with the base of site_name/content. initialize a
        print(f"Beginning scan for invalid filenames in site {site_name}")
        site_dictionary = {
            f'{site_name}': {
                'bad_assets': rule_enforcer.traverse(
                    current_parent_folder=f'{site_name}/content',
                    site_full_assets_list=[], # always empty initially
                    skip_sites=["_Auto-Migrated Global_", "_skeleton.cofc.edu"] # sites to skip enforcement
                )
            } 
        }
        site_dictionary[site_name]['publish_result'] = rule_enforcer.publish_site(site_id) 
        site_dicts.append(site_dictionary)
        with open('site_read.json', 'w') as f:
            json.dump(site_dicts, f)
        print(f"Completed scan of site {site_name}")

if __name__ == "__main__":
    # One site: redesign.cofc.edu 
    # test()
    # All sites!
    main()

Notes

  • If your site(s) does not have a root /content folder (e.g. sitename.edu/content/* in Cascade), be sure to remove the /content portion of the current_parent_folder argument.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

py-cascade8-filename-enforcer-0.0.2.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file py-cascade8-filename-enforcer-0.0.2.tar.gz.

File metadata

  • Download URL: py-cascade8-filename-enforcer-0.0.2.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.6.12

File hashes

Hashes for py-cascade8-filename-enforcer-0.0.2.tar.gz
Algorithm Hash digest
SHA256 155e36b9877ff167a42977bb144475a596a00ff4dcf6d78083ce27b723d09e76
MD5 7441ff44b514059fa01c854f909e0814
BLAKE2b-256 b9afafa7e16a13ea760182681150d368da758c4d5f1e7b9eafb17f3f254c344e

See more details on using hashes here.

File details

Details for the file py_cascade8_filename_enforcer-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: py_cascade8_filename_enforcer-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 20.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.6.12

File hashes

Hashes for py_cascade8_filename_enforcer-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 eb8346d900e80199f04dbd9285ac308001d12e7d9ace9815270e6b3590cee51a
MD5 5078f0043c475fe0c35668707d4a9130
BLAKE2b-256 7939c7f2fa3fd7d7b5c179a0fe68009f6d991a68b2d29c72d2c5aeaa83807015

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page