Categories
Django Python

Django: Limiting User Access to Views

In this post, we would like to see how we can limit user accesses to our Django views.

Login Required & Permission Required Decorators

If you have worked with Django, you probably have used the login_required decorator already. Adding the decorator to a view limits access only to the logged in users. If the user is not logged in, s/he is redirected to the default login page. Or we can pass a custom login url to the decorator for that purpose.

Let’s see an example:

There’s another nice decorator – permission_required which works in a similar fashion:

Awesome but let’s learn how do they work internally.

How do they work?

We saw the magic of the login_required and permission_required decorators. But we’re the men of science and we don’t like to believe in magic. So let’s unravel the mystery of these useful decorators.

Here’s the code for the login_required decorator:

By reading the code, we can see that the login_required decorator uses another decorator – user_passes_test which takes/uses a callable to determine whether the user should have access to this view. The callable must accept an user instance and return a boolean value. user_passes_test returns a decorator which is applied to our view.

If we see the source of permission_required, we would see something quite similar. It also uses the same user_passes_test decorator.

Building Our Own Decorators

Now that we know how to limit access to a view based on whether the logged in user passes a test, it’s quite simple for us to build our own decorators for various purposes. Let’s say we want to allow access only to those users who have verified their emails.

Now we can use the decorator to a view like:

Users who have verified their email addresses will be able to access this view. And if they didn’t, they will be redirected to the login view. Using the reason query string, we can display a nice message explaining what’s happening.

Please note, we have used two decorators on the same view. We can use multiple decorators like this to make sure the user passes all the tests we require them to.

Categories
Bangla Data Science

āĻšāĻŋāĻ¸ā§āĻŸā§‹āĻ—ā§āϰāĻžāĻŽ āύāĻžāĻ•āĻŋ āĻŦāĻžāϰ āϚāĻžāĻ°ā§āϟ?

āĻĄāĻžāϟāĻž āĻ­āĻŋāϜ⧁⧟āĻžāϞāĻžāχāĻœā§‡āĻļāύ⧇āϰ āϏāĻŽā§Ÿ āφāĻŽāϰāĻž āĻĒā§āϰāĻžā§ŸāĻļāχ āĻšāĻŋāĻ¸ā§āĻŸā§‹āĻ—ā§āϰāĻžāĻŽ āĻ“ āĻŦāĻžāϰ āϚāĻžāĻ°ā§āϟ āĻāϰ āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻĻ⧇āĻ–āĻŦā§‹ āĨ¤ āĻŦāĻžāϰ āϚāĻžāĻ°ā§āϟ āĻāĻŦāĻ‚ āĻšāĻŋāĻ¸ā§āĻŸā§‹āĻ—ā§āϰāĻžāĻŽ āĻĻ⧇āĻ–āϤ⧇ āĻ…āύ⧇āĻ•āϟāĻž āĻāĻ•āχ āϰāĻ•āĻŽ āĨ¤ āĻĻ⧁āĻŸā§‹āϤ⧇āχ āφāĻŽāϰāĻž āϞāĻŽā§āĻŦāĻž āϞāĻŽā§āĻŦāĻž āĻŦāĻžāϰ āφāĻ•āĻŋāρ āĨ¤ x-axis āĻ āχāĻ¨ā§āĻĄāĻŋāĻĒ⧇āĻ¨ā§āĻĄā§‡āĻ¨ā§āϟ āĻ­ā§āϝāĻžāϰāĻŋā§Ÿā§‡āĻŦāϞ āĻ“ y-axis āĻ āĻĄāĻŋāĻĒ⧇āĻ¨ā§āĻĄā§‡āĻ¨ā§āϟ āĻ­ā§āϝāĻžāϰāĻŋā§Ÿā§‡āĻŦāϞ āĻāϰ āĻŽāĻžāύ āĻŦāϏāĻŋā§Ÿā§‡ āĻĻāĻŋā§Ÿā§‡ āφāĻŽāϰāĻž āϏ⧁āĻ¨ā§āĻĻāϰ āĻ•āϰ⧇ āĻšāĻŋāĻ¸ā§āĻŸā§‹āĻ—ā§āϰāĻžāĻŽ āĻ“ āĻŦāĻžāϰ āϚāĻžāĻ°ā§āϟ āĻāϕ⧇āρ āĻĢ⧇āϞāĻŋ āĨ¤

āĻĻ⧇āĻ–āϤ⧇ āĻāĻ•āχ āϰāĻ•āĻŽ āĻšāϞ⧇āĻ“ āĻĻ⧁āĻŸā§‹āϰ āĻŽāĻ§ā§āϝ⧇ āĻ°ā§Ÿā§‡āϛ⧇ āĻŽā§ŒāϞāĻŋāĻ• āĻĒāĻžāĻ°ā§āĻĨāĻ•ā§āϝ āĨ¤ āĻšāĻŋāĻ¸ā§āĻŸā§‹āĻ—ā§āϰāĻžāĻŽ āϟāĻž āφāĻŽāϰāĻž āϰ⧇āĻ¨ā§āϜ āĻŦāĻž āĻĄāĻŋāĻ¸ā§āĻŸā§āϰāĻŋāĻŦāĻŋāωāĻļāĻžāύ⧇āϰ āĻ•ā§āώ⧇āĻ¤ā§āϰ⧇ āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰāĻŋ āĨ¤ āϝ⧇āĻŽāύ āύāĻŋāĻšā§‡āϰ āĻšāĻŋāĻ¸ā§āĻŸā§‹āĻ—ā§āϰāĻžāĻŽāϟāĻž āĻĻ⧇āĻ–āĻŋ:

āĻāĻ–āĻžāύ⧇ āφāĻŽāϰāĻž āĻĻ⧇āĻ–āĻ›āĻŋ āĻŦāĻŋāĻ­āĻŋāĻ¨ā§āύ āĻŦ⧟āϏ⧇āϰ āĻŽāĻžāύ⧁āώ, āϝāĻžāϰāĻž āϛ⧁āϟāĻŋ āĻ•āĻžāϟāĻžāϤ⧇ āϝāĻžā§Ÿ āϤāĻžāĻĻ⧇āϰ āĻŽāĻ§ā§āϝ⧇ āĻ•āϤāϜāύ āĻšā§‹āĻŸā§‡āϞ⧇ āĻĨāĻžāϕ⧇ āĨ¤ āφāĻŽāϰāĻž āĻ–ā§‡ā§ŸāĻžāϞ āĻ•āϰāϞ⧇ āĻĻ⧇āĻ–āĻŦā§‹, āĻāĻ–āĻžāύ⧇ ⧍ā§Ļ āĻŦāĻ›āϰ⧇āϰ āύāĻŋāĻšā§‡ āϝāĻžāϰāĻž, āϤāĻžāĻĻ⧇āϰ āĻŽāĻ§ā§āϝ⧇ ā§Ģ āϜāύ āĻšā§‹āĻŸā§‡āϞ⧇ āĻĨāĻžāϕ⧇, ⧍⧧-ā§Šā§Ļ āĻāϰ āĻŽāĻ§ā§āϝ⧇ ā§§ā§Ģ āϜāύ, ā§Šā§§-ā§Ēā§Ļ āĻāϰ āĻŽāĻ§ā§āϝ⧇ āφāϛ⧇ ā§§ā§Ļ āϜāύ āĻāĻŦāĻ‚ ā§Ēā§§-ā§Ģā§Ļ āĻŦ⧟āϏ⧀āĻĻ⧇āϰ āĻŽāĻ§ā§āϝ⧇ ā§Ģ āϜāύ āĻāϰ āĻ•āĻŽ āĨ¤

āϞāĻ•ā§āĻˇā§āϝ āĻ•āϰāĻŋ, āĻāĻ–āĻžāύ⧇ āφāĻŽāĻžāĻĻ⧇āϰ āĻĄāĻžāϟāĻž āύāĻŋāωāĻŽā§‡āϰāĻŋāĻ•ā§āϝāĻžāϞ āĻāĻŦāĻ‚ āφāĻŽāϰāĻž āϰ⧇āĻ¨ā§āϜ āύāĻŋā§Ÿā§‡ āĻ•āĻžāϜ āĻ•āϰāĻ›āĻŋ – āĻŦ⧟āϏ⧇āϰ āϰ⧇āĻ¨ā§āϜ āĨ¤ āĻŦāĻŋāĻ­āĻŋāĻ¨ā§āύ āĻŦ⧟āϏ⧇āϰ āĻŽāĻžāύ⧁āώāϕ⧇ āύāĻŋāĻ°ā§āĻĻāĻŋāĻˇā§āϟ āĻ•āĻŋāϛ⧁ āϰ⧇āĻ¨ā§āĻœā§‡ āφāĻŽāϰāĻž āĻĄāĻŋāĻ¸ā§āĻŸā§āϰāĻŋāĻŦāĻŋāωāϟ āĻ•āϰ⧇āĻ›āĻŋ āĨ¤ āĻāĻ–āĻžāύ⧇ āĻĄāĻžāϟāĻžāϰ āĻāχ āϰ⧇āĻ¨ā§āĻœā§‡ āϕ⧋āύ āĻ—ā§āϝāĻžāĻĒ āύ⧇āχ āĨ¤ āĻāĻŦāĻ‚ āĻšāĻŋāĻ¸ā§āĻŸā§‹āĻ—ā§āϰāĻžāĻŽā§‡āϰ āĻĒāĻžāĻ°ā§āϟāϗ⧁āϞ⧋āϕ⧇ āĻŦāĻž āĻŦāĻžāϰ āϗ⧁āϞ⧋āϕ⧇ āĻ…āĻ¨ā§āϝ āϕ⧋āύ āĻ…āĻ°ā§āĻĄāĻžāϰ⧇ āϰāĻŋ-āĻā§āϝāĻžāϰ⧇āĻ¨ā§āϜ āĻ•āϰāĻžāϰ āϏ⧁āϝ⧋āĻ— āύ⧇āχ āĨ¤

āĻāĻŦāĻžāϰ āĻĻ⧇āĻ–āĻŋ āĻāĻ•āϟāĻŋ āĻŦāĻžāϰ āϚāĻžāĻ°ā§āϟ:

āĻāχ āϚāĻžāĻ°ā§āĻŸā§‡ āφāĻŽāϰāĻž āĻĻ⧇āĻ–āĻ›āĻŋ āĻŦāĻŋāĻ­āĻŋāĻ¨ā§āύ āĻŦā§āĻ°ā§āϝāĻžāĻ¨ā§āĻĄā§‡āϰ āĻ—āĻžā§œāĻŋ āύāĻŋāĻ°ā§āĻŽāĻžāϤāĻžāĻĻ⧇āϰ āϜāĻžāύ⧁⧟āĻžāϰ⧀ āĻŽāĻžāϏ⧇ āĻŦāĻŋāĻ•ā§āϰāĻŋāϤ āĻ—āĻžā§œāĻŋāϰ āϏāĻ‚āĻ–ā§āϝāĻž āĨ¤ āĻāĻ–āĻžāύ⧇ āĻĒā§āϰāϧāĻžāύ āϞāĻ•ā§āώāĻ¨ā§€ā§Ÿ āĻŦāĻŋāώ⧟ āĻšāϞ⧋ āĻāχāĻŦāĻžāϰ āφāĻŽāĻžāĻĻ⧇āϰ āĻĄāĻžāϟāĻž āĻ•āĻŋāĻ¨ā§āϤ⧁ āύāĻŋāωāĻŽā§‡āϰāĻŋāĻ•ā§āϝāĻžāϞ āύāĻž, āĻŦāϰāĻ‚ āĻ•ā§āϝāĻžāĻŸā§‡āĻ—āϰāĻŋāĻ•ā§āϝāĻžāϞ āĨ¤ āĻŦāĻŋāĻ­āĻŋāĻ¨ā§āύ āĻŦā§āĻ°ā§āϝāĻžāĻ¨ā§āĻĄā§‡āϰ āĻŽāĻ§ā§āϝ⧇ āϤ⧁āϞāύāĻž āĻ•āϰāϤ⧇ āφāĻŽāϰāĻž āĻāχ āĻŦāĻžāϰ āϚāĻžāĻ°ā§āϟāϟāĻŋ āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰāϤ⧇ āĻĒāĻžāϰāĻŋ āĨ¤ āĻŦāĻžāϰ āϚāĻžāĻ°ā§āĻŸā§‡āϰ āĻ•ā§āώ⧇āĻ¤ā§āϰ⧇ āĻŦāĻžāϰ āϗ⧁āϞ⧋āϕ⧇ āϰāĻŋ-āĻā§āϝāĻžāϰ⧇āĻ¨ā§āϜ āĻ•āϰāϞ⧇āĻ“ āϕ⧋āύ āϏāĻŽāĻ¸ā§āϝāĻž āĻšā§Ÿ āύāĻž āĨ¤ āĻāĻŦāĻ‚ āĻŦāĻžāϰ āϗ⧁āϞ⧋ āĻāĻ•āϟāĻž āφāϰ⧇āĻ•āϟāĻžāϰ āϏāĻžāĻĨ⧇ āĻ•āĻžāύ⧇āĻ•ā§āĻŸā§‡āĻĄ āύāĻž, āĻŽāĻžāĻ āĻ–āĻžāύ⧇ āϤāĻžāχ āĻ—ā§āϝāĻžāĻĒ āĻĨāĻžāϕ⧇ āĨ¤


TL;DR – āĻšāĻŋāĻ¸ā§āĻŸā§‹āĻ—ā§āϰāĻžāĻŽ āĻāĻ•āϟāĻž āĻ­ā§āϝāĻžāϰāĻŋā§Ÿā§‡āĻŦāϞ⧇āϰ āĻĄāĻŋāĻ¸ā§āĻŸā§āϰāĻŋāĻŦāĻŋāωāĻļāĻžāύ āϰāĻŋāĻĒā§āϰ⧇āĻœā§‡āĻ¨ā§āϟ āĻ•āϰāĻžāϰ āϜāĻ¨ā§āϝ āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰāĻž āĻšā§Ÿ, āĻŦāĻžāϰ āϚāĻžāĻ°ā§āϟ āĻŦāĻŋāĻ­āĻŋāĻ¨ā§āύ āĻ­ā§āϝāĻžāϰāĻŋā§Ÿā§‡āĻŦāϞ⧇āϰ āĻŽāĻ§ā§āϝ⧇ āϤ⧁āϞāύāĻž āĻ•āϰāĻžāϰ āϜāĻ¨ā§āϝ āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰāĻž āĻšā§Ÿ āĨ¤

Categories
Python

Embedding IPython in your application

If you work with Python regularly, you probably know about IPython already. IPython has web based notebooks, QT based GUI consoles and plain old simple Terminal based REPL which is simply fantastic. But that’s not all, we can also embed IPython in our applications too. And this can lead to a number of potential use cases.

Use Cases

A common use case could be to drop into a IPython shell for quick interactive debugging. This can come very handy during prototyping.

Let’s see an example:

When we run this code, we will get a nice IPython REPL where we can try out things. In our case, we haven’t done much except defining a variable named name. We can print it out.

I use Iron.io workers/queues/caches at my day to day job. So I often need to check status of the workers or get the size of a queue or even queue a few workers. I also need to check a few records on Mongodb. An interactive prompt can be really helpful for these.

Now I can just do launch_workers("send_emails", 3) to launch 3 worker instances for the “send_emails” worker. Or get the number of buyers with more than 100 purhcases with the top_buyers() function.

Customizing The Prompt

When we embed IPython, it displays it’s common banner when starting.

We can easily disable that. To do so, we need to pass empty string to the banner1 parameter to the embed method.

Or we can further customize the 2nd banner or the exit message like this: