<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Sagar Dhone on Medium]]></title>
        <description><![CDATA[Stories by Sagar Dhone on Medium]]></description>
        <link>https://medium.com/@sdhone98?source=rss-c535ff2a33------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*VF7NrAdy6qd6tkXq</url>
            <title>Stories by Sagar Dhone on Medium</title>
            <link>https://medium.com/@sdhone98?source=rss-c535ff2a33------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 08:47:35 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sdhone98/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Custom Decorator in Python for Validating User Based on Function-Level Roles]]></title>
            <link>https://medium.com/@sdhone98/custom-decorator-in-python-for-validating-user-based-on-function-level-roles-85d1d0381425?source=rss-c535ff2a33------2</link>
            <guid isPermaLink="false">https://medium.com/p/85d1d0381425</guid>
            <category><![CDATA[validation-rule]]></category>
            <category><![CDATA[decorators-in-python]]></category>
            <category><![CDATA[custom-decorators]]></category>
            <category><![CDATA[user-role-and-permission]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Sagar Dhone]]></dc:creator>
            <pubDate>Wed, 04 Sep 2024 17:09:07 GMT</pubDate>
            <atom:updated>2024-09-04T17:09:07.898Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*3TaKZuoHFb3QXxhP" /><figcaption>Photo by <a href="https://unsplash.com/@rubaitulazad?utm_source=medium&amp;utm_medium=referral">Rubaitul Azad</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>In this article, we will explore how to create a custom decorator in Python that validates user access based on function-level roles. This is particularly useful for APIs where different endpoints require different levels of user access. We will use Django REST framework to demonstrate this concept.</p><h3>Understanding the Concept</h3><p>A decorator in Python is a function that wraps another function or method, adding some functionality before or after the wrapped function runs. In this case, we will create a custom decorator that checks if the user has the necessary role to access the API endpoint.</p><h4>Steps to Create the Custom Decorator</h4><ol><li><strong>Define the Allowed User Roles: </strong>First, we need to specify the list of user roles that are allowed to access the function. This list can be customized as per the requirements.</li></ol><pre>VALID_USERS_LIST = [&#39;Super User&#39;, &#39;Admin&#39;, &#39;Level 1&#39;]</pre><p>2. <strong>Create the Custom Decorator: </strong>We will create a decorator function named custom_decorator which takes a list of allowed roles as an argument. Inside this decorator, we define another function that wraps the actual view function.</p><pre>from functools import wraps<br>from rest_framework.response import Response<br>from rest_framework import status<br><br>def custom_decorator(allowed_roles):<br>    def decorator(view_func):<br>        @wraps(view_func)<br>        def wrapper(*args, **kwargs):<br>            # Get user details from request headers<br>            current_user = &#39;Tester&#39;  # Placeholder for actual user extraction logic<br><br>            # Check if user role is not allowed, throw an exception<br>            if current_user not in allowed_roles:<br>                return Response(data=&#39;Tester user doesn\&#39;t have access.&#39;, status=status.HTTP_401_UNAUTHORIZED)<br><br>            return view_func(*args, **kwargs)<br><br>        return wrapper<br><br>    return decorator</pre><p>3. <strong>Apply the Custom Decorator to the View Function:</strong> Now, we apply the custom decorator to a view function to enforce the role-based access control. The decorator will check if the user has the necessary role before allowing access to the function.</p><pre>from rest_framework.decorators import api_view<br><br>@api_view([&#39;GET&#39;])<br>@custom_decorator(VALID_USERS_LIST)<br>def fetch_data_from_database(request, para_1, para_2):<br>    try:<br>        # Write business logic<br>        return Response(data=&#39;Done&#39;, status=status.HTTP_200_OK)<br><br>    except CustomException as e:<br>        return Response(data=&#39;Error.&#39;, status=status.HTTP_404_NOT_FOUND)<br><br>    except Exception as e:<br>        return Response(data=&#39;Internal Server Error.&#39;, status=status.HTTP_500_INTERNAL_SERVER_ERROR)</pre><h4>Detailed Process</h4><ol><li><strong>Defining the Allowed Roles</strong>:</li></ol><ul><li>We create a list VALID_USER_LIST that contains the roles allowed to access the API endpoint.</li></ul><p><strong>2. Creating the Decorator</strong>:</p><ul><li>The custom_decorator function takes allowed_roles as an argument.</li><li>Inside this function, another function decorator is defined which takes the view_func as an argument.</li><li>The decorator function defines a wrapper function that will wrap the actual view function.</li><li>Inside the wrapper function, we extract the current user role from the request (for demonstration, we use a placeholder value).</li><li>We then check if the current user role is in the list of allowed roles. If not, we return a 401 Unauthorized response.</li><li>If the user role is allowed, we call the original view function.</li></ul><p><strong>3. Applying the Decorator</strong>:</p><ul><li>We apply the custom_decorator to the fetch_data_from_database view function.</li><li>The decorator ensures that only users with roles specified in VALID_USER_LIST can access this endpoint.</li></ul><p><em>This approach ensures a clean and reusable way to enforce role-based access control in your Django REST framework applications. By using decorators, we can keep our code modular and maintainable.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=85d1d0381425" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Spotify Downloader: Get Your Favorite Songs]]></title>
            <link>https://medium.com/@sdhone98/spotify-downloader-get-your-favorite-songs-29bcf441a674?source=rss-c535ff2a33------2</link>
            <guid isPermaLink="false">https://medium.com/p/29bcf441a674</guid>
            <category><![CDATA[spotify-api]]></category>
            <category><![CDATA[downloading-music]]></category>
            <category><![CDATA[spotify]]></category>
            <category><![CDATA[python-programming]]></category>
            <category><![CDATA[tech-tips]]></category>
            <dc:creator><![CDATA[Sagar Dhone]]></dc:creator>
            <pubDate>Fri, 16 Aug 2024 14:05:34 GMT</pubDate>
            <atom:updated>2024-08-16T14:05:34.547Z</atom:updated>
            <content:encoded><![CDATA[<p>In the world of music streaming, Spotify reigns supreme. With its vast library of songs, podcasts, and other audio content, Spotify has become the go-to platform for music lovers worldwide. However, one of the drawbacks of streaming services is that you need an internet connection to listen to your music. This can be a problem if you’re traveling, commuting, or simply don’t have reliable internet access.</p><p>That’s where Spotify Songs Downloder comes in. This simple yet powerful tool allows you to download your favorite Spotify songs and playlists for offline listening. This means you can enjoy your music anytime, anywhere, even without an internet connection.</p><figure><img alt="spotify app screen" src="https://cdn-images-1.medium.com/max/1024/0*9BjLuOqK6khx0Eid" /><figcaption>Photo by <a href="https://unsplash.com/@nadineshaabana?utm_source=medium&amp;utm_medium=referral">Nadine Shaabana</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p><strong>How to Use Spotify Songs Downloder</strong></p><p>Using Spotify Songs Downloder is easy. Simply follow these steps:</p><ol><li><strong>Install the software.</strong> You can download Spotify Songs Downloder from the project’s GitHub repository: <a href="https://github.com/sdhone98/SpotifyDownloder">https://github.com/sdhone98/SpotifyDownloder</a></li><li><strong>Launch the software.</strong> Once the software is installed, launch it and sign in to your Spotify account.</li><li><strong>Search for songs or playlists.</strong> Use the search bar to find the songs or playlists you want to download.</li><li><strong>Select songs or playlists.</strong> Once you’ve found the songs or playlists you want to download, simply select them and click the “Download” button.</li><li><strong>Enjoy your music!</strong> The songs or playlists will be downloaded to your computer. You can then listen to them offline at your convenience.</li></ol><blockquote><strong>A Simple Project for College Students</strong></blockquote><p>Spotify Songs Downloder is also a great project for college students who are learning Python. The project is relatively simple to implement and can be completed in a short amount of time. It’s also a great way to learn about the Spotify API and how to interact with it using Python.</p><blockquote><strong>Benefits of Spotify Songs Downloder</strong></blockquote><p>There are many benefits to using Spotify Songs Downloder, including:</p><ul><li><strong>Saves you money.</strong> If you’re a premium Spotify user, you can download songs and playlists for offline listening without using up your data. This can save you money on your data plan.</li><li><strong>Gives you more control over your music.</strong> With Spotify Songs Downloder, you can download your favorite songs and playlists and listen to them whenever you want, even without an internet connection. This gives you more control over your music listening experience.</li><li><strong>Helps you learn Python.</strong> Spotify Songs Downloder is a great project for college students who are learning Python. It’s relatively simple to implement and can be completed in a short amount of time. It’s also a great way to learn about the Spotify API and how to interact with it using Python.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rgFAgmLxyxbgOozNadhv9w.jpeg" /></figure><p><strong><em>Conclusion</em></strong></p><p><em>Spotify Songs Downloder is a simple and legal way to download your favorite songs and playlists from Spotify. It’s a great tool for anyone who wants to listen to their music offline, and it’s also a great project for college students who are learning Python.</em></p><p><em>I hope this article has been helpful. If you have any questions, please feel free to leave a comment below.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=29bcf441a674" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>