Skip to content

Instantly share code, notes, and snippets.

@Hypro999
Last active October 26, 2021 06:26
Show Gist options
  • Save Hypro999/36f94c41da59b61efae0b9c14af55e71 to your computer and use it in GitHub Desktop.
Save Hypro999/36f94c41da59b61efae0b9c14af55e71 to your computer and use it in GitHub Desktop.
A small script to do some initialization for your django app.
""" To be executed from within your django application for extra initialization """
import os
def touch_init(directory_name, alt="__init__.py"):
target = os.path.join(directory_name, alt)
with open(target, "a+") as f:
print("touching file: {}".format(target))
f.close()
def check_and_add_directory(app_path, directory_name):
"""
arguments: (String app_path, String directory_name)
functionality: check to see if that directory exists in the app and if it
doesn't then add it .
return type: String to the directory path
"""
target_path = os.path.join(app_path, directory_name)
if not os.path.exists(target_path):
print("creating directory: {}".format(target_path))
os.makedirs(target_path)
return target_path
if __name__ == "__main__":
app_path = os.path.dirname(os.path.abspath(__file__))
base_dir, app_name = os.path.split(app_path)
# static resources folder(s)
static = os.path.join('static', app_name)
static_folders = [static, os.path.join(static, 'css'), os.path.join(static, 'js'), os.path.join(static, 'img'), os.path.join(static, 'doc'), os.path.join(static, 'plugins')]
for folder in static_folders:
check_and_add_directory(app_path, folder)
# templates
templates = os.path.join('templates', app_name)
check_and_add_directory(app_path, templates)
# signals
touch_init(check_and_add_directory(app_path, "signals"))
# models
touch_init(check_and_add_directory(app_path, "models"))
# templatetags
touch_init(check_and_add_directory(app_path, 'templatetags'))
# utils
touch_init(check_and_add_directory(app_path, "utils"))
# middleware
touch_init(check_and_add_directory(app_path, "middleware"))
# serializers
touch_init(check_and_add_directory(app_path, "serializers"))
# urls
touch_init(app_path, alt="urls.py")
print("done.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment