Last active
December 22, 2017 21:27
-
-
Save oleoneto/99415d75307507fb9e260b10c301da00 to your computer and use it in GitHub Desktop.
Updated version of the shell script to automate the creation of Django projects with their respective apps. This script also initializes a local git repository.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Requirements: Python3.6, Django and Git | |
| # A handy script for creating Django Projects and their respective applications | |
| # Usage: ./djangoproject.sh PROJECT_NAME APP_NAME1 APP_NAME2 APP_NAME3 APP_NAME4 | |
| SITE=$1 | |
| firstapp=$2 | |
| secondapp=$3 | |
| thirdapp=$4 | |
| fourthapp=$5 | |
| print_usage(){ | |
| echo "newdjango: Creates a django project and it specified apps." | |
| echo "usage: newdjango PROJECTNAME" | |
| } | |
| print_structure(){ | |
| echo "newdjango: Directory structure." | |
| echo "├── APP1 | |
| │ ├── __init__.py | |
| │ ├── admin.py | |
| │ ├── apps.py | |
| │ ├── migrations | |
| │ │ └── __init__.py | |
| │ ├── models.py | |
| │ ├── tests.py | |
| │ └── views.py | |
| ├── PROJECT | |
| │ ├── __init__.py | |
| │ ├── settings.py | |
| │ ├── urls.py | |
| │ └── wsgi.py | |
| ├── _media | |
| │ └── APP1 | |
| │ └── uploads | |
| ├── _static | |
| │ ├── css | |
| │ │ └── master.css | |
| │ ├── img | |
| │ ├── js | |
| │ │ └── master.js | |
| │ └── video | |
| ├── _templates | |
| │ ├── APP1 | |
| │ │ └── APP1.html | |
| │ ├── masters | |
| │ │ ├── index.html | |
| │ │ ├── login.html | |
| │ │ ├── logout.html | |
| │ │ └── master.html | |
| │ └── snippets | |
| │ ├── snip_blog_author.html | |
| │ ├── snip_blog_post.html | |
| │ ├── snip_error_http.html | |
| │ ├── snip_error_report.html | |
| │ ├── snip_main_footer.html | |
| │ ├── snip_main_header.html | |
| │ ├── snip_main_nav.html | |
| │ ├── snip_media_carousel.html | |
| │ ├── snip_media_gallery.html | |
| │ ├── snip_media_viewer_image.html | |
| │ └── snip_media_viewer_video.html | |
| └── manage.py" | |
| } | |
| print_help(){ | |
| echo "newdjango: Generates a django project directory with all the necessay files and subdirectories." | |
| echo " The setup makes and commits migrations to the database as well as enables a .git repository." | |
| echo " At the end of the setup, a server is also run on port 3009." | |
| } | |
| make_main(){ | |
| echo "$1" | |
| django-admin startproject $SITE | |
| cd $SITE | |
| } | |
| make_static(){ | |
| # Generating static directory | |
| mkdir _static | |
| mkdir -p _static/css | |
| touch _static/css/master.css | |
| mkdir -p _static/js | |
| touch _static/js/master.js | |
| mkdir -p _static/img | |
| mkdir -p _static/video | |
| } | |
| make_templates_and_snippets(){ | |
| # Make templates directory | |
| mkdir _templates | |
| cd _templates | |
| # master html files... | |
| mkdir masters | |
| cd masters | |
| touch index.html | |
| touch master.html | |
| touch login.html | |
| touch logout.html | |
| cd .. | |
| # html snippets... | |
| mkdir snippets | |
| cd snippets | |
| # __main site | |
| touch snip_main_nav.html | |
| touch snip_main_header.html | |
| touch snip_main_footer.html | |
| # ___blog | |
| touch snip_blog_post.html | |
| touch snip_blog_author.html | |
| #____media viewers | |
| touch snip_media_carousel.html | |
| touch snip_media_gallery.html | |
| touch snip_media_viewer_video.html | |
| touch snip_media_viewer_image.html | |
| #___system error | |
| touch snip_error_http.html | |
| touch snip_error_report.html | |
| cd .. | |
| } | |
| make_migrations_and_create_user(){ | |
| # Setting up migrations | |
| echo "Setting up migrations..." | |
| python3.6 manage.py migrate | |
| # Creating site user | |
| echo "Create superuser..." | |
| python3.6 manage.py createsuperuser | |
| } | |
| make_git_repo(){ | |
| # Setting up git | |
| echo "Initializing git..." | |
| git init | |
| git add * | |
| git commit -m "Initial commit" | |
| git checkout -b development | |
| } | |
| run_server(){ | |
| # Run the Django server | |
| echo "Running server on port 3009" | |
| python3.6 manage.py runserver | |
| } | |
| if [ "$SITE" ] | |
| then | |
| if [ "$SITE" = "-h" ]; then | |
| print_help | |
| elif [ "$SITE" = "-st" ]; then | |
| print_structure | |
| else | |
| make_main | |
| make_static | |
| make_templates_and_snippets | |
| # Creating apps and their respective html files | |
| if [ "$firstapp" ]; then | |
| echo "Creating $firstapp..." | |
| cd .. | |
| django-admin startapp $firstapp | |
| mkdir -p _media/$firstapp/uploads | |
| mkdir -p _templates/$firstapp | |
| cd _templates | |
| touch $firstapp/$firstapp.html | |
| fi | |
| if [ "$secondapp" ]; then | |
| echo "Creating $secondapp..." | |
| cd .. | |
| django-admin startapp $secondapp | |
| mkdir -p _media/$secondapp/uploads | |
| mkdir -p _templates/$secondapp | |
| cd _templates | |
| touch $secondapp/$secondapp.html | |
| fi | |
| if [ "$thirdapp" ]; then | |
| echo "Creating $thirdapp..." | |
| cd .. | |
| django-admin startapp $thirdapp | |
| mkdir -p _media/$thirdapp/uploads | |
| mkdir -p _templates/$thirdapp | |
| cd _templates | |
| touch $thirdapp/$thirdapp.html | |
| fi | |
| if [ "$fourthapp" ]; then | |
| echo "Creating $fourthapp..." | |
| cd .. | |
| django-admin startapp $fourthapp | |
| mkdir -p _media/$fourthapp/uploads | |
| mkdir -p _templates/$fourthapp | |
| cd _templates | |
| touch $fourthapp/$fourthapp.html | |
| fi | |
| # Back to main project directory | |
| cd .. | |
| # end of project creation... | |
| echo "Project: $SITE created in $PWD." | |
| make_migrations_and_create_user | |
| make_git_repo | |
| run_server | |
| fi | |
| else | |
| print_usage | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment