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: