<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ahmed I. Elsayed</title>
    <description>The latest articles on DEV Community by Ahmed I. Elsayed (@leondaz).</description>
    <link>https://dev.to/leondaz</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F347289%2Fa4d82799-575c-4cce-a385-7b55bfadfaa2.jpeg</url>
      <title>DEV Community: Ahmed I. Elsayed</title>
      <link>https://dev.to/leondaz</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/leondaz"/>
    <language>en</language>
    <item>
      <title>Unleash django CBVs</title>
      <dc:creator>Ahmed I. Elsayed</dc:creator>
      <pubDate>Sat, 30 May 2020 05:31:33 +0000</pubDate>
      <link>https://dev.to/leondaz/unleash-django-cbvs-32ep</link>
      <guid>https://dev.to/leondaz/unleash-django-cbvs-32ep</guid>
      <description>&lt;p&gt;When starting to learn django, You probably stick with functional views most of the time till you get used to idea of a view, Then all of a sudden you hear about &lt;em&gt;CBVs&lt;/em&gt; (Class based views)&lt;/p&gt;

&lt;p&gt;$$ {\frac{2}{3}} $$&lt;/p&gt;

</description>
      <category>python</category>
      <category>django</category>
    </item>
    <item>
      <title>How to execute code from admin page in django</title>
      <dc:creator>Ahmed I. Elsayed</dc:creator>
      <pubDate>Sat, 23 May 2020 02:02:37 +0000</pubDate>
      <link>https://dev.to/leondaz/how-to-execute-code-from-admin-page-in-django-229b</link>
      <guid>https://dev.to/leondaz/how-to-execute-code-from-admin-page-in-django-229b</guid>
      <description>&lt;h3&gt;
  
  
  How to execute code from admin page in django
&lt;/h3&gt;

&lt;p&gt;So I've been searching in stackoverflow a while ago and came across a nice question, The owner wants to have a button on the admin page to execute code that's &lt;em&gt;dynamically&lt;/em&gt; generated. I've answered it there but I still want to emphasize how important this topic is and this is why I'm writing this right now. &lt;/p&gt;

&lt;p&gt;This is what the &lt;code&gt;execute code&lt;/code&gt; button looks like&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0AOiklNx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/OnhggVt.png%3F1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0AOiklNx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/OnhggVt.png%3F1" alt="Execute button image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;WE WILL NOT BE CREATING THE SAME THING FROM THE QUESTION, WE'LL BE CREATING SOMETHING MORE GENERIC THAT'S NOT QUESTION RELATED&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;br&gt;
This article will be divided into sections as follows 

&lt;ol&gt;
&lt;li&gt;Naive implementation, Without taking anything in consideration.&lt;/li&gt;
&lt;li&gt;Execute the code with &lt;code&gt;Execute&lt;/code&gt; button on admin page.&lt;/li&gt;
&lt;li&gt;Security measures, What's the worst that can happen?&lt;/li&gt;
&lt;li&gt;Best practices of &lt;em&gt;dynamically&lt;/em&gt; generated code.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's get started.&lt;br&gt;
&lt;/p&gt;


&lt;h3 id="naive-implementation"&gt;Naive implementation&lt;/h3&gt;

&lt;p&gt;First of all, We're gonna have a model called &lt;code&gt;Rule&lt;/code&gt; and this &lt;code&gt;Rule&lt;/code&gt; model will have attributes 2 attributes, an attribute to hold the code itself and another for tracking if this was executed before.&lt;/p&gt;

&lt;p&gt;So basically a &lt;code&gt;Rule&lt;/code&gt; is an executable object, It's just a fancy name, name it whatever you want.&lt;/p&gt;

&lt;p&gt;This is how I've written the code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# models.py
class  Rule(models.Model):
    code = models.TextField()
    was_executed_before = models.BooleanField(default=False) # it was not executed before.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Pretty much everything is self-explanatory, We want to do something at the row level, to the &lt;code&gt;Rule&lt;/code&gt; itself, this means we're gonna add a method to the &lt;code&gt;Rule&lt;/code&gt; model here, We'll call it &lt;code&gt;execute&lt;/code&gt; and when we call &lt;code&gt;rule_obj.execute()&lt;/code&gt;, This executes the code. Python has a builtin function &lt;code&gt;eval(code: str)&lt;/code&gt; that takes the code as a string and evaluates it, returns what the code have returned.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;e.g&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# ran in IDLE
x = eval('[1, 2, 3]')
x 
# [1, 2, 3]
type(x)
# &amp;lt;class 'list'&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is &lt;code&gt;eval()&lt;/code&gt; in a nutshell, back on topic, This is our model now&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# models.py
class  Rule(models.Model):
    code = models.TextField()
    was_executed_before = models.BooleanField(default=False) # it was not executed before.

    def execute(self):
        # omit return or leave it depending on your needs
        return eval(self.code)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Let's talk a bit about this, Are we passing a &lt;code&gt;TextField&lt;/code&gt; object to eval? actually, if you're using &lt;code&gt;PyCharm&lt;/code&gt; IDE, the &lt;code&gt;self.code&lt;/code&gt; will be marked yellow, You can't pass a non-string in &lt;code&gt;eval()&lt;/code&gt;, This actually takes us to how &lt;code&gt;repr()&lt;/code&gt; and &lt;code&gt;str()&lt;/code&gt;work, the &lt;code&gt;TextField&lt;/code&gt; object is represented by it's value this is why this works and this is 100% correct, It's not a hack, It's the same as &lt;code&gt;Rule.code = "Whatever"&lt;/code&gt;, It's a string.&lt;/p&gt;

&lt;p&gt;Let's register our model in the admin page first&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# admin.py
....
from .models import Rule

admin.site.register(Rule)  
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Let's create a new &lt;code&gt;Rule&lt;/code&gt; object, btw you could have used &lt;code&gt;python manage.py shell&lt;/code&gt; to do this and this is totally fine but we need to add a button in the admin page anyway.&lt;/p&gt;

&lt;p&gt;This is the &lt;code&gt;Rule&lt;/code&gt; object I've created, It's useless, Let's be sure of our logic, We'll override the &lt;code&gt;save&lt;/code&gt; method temporary to execute it after it gets saved.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Qmee7rwl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/GUTitib.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Qmee7rwl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/GUTitib.png" alt="Creating a Rule object from admin panel"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;add this to your &lt;code&gt;Rule&lt;/code&gt; model&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def save(self, **kwargs):
    super().save(**kwargs)
    self.execute()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now, Open the &lt;code&gt;Rule&lt;/code&gt; object you've created and click save without changing anything, This calls &lt;code&gt;save&lt;/code&gt; and you'll notice in the console you've &lt;code&gt;Article written by LeOndaz&lt;/code&gt; printed. Our naive implementation works.&lt;br&gt;
&lt;/p&gt;


&lt;h3 id="execute-button"&gt;Adding an execution button&lt;/h3&gt;

&lt;p&gt;Let's add a button to execute this rule when we click this button, To add a button, We'll override how django renders the &lt;code&gt;change_form&lt;/code&gt;, basically, the &lt;code&gt;change_form&lt;/code&gt; is the form in the images I used above, the form used in changing models data.&lt;br&gt;&lt;/p&gt;

&lt;p&gt;Overriding django templates is straight-forward, The path to override this template for a specific model is &lt;code&gt;&amp;lt;app_name&amp;gt;/templates/admin/&amp;lt;app_name&amp;gt;/&amp;lt;model_name&amp;gt;/change_form.html&lt;/code&gt; &lt;br&gt;&lt;br&gt;
In my case, My app is called &lt;code&gt;core&lt;/code&gt; and my model is called &lt;code&gt;Rule&lt;/code&gt; so I'll use &lt;code&gt;core/templates/admin/core/rule/change_form.html&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;This is the content I've.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{# this is core/templates/admin/core/rule/change_form.html #}
{% extends 'admin/change_form.html' %}

{% block submit_buttons_bottom %}
    {{ block.super }} {# This calls super to get the default layout#}
    {# now let's add a button underneath it  #}
    &amp;lt;div  class="submit-row"&amp;gt;  {# this button will POST request with the name 'execute' #}
       &amp;lt;input  type="submit"  value="Execute the rule"  name="execute"&amp;gt;
    &amp;lt;/div&amp;gt;
{% endblock %}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;NOTES&lt;/strong&gt;: &lt;code&gt;submit-row&lt;/code&gt; class is a django builtin that they use in all of their buttons, This creates the grey area that contains the buttons like this. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--r6WxTISL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/pnPsReX.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--r6WxTISL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/pnPsReX.png" alt="Grey box image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;value&lt;/code&gt; is the text that will be shown on the button.  We didn't include &lt;code&gt;&amp;lt;form&amp;gt;&lt;/code&gt; because we're actually inside a form.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3Fmu01oZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/VWIg2H3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3Fmu01oZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/VWIg2H3.png" alt="Source code image to prove that we're really in a form"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notice the area surrounding the button, this is &lt;code&gt;submit-row&lt;/code&gt; in action.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9ir0Mp4g--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/g2XAm7k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9ir0Mp4g--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/g2XAm7k.png" alt="Grey area around the button"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's add the functionality of this button using a &lt;code&gt;ModelAdmin&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# admin.py
from django.shortcuts import redirect

class RuleAdmin(admin.ModelAdmin):
    def response_change(self, request, obj):
        if "execute" in request.POST:
            if not obj.was_executed_before:
                try:    
                    obj.execute()
                    obj.was_executed_before = True
                    obj.save()
                except (ValueError, TypeError):
                    pass
        return redirect(".")
        # if we didn't find 'execute' in POST data
        return  super().response_change(request, obj)

# don't forget to register the RuleAdmin with Rule
admin.site.register(Rule, RuleAdmin)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Let's save, go to our button and click it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[22/May/2020 23:26:01] "GET /admin/core/rule/1/change/ HTTP/1.1" 200 4751
[22/May/2020 23:26:02] "GET /admin/jsi18n/ HTTP/1.1" 200 3223
Article written by LeOndaz
[22/May/2020 23:26:04] "POST /admin/core/rule/1/change/ HTTP/1.1" 302 0
[22/May/2020 23:26:04] "GET /admin/core/rule/1/change/ HTTP/1.1" 200 4759
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;So it is actually working.&lt;br&gt;
&lt;/p&gt;


&lt;h3 id="security-measures"&gt;Security measures&lt;/h3&gt;

&lt;p&gt;I've added those to the top of my &lt;code&gt;models.py&lt;/code&gt; and pretty much any basic &lt;code&gt;models.py&lt;/code&gt; file will have those&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from django.contrib.auth import get_user_model
User = get_user_model()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now imagine owning a blog with moderators, One of those moderators can use Python, So he created this rule in the&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--A-ldX4n2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/z5IsLf0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--A-ldX4n2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/z5IsLf0.png" alt="Imgur"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;and he got this output &lt;code&gt;['test']&lt;/code&gt;&lt;br&gt;
So he actually can access everything on your server, He can get all of your users and change their password to something that he knows and bam, Your server is hacked! Now imagine if he's not a moderator, Someone who got access to the admin page, This is serious, This is why the implementation mentioned is only for demonstration purposes to make sure you get the idea.&lt;br&gt;
&lt;/p&gt;


&lt;h3 id="best-practices"&gt;Best practices of dynamically generated code&lt;/h3&gt;

&lt;p&gt;One of the best practices of creating dynamically generated code is to verify that this code matches a specific pattern, You don't want to prevent typing &lt;code&gt;eval()&lt;/code&gt; in the code, You want to allow typing something and prevent all others, This is why we'll use regex, We want to prevent running code that we don't like, including &lt;code&gt;eval()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Say we want to allow &lt;code&gt;print&lt;/code&gt; and prevent &lt;code&gt;eval&lt;/code&gt;, we won't verify if &lt;code&gt;eval()&lt;/code&gt; is in the code, instead, we will verify if &lt;code&gt;print()&lt;/code&gt; is there using regex and if &lt;code&gt;eval()&lt;/code&gt; is found, It's automatically ignored.&lt;/p&gt;

&lt;p&gt;Matching print statements, tested on &lt;a href="https://regexr.com/"&gt;regexr&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;print\("[a-zA-Z0-9]"\)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;So this matches any &lt;code&gt;print("whatever")&lt;/code&gt; and it automatically ignores any other code, So instead of limiting the code to use, We limit it to the code we want to use, I hope this makes sense.&lt;/p&gt;

&lt;p&gt;Now, in any file (but you'll have to import it) or in &lt;code&gt;models.py&lt;/code&gt;, add this function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# models.py
import re
valid_code_pattern = """print\("[a-zA-Z0-9]"\)"""
def valid_python_code(code):
    if re.match(valid_code_pattern, code):
        return True
    return False
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This will return &lt;code&gt;True&lt;/code&gt; if our code is valid with respect to how we defined the word &lt;em&gt;Valid&lt;/em&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def  execute(self):
    if valid_python_code(self.code):
        return eval(self.code)
    else:
        raise Exception
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;and this should only allow &lt;code&gt;print&lt;/code&gt; statements, This is much better and &lt;em&gt;SAFER&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://stackoverflow.com/questions/61842009/how-do-i-make-the-user-edit-update-the-py-file-in-django"&gt;Here's&lt;/a&gt; the question I was talking about&lt;/p&gt;

&lt;p&gt;So hopefully you've understood everything presented in this article, Feel free to say anything that can improve future articles but till then, stay safe.&lt;/p&gt;

</description>
      <category>python</category>
      <category>django</category>
    </item>
  </channel>
</rss>
