Categories
Python

Django: Sending HTML Only Email

Prior to Django 1.7 (currently in dev), we don’t have any option to pass html version of an email to “send_mail()” from django.core.mail module. Even in the newer version, you can pass html template as an alternative but you have to pass a plain text version as well. What if you want to send html version, only? This is what I did –

Now you would pass html as the message parameter. I use django templates with “render_to_string()” function from django.template.loader module.

Do you know any other, perhaps smarter way to accomplish this? Please share in the comments! 🙂

Categories
Python

Python Social Auth: Custom pipeline

Eventually, from “Django Social Auth”, came “Python Social Auth” which is compatible with a number of other frameworks as well. This package is now the recommended one for integrating with social networks. If you have worked with Django Social Auth in the past, the setup and configuration is pretty straightforward. The major difference is in the package names (obviously!).

So, in one of my apps, I am using Facebook logins. And despite the inherent security risk (I am not going to elaborate here), I have been asked to implement the “associate by email address” feature. The idea is simple – if there is an user account already with the same email address of the social account being linked, connect them together.

Like Django Social Auth before, Python Social Auth has the same Pipeline architecture. The SOCIAL_AUTH_PIPELINE is a tuple of functions which are passed various data (when I say various, don’t get me wrong, it pretty much passes everything in the app). The data get passed to the first function, the return value of the first function get passed to the second – this way, it’s a pipeline of functions where one function’s return value is fed into another.

With that in mind, I just need to create a function that sits somewhere in the pipeline and checks for the email address returned from the social network. If any existing user accounts found, associate that as the user. So, I created a function in “core/utils.py” so that the full function identifier would be – “core.utils.associate_by_email”.

The contents are:

We are accepting all the arguments to the function using **kwargs. We’re then grabbing the email address from the “details” dictionary and setting the user if found. Being lazy, I surrounded the block with a try..except block for any exceptions which might arise.

Now, in settings, I added the pipeline like:

So, we set the user before we create an user. Thus if there is an existing user, we just update that user, otherwise, we create a new one.

I really love how simple and easy it is to extend the Python Social Auth package, no wonder it’s become the de-facto choice for social networks integration. 🙂

Categories
Python

Async execution in Python using multiprocessing Pool

The “multiprocessing” module has a class Pool that is quite convenient if we want to do parallel processing. The API is simple and rather straightforward. I wanted to see if I can craft an example out of the official docs and here’s the code:

Let’s see whta we’re doing here – we have two functions, the main function “square” and the callback function “cb_func” which just prints out the result. We want to run the main function asynchronously and as soon as the result is available, we want the callback to execute, printing out the result.

What we’re doing inside the AsyncFactory is storing the function and callback function inside an instance. Then we create a worker pool. The worker pool by default uses the available CPUs. We can also pass values to the “processes” argument to determine the number of worker processes in the pool.

Then we repeatedly call the apply_async on the Pool object to pass the function with the arguments. Finally, we wait for the pool to close it’s workers and rest in peace. As soon as each worker returns a value, the callback would print it out. We have also added a print statement to check the process ID, passed value and sleep duration.

Isn’t it fun?