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-cascade8-filename-enforcer
  • Create a python file, e.g. test.py
  • Inside the file, add the following content
from cascade8_filename_enforcer.enforcer 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. 
import os
# 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"
site_id = "id_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) 
publish_result = rule_enforcer.publish_site(site_id)
print(publish_result)

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.3.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

File details

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

File metadata

  • Download URL: py-cascade8-filename-enforcer-0.0.3.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.3.tar.gz
Algorithm Hash digest
SHA256 2b147c375242ebea99df983d7b5e33f421a7f517e1177f05a9fd2adbffec808a
MD5 1d92e7eca5c0868b9b7d93645c51b625
BLAKE2b-256 8fda423368439cafff68837292f3ce9593b77c79bca4d5983d19622e9bdd81c6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: py_cascade8_filename_enforcer-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 8.5 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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 76e1074decf7422b0a972fee9282072a2cd6942258d463fce3eb94ec1b994327
MD5 41907cadf8c74081b90be5351e380cb2
BLAKE2b-256 f976b97cc778aee8ce5f7f6ea0955544d69e57095dadceec6a0863616fad90dc

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