Last active
February 28, 2021 04:45
-
-
Save liviocunha/a0170fb0c5b04cbf8f19a444c622359a to your computer and use it in GitHub Desktop.
django boilerplate simple
This file contains 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
# V 0.0.1 | |
# 2021-02-28 | |
# Inspired by Regis do Python - https://github.com/rg3915 | |
# Shell script to create a very simple Django project. | |
# This script require Python 3.x and pyenv | |
# Settings.py is config to Django 2.2.19 | |
# The project contains: | |
# Settings config | |
# Admin config | |
# Download: | |
# git clone https://gist.github.com/a0170fb0c5b04cbf8f19a444c622359a.git /tmp/boilerplatesimple | |
# cp /tmp/boilerplatesimple/boilerplatesimple.sh . | |
# Usage: | |
# Type the following command, you can change the project name. | |
# source boilerplatesimple.sh myproject | |
# Colors | |
red=`tput setaf 1` | |
green=`tput setaf 2` | |
reset=`tput sgr0` | |
PROJECT=${1-myproject} | |
echo "${green}>>> The name of the project is '$PROJECT'.${reset}" | |
echo "${green}>>> Creating README.md${reset}" | |
cat << EOF > README.md | |
## This project was done with: | |
* Python 3.8.2 | |
* Django 2.2.19 | |
## How to run project? | |
* Clone this repository. | |
* Create virtualenv with Python 3. | |
* Active the virtualenv. | |
* Install dependences. | |
* Run the migrations. | |
\`\`\` | |
git clone https://github.com/liviocunha/myproject.git | |
cd myproject | |
python3 -m venv .venv | |
source .venv/bin/activate | |
pip install -r requirements.txt | |
python contrib/env_gen.py | |
python manage.py migrate | |
\`\`\` | |
EOF | |
echo "${green}>>> Creating virtualenv${reset}" | |
python3 -m venv .venv | |
echo "${green}>>> .venv is created${reset}" | |
# active | |
sleep 2 | |
echo "${green}>>> activate the .venv${reset}" | |
source .venv/bin/activate | |
PS1="(`basename \"$VIRTUAL_ENV\"`)\e[1;34m:/\W\e[00m$ " | |
sleep 2 | |
# installdjango | |
echo "${green}>>> Installing the Django${reset}" | |
pip install -U pip | |
pip install django==2.2.19 dj-database-url django-widget-tweaks python-decouple django-extensions | |
pip freeze > requirements.txt | |
# Create contrib/env_gen.py | |
echo "${green}>>> Creating the contrib/env_gen.py${reset}" | |
mkdir contrib | |
cat << EOF > contrib/env_gen.py | |
""" | |
Python SECRET_KEY generator. | |
""" | |
import random | |
chars = "abcdefghijklmnopqrstuvwxyz01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()?" | |
size = 50 | |
secret_key = "".join(random.sample(chars, size)) | |
chars = "abcdefghijklmnopqrstuvwxyz01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ@#$%_" | |
size = 20 | |
password = "".join(random.sample(chars, size)) | |
CONFIG_STRING = """ | |
DEBUG=True | |
SECRET_KEY=%s | |
ALLOWED_HOSTS=127.0.0.1, .localhost | |
#DATABASE_URL=postgres://USER:PASSWORD@HOST:PORT/NAME | |
#DB_NAME= | |
#DB_USER= | |
#DB_PASSWORD=%s | |
#DB_HOST=localhost | |
#DEFAULT_FROM_EMAIL= | |
#EMAIL_BACKEND=django.core.mail.backends.smtp.EmailBackend | |
#EMAIL_HOST= | |
#EMAIL_PORT= | |
#EMAIL_HOST_USER= | |
#EMAIL_HOST_PASSWORD= | |
#EMAIL_USE_TLS=True | |
""".strip() % (secret_key, password) | |
# Writing our configuration file to '.env' | |
with open('.env', 'w') as configfile: | |
configfile.write(CONFIG_STRING) | |
print('Success!') | |
print('Type: cat .env') | |
EOF | |
echo "${green}>>> Run contrib/env_gen.py${reset}" | |
python3 contrib/env_gen.py | |
echo "${green}>>> Creating .gitignore${reset}" | |
cat << EOF > .gitignore | |
__pycache__/ | |
*.py[cod] | |
*.sqlite3 | |
*.env | |
*.DS_Store | |
.venv/ | |
staticfiles/ | |
.ipynb_checkpoints/ | |
EOF | |
# createproject | |
echo "${green}>>> Creating the project '$PROJECT' ...${reset}" | |
django-admin.py startproject $PROJECT . | |
cd $PROJECT | |
echo "${green}>>> Creating the app 'core' ...${reset}" | |
python3 ../manage.py startapp core | |
# up one level | |
cd .. | |
# ********** EDITING FILES ********** | |
echo "${green}>>> Editing settings.py${reset}" | |
# delete lines | |
sed -i '23,28d' $PROJECT/settings.py | |
# write new lines | |
sed -i '14i from decouple import config, Csv' $PROJECT/settings.py | |
sed -i '15i from dj_database_url import parse as dburl' $PROJECT/settings.py | |
sed -i "25i SECRET_KEY = config('SECRET_KEY')\n" $PROJECT/settings.py | |
sed -i "27i # SECURITY WARNING: don't run with debug turned on in production!" $PROJECT/settings.py | |
sed -i "28i DEBUG = config('DEBUG', default=False, cast=bool)\n" $PROJECT/settings.py | |
sed -i "30i ALLOWED_HOSTS = config('ALLOWED_HOSTS', default=[], cast=Csv())" $PROJECT/settings.py | |
sed -i "42i \ \ \ \ 'django_extensions'," $PROJECT/settings.py | |
sed -i "43i \ \ \ \ '$PROJECT.core'" $PROJECT/settings.py | |
# delete lines | |
sed -i '80,85d' $PROJECT/settings.py | |
# write new lines | |
sed -i "80i default_dburl = 'sqlite:///' + os.path.join(BASE_DIR, 'db.sqlite3')" $PROJECT/settings.py | |
sed -i "81i DATABASES = {" $PROJECT/settings.py | |
sed -i "82i \ \ \ \ 'default': config('DATABASE_URL', default=default_dburl, cast=dburl)," $PROJECT/settings.py | |
sed -i "83i \}" $PROJECT/settings.py | |
echo "STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')" >> $PROJECT/settings.py | |
echo "${green}>>> Creating core/urls.py${reset}" | |
cat << EOF > $PROJECT/core/urls.py | |
from django.urls import path | |
# from $PROJECT.core import views as v | |
app_name = 'core' | |
urlpatterns = [ | |
# path('', v.index, name='index'), | |
] | |
EOF | |
echo "${green}>>> Editing urls.py${reset}" | |
cat << EOF > $PROJECT/urls.py | |
from django.urls import include, path | |
from django.contrib import admin | |
urlpatterns = [ | |
path('', include('$PROJECT.core.urls', namespace='core')), | |
path('admin/', admin.site.urls), | |
] | |
EOF | |
# migrate | |
python3 manage.py makemigrations | |
python3 manage.py migrate | |
echo -n "Create superuser? (y/N) " | |
read answer | |
if [ "$answer" == "y" ]; then | |
echo "${green}>>> Creating a 'admin' user ...${reset}" | |
echo "${green}>>> The password must contain at least 8 characters.${reset}" | |
echo "${green}>>> Password suggestions: demodemo${reset}" | |
python manage.py createsuperuser --username='admin' --email='' | |
fi | |
echo "${red}>>> Important: Dont add .env in your public repository.${reset}" | |
echo "${red}>>> KEEP YOUR SECRET_KEY AND PASSWORDS IN SECRET!!!\n${reset}" | |
echo "${green}>>> Done${reset}" | |
echo "${red}>>> Finally, delete the boilerplatesimple.sh\n${reset}" | |
# https://www.gnu.org/software/sed/manual/sed.html |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment