<?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 Malik Albeik on Medium]]></title>
        <description><![CDATA[Stories by Malik Albeik on Medium]]></description>
        <link>https://medium.com/@malikalbeik?source=rss-c2f8c5c8aa09------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*kKrfksE1aOUWVjr3OryeTg.jpeg</url>
            <title>Stories by Malik Albeik on Medium</title>
            <link>https://medium.com/@malikalbeik?source=rss-c2f8c5c8aa09------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 23:28:34 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@malikalbeik/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[Is FastAPI going to replace Django?]]></title>
            <link>https://medium.datadriveninvestor.com/is-fastapi-going-to-replace-django-87441de76383?source=rss-c2f8c5c8aa09------2</link>
            <guid isPermaLink="false">https://medium.com/p/87441de76383</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[fastapi]]></category>
            <category><![CDATA[django]]></category>
            <category><![CDATA[backend]]></category>
            <category><![CDATA[rest-api]]></category>
            <dc:creator><![CDATA[Malik Albeik]]></dc:creator>
            <pubDate>Sun, 24 May 2020 19:01:01 GMT</pubDate>
            <atom:updated>2020-05-25T19:53:44.610Z</atom:updated>
            <content:encoded><![CDATA[<p>If you are a backend developer then there is no chance that you haven’t heard of Django, it is usually the preferred framework for backend development in Python. But as Django got bigger and bigger we start to encounter new problems with the way that Django is implemented. Well I am happy to tell you that FastAPI is here to help put an end to these problems.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Zlbl19FsVrW4UoJ34p5EzA.jpeg" /></figure><h3>What is FastAPI?</h3><p>FastAPI is a Python web framework for building APIs that focuses on speed, both in development and runtime, it is built on top of <a href="https://www.starlette.io/">Starlette</a> &amp; <a href="https://pydantic-docs.helpmanual.io/">Pydantic</a> which is the main reason for its speed. It is still relatively new and in general a much smaller project than Django.</p><p>FastAPI got the best practices of many frameworks and combined them to make this grand framework that has the best of all. Some of these features include NodeJS like performance, dependency injection, automatically generated interactive API docs, and native asynchronous support. FastAPI also supports industry standards like openAPI and JSON Schema.</p><h3>Problems with Django</h3><p>While Django is a great framework, the main problem is that it became a huge project. Thus, implementing new features or changing some, takes a lot of time and effort.</p><p>With Django growing to its current site new features are either added to the main Django project, or somebody comes along and writes a package for it. Whenever we want to use this feature we have to get it from the package. and while this modularity of Django is awesome, we can see that there is a little bit of a mix in what gets prioritized to be put in the main Django project and what gets moved to a separate package.<br>An example of this can be seen in how Django to this day doesn’t have native support for RestAPIs, you have to add the <a href="https://www.django-rest-framework.org/">Django REST framework</a> package to have such support. Meanwhile Jinja is available out of the box. This is mainly due to Django being an old project started in the times where APIs weren’t as famous as current times.</p><p>This is perfect for using Django as a Full Stack app without implementing an API, just using Jinja to render your views. But most of the people that I know don’t do that but instead, add the Django Rest framework and use Django as an API to be consumed from their React, Angular, or some other Front-End app.</p><p><a href="https://www.datadriveninvestor.com/2020/03/29/microsoft-having-an-edge-over-chrome/">DataDrivenInvestor | Microsoft Having An &#39;Edge&#39; Over Chrome</a></p><p>As Django grew, its development cycle has been slowing down. Adding new features and catching up to industry standards has been slow. Features like asynchronous, WebSocket, and NoSQL database support still haven’t been added to the main project.</p><h3>But Why FastAPI?</h3><p>FastAPI tried to solve most of Django’s problems and for the most part it did. It implemented most of the features that Django is missing and even added some more like auto validation of incoming requests, great editor support, and GraphQL support.<br>For some people another nice feature of FastAPI is a faster development pace, due to it being a new project.</p><p>Django comes with a lot of features that most people don’t use but some find useful for specialized uses, this makes it an overkill. Flask on the other side, another widely used Python framework, is the exact opposite. it comes bare of features and you have to add everything yourself. Well, FastAPI is right in the middle, it isn’t a huge project implementing its ORM, it is also not a bare project that you need to add a lot of modules to make it do basic work. And this is what makes it so great.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mK7gx0yT5HeKbpQ30NS3yg.jpeg" /><figcaption>How FastAPI compares to other popular Python Web Frameworks</figcaption></figure><h3>Okay, Okay, if FastAPI is that perfect why don’t we get rid of Django?</h3><p>No, wait not that fast. Django is still a great framework, and I even wrote my own website’s backend in it. its admin page is one of the best, and it is suited for lots of use cases. It has a huge amount of power under the hood and is much more stable than FastAPI, it has been in use for many years now and some huge platforms rely on it #Instagram😅. it is not going anywhere soon and it shouldn’t. But at the same time FastAPI is a great new framework and it is doing some stuff much better than most other frameworks are. so it is worth keeping your eyes on. you can visit FastAPI’s docs <a href="https://fastapi.tiangolo.com/">here</a>.</p><p>What do you think about Django and FastAPI, I would love to hear your thoughts. You can either leave a comment down or you can reach me on twitter @malikalbeik.</p><p>If you enjoyed this post and would like to read more posts about new trends in Python, Cloud Computing, and FastAPI please come visit me at <a href="https://malikalbeik.com/">malikalbeik.com</a></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2Fb2a0d6%3Fas_embed%3Dtrue&amp;dntp=1&amp;display_name=Upscribe&amp;url=https%3A%2F%2Fupscri.be%2Fb2a0d6%2F&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/0707f5c806284d01a4a13c7b13a91ce3/href">https://medium.com/media/0707f5c806284d01a4a13c7b13a91ce3/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=87441de76383" width="1" height="1" alt=""><hr><p><a href="https://medium.datadriveninvestor.com/is-fastapi-going-to-replace-django-87441de76383">Is FastAPI going to replace Django?</a> was originally published in <a href="https://medium.datadriveninvestor.com/">DataDrivenInvestor</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Monitoring user actions with LogEntry in Django Admin]]></title>
            <link>https://medium.datadriveninvestor.com/monitoring-user-actions-with-logentry-in-django-admin-8c9fbaa3f442?source=rss-c2f8c5c8aa09------2</link>
            <guid isPermaLink="false">https://medium.com/p/8c9fbaa3f442</guid>
            <category><![CDATA[user-activity-monitoring]]></category>
            <category><![CDATA[backend]]></category>
            <category><![CDATA[django]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[django-admin]]></category>
            <dc:creator><![CDATA[Malik Albeik]]></dc:creator>
            <pubDate>Wed, 05 Feb 2020 17:56:01 GMT</pubDate>
            <atom:updated>2020-02-05T17:56:01.260Z</atom:updated>
            <content:encoded><![CDATA[<p>Django is a one of the best web frameworks that is mostly focused on speed and scalability and because of how easy it is to use and deploy you can find yourself dealing with a huge application that has huge sets of data in no time. While this is definitely not something to be sad about it does introduce some new challenges for usability and maintainability. one of these challenges that I found myself dealing with is the ability to see users activity and track what they did from the Django admin page. And conveniently enough it can be implemented with a couple lines of code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*4onPlFxQA3iR_XIw" /><figcaption>Photo by <a href="https://unsplash.com/@kmuza?utm_source=medium&amp;utm_medium=referral">Carlos Muza</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3><strong>TL;DR:</strong></h3><p>You can implement a user activity monitoring system by adding the GitHub Gist at the bottom to any admin.py file in your project. This will allow you to see any change, addition, and deletion that happens in any model and know who made it by using the already saved data of Django Admin’s LogEntry model.</p><h4>What Are Django Admin’s LogEntries</h4><p>Whenever a user adds, deletes, or even changes an object in Django admin that action is recorded using a model called <strong>LogEntry </strong>in a table in the database called django_admin_log. The model of a LogEntry is very simple it includes fields like: action_time, user, object_id, action_flag, and change_message, which includes the names of the fields that were changed.</p><p>Now the models is already given to us by the standard Administration Django app, which is automatically initialized when we start a Django project using the django-admin startprojetCommand. So we don’t have to do any changes to the model, instead what we have to do is register the LogEntery model in any admin.py file and it is just like registering any other model.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/005c1a50cb11767e74b22e2c0fa99a36/href">https://medium.com/media/005c1a50cb11767e74b22e2c0fa99a36/href</a></iframe><p>And now if we run the server we can see that we have a new app in the admin page called Administration that has our log entries inside of it.</p><p>And while this is what we have to do mostly to get the log entries to show in the admin page there is still some additions to this that we can make to make it safer and more intuitive to use:</p><h4>Managing Permissions:</h4><p>It is very important to set some kind of permission system for our log entries. in the state that it is in right now any staff user can view, change, and even delete any log entry, this is dangerous. So we should set 4 permissions for the LogEntryAdmin class: has_add_permission, has_change_permission, has_delete_permission, and has_view_permission. <br>For the first three I think that nobody in the app should have such permissions because it might be really harmful for the database to add, delete, or change a log entry, so we should set this to always return False.<br>As for the fourth one, the view permission, it is a matter of personal preference. I like to keep the permission to see log entries just for super users. but you are free to change it to return whatever you want. We can set the permission by overriding the following methods in the LogEntryAdmin class:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/65f661585e070c16e17105a75ec80d2a/href">https://medium.com/media/65f661585e070c16e17105a75ec80d2a/href</a></iframe><h4>Adding The Edited Object’s Link</h4><p>Another neat thing that we can add to this feature is the ability to open the edited object by clicking on its link. and this can be done using Django’s urls.reverse function by adding this it as a method of the LogEntryAdmin class:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/47b98247798a87f21924c98422eab1d9/href">https://medium.com/media/47b98247798a87f21924c98422eab1d9/href</a></iframe><p>You can also add object_link to the list_display so that it shows in the list of logs.</p><h4>Conclusion</h4><p>combining all of this together gives us the LogEntryAdmin class in which when added to any admin.py file will result in the superuser being able to see all logs and monitor all the activities that happen in the Django admin page.</p><p>You can find the combined LogEntryAdmin class here:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ad2663c4478f92fa87ff7118dcadbb72/href">https://medium.com/media/ad2663c4478f92fa87ff7118dcadbb72/href</a></iframe><p>And that is it, as simple as this🥳.</p><p>If you enjoyed this tutorial please don’t forget to give it a clap, share it on social media, and follow me for more tutorials like this.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8c9fbaa3f442" width="1" height="1" alt=""><hr><p><a href="https://medium.datadriveninvestor.com/monitoring-user-actions-with-logentry-in-django-admin-8c9fbaa3f442">Monitoring user actions with LogEntry in Django Admin</a> was originally published in <a href="https://medium.datadriveninvestor.com/">DataDrivenInvestor</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to periodically backup a file to Dropbox using Python and Cron jobs]]></title>
            <link>https://medium.com/@malikalbeik/how-to-periodically-backup-a-file-to-dropbox-using-python-and-cron-jobs-82ce2fdb97a3?source=rss-c2f8c5c8aa09------2</link>
            <guid isPermaLink="false">https://medium.com/p/82ce2fdb97a3</guid>
            <category><![CDATA[backup]]></category>
            <category><![CDATA[dropbox]]></category>
            <category><![CDATA[cron]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[automation]]></category>
            <dc:creator><![CDATA[Malik Albeik]]></dc:creator>
            <pubDate>Sun, 26 Jan 2020 23:45:30 GMT</pubDate>
            <atom:updated>2020-01-26T23:45:30.092Z</atom:updated>
            <content:encoded><![CDATA[<p>Taking a backups is an essential part of any workflow and doing so everyday, every week, or even every month might seem a little tedious. That is why we will be automating this task by using python and cron jobs.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/590/1*DeJ6Y8-MlGFbopzqMAMibw.png" /></figure><h3>TL;DR</h3><p>Make sure that you have python3 pip and dropbox’s python SDK installed and then download <a href="https://gist.github.com/malikalbeik/3aa45a9d55b83925a1ec3dcabd8ad8dc">this GitHub gist</a> and replace the constants with your own and then move on to setting a crontab to run the python script in the crontab part bellow.</p><p>To start make sure that you have python3 and pip installed on your machine and then install <a href="https://github.com/dropbox/dropbox-sdk-python">Dropbox’s python SDK</a>. you can do that by running the following command: pip install dropbox</p><h3>Creating a Dropbox app and generating an access token using OAuth 2</h3><p>Go to <a href="https://www.dropbox.com/developers/apps/create">https://www.dropbox.com/developers/apps/create</a> to create a new app on the DBX Platform and:<br>1. Chose Dropbox API.<br>2. Choose the type of access you need: for this project we don’t want to give full Dropbox access to our app so we will stick with App Folder.<br>3. Name your app.</p><p>After following these steps you should end up with a screen similar to this:</p><figure><img alt="How your screen should look after creating your app" src="https://cdn-images-1.medium.com/max/484/1*OiLdz2ttzTrCs6lDHcXoFw.jpeg" /></figure><p>At the bottom of that page you under generated access tokens you should find a button called generate and press it to generate your token. Be sure to copy that token and keep it in your notes we will use it in a bit.</p><p>After you have done all of that it is time to create the python file and starting off by importing that following:</p><pre>import sys<br>import dropbox<br>from dropbox.files import WriteMode<br>from dropbox.exceptions import ApiError, AuthError<br>import datetime</pre><blockquote>Be aware that naming your python file dropbox.py will result in some problems importing the dropbox SDK due to how python manages imports</blockquote><p>Now we have to declare some constants for our script and that is where the datetime module comes into use. In our specific use case we will be taking daily backups but even if you will be taking monthly backups it is really important to give your backup files a clear name to know when it was created and also for later uses. That is why we will be time stamping our backup files. Now to declare our constants:</p><pre>dt = datetime.datetime.today()<br>TOKEN = &#39;&lt;Replace with your Access Token&gt;&#39;<br>LOCALFILE = &#39;&lt;Replace with the file that you want to backup&gt;&#39;<br># Don&#39;t forget to add the file extension at the end of BACKUPPATH.<br>BACKUPPATH = f&quot;/{dt.day}-{dt.month}-{dt.year}&quot;</pre><p>Now we can move on to declaring the function that will upload the file to dropbox. this function will get two arguments a localFile and a backupPath. it will open the localFile and copy it to the specified backupPath. bu be careful because we are setting overwrite to be true so that if the file already exists in that path the script will just replace the old file with the new one.</p><pre>def backup(localFile, backupPath):<br>    with open(localFile, &#39;rb&#39;) as f:<br>    # We use WriteMode=overwrite to make sure that the settings in<br>    # the file are changed on upload<br>    print(&quot;Uploading &quot; + localFile + &quot; to Dropbox as &quot; + backupPath + &quot;...&quot;)<br>    try:<br>        dbx.files_upload(<br>                         f.read(), backupPath,<br>                         mode=WriteMode(&#39;overwrite&#39;)<br>                         )<br>    except ApiError as err:<br>    # This checks for the specific error where a user doesn&#39;t have<br>    # enough Dropbox space quota to upload this file<br>        if (err.error.is_path() and<br>            err.error.get_path().reason.is_insufficient_space()):<br>            sys.exit(&quot;ERROR: Cannot back up; insufficient space.&quot;)<br>        elif err.user_message_text:<br>            print(err.user_message_text)<br>            sys.exit()<br>        else:<br>            print(err)<br>            sys.exit()</pre><p>After defining the function that uploads the file to dropbox now it is time to use this function to upload the file in the main function.</p><pre>if __name__ == &#39;__main__&#39;:<br>    # Create an instance of a Dropbox class, which can make<br>    # requests to the API.<br>    print(&quot;Creating a Dropbox object...&quot;)<br>    dbx = dropbox.Dropbox(TOKEN)</pre><pre># Check that the access token is valid<br>    try:<br>        dbx.users_get_current_account()<br>    except AuthError:<br>        sys.exit(&quot;ERROR: Invalid access token; try re-generating an<br>                  access token from the app console on the web.&quot;)</pre><pre># Create a backup of the current settings file<br>    backup(LOCALFILE, BACKUPPATH)</pre><pre>print(&quot;Done!&quot;)</pre><p>And that is it for the Python part test it by specifying a test file and running it to see if you see it uploaded to your dropbox folder. we did it 🤗 now lets move on to setting the crontab up.</p><h3>Setting Cron to run our Python script everyday</h3><p>Now that we have a working python script that backs up our file the next step is to set a crontab to run the python script every now and then for a constant automatic backup.</p><p>Lets start by editing our default crontab file using the command: crontab -e <br>After that we need to go to the end of the file add a one line that will specify when to run our script. for a daily backup at 7:00AM we will be adding this line:</p><pre>00 07 * * * cd python-files-directory-path &amp;&amp; $(which python3) python-file-name.py</pre><p>After adding the line save and close the file and the crontab will be installed and run on time.</p><p>now there is one thing to consider here is that if you are running this on a remote server the time on the server might be different from the time in your current location. and even though it looks very simple a lot of people forget this and then wonder why their crontabs aren’t working right. to avoid this and know your server’s time use the command date</p><p>If you are interested the code from this tutorial is hosted in GitHub gists:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/99defe601db1bf77abaf1490cd0c96e6/href">https://medium.com/media/99defe601db1bf77abaf1490cd0c96e6/href</a></iframe><p>And that is it, as simple as this🥳.</p><p>If you enjoyed this tutorial please don’t forget to give it a clap, share it on social media, and follow me for more tutorials like this.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=82ce2fdb97a3" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>