Running Django on Google App Engine and setting up local environment - GitAmrita/AdmissionProfessorBackend GitHub Wiki

Install google-cloud-sdk

Download Google Cloud Package from the following location and extract the file to any location on your file system.
https://cloud.google.com/sdk/docs/
Run gcloud init to initialize the sdk.
./google-cloud-sdk/bin/gcloud init
Run the command so that your identity can be used as a proxy to test code calling APIs from that machine.
gcloud auth application-default login

Clone the repository in your local machine

git clone https://github.com/GitAmrita/AdmissionProfessorBackend.git
Go to the directory that contains the code:
cd into AdmissionProfessorBackend/admissionprofessor

Install cloud_sql_proxy

Install cloud_sql_proxyin the directory that contains the manage.py for the app which is AdmissionProfessorBackend/admissionprofessor.
Download proxy from
curl -o cloud_sql_proxy https://dl.google.com/cloudsql/cloud_sql_proxy.darwin.amd64
Make proxy execuable
chmod +x cloud_sql_proxy

Google cloud instance MySQL

Instance: admprof-instance user:root password:ap@people123
Go to the SDK directory and use the Cloud SDK from command-line to run the following command. Copy the connectionName value for the next step
gcloud beta sql instances describe admprofessor-instance
This step establishes a connection from your local computer to your Cloud SQL instance for local testing purposes. Keep the Cloud SQL Proxy running the entire time you test your application locally.
Initialize the cloud sql instance
cloud_sql_proxy -instances=admissionprofessorbackend:us-central1:admprofessor-instance=tcp:3306

Create database on MySQL

In a separate command tab open mySQL
mysql -h 127.0.0.1 -u root -p

CREATE USER 'root' IDENTIFIED BY 'ap@people123';
GRANT ALL ON *.* TO 'root';```      
Configure database settings (this is one time setting and changes are pushed to git)           

[START db_setup]

if os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine'): # Running on production App Engine, so connect to Google Cloud SQL using # the unix socket at /cloudsql/ DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'HOST': '/cloudsql/admissionprofessorbackend:us-central1:admprof-instance', 'NAME': 'admprofessor', 'USER': 'root', 'PASSWORD': 'ap@people123', } } else: # Running locally so connect to either a local MySQL instance or connect to # Cloud SQL via the proxy. To start the proxy via command line: # # $ cloud_sql_proxy -instances=[INSTANCE_CONNECTION_NAME]=tcp:3306 # # See https://cloud.google.com/sql/docs/mysql-connect-proxy DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'HOST': '127.0.0.1', 'PORT': '3306', 'NAME': 'admprofessor', 'USER': 'root', 'PASSWORD': 'ap@people123', } }

[END db_setup]


### To run app on local computer, create virtual environment and install dependencies. If you give any other name for  your virtual environment, please add it to gitignore.        
`virtualenv admvenv`      
`source admvenv/bin/activate`                  
`pip install -r requirements-vendor.txt -t lib/ `               
`pip install -r requirements.txt`                    

### Run django migrations to set up models     
`python manage.py makemigrations admprofapps`      
`python manage.py migrate`      

### Start local web server and enter the address. Your django app should work
`python manage.py runserver`    
`http://localhost:8000`

### Django admin console     
To create super user    
`python manage.py createsuperuser`      
It has already been created with credentials below      
user:`admin` password:`su@people123` email:`[email protected]`     
Log on to admin site    
`http://localhost:8000/admin/`

### Deploy the app to the app engine standard environment      
Gather all the static content into one folder. Make sure settings.py has the following setup       
`STATIC_ROOT = 'static'`
`STATIC_URL = '/static/'`      
This command moves all of the app's static files into the folder specified by STATIC_ROOT in settings.py.    
`python manage.py collectstatic`        
Make sure `appengine_configure` and the `app.yaml` file are in the same directory. AppEngine gets the path to look for dependencies from this file.        
Upload the app by running the following command from the directory that contains `app.yaml`     
`gcloud app deploy`      

### See the app run in cloud     
`https://admissionprofessorbackend.appspot.com`