Integrate Django with NodeJS?

There is some article talk about how to integrate Django with NodeJS. As we know, Django is great and powerful for certain tasks but in term of speed and multitask. We may need something else. I don’t know much about NodeJS but if we can let NodeJS handle realtime and multiplayer tasks. Django just focus on what it is great for (e.g. login authentication, and etc). Then maybe it is not a bad idea.

How to use both Django & NodeJS as backend for your application
http://www.cuelogic.com/blog/how-to-use-both-django-nodejs-as-backend-for-your-application/

Realtime Django Using Node.js and Socket.IO
http://www.maxburstein.com/blog/realtime-django-using-nodejs-and-socketio/

Posted in Django, NodeJS | Leave a comment

How to verify Authentication with Facebook Token

First we need to understand what is going on when user submit the request using their facebook account for login

1) User submit the REQUEST using their Facebook account
The mobile app or website will send a “Request” to Facebook

2) Facebook return “FB token” with some params back to the app

3) The mobile app / application will call the API communicate with the database. There is some params will be passed to the database via the API call at the same time. http://localhost:8000/api/social/convert-token

4) Once the database retrieve the API call with the FB token, the “Django Rest Framework Social Oauth” will “Create User + Access_token” in the database

5) Django Rest Framework social Oauth / database will return the “Access_token & Refresh_token” back to the application

Now, we understand the process / protocol and what information have been sent between app and the database. And how user get the access token and refresh token. It is time for testing. As we don’t have the application ready yet, we can use “POSTMAN” for testing purpose to verify our API and backend database.

First we need to get the FB token from Facebook
https://developers.facebook.com/tools/accesstoken/
Copy the “User Token”

In the POSTMAN, Use “POST” with the following Params

  • grant_type – convert_token
  • client_id
  • client_secret
  • backend – facebook
  • token – [ that’s the user token you just generate from the facebook ]

Then “Send”, if you can see the access token return from the database in JSON format. It means working fine. You should see user create in the Django database and there is access token associate with the user. However, the access token we used it doesn’t retrieve the email address of the user.

Screen Shot 2017-02-20 at 6.09.17 AM.png

In order to get the email address of the user, we need to get the access token from https://developers.facebook.com/tools/explorer/

Select “Get User Access Token” from the pop down list.

Screen Shot 2017-02-20 at 6.15.19 AM.png

And replace the previous token with the latest one which you just get it from https://developers.facebook.com/tools/explorer/

Send the request from the POSTMAN again. After that, if you check the user info in the Django again. You should be the user profile with email info now.

Screen Shot 2017-02-20 at 6.18.00 AM.png

To test the logout, it is easy.

It is similar for the Sign in process. In the POSTMAN, open another TAB and send the request with the following params

  • client_id
  • client_secret
  • token

If everything works fine, you should not expect to see anything return from the database.

Screen Shot 2017-02-20 at 6.21.03 AM.png

To confirm, check the Access tokens in the Django. The token should be removed

Posted in facebook, Postman, Python | Leave a comment

How to setup Facebook Oauth

When we create an Application, we should also consider to let people using their Facebook account for Sign-in purpose. In that case, we need to setup Facebook Oauth.

First you have to create developer account in Facebook.
Go to http://developers.facebook.com for registration.
Once you have an account, then you can create a New App.

Pick the name you like for “Display Name”
You also need to specific your email address, and Category.

screen-shot-2017-02-19-at-6-16-40-pm

App ID and App Secret will be created for this application

screen-shot-2017-02-19-at-6-20-59-pm

Then you need to add platform which you want your application to be used. In my case, I want to use facebook oauth for web site only. I select “Website”

screen-shot-2017-02-19-at-6-21-38-pm

For www option, there is a field for the URL. Just set it point to localhost host at this moment. Port is 8000 which it is django default TCP port

screen-shot-2017-02-19-at-6-22-08-pm

We have everything setup at the facebook side.
Now, we need to install django-rest-framework-social-oauth2 in the local host
For more information and detail about django-rest-framework. Here is the URL:

https://github.com/PhilipGarnero/django-rest-framework-social-oauth2

(foodtasker) tiger:foodtasker xxxxx$ pip install django-rest-framework-social-oauth2==1.0.5

Add django-rest-framework-social-oauth2 in the requirement.txt which we previously created for Heroku. So, we can tell Heroku what applications we need to install at Heroku

django-rest-framework-social-oauth2==1.0.5

In order to use django-rest-framework-social-oauth2 in Django, we need to modify the Settings.py. Below is the list of the things we have to add there. Otherwise, it wouldn’t work

INSTALLED_APPS = [
    'oauth2_provider',
    'social_django',
    'rest_framework_social_oauth2',
]

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                ::::
                'social_django.context_processors.backends',
                'social_django.context_processors.login_redirect',
            ],
        },
    },
]

AUTHENTICATION_BACKENDS = (
    'social_core.backends.facebook.FacebookOAuth2',
    'rest_framework_social_oauth2.backends.DjangoOAuth2',
    'django.contrib.auth.backends.ModelBackend',
)

# Facebook configuration
SOCIAL_AUTH_FACEBOOK_KEY = 'xxxxx'
SOCIAL_AUTH_FACEBOOK_SECRET = 'xxxxxx'

# Define SOCIAL_AUTH_FACEBOOK_SCOPE to get extra permissions from facebook. Email is not sent by default, to get it, you must request the email permission:
SOCIAL_AUTH_FACEBOOK_SCOPE = ['email']
SOCIAL_AUTH_FACEBOOK_PROFILE_EXTRA_PARAMS = {
    'fields': 'id, name, email'
}

Once all the things have been added, we can run the migrate commands again to refresh the change we just did

(foodtasker) tiger:foodtasker xxxxx$ python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, foodtaskerapp, oauth2_provider, sessions, social_django
Running migrations:
  Rendering model states... DONE
  Applying oauth2_provider.0001_initial... OK
  Applying oauth2_provider.0002_08_updates... OK
  Applying oauth2_provider.0003_auto_20160316_1503... OK
  Applying oauth2_provider.0004_auto_20160525_1623... OK
  Applying social_django.0001_initial... OK
  Applying social_django.0002_add_related_name... OK
  Applying social_django.0003_alter_email_max_length... OK
  Applying social_django.0004_auto_20160423_0400... OK
  Applying social_django.0005_auto_20160727_2333... OK
  Applying social_django.0006_partial... OK
(foodtasker) tiger:foodtasker xxxxx$ 

If you run the django development server again, and check the admin page.
You will see there are a few additional things have been added:

  • Django Oauth Toolkit
  • Social_Django

screen-shot-2017-02-19-at-6-43-20-pm

Select Application from “DJANGO_OAUTH_TOOLKIT” and add application. Fill in the form according to the pictures below. Client id and Client secret will be automatically generated by the system. No need to change it

screen-shot-2017-02-19-at-6-45-32-pm

 

 

Posted in facebook, Heroku, Python | Leave a comment

How to deploy Django app in Heroku

You just create a Django app, you want to publish it and show it to public. Heroku is one of the popular place you can host your site there. But you need to know how to deploy it. Here is the step-by-step procedure.

First you need to login to the Heroku

(foodtasker) tiger:foodtasker xxxxx$ heroku login
Enter your Heroku credentials.
Email: xxxxx@gmail.com
Password (typing will be hidden): 
Logged in as xxxxx@gmail.com
(foodtasker) tiger:foodtasker xxxxx$ 

Then create project at Heroku

(foodtasker) tiger:foodtasker xxxxx$ heroku create
Creating app... done, ⬢ thawing-reaches-94509
https://thawing-reaches-94509.herokuapp.com/ | https://git.heroku.com/thawing-reaches-94509.git
(foodtasker) tiger:foodtasker xxxxx$ 

You need to install “gunicorn” for web server running at Heroku

(foodtasker) tiger:foodtasker xxxxx$ pip install gunicorn==19.6.0
Collecting gunicorn==19.6.0
  Downloading gunicorn-19.6.0-py2.py3-none-any.whl (114kB)
    100% |████████████████████████████████| 122kB 5.1MB/s 
Installing collected packages: gunicorn
Successfully installed gunicorn-19.6.0
(foodtasker) tiger:foodtasker xxxxx$ 

Check and see what applications you have installed in the project

(foodtasker) tiger:foodtasker xxxxx$ pip freeze
Django==1.10
gunicorn==19.6.0
olefile==0.44
Pillow==4.0.0
(foodtasker) tiger:foodtasker xxxxx$ 

Then create a textfile “requirements.txt” and paste the list of applications you have installed in the project

You also need to create textfile “runtime.txt” and specific which python version you want to use in Heroku. In my case, I am going to use python 3.6.0 for local and Heroku

python-3.6.0

In the last, you need to create textfile “Procfile” to specific what application you want to start at Heroku

web: gunicorn foodtasker.wsgi --log-file -

In the settings.py, we need to specific the STATIC_ROOT and which database will be used in the Heroku

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

import dj_database_url
db_from_env = dj_database_url.config()
DATABASES['default'].update(db_from_env)

In the wsgi.py, you need to tell Heroku using whitenoise to serve static file

# Use whitenoise package to serve static files on Heroku
from whitenoise.django import DjangoWhiteNoise

application = DjangoWhiteNoise(application)

Add all the files to the GIT

(foodtasker) tiger:foodtasker xxxxx$ git add .
(foodtasker) tiger:foodtasker xxxxx$ git commit -m "fix"
[master 5b4bbd4] fix
 1 file changed, 1 insertion(+), 1 deletion(-)
(foodtasker) tiger:foodtasker xxxxx$ 

Push the master branch from local to Heroku

(foodtasker) tiger:foodtasker xxxxx$ git push heroku master
Counting objects: 63, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (55/55), done.
Writing objects: 100% (63/63), 332.45 KiB | 0 bytes/s, done.
Total 63 (delta 11), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote: 

Run Django migrate in the Heroku

(foodtasker) tiger:foodtasker xxxxx$ 
(foodtasker) tiger:foodtasker xxxxx$ heroku run python manage.py migrate
Running python manage.py migrate on ⬢ thawing-reaches-94509... up, run.1421 (Free)
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, foodtaskerapp, sessions
Running migrations:
  Rendering model states... DONE
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying foodtaskerapp.0001_initial... OK
  Applying sessions.0001_initial... OK
(foodtasker) tiger:foodtasker whoismikechan$ 

Create super user in Heroku which it is used to manage the project

(foodtasker) tiger:foodtasker xxxxx$ heroku run python manage.py createsuperuser
Running python manage.py createsuperuser on ⬢ thawing-reaches-94509... up, run.2410 (Free)
Username (leave blank to use 'u9090'): admin
Email address: xxxxx@gmail.com
Password: 
Password (again): 
Superuser created successfully.
(foodtasker) tiger:foodtasker xxxxx$ 

Open the application in the Heroku

(foodtasker) tiger:foodtasker xxxxx$ heroku open

Checking the log at Heroku, you can use command “heroku logs”

(foodtasker) tiger:foodtasker xxxxx$ heroku logs 
2017-02-18T17:22:06.601621+00:00 heroku[router]: at=info method=GET path="/static/js/bootstrap.min.js" host=thawing-reaches-94509.herokuapp.com request_id=45a8c4d1-75b0-4433-bb6c-fd8e35bbd7ab fwd="210.6.84.60" dyno=web.1 connect=1ms service=3ms status=304 bytes=144
Posted in Heroku | Leave a comment

Unable to “import matplotlib.pyplot as plt” in virtualenv

If you see the same issue (See the error message below) when you try to import matplotlib.pyplot in the Virtualenv, the following solution could be used as workaround

>>> import matplotlib.pyplot as plt
Traceback (most recent call last):
  File "", line 1, in 
  File "/Users/xxxxx/Development/ML/finance/lib/python3.6/site-packages/matplotlib/pyplot.py", line 115, in 
    _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
  File "/Users/xxxxx/Development/ML/finance/lib/python3.6/site-packages/matplotlib/backends/__init__.py", line 32, in pylab_setup
    globals(),locals(),[backend_name],0)
  File "/Users/xxxxx/Development/ML/finance/lib/python3.6/site-packages/matplotlib/backends/backend_macosx.py", line 19, in 
    from matplotlib.backends import _macosx
RuntimeError: Python is not installed as a framework. The Mac OS X backend will not be able to function correctly if Python is not installed as a framework. See the Python documentation for more information on installing Python as a framework on Mac OS X. Please either reinstall Python as a framework, or try one of the other backends. If you are using (Ana)Conda please install python.app and replace the use of 'python' with 'pythonw'. See 'Working with Matplotlib on OSX' in the Matplotlib FAQ for more information.
>>> 

This solution worked for me. If you already installed matplotlib using pip on your virtual environment, you can just type the following:

$ cd ~/.matplotlib
$ vi matplotlibrc

Edit the file “matplotlibrc with content -> “backend: TkAgg”

Posted in Python | Leave a comment