Categories
Python

Embed a Python REPL in your program

So you love the Django shell? The interactive prompt that has your Django stuff loaded and ready for some quick prototyping? You always wanted to embed something similar in your Python apps too? Well, it’s painlessly simple with the “code” module. It offers us the “InteractiveConsole” class which we can extend to quickly get a REPL which is basically the Python REPL with our customizations. The code is heavily documented. Going through it should be adequate to understand whats going on:

What did we do? We subclassed InteractiveConsole to create our own implementation. And then ran it 🙂

Test run outputs:

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. 🙂