Advertisement
karmaEl

Django Project apps Generator. V0.2

Apr 29th, 2016
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 16.77 KB | None | 0 0
  1. #!/bin/bash
  2. clear
  3. printf "# Simple Bash Script\n"
  4. printf "# Purpose : Create Virtualenv, Update pip, install django, start django project\n"
  5. printf "# Recomend : terminal / console background color dark (black)\n"
  6. printf "# kael\n"
  7. printf " \n"
  8. printf " \n"
  9.  
  10. # cek virtualenv install or not
  11. PIP="which pip"
  12.  
  13. if [ $(exec $PIP) ];
  14. then
  15.     printf "[+] pip Installed"
  16.     printf "\n"
  17. else
  18.     printf "[+] Installing pip\n"
  19.     sudo apt-get install python-pip
  20.     printf "\n"
  21. fi
  22.  
  23. VENV="virtualenv --version"
  24. if [ $(exec $VENV) ];
  25. then
  26.     printf "[+] Virtualenv Installed Version : "
  27.     printf $(exec $VENV)
  28.     printf "\n"
  29. else
  30.     printf "[+] Installing Virtualenv\n"
  31.     pip install virtualenv
  32.     printf "\n"
  33.     printf "[+] Update Virtualenv\n"
  34.     pip install --upgrade virtualenv
  35.     printf "\n"
  36. fi
  37.  
  38. NPMV="npm --version"
  39. if [ $(exec $NPMV) ];
  40. then
  41.     printf "[+] npm Installed"
  42.     printf "\n"
  43. else
  44.   printf "[+] Installing NPM\n"
  45.        NPM="sudo apt-get install npm"
  46.        exec $NPM
  47.        printf "\n"
  48. fi
  49.    
  50. BV="bower --version"
  51. if [ ! $(exec $BV) ];
  52. then
  53.     printf "[+] Installing Bower\n"
  54.     BOWER="npm install -g bower"
  55.     exec $BOWER
  56.     printf "\n"
  57. else
  58.     printf "[+] Bower Installed Version "
  59.     printf $(exec $BV)
  60.     printf "\n"
  61. fi
  62.  
  63.  
  64. WDIR=pwd
  65. printf "[-] working directory : "
  66. $WDIR
  67. printf "[-] Masukkan Nama Virtualenv : "
  68. read venv
  69.  
  70. echo "[-] Creating Virtualev : "  $venv
  71. virtualenv $venv
  72. printf "\n"
  73. printf "[-] Moving To Virtualenv Directory \n"
  74. cd $venv
  75. printf "[-] working directory"
  76. pwd
  77. printf "\n"
  78. printf "[-] Activating Virtualenv" $venv
  79. source bin/activate
  80. printf "\n"
  81. printf "[-] Upgrade PIP\n"
  82. pip install --upgrade pip
  83. printf "[-] Installing Django\n"
  84. pip install django
  85. printf "\n\n"
  86. printf "[+] Virtualelnv (Finished)\n"
  87. printf "[+] Upgrade Pip (Finished)\n"
  88. printf "[+] Django Install (Finished)\n"
  89. printf "[+] To enter the new virtualenv follow this steps :\n"
  90. printf "[-] cd "
  91. pwd
  92. printf "\n"
  93. printf "[-] sudo su \n"
  94. printf "[-] source bin/activate\n"
  95. printf "\n"
  96.  
  97. mn="Masukkan Pilihan Anda : "
  98. pil=("[ 1 ] Create New Django Project"
  99.      "[ 2 ] Create New Django App"
  100.      "[ 3 ] Run Server ( 0.0.0.0:8000)"
  101.      "Quit"
  102.     )
  103. select pl in "${pil[@]}"
  104. do
  105.     case $pl in
  106.         "[ 1 ] Create New Django Project")
  107.             printf "[-] Enter Django Project Name : "
  108.             read newp
  109.             python bin/django-admin.py startproject $newp
  110.             printf "[+] Create Django Project : " $newp "\n"
  111.             printf "[-] Moving To Project Directory\n"
  112.             cd $newp
  113.             printf "[ Success !! ]\n"
  114.             printf "[+] Tambahkan 'DIRS': [BASE_DIR+'/templates',] pada \"settings.py > TEMPLATES\" \n"
  115.             printf "[+] Put - STATICFILES_DIR (settings.py)\n"
  116.             echo -e "#STATICFILES_DIRS = (" >> $newp/settings.py
  117.             echo -e "#    os.path.join(BASE_DIR,'static')," >> $newp/settings.py
  118.             echo -e "#)" >> $newp/settings.py
  119.             #printf "[+] Put - static directory\n"
  120.             #mkdir static
  121.             printf "[+] Put - templates directory\n"
  122.             mkdir templates
  123.             printf "[+] Installing Bootstrap from Bower \n"
  124.             bower install bootstrap
  125.             printf "[+] Creating Static Folder\n"
  126.             mv bower_components/bootstrap/dist static
  127.             rm -rf bower_components
  128.             # cp -R ~/Desktop/static/css static/css
  129.             # cp -R ~/Desktop/static/fonts static/fonts
  130.             # cp -R ~/Desktop/static/js static/js
  131.             printf "[+] Generating base.html templates\n"
  132.             echo -e "{% load staticfiles %}" >> templates/base.html
  133.             echo -e "<html>" >> templates/base.html
  134.             echo -e "<head>" >> templates/base.html
  135.             echo -e "    <script type=\"text/javascript\" src=\"{% static 'js/bootstrap.min.js' %}\"></script>" >> templates/base.html
  136.             echo -e "    <link rel=\"stylesheet\" type=\"text/css\" href=\"{% static 'css/bootstrap.min.css' %}\" />" >> templates/base.html
  137.             echo -e "    <title>List Event</title>" >> templates/base.html
  138.             echo -e "</head>" >> templates/base.html
  139.             echo -e "<body>" >> templates/base.html
  140.             echo -e "<div class=\"container\">" >> templates/base.html
  141.             echo -e "    <div class=\"row\">" >> templates/base.html
  142.             echo -e "        <div class=\"col-sm-12\">" >> templates/base.html
  143.             echo -e "        <nav class=\"navbar\">" >> templates/base.html
  144.             echo -e "            <ul class=\"nav navbar-nav\">" >> templates/base.html
  145.             echo -e "                <li class=\"active\"><a href=\"/class\">Home</a></li>" >> templates/base.html
  146.             echo -e "                <li><a href=\"/class/artikel\">Artikel</a></li>" >> templates/base.html
  147.             echo -e "            </ul>" >> templates/base.html
  148.             echo -e "        </nav>" >> templates/base.html
  149.             echo -e "" >> templates/base.html
  150.             echo -e "        {% block content %}" >> templates/base.html
  151.             echo -e "        {% endblock %}" >> templates/base.html
  152.             echo -e "        </div>" >> templates/base.html
  153.             echo -e "    </div>" >> templates/base.html
  154.             echo -e "</div>" >> templates/base.html
  155.             echo -e "</body>" >> templates/base.html
  156.             echo -e "</html>" >> templates/base.html
  157.  
  158.  
  159.             printf "[+] Generating form.html templates\n"
  160.             echo -e "{% extends 'base.html' %}" >> templates/form.html
  161.             echo -e "{% load cb_extras %}" >> templates/form.html
  162.             echo -e "<div class=\"col-sm-12\">" >> templates/form.html
  163.             echo -e "    {% block content %}" >> templates/form.html
  164.             echo -e "<form action=\"/class/update/{{ id }}/\" method=\"POST\">" >> templates/form.html
  165.             echo -e "{% csrf_token %}" >> templates/form.html
  166.             echo -e "    {% for frm in form %}" >> templates/form.html
  167.             echo -e "        <div class=\"form-group\">" >> templates/form.html
  168.             echo -e "            {{ frm.label }}" >> templates/form.html
  169.             echo -e "            {{ frm|addcss:"form-control" }}" >> templates/form.html
  170.             echo -e "        </div>" >> templates/form.html
  171.             echo -e "    {% endfor %}" >> templates/form.html
  172.             echo -e "    <div class=\"pull-right\">" >> templates/form.html
  173.             echo -e "        <input type=\"submit\" class=\"btn btn-primary\" value=\"Simpan\"> " >> templates/form.html
  174.             echo -e "        <a href=\"/class\" class=\"btn btn-default\">Batal</a> " >> templates/form.html
  175.             echo -e "    </div>" >> templates/form.html
  176.             echo -e "</form>" >> templates/form.html
  177.             echo -e "</div>" >> templates/form.html
  178.             echo -e "{% endblock %}" >> templates/form.html
  179.  
  180.             printf "\n\n"
  181.             printf "Press Enter To Show Options !! \n \n"
  182.             ;;
  183.         "[ 2 ] Create New Django App")
  184.             clear
  185.             printf "#==\n"
  186.             printf "# Create Django App\n"
  187.             printf "#==\n"
  188.             printf "\n"
  189.             printf "Enter Django App Name : "
  190.             read AppName
  191.             python manage.py startapp $AppName
  192.             printf "[ Create Success !! ]\n"
  193.             printf "[+] Generating Models.py \n"
  194.             echo -e "# Sample Class Models\n" >> $AppName/models.py
  195.             echo -e "#class <<NamaModel>>(models.Models):" >> $AppName/models.py
  196.             echo -e "#    field..1 = models.CharField(max_length=80)" >> $AppName/models.py
  197.             echo -e "#    field..2 = models.TextField()" >> $AppName/models.py
  198.             echo -e "#    class meta:" >> $AppName/models.py
  199.             echo -e "#        # app_label:'labelName'" >> $AppName/models.py
  200.             printf "[+] Generating Forms.py\n"
  201.             echo -e "from django import forms" >> $AppName/forms.py
  202.             echo -e "from .models import <<NamaModel>>" >> $AppName/forms.py
  203.             echo -e "class ArtikelForm(forms.ModelForm):" >> $AppName/forms.py
  204.             echo -e "    class Meta:" >> $AppName/forms.py
  205.             echo -e "        model = <<NamaModels>>" >> $AppName/forms.py
  206.             echo -e "        fields = ['<<field..1>>', '<<field..n>>']" >> $AppName/forms.py
  207.  
  208.             printf "[+] Generating Views.py \n"
  209.             echo -e "#Sample View Include pagination list content, detail content" >> $AppName/views.py
  210.             echo -e "#from django.http import HttpResponse" >> $AppName/views.py
  211.             echo -e "from django.shortcuts import render_to_response, redirect, render" >> $AppName/views.py
  212.             echo -e "from django.views.generic import View, CreateView, TemplateView, UpdateView, DetailView" >> $AppName/views.py
  213.             echo -e "#from django.template import RequestContext" >> $AppName/views.py
  214.             echo -e "#from cbase.forms import *" >> $AppName/views.py
  215.             echo -e "#from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger" >> $AppName/views.py
  216.             echo -e "#from django.core.context_processors import csrf" >> $AppName/views.py
  217.             echo -e "#from django.core.exceptions import ObjectDoesNotExist" >> $AppName/views.py
  218.             echo -e "#from django.core.urlresolvers import reverse_lazy" >> $AppName/views.py
  219.             echo -e "#" >> $AppName/views.py
  220.             echo -e "#class MyView(View):" >> $AppName/views.py
  221.             echo -e "#    def get(self, request):" >> $AppName/views.py
  222.             echo -e "#        list_artikel = Artikel.objects.all()" >> $AppName/views.py
  223.             echo -e "#        paginator = Paginator(list_artikel,3)" >> $AppName/views.py
  224.             echo -e "#       page = request.GET.get('page')" >> $AppName/views.py
  225.             echo -e "#        try:" >> $AppName/views.py
  226.             echo -e "#            lst_artikel = paginator.page(page)" >> $AppName/views.py
  227.             echo -e "#        except PageNotAnInteger:" >> $AppName/views.py
  228.             echo -e "#            lst_artikel = paginator.page(1)" >> $AppName/views.py
  229.             echo -e "#        except EmptyPage:" >> $AppName/views.py
  230.             echo -e "#            lst_artikel = paginator.page(paginator.num_pages)" >> $AppName/views.py
  231.             echo -e "#        return render_to_response('home.html',{'list_artikel': lst_artikel})" >> $AppName/views.py
  232.             echo -e "#" >> $AppName/views.py
  233.             echo -e "#class AboutMe(View):" >> $AppName/views.py
  234.             echo -e "#    nama = \"Eldo Greshard\"" >> $AppName/views.py
  235.             echo -e "#" >> $AppName/views.py
  236.             echo -e "#    def get(self, request):" >> $AppName/views.py
  237.             echo -e "#        return render_to_response('about.html',{'nama': self.nama,})" >> $AppName/views.py
  238.             echo -e "#" >> $AppName/views.py
  239.             echo -e "#class UbahAbout(AboutMe):" >> $AppName/views.py
  240.             echo -e "#    nama = \"Diubah\"" >> $AppName/views.py
  241.             echo -e "#" >> $AppName/views.py
  242.             echo -e "#class Artikel_view(View):" >> $AppName/views.py
  243.             echo -e "#    form_c = ArtikelForm" >> $AppName/views.py
  244.             echo -e "#    model_c = Artikel" >> $AppName/views.py
  245.             echo -e "#    def get(self, request):" >> $AppName/views.py
  246.             echo -e "#        context = RequestContext(request,{})" >> $AppName/views.py
  247.             echo -e "#        context['form'] = self.form_c" >> $AppName/views.py
  248.             echo -e "#        return render_to_response('artikel.html', context)" >> $AppName/views.py
  249.             echo -e "#" >> $AppName/views.py
  250.             echo -e "#class UbahArtikel(View):" >> $AppName/views.py
  251.             echo -e "#    form_c = ArtikelForm" >> $AppName/views.py
  252.             echo -e "#    model_c = Artikel" >> $AppName/views.py
  253.             echo -e "#    def get(self,request, id):" >> $AppName/views.py
  254.             echo -e "#        data = self.model_c.objects.get(id = int(id))" >> $AppName/views.py
  255.             echo -e "#        context = RequestContext(request, {})" >> $AppName/views.py
  256.             echo -e "#        context['form'] = self.form_c(" >> $AppName/views.py
  257.             echo -e "#            initial={" >> $AppName/views.py
  258.             echo -e "#                'judul': data.judul," >> $AppName/views.py
  259.             echo -e "#                'author': data.author," >> $AppName/views.py
  260.             echo -e "#                'artikel': data.artikel," >> $AppName/views.py
  261.             echo -e "#            }" >> $AppName/views.py
  262.             echo -e "#        ) " >> $AppName/views.py
  263.             echo -e "#        context['id'] = int(id)" >> $AppName/views.py
  264.             echo -e "#        return render_to_response(\"ubah.html\",context)" >> $AppName/views.py
  265.             echo -e "#" >> $AppName/views.py
  266.             echo -e "#class CgCreate(CreateView):" >> $AppName/views.py
  267.             echo -e "#    model = Artikel" >> $AppName/views.py
  268.             echo -e "#    fields = ['judul','author','artikel']" >> $AppName/views.py
  269.             echo -e "#    success_url=\"/class/artikel\"" >> $AppName/views.py
  270.             echo -e "#    template_name=\"artikel.html\"" >> $AppName/views.py
  271.             echo -e "#    def form_invalid(self, form):" >> $AppName/views.py
  272.             echo -e "#        return super(CgCreate, self).form_invalid(form)" >> $AppName/views.py
  273.             echo -e "#" >> $AppName/views.py
  274.             echo -e "#class CgUpdate(UpdateView):" >> $AppName/views.py
  275.             echo -e "#    model = Artikel" >> $AppName/views.py
  276.             echo -e "#    slug_field = 'id'" >> $AppName/views.py
  277.             echo -e "#    fields = ['judul','author','artikel']" >> $AppName/views.py
  278.             echo -e "#    success_url=\"/class/artikel\"" >> $AppName/views.py
  279.             echo -e "#" >> $AppName/views.py
  280.             echo -e "#class CgDetail(DetailView):" >> $AppName/views.py
  281.             echo -e "#    model = Artikel" >> $AppName/views.py
  282.             echo -e "#    template_name = 'detail.html'" >> $AppName/views.py
  283.             echo -e "#" >> $AppName/views.py
  284.             echo -e "#    def get_context_data(self, **kwargs):" >> $AppName/views.py
  285.             echo -e "#        context = super(CgDetail, self).get_context_data(**kwargs)" >> $AppName/views.py
  286.             echo -e "#        return context" >> $AppName/views.py
  287.  
  288.             printf "[+] Generating Custom Template Tags.py \n"
  289.             mkdir $AppName/templatetags
  290.             touch $AppName/templatetags/__init__.py
  291.             touch $AppName/templatetags/ct_$AppName.py
  292.             echo -e "#Sample Template Tags" >> $AppName/templatetags/ct_$AppName.py
  293.             echo -e "#from django import template" >> $AppName/templatetags/ct_$AppName.py
  294.             echo -e "#" >> $AppName/templatetags/ct_$AppName.py
  295.             echo -e "#register = template.Library()" >> $AppName/templatetags/ct_$AppName.py
  296.             echo -e "#" >> $AppName/templatetags/ct_$AppName.py
  297.             echo -e "#@register.filter(name='addcss')" >> $AppName/templatetags/ct_$AppName.py
  298.             echo -e "#def addcss(field, css):" >> $AppName/templatetags/ct_$AppName.py
  299.             echo -e "#    return field.as_widget(attrs={"class":css})" >> $AppName/templatetags/ct_$AppName.py
  300.  
  301.             printf "[+] Generating urls.py \n"
  302.             echo -e "#Sample urls.py" >> $AppName/urls.py
  303.             echo -e "#from django.conf.urls import url" >> $AppName/urls.py
  304.             echo -e "#from cbase.views import *" >> $AppName/urls.py
  305.             echo -e "#" >> $AppName/urls.py
  306.             echo -e "#urlpatterns = [" >> $AppName/urls.py
  307.             echo -e "#    url(r'^$', MyView.as_view() , name=\"index\")," >> $AppName/urls.py
  308.             echo -e "#    url(r'^about', UbahAbout.as_view(), name=\"about\")," >> $AppName/urls.py
  309.             echo -e "#    url(r'^artikel', Artikel_view.as_view(), name=\"artikel\")," >> $AppName/urls.py
  310.             echo -e "#    url(r'^simpan', CgCreate.as_view(), name=\"Create\")," >> $AppName/urls.py
  311.             echo -e "#    url(r'^ubah/(\d+)', UbahArtikel.as_view(),name=\"update\")," >> $AppName/urls.py
  312.             echo -e "#    url(r'^update/(?P<pk>\d+)/$', CgUpdate.as_view(), name=\"ubah\")," >> $AppName/urls.py
  313.             echo -e "#    url(r'^detail/(?P<pk>\d+)/$', CgDetail.as_view(), name='detail')," >> $AppName/urls.py
  314.             echo -e "#]" >> $AppName/urls.py
  315.  
  316.             printf "\n\n"
  317.             printf "Press Enter To Show Options !! \n \n"
  318.             ;;
  319.         "[ 3 ] Run Server ( 0.0.0.0:8000)")
  320.             printf "Starting Django Server http://localhost:8000\n"
  321.             fuser -k 8000/tcp
  322.             gnome-terminal -x sh -c "python manage.py runserver | less"  &
  323.             ;;
  324.         "Quit")
  325.             clear
  326.             break
  327.             ;;
  328.         *)
  329.             echo Pilihan Tidak Ditemukan
  330.             ;;
  331.     esac
  332. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement