Commit 0542fafa authored by Bonnegent Sebastien's avatar Bonnegent Sebastien
Browse files

cours 2 à jour

parent 60643088
all: cours1.html cours2.html
all: cours1.html cours2.html cours3.html
%.html: %.md
pandoc -s -t revealjs -V revealjs-url=./reveal.js -V theme=beige -o $@ $<
......@@ -12,17 +12,15 @@ séance.
* mise en place du projet
* mise en place de l'application
* migration
* 1er classe (Port)
* 1er modèle (Port)
* shell
## Cours 2
* frontend
- administration
- serveur de développement
- urls, templates, vues
* interface d'administration
* modèles avec relations (Service, Groupe)
## Cours 3
* frontend: manipulation des objets
* suite des modèles avec relations (Regle, Parefeu)
* logging
* messages
......
......@@ -144,7 +144,7 @@ code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warni
<h2>L’environnement pipenv<a href="#/fn2" class="footnote-ref" id="fnref2"><sup>2</sup></a></h2>
<pre><code>$ git clone https://gitlab.insa-rouen.fr/bonnegent/cours_django.git
$ cd cours_django
$ mkdir fwm
$ mkdir fwm1
$ cd !$
$ pipenv --python 3.6
$ pipenv install django django-extensions ipython</code></pre>
......@@ -362,17 +362,11 @@ $ pipenv run ./manage.py migrate
<a class="sourceLine" id="cb24-15" title="15">In [<span class="dv">5</span>]: Port.objects.<span class="bu">all</span>().delete()</a></code></pre></div>
</section><section id="webuimodels.py-3" class="slide level2">
<h2>webui/models.py</h2>
<p>Faire les classes suivantes.</p>
<p>Faire la classe suivante</p>
<h3 id="adresse">Adresse</h3>
<pre><code>- nom: chaine de caractères (25)
- ip: une adresse IP
- masque: le masque réseau (défaut: 32)</code></pre>
<h3 id="groupe">Groupe</h3>
<pre><code>- nom: chaine de caractères (25)
- adresses: une liste d&#39;Adresse</code></pre>
<h3 id="service">Service</h3>
<pre><code>- nom: chaine de caractères (25)
- ports: une liste de ports</code></pre>
</section></section>
<section><section id="documentation" class="title-slide slide level1"><h1>Documentation</h1></section><section id="django-1" class="slide level2">
<h2>Django</h2>
......
......@@ -44,7 +44,7 @@ title: Django par la pratique 1/7
~~~
$ git clone https://gitlab.insa-rouen.fr/bonnegent/cours_django.git
$ cd cours_django
$ mkdir fwm
$ mkdir fwm1
$ cd !$
$ pipenv --python 3.6
$ pipenv install django django-extensions ipython
......@@ -330,18 +330,12 @@ In [5]: Port.objects.all().delete()
~~~
## webui/models.py
Faire les classes suivantes.
Faire la classe suivante
### Adresse
- nom: chaine de caractères (25)
- ip: une adresse IP
- masque: le masque réseau (défaut: 32)
### Groupe
- nom: chaine de caractères (25)
- adresses: une liste d'Adresse
### Service
- nom: chaine de caractères (25)
- ports: une liste de ports
# Documentation
## Django
......
......@@ -100,16 +100,22 @@ code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warni
<p class="author">Sébastien Bonnegent</p>
</section>
<section><section id="départ" class="title-slide slide level1"><h1>Départ</h1></section><section id="support-de-cours" class="slide level2">
<h2>Support de cours</h2>
<p>Attention au remplacement de fwm !</p>
<pre><code>$ git pull
$ git checkout cours_2</code></pre>
<section><section id="départ" class="title-slide slide level1"><h1>Départ</h1></section><section id="fichiers" class="slide level2">
<h2>Fichiers</h2>
<ul>
<li>support: cours2.html</li>
</ul>
<h3 id="lancien">L’ancien</h3>
<pre><code>$ cd fwm1
$ pipenv --rm</code></pre>
<h3 id="le-nouveau">Le nouveau</h3>
<pre><code>$ cd fwm2
$ pipenv install</code></pre>
</section><section id="au-programme" class="slide level2">
<h2>Au programme</h2>
<ul>
<li>l’interface d’administration</li>
<li>les modèles</li>
<li>les modèles avec relations (Service, Groupe)</li>
</ul>
</section></section>
<section><section id="administration" class="title-slide slide level1"><h1>Administration</h1></section><section id="serveur-de-développement" class="slide level2">
......@@ -120,12 +126,13 @@ $ git checkout cours_2</code></pre>
<li>permet de tester rapidement le projet</li>
</ul>
<p>=&gt; http://127.0.0.1:8000</p>
</section><section id="webuiadmin.py" class="slide level2">
<h2>webui/admin.py</h2>
<div class="sourceCode" id="cb3"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb3-1" title="1"><span class="im">from</span> django.contrib <span class="im">import</span> admin</a>
<a class="sourceLine" id="cb3-2" title="2"><span class="im">from</span> .models <span class="im">import</span> Port</a>
<a class="sourceLine" id="cb3-3" title="3"></a>
<a class="sourceLine" id="cb3-4" title="4">admin.site.register(Port)</a></code></pre></div>
</section><section id="à-modifier" class="slide level2">
<h2>À modifier</h2>
<h3 id="webuiadmin.py">webui/admin.py</h3>
<div class="sourceCode" id="cb4"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb4-1" title="1"><span class="im">from</span> django.contrib <span class="im">import</span> admin</a>
<a class="sourceLine" id="cb4-2" title="2"><span class="im">from</span> .models <span class="im">import</span> Port</a>
<a class="sourceLine" id="cb4-3" title="3"></a>
<a class="sourceLine" id="cb4-4" title="4">admin.site.register(Port)</a></code></pre></div>
</section><section id="accéder-à-ladmin" class="slide level2">
<h2>accéder à l’admin</h2>
<pre><code>$ pipenv run ./manage.py runserver
......@@ -156,16 +163,16 @@ Bypass password validation and create user anyway? [y/N]: y
Superuser created successfully.</code></pre>
</section><section id="manage.py-shell_plus" class="slide level2">
<h2>./manage.py shell_plus</h2>
<div class="sourceCode" id="cb6"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb6-1" title="1">In [<span class="dv">1</span>]: User.objects.<span class="bu">all</span>() </a>
<a class="sourceLine" id="cb6-2" title="2">Out[<span class="dv">1</span>]: <span class="op">&lt;</span>QuerySet [<span class="op">&lt;</span>User: admin<span class="op">&gt;</span>]<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb6-3" title="3"></a>
<a class="sourceLine" id="cb6-4" title="4">In [<span class="dv">2</span>]: u <span class="op">=</span> User.objects.get(username<span class="op">=</span><span class="st">&#39;admin&#39;</span>) </a>
<a class="sourceLine" id="cb6-5" title="5"></a>
<a class="sourceLine" id="cb6-6" title="6">In [<span class="dv">3</span>]: u.is_superuser </a>
<a class="sourceLine" id="cb6-7" title="7">Out[<span class="dv">3</span>]: <span class="va">True</span></a>
<a class="sourceLine" id="cb6-8" title="8"></a>
<a class="sourceLine" id="cb6-9" title="9">In [<span class="dv">4</span>]: u.is_staff </a>
<a class="sourceLine" id="cb6-10" title="10">Out[<span class="dv">4</span>]: <span class="va">True</span></a></code></pre></div>
<div class="sourceCode" id="cb7"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb7-1" title="1">In [<span class="dv">1</span>]: User.objects.<span class="bu">all</span>() </a>
<a class="sourceLine" id="cb7-2" title="2">Out[<span class="dv">1</span>]: <span class="op">&lt;</span>QuerySet [<span class="op">&lt;</span>User: admin<span class="op">&gt;</span>]<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb7-3" title="3"></a>
<a class="sourceLine" id="cb7-4" title="4">In [<span class="dv">2</span>]: u <span class="op">=</span> User.objects.get(username<span class="op">=</span><span class="st">&#39;admin&#39;</span>) </a>
<a class="sourceLine" id="cb7-5" title="5"></a>
<a class="sourceLine" id="cb7-6" title="6">In [<span class="dv">3</span>]: u.is_superuser </a>
<a class="sourceLine" id="cb7-7" title="7">Out[<span class="dv">3</span>]: <span class="va">True</span></a>
<a class="sourceLine" id="cb7-8" title="8"></a>
<a class="sourceLine" id="cb7-9" title="9">In [<span class="dv">4</span>]: u.is_staff </a>
<a class="sourceLine" id="cb7-10" title="10">Out[<span class="dv">4</span>]: <span class="va">True</span></a></code></pre></div>
</section><section id="administration-1" class="slide level2">
<h2>Administration</h2>
<p>Créer les ports suivants:</p>
......@@ -174,16 +181,47 @@ Superuser created successfully.</code></pre>
<li>389/tcp</li>
<li>443/tcp</li>
</ul>
</section><section id="affichage" class="slide level2">
<h2>Affichage</h2>
</section><section id="à-modifier-1" class="slide level2">
<h2>à modifier</h2>
<h3 id="webuiadmin.py-1">webui/admin.py</h3>
<div class="sourceCode" id="cb7"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb7-1" title="1"><span class="kw">class</span> PortAdmin(admin.ModelAdmin):</a>
<a class="sourceLine" id="cb7-2" title="2"> list_display <span class="op">=</span> (<span class="st">&#39;debut&#39;</span>, <span class="st">&#39;fin&#39;</span>, <span class="st">&#39;protocole&#39;</span>)</a>
<a class="sourceLine" id="cb7-3" title="3"></a>
<a class="sourceLine" id="cb7-4" title="4"></a>
<a class="sourceLine" id="cb7-5" title="5">admin.site.register(Port, PortAdmin)</a></code></pre></div>
<div class="sourceCode" id="cb8"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb8-1" title="1"><span class="kw">class</span> PortAdmin(admin.ModelAdmin):</a>
<a class="sourceLine" id="cb8-2" title="2"> list_display <span class="op">=</span> (<span class="st">&#39;debut&#39;</span>, <span class="st">&#39;fin&#39;</span>, <span class="st">&#39;protocole&#39;</span>)</a>
<a class="sourceLine" id="cb8-3" title="3"></a>
<a class="sourceLine" id="cb8-4" title="4"></a>
<a class="sourceLine" id="cb8-5" title="5">admin.site.register(Port, PortAdmin)</a></code></pre></div>
</section><section id="à-faire" class="slide level2">
<h2>à faire</h2>
<ul>
<li>Adresse dans la partie administration</li>
<li>afficher les colonnes: nom, ip et masque</li>
<li>créer les Adresses suivantes:
<ul>
<li>localhost 127.0.0.0/8</li>
<li>www.insa-rouen.fr 193.49.10.214/32</li>
<li>dsi.insa-rouen.fr 193.49.10.146/32</li>
</ul></li>
</ul>
</section></section>
<section><section id="service" class="title-slide slide level1"><h1>Service</h1></section><section id="manytomany" class="slide level2">
<section><section id="service" class="title-slide slide level1"><h1>Service</h1></section><section id="factorisation" class="slide level2">
<h2>Factorisation</h2>
<ul>
<li>Adresse et Service ont un attribut <strong>nom</strong></li>
<li>=&gt; création d’une classe abstraite</li>
</ul>
</section><section id="à-modifier-2" class="slide level2">
<h2>à modifier</h2>
<h3 id="webuimodels.py">webui/models.py</h3>
<div class="sourceCode" id="cb9"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb9-1" title="1"><span class="kw">class</span> AvecNom(models.Model):</a>
<a class="sourceLine" id="cb9-2" title="2"> <span class="co">&quot;&quot;&quot;Classe abstraite pour les classes ayant un nom&quot;&quot;&quot;</span></a>
<a class="sourceLine" id="cb9-3" title="3"> nom <span class="op">=</span> models.CharField(default<span class="op">=</span><span class="st">&#39;&#39;</span>, max_length<span class="op">=</span><span class="dv">25</span>)</a>
<a class="sourceLine" id="cb9-4" title="4"></a>
<a class="sourceLine" id="cb9-5" title="5"> <span class="kw">class</span> Meta(<span class="bu">object</span>):</a>
<a class="sourceLine" id="cb9-6" title="6"> abstract <span class="op">=</span> <span class="va">True</span></a>
<a class="sourceLine" id="cb9-7" title="7"> ordering <span class="op">=</span> (<span class="st">&#39;nom&#39;</span>, )</a>
<a class="sourceLine" id="cb9-8" title="8"></a>
<a class="sourceLine" id="cb9-9" title="9"> <span class="kw">def</span> <span class="fu">__str__</span>(<span class="va">self</span>):</a>
<a class="sourceLine" id="cb9-10" title="10"> <span class="cf">return</span> <span class="va">self</span>.nom</a></code></pre></div>
</section><section id="manytomany" class="slide level2">
<h2>ManyToMany</h2>
<ul>
<li>relation type 0 à n vers une autre classe</li>
......@@ -192,15 +230,18 @@ Superuser created successfully.</code></pre>
<li>pas de save()</li>
<li>cas de Service vers Port</li>
</ul>
</section><section id="webuimodels.py" class="slide level2">
<h2>webui/models.py</h2>
<div class="sourceCode" id="cb8"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb8-1" title="1"><span class="kw">class</span> Service(AvecNom):</a>
<a class="sourceLine" id="cb8-2" title="2"> <span class="co">&quot;&quot;&quot;Un service regroupe un ensemble de ports.</span></a>
<a class="sourceLine" id="cb8-3" title="3"></a>
<a class="sourceLine" id="cb8-4" title="4"><span class="co"> :param ports: une liste de Ports</span></a>
<a class="sourceLine" id="cb8-5" title="5"><span class="co"> &quot;&quot;&quot;</span></a>
<a class="sourceLine" id="cb8-6" title="6"> nom <span class="op">=</span> models.CharField(default<span class="op">=</span><span class="st">&#39;&#39;</span>, max_length<span class="op">=</span><span class="dv">25</span>)</a>
<a class="sourceLine" id="cb8-7" title="7"> ports <span class="op">=</span> models.ManyToManyField(Port)</a></code></pre></div>
</section><section id="à-modifier-3" class="slide level2">
<h2>à modifier</h2>
<h3 id="webuimodels.py-1">webui/models.py</h3>
<div class="sourceCode" id="cb10"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb10-1" title="1"><span class="kw">class</span> Service(AvecNom):</a>
<a class="sourceLine" id="cb10-2" title="2"> <span class="co">&quot;&quot;&quot;Un service regroupe un ensemble de ports.</span></a>
<a class="sourceLine" id="cb10-3" title="3"></a>
<a class="sourceLine" id="cb10-4" title="4"><span class="co"> :param ports: une liste de Ports</span></a>
<a class="sourceLine" id="cb10-5" title="5"><span class="co"> &quot;&quot;&quot;</span></a>
<a class="sourceLine" id="cb10-6" title="6"> nom <span class="op">=</span> models.CharField(default<span class="op">=</span><span class="st">&#39;&#39;</span>, max_length<span class="op">=</span><span class="dv">25</span>)</a>
<a class="sourceLine" id="cb10-7" title="7"> ports <span class="op">=</span> models.ManyToManyField(Port)</a></code></pre></div>
<pre><code>$ pipenv run ./manage.py makemigrations
$ pipenv run ./manage.py migrate</code></pre>
</section><section id="administration-2" class="slide level2">
<h2>Administration</h2>
<ul>
......@@ -215,202 +256,101 @@ Superuser created successfully.</code></pre>
</ul>
</section><section id="shell_plus" class="slide level2">
<h2>shell_plus</h2>
<div class="sourceCode" id="cb9"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb9-1" title="1">web <span class="op">=</span> Service.objects.get(nom<span class="op">=</span><span class="st">&quot;web&quot;</span>)</a>
<a class="sourceLine" id="cb9-2" title="2">p22 <span class="op">=</span> Port.objects.get(debut<span class="op">=</span><span class="dv">22</span>)</a>
<a class="sourceLine" id="cb9-3" title="3">p80 <span class="op">=</span> Port.objects.get(debut<span class="op">=</span><span class="dv">80</span>)</a>
<a class="sourceLine" id="cb9-4" title="4">p443 <span class="op">=</span> Port.objects.get(debut<span class="op">=</span><span class="dv">443</span>)</a></code></pre></div>
<div class="sourceCode" id="cb12"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb12-1" title="1">web <span class="op">=</span> Service.objects.get(nom<span class="op">=</span><span class="st">&quot;web&quot;</span>)</a>
<a class="sourceLine" id="cb12-2" title="2">p22 <span class="op">=</span> Port.objects.get(debut<span class="op">=</span><span class="dv">22</span>)</a>
<a class="sourceLine" id="cb12-3" title="3">p80 <span class="op">=</span> Port.objects.get(debut<span class="op">=</span><span class="dv">80</span>)</a>
<a class="sourceLine" id="cb12-4" title="4">p443 <span class="op">=</span> Port.objects.get(debut<span class="op">=</span><span class="dv">443</span>)</a></code></pre></div>
</section><section id="manipulation" class="slide level2">
<h2>Manipulation</h2>
<div class="sourceCode" id="cb10"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb10-1" title="1">In [<span class="dv">5</span>]: web.ports.add(p80, p443)</a>
<a class="sourceLine" id="cb10-2" title="2">In [<span class="dv">6</span>]: web.ports.<span class="bu">all</span>() <span class="co"># pas de double !</span></a>
<a class="sourceLine" id="cb10-3" title="3">Out[<span class="dv">6</span>]: <span class="op">&lt;</span>QuerySet [<span class="op">&lt;</span>Port: <span class="dv">80</span>(tcp)<span class="op">&gt;</span>, <span class="op">&lt;</span>Port: <span class="dv">443</span>(tcp)<span class="op">&gt;</span>]<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb10-4" title="4"></a>
<a class="sourceLine" id="cb10-5" title="5">In [<span class="dv">7</span>]: web.ports.add(p22)</a>
<a class="sourceLine" id="cb10-6" title="6">In [<span class="dv">8</span>]: web.ports.<span class="bu">all</span>()</a>
<a class="sourceLine" id="cb10-7" title="7">Out[<span class="dv">8</span>]: <span class="op">&lt;</span>QuerySet [<span class="op">&lt;</span>Port: <span class="dv">22</span>(tcp)<span class="op">&gt;</span>, <span class="op">&lt;</span>Port: <span class="dv">80</span>(tcp)<span class="op">&gt;</span>, <span class="op">&lt;</span>Port: <span class="dv">443</span>(tcp)<span class="op">&gt;</span>]<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb10-8" title="8"></a>
<a class="sourceLine" id="cb10-9" title="9">In [<span class="dv">9</span>]: web.ports.remove(p22)</a>
<a class="sourceLine" id="cb10-10" title="10">In [<span class="dv">10</span>]: web.ports.<span class="bu">all</span>()</a>
<a class="sourceLine" id="cb10-11" title="11">Out[<span class="dv">10</span>]: <span class="op">&lt;</span>QuerySet [<span class="op">&lt;</span>Port: <span class="dv">80</span>(tcp)<span class="op">&gt;</span>, <span class="op">&lt;</span>Port: <span class="dv">443</span>(tcp)<span class="op">&gt;</span>]<span class="op">&gt;</span></a></code></pre></div>
<div class="sourceCode" id="cb13"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb13-1" title="1">In [<span class="dv">5</span>]: web.ports.add(p80, p443)</a>
<a class="sourceLine" id="cb13-2" title="2">In [<span class="dv">6</span>]: web.ports.<span class="bu">all</span>() <span class="co"># pas de double !</span></a>
<a class="sourceLine" id="cb13-3" title="3">Out[<span class="dv">6</span>]: <span class="op">&lt;</span>QuerySet [<span class="op">&lt;</span>Port: <span class="dv">80</span>(tcp)<span class="op">&gt;</span>, <span class="op">&lt;</span>Port: <span class="dv">443</span>(tcp)<span class="op">&gt;</span>]<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb13-4" title="4"></a>
<a class="sourceLine" id="cb13-5" title="5">In [<span class="dv">7</span>]: web.ports.add(p22)</a>
<a class="sourceLine" id="cb13-6" title="6">In [<span class="dv">8</span>]: web.ports.<span class="bu">all</span>()</a>
<a class="sourceLine" id="cb13-7" title="7">Out[<span class="dv">8</span>]: <span class="op">&lt;</span>QuerySet [<span class="op">&lt;</span>Port: <span class="dv">22</span>(tcp)<span class="op">&gt;</span>, <span class="op">&lt;</span>Port: <span class="dv">80</span>(tcp)<span class="op">&gt;</span>, <span class="op">&lt;</span>Port: <span class="dv">443</span>(tcp)<span class="op">&gt;</span>]<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb13-8" title="8"></a>
<a class="sourceLine" id="cb13-9" title="9">In [<span class="dv">9</span>]: web.ports.remove(p22)</a>
<a class="sourceLine" id="cb13-10" title="10">In [<span class="dv">10</span>]: web.ports.<span class="bu">all</span>()</a>
<a class="sourceLine" id="cb13-11" title="11">Out[<span class="dv">10</span>]: <span class="op">&lt;</span>QuerySet [<span class="op">&lt;</span>Port: <span class="dv">80</span>(tcp)<span class="op">&gt;</span>, <span class="op">&lt;</span>Port: <span class="dv">443</span>(tcp)<span class="op">&gt;</span>]<span class="op">&gt;</span></a></code></pre></div>
</section><section id="affectation" class="slide level2">
<h2>Affectation</h2>
<div class="sourceCode" id="cb11"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb11-1" title="1">In [<span class="dv">17</span>]: web.ports.<span class="bu">set</span>([p80, p22])</a>
<a class="sourceLine" id="cb11-2" title="2">In [<span class="dv">18</span>]: web.ports.<span class="bu">all</span>()</a>
<a class="sourceLine" id="cb11-3" title="3">Out[<span class="dv">18</span>]: <span class="op">&lt;</span>QuerySet [<span class="op">&lt;</span>Port: <span class="dv">22</span>(tcp)<span class="op">&gt;</span>, <span class="op">&lt;</span>Port: <span class="dv">80</span>(tcp)<span class="op">&gt;</span>]<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb11-4" title="4">In [<span class="dv">19</span>]: web.ports.<span class="bu">set</span>([p80, p443])</a></code></pre></div>
<div class="sourceCode" id="cb14"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb14-1" title="1">In [<span class="dv">17</span>]: web.ports.<span class="bu">set</span>([p80, p22])</a>
<a class="sourceLine" id="cb14-2" title="2">In [<span class="dv">18</span>]: web.ports.<span class="bu">all</span>()</a>
<a class="sourceLine" id="cb14-3" title="3">Out[<span class="dv">18</span>]: <span class="op">&lt;</span>QuerySet [<span class="op">&lt;</span>Port: <span class="dv">22</span>(tcp)<span class="op">&gt;</span>, <span class="op">&lt;</span>Port: <span class="dv">80</span>(tcp)<span class="op">&gt;</span>]<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb14-4" title="4">In [<span class="dv">19</span>]: web.ports.<span class="bu">set</span>([p80, p443])</a></code></pre></div>
</section><section id="filtrage" class="slide level2">
<h2>Filtrage</h2>
<div class="sourceCode" id="cb12"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb12-1" title="1">In [<span class="dv">23</span>]: filtre <span class="op">=</span> Service.objects.<span class="bu">filter</span></a>
<a class="sourceLine" id="cb12-2" title="2">In [<span class="dv">24</span>]: filtre(ports__debut__gt<span class="op">=</span><span class="dv">80</span>)</a>
<a class="sourceLine" id="cb12-3" title="3">Out[<span class="dv">24</span>]: <span class="op">&lt;</span>QuerySet [<span class="op">&lt;</span>Service: ldap<span class="op">&gt;</span>, <span class="op">&lt;</span>Service: web<span class="op">&gt;</span>]<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb12-4" title="4"></a>
<a class="sourceLine" id="cb12-5" title="5">In [<span class="dv">25</span>]: filtre(ports__debut__gt<span class="op">=</span><span class="dv">22</span>)</a>
<a class="sourceLine" id="cb12-6" title="6">Out[<span class="dv">25</span>]: <span class="op">&lt;</span>QuerySet [<span class="op">&lt;</span>Service: dns<span class="op">&gt;</span>, <span class="op">&lt;</span>Service: ldap<span class="op">&gt;</span>,</a>
<a class="sourceLine" id="cb12-7" title="7"> <span class="op">&lt;</span>Service: web<span class="op">&gt;</span>, <span class="op">&lt;</span>Service: web<span class="op">&gt;</span>]<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb12-8" title="8"></a>
<a class="sourceLine" id="cb12-9" title="9">In [<span class="dv">26</span>]: filtre(ports__debut__gt<span class="op">=</span><span class="dv">22</span>).distinct()</a>
<a class="sourceLine" id="cb12-10" title="10">Out[<span class="dv">26</span>]: <span class="op">&lt;</span>QuerySet [<span class="op">&lt;</span>Service: dns<span class="op">&gt;</span>, <span class="op">&lt;</span>Service: ldap<span class="op">&gt;</span>, <span class="op">&lt;</span>Service: web<span class="op">&gt;</span>]<span class="op">&gt;</span></a></code></pre></div>
<div class="sourceCode" id="cb15"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb15-1" title="1">In [<span class="dv">23</span>]: filtre <span class="op">=</span> Service.objects.<span class="bu">filter</span></a>
<a class="sourceLine" id="cb15-2" title="2">In [<span class="dv">24</span>]: filtre(ports__debut__gt<span class="op">=</span><span class="dv">80</span>)</a>
<a class="sourceLine" id="cb15-3" title="3">Out[<span class="dv">24</span>]: <span class="op">&lt;</span>QuerySet [<span class="op">&lt;</span>Service: ldap<span class="op">&gt;</span>, <span class="op">&lt;</span>Service: web<span class="op">&gt;</span>]<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb15-4" title="4"></a>
<a class="sourceLine" id="cb15-5" title="5">In [<span class="dv">25</span>]: filtre(ports__debut__gt<span class="op">=</span><span class="dv">22</span>)</a>
<a class="sourceLine" id="cb15-6" title="6">Out[<span class="dv">25</span>]: <span class="op">&lt;</span>QuerySet [<span class="op">&lt;</span>Service: dns<span class="op">&gt;</span>, <span class="op">&lt;</span>Service: ldap<span class="op">&gt;</span>,</a>
<a class="sourceLine" id="cb15-7" title="7"> <span class="op">&lt;</span>Service: web<span class="op">&gt;</span>, <span class="op">&lt;</span>Service: web<span class="op">&gt;</span>]<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb15-8" title="8"></a>
<a class="sourceLine" id="cb15-9" title="9">In [<span class="dv">26</span>]: filtre(ports__debut__gt<span class="op">=</span><span class="dv">22</span>).distinct()</a>
<a class="sourceLine" id="cb15-10" title="10">Out[<span class="dv">26</span>]: <span class="op">&lt;</span>QuerySet [<span class="op">&lt;</span>Service: dns<span class="op">&gt;</span>, <span class="op">&lt;</span>Service: ldap<span class="op">&gt;</span>, <span class="op">&lt;</span>Service: web<span class="op">&gt;</span>]<span class="op">&gt;</span></a></code></pre></div>
</section><section id="relation-inverse" class="slide level2">
<h2>Relation inverse</h2>
<div class="sourceCode" id="cb13"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb13-1" title="1">In [<span class="dv">34</span>]: Port.objects.<span class="bu">filter</span>(service<span class="op">=</span>web)</a>
<a class="sourceLine" id="cb13-2" title="2">Out[<span class="dv">34</span>]: <span class="op">&lt;</span>QuerySet [<span class="op">&lt;</span>Port: <span class="dv">80</span>(tcp)<span class="op">&gt;</span>, <span class="op">&lt;</span>Port: <span class="dv">443</span>(tcp)<span class="op">&gt;</span>]<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb13-3" title="3"></a>
<a class="sourceLine" id="cb13-4" title="4">In [<span class="dv">35</span>]: p80.service_set.<span class="bu">all</span>()</a>
<a class="sourceLine" id="cb13-5" title="5">Out[<span class="dv">35</span>]: <span class="op">&lt;</span>QuerySet [<span class="op">&lt;</span>Service: web<span class="op">&gt;</span>]<span class="op">&gt;</span></a></code></pre></div>
<div class="sourceCode" id="cb16"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb16-1" title="1">In [<span class="dv">34</span>]: Port.objects.<span class="bu">filter</span>(service<span class="op">=</span>web)</a>
<a class="sourceLine" id="cb16-2" title="2">Out[<span class="dv">34</span>]: <span class="op">&lt;</span>QuerySet [<span class="op">&lt;</span>Port: <span class="dv">80</span>(tcp)<span class="op">&gt;</span>, <span class="op">&lt;</span>Port: <span class="dv">443</span>(tcp)<span class="op">&gt;</span>]<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb16-3" title="3"></a>
<a class="sourceLine" id="cb16-4" title="4">In [<span class="dv">35</span>]: p80.service_set.<span class="bu">all</span>()</a>
<a class="sourceLine" id="cb16-5" title="5">Out[<span class="dv">35</span>]: <span class="op">&lt;</span>QuerySet [<span class="op">&lt;</span>Service: web<span class="op">&gt;</span>]<span class="op">&gt;</span></a></code></pre></div>
</section><section id="administration-3" class="slide level2">
<h2>Administration</h2>
<h3 id="améliorations">Améliorations</h3>
<ul>
<li>afficher la liste des ports</li>
<li>ordre de tri par nom</li>
<li>champs de recherche</li>
</ul>
<h3 id="webuimodels.py-1">webui/models.py</h3>
<div class="sourceCode" id="cb14"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb14-1" title="1"><span class="kw">class</span> Service(models.Model):</a>
<a class="sourceLine" id="cb14-2" title="2"> <span class="kw">class</span> Meta(<span class="bu">object</span>):</a>
<a class="sourceLine" id="cb14-3" title="3"> ordering <span class="op">=</span> (<span class="st">&#39;nom&#39;</span>, )</a>
<a class="sourceLine" id="cb14-4" title="4"></a>
<a class="sourceLine" id="cb14-5" title="5"> <span class="kw">def</span> liste_des_ports(<span class="va">self</span>):</a>
<a class="sourceLine" id="cb14-6" title="6"> <span class="cf">return</span> <span class="st">&quot;, &quot;</span>.join([<span class="bu">str</span>(p)</a>
<a class="sourceLine" id="cb14-7" title="7"> <span class="cf">for</span> p <span class="kw">in</span> <span class="va">self</span>.ports.<span class="bu">all</span>()])</a></code></pre></div>
</section><section id="à-modifier-4" class="slide level2">
<h2>à modifier</h2>
<h3 id="webuimodels.py-2">webui/models.py</h3>
<div class="sourceCode" id="cb17"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb17-1" title="1"><span class="kw">class</span> Service(models.Model):</a>
<a class="sourceLine" id="cb17-2" title="2"> <span class="kw">class</span> Meta(<span class="bu">object</span>):</a>
<a class="sourceLine" id="cb17-3" title="3"> ordering <span class="op">=</span> (<span class="st">&#39;nom&#39;</span>, )</a>
<a class="sourceLine" id="cb17-4" title="4"></a>
<a class="sourceLine" id="cb17-5" title="5"> <span class="kw">def</span> liste_des_ports(<span class="va">self</span>):</a>
<a class="sourceLine" id="cb17-6" title="6"> <span class="cf">return</span> <span class="st">&quot;, &quot;</span>.join([<span class="bu">str</span>(p)</a>
<a class="sourceLine" id="cb17-7" title="7"> <span class="cf">for</span> p <span class="kw">in</span> <span class="va">self</span>.ports.<span class="bu">all</span>()])</a></code></pre></div>
</section><section id="webuiadmin.py-2" class="slide level2">
<h2>webui/admin.py</h2>
<div class="sourceCode" id="cb15"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb15-1" title="1"><span class="im">from</span> .models <span class="im">import</span> Service</a>
<a class="sourceLine" id="cb15-2" title="2"><span class="kw">class</span> ServiceAdmin(admin.ModelAdmin):</a>
<a class="sourceLine" id="cb15-3" title="3"> list_display <span class="op">=</span> (<span class="st">&#39;nom&#39;</span>, <span class="st">&#39;liste_des_ports&#39;</span>)</a>
<a class="sourceLine" id="cb15-4" title="4"> search_fields <span class="op">=</span> (<span class="st">&#39;nom&#39;</span>, <span class="st">&#39;ports__debut&#39;</span>)</a>
<a class="sourceLine" id="cb15-5" title="5"></a>
<a class="sourceLine" id="cb15-6" title="6">admin.site.register(Service, ServiceAdmin)</a></code></pre></div>
<div class="sourceCode" id="cb18"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb18-1" title="1"><span class="im">from</span> .models <span class="im">import</span> Service</a>
<a class="sourceLine" id="cb18-2" title="2"><span class="kw">class</span> ServiceAdmin(admin.ModelAdmin):</a>
<a class="sourceLine" id="cb18-3" title="3"> list_display <span class="op">=</span> (<span class="st">&#39;nom&#39;</span>, <span class="st">&#39;liste_des_ports&#39;</span>)</a>
<a class="sourceLine" id="cb18-4" title="4"> search_fields <span class="op">=</span> (<span class="st">&#39;nom&#39;</span>, <span class="st">&#39;ports__debut&#39;</span>)</a>
<a class="sourceLine" id="cb18-5" title="5"></a>
<a class="sourceLine" id="cb18-6" title="6">admin.site.register(Service, ServiceAdmin)</a></code></pre></div>
</section></section>
<section><section id="modèles" class="title-slide slide level1"><h1>Modèles</h1></section><section id="factorisation" class="slide level2">
<h2>Factorisation</h2>
<ul>
<li>Adresse et Service ont un attribut <strong>nom</strong></li>
<li>modifier les classes pour utiliser cette classe abstraite</li>
</ul>
</section><section id="webuimodels.py-2" class="slide level2">
<h2>webui/models.py</h2>
<div class="sourceCode" id="cb16"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb16-1" title="1"><span class="kw">class</span> AvecNom(models.Model):</a>
<a class="sourceLine" id="cb16-2" title="2"> <span class="co">&quot;&quot;&quot;Classe abstraite pour les classes ayant un nom&quot;&quot;&quot;</span></a>
<a class="sourceLine" id="cb16-3" title="3"> nom <span class="op">=</span> models.CharField(default<span class="op">=</span><span class="st">&#39;&#39;</span>, max_length<span class="op">=</span><span class="dv">25</span>)</a>
<a class="sourceLine" id="cb16-4" title="4"></a>
<a class="sourceLine" id="cb16-5" title="5"> <span class="kw">class</span> Meta(<span class="bu">object</span>):</a>
<a class="sourceLine" id="cb16-6" title="6"> abstract <span class="op">=</span> <span class="va">True</span></a>
<a class="sourceLine" id="cb16-7" title="7"> ordering <span class="op">=</span> (<span class="st">&#39;nom&#39;</span>, )</a>
<a class="sourceLine" id="cb16-8" title="8"></a>
<a class="sourceLine" id="cb16-9" title="9"> <span class="kw">def</span> <span class="fu">__str__</span>(<span class="va">self</span>):</a>
<a class="sourceLine" id="cb16-10" title="10"> <span class="cf">return</span> <span class="va">self</span>.nom</a></code></pre></div>
</section><section id="groupe" class="slide level2">
<h2>Groupe</h2>
<p>À faire</p>
<section><section id="groupe" class="title-slide slide level1"><h1>Groupe</h1></section><section id="à-faire-1" class="slide level2">
<h2>À faire</h2>
<h3 id="webuimodels.py-3">webui/models.py</h3>
<ul>
<li>nom</li>
<li>adresses: de 0 à N Adresse</li>
</ul>
</section><section id="à-faire-2" class="slide level2">
<h2>À faire</h2>
<h3 id="webuiadmin.py-3">webui/admin.py</h3>
<ul>
<li>interface d’administration</li>
<li>colonne ‘liste_des_adresses’</li>
</ul>
</section></section>
<section><section id="règle" class="title-slide slide level1"><h1>Règle</h1></section><section id="attributs" class="slide level2">
<h2>Attributs</h2>
<ul>
<li>priorite: un entier positif</li>
<li>groupe: un groupe d’Adresse</li>
<li>services: une liste de Service</li>
<li>interface: une chaine de caractères (25)</li>
<li>actif: un booléen</li>
<li>sens: un élément de la liste Input, Forward, Output</li>
<li>parefeu: un Parefeu ‘modèle’</li>
</ul>
</section><section id="foreignkey" class="slide level2">
<h2>ForeignKey</h2>
<ul>
<li>cas de Groupe et Parefeu</li>
<li>lien vers une seule instance d’une autre classe</li>
<li>chargé par l’ORM (regle.groupe.nom)</li>
<li>null: valeur nulle base de données</li>
<li>blank: valeur nulle formulaire</li>
<li>on_delete <a href="#/fn1" class="footnote-ref" id="fnref1"><sup>1</sup></a>: comportement lors de la suppression de la cible
<ul>
<li>CASCADE: suppression aussi de l’instance</li>
<li>SET_NULL: valeur nulle</li>
<li>DO_NOTHING</li>
<li></li>
</ul></li>
</ul>
</section><section id="webuimodels.py-4" class="slide level2">
<h2>webui/models.py</h2>
<div class="sourceCode" id="cb17"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb17-1" title="1">SENS <span class="op">=</span> ((<span class="st">&quot;i&quot;</span>, <span class="st">&quot;Input&quot;</span>), (<span class="st">&quot;f&quot;</span>, <span class="st">&quot;Forward&quot;</span>), (<span class="st">&quot;o&quot;</span>, <span class="st">&quot;Output&quot;</span>))</a>
<a class="sourceLine" id="cb17-2" title="2"></a>
<a class="sourceLine" id="cb17-3" title="3"><span class="kw">class</span> Regle(models.Model):</a>
<a class="sourceLine" id="cb17-4" title="4"> priorite <span class="op">=</span> models.PositiveIntegerField(default<span class="op">=</span><span class="dv">0</span>)</a>
<a class="sourceLine" id="cb17-5" title="5"> groupe <span class="op">=</span> models.ForeignKey(Groupe, blank<span class="op">=</span><span class="va">True</span>, null<span class="op">=</span><span class="va">True</span>,</a>
<a class="sourceLine" id="cb17-6" title="6"> on_delete<span class="op">=</span>models.CASCADE)</a>
<a class="sourceLine" id="cb17-7" title="7"> services <span class="op">=</span> models.ManyToManyField(Service)</a>
<a class="sourceLine" id="cb17-8" title="8"> interface <span class="op">=</span> models.CharField(default<span class="op">=</span><span class="st">&quot;&quot;</span>, max_length<span class="op">=</span><span class="dv">25</span>,</a>
<a class="sourceLine" id="cb17-9" title="9"> blank<span class="op">=</span><span class="va">True</span>)</a>
<a class="sourceLine" id="cb17-10" title="10"> actif <span class="op">=</span> models.BooleanField(default<span class="op">=</span><span class="va">True</span>)</a>
<a class="sourceLine" id="cb17-11" title="11"> sens <span class="op">=</span> models.CharField(max_length<span class="op">=</span><span class="dv">1</span>, choices<span class="op">=</span>SENS,</a>
<a class="sourceLine" id="cb17-12" title="12"> default<span class="op">=</span><span class="st">&quot;i&quot;</span>)</a>
<a class="sourceLine" id="cb17-13" title="13"> parefeu <span class="op">=</span> models.ForeignKey(Parefeu, on_delete<span class="op">=</span>models.CASCADE)</a></code></pre></div>
</section></section>
<section><section id="parefeu" class="title-slide slide level1"><h1>Parefeu</h1></section><section id="à-faire" class="slide level2">
</section><section id="à-faire-3" class="slide level2">
<h2>À faire</h2>
<h3 id="webuimodels.py-5">webui/models.py</h3>
<h3 id="administration-4">Administration</h3>
<ul>
<li>nom</li>
<li>modification: date/heure de dernière modification</li>
<li>modele: héritage d’un Parefeu</li>
<li>admin: administrateur du parefeu (User)</li>
</ul>
</section><section id="webuimodels.py-6" class="slide level2">
<h2>webui/models.py</h2>
<div class="sourceCode" id="cb18"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb18-1" title="1"><span class="im">from</span> django.contrib.auth.models <span class="im">import</span> User</a>
<a class="sourceLine" id="cb18-2" title="2"><span class="kw">class</span> Parefeu(AvecNom):</a>
<a class="sourceLine" id="cb18-3" title="3"> <span class="co"># mise à jour auto à chaque save()</span></a>
<a class="sourceLine" id="cb18-4" title="4"> modification <span class="op">=</span> models.DateTimeField(auto_now<span class="op">=</span><span class="va">True</span>)</a>
<a class="sourceLine" id="cb18-5" title="5"> modele <span class="op">=</span> models.ForeignKey(<span class="st">&#39;Parefeu&#39;</span>, null<span class="op">=</span><span class="va">True</span>, blank<span class="op">=</span><span class="va">True</span>,</a>
<a class="sourceLine" id="cb18-6" title="6"> on_delete<span class="op">=</span>models.CASCADE)</a>
<a class="sourceLine" id="cb18-7" title="7"> admin <span class="op">=</span> models.ForeignKey(User, on_delete<span class="op">=</span>models.CASCADE)</a>
<a class="sourceLine" id="cb18-8" title="8"></a>
<a class="sourceLine" id="cb18-9" title="9"> <span class="kw">class</span> Meta(<span class="bu">object</span>):</a>
<a class="sourceLine" id="cb18-10" title="10"> verbose_name_plural <span class="op">=</span> <span class="st">&quot;parefeux&quot;</span></a></code></pre></div>
</section><section id="webuimodels.py-7" class="slide level2">
<h2>webui/models.py</h2>
<div class="sourceCode" id="cb19"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb19-1" title="1"> <span class="kw">def</span> nombre_de_regles(<span class="va">self</span>):</a>
<a class="sourceLine" id="cb19-2" title="2"> nb <span class="op">=</span> <span class="va">self</span>.regle_set.count()</a>
<a class="sourceLine" id="cb19-3" title="3"> <span class="cf">if</span> <span class="va">self</span>.modele:</a>
<a class="sourceLine" id="cb19-4" title="4"> nb <span class="op">+=</span> <span class="va">self</span>.modele.nombre_de_regles()</a>
<a class="sourceLine" id="cb19-5" title="5"> <span class="cf">return</span> nb</a></code></pre></div>
</section><section id="à-faire-1" class="slide level2">
<h2>À faire</h2>
<h3 id="webuiadmin.py-4">webui/admin.py</h3>
<li>ajouter un groupe <strong>Serveurs Web</strong> contenant
<ul>
<li>interface d’administration</li>
<li>colonnes:
<ul>
<li>nom</li>
<li>modele</li>
<li>nombre_de_regles (modele + parefeu)</li>
<li>modification</li>
<li>dsi.insa-rouen.fr</li>
<li>www.insa-rouen.fr</li>
</ul></li>
</ul>
</section><section id="webuiadmin.py-5" class="slide level2">
<h2>webui/admin.py</h2>
<div class="sourceCode" id="cb20"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb20-1" title="1"><span class="kw">class</span> ParefeuAdmin(admin.ModelAdmin):</a>
<a class="sourceLine" id="cb20-2" title="2"> list_display <span class="op">=</span> (<span class="st">&quot;nom&quot;</span>, <span class="st">&quot;modele&quot;</span>,</a>
<a class="sourceLine" id="cb20-3" title="3"> <span class="st">&quot;nombre_de_regles&quot;</span>, <span class="st">&quot;modification&quot;</span>)</a>
<a class="sourceLine" id="cb20-4" title="4"> date_hierarchy <span class="op">=</span> <span class="st">&#39;modification&#39;</span></a></code></pre></div>
</section></section>
<section><section id="fin" class="title-slide slide level1"><h1>FIN !</h1></section></section>
<section class="footnotes">
<hr />
<ol>
<li id="fn1"><p>https://docs.djangoproject.com/fr/2.2/ref/models/fields/<a href="#/fnref1" class="footnote-back"></a></p></li>
</ol>
</section>
<section><section id="fin" class="title-slide slide level1"><h1>Fin !</h1></section></section>
</div>
</div>
......
......@@ -4,17 +4,24 @@ author:
title: Django par la pratique 2/7
---
# Départ
## Support de cours
Attention au remplacement de fwm !
## Fichiers
- support: cours2.html
### L'ancien
~~~
$ git pull
$ git checkout cours_2
$ cd fwm1
$ pipenv --rm
~~~
### Le nouveau
~~~
$ cd fwm2
$ pipenv install
~~~
## Au programme
- l'interface d'administration
- les modèles
- les modèles avec relations (Service, Groupe)
# Administration
## Serveur de développement
......@@ -27,7 +34,8 @@ $ pipenv run ./manage.py runserver
=> http://127.0.0.1:8000
## webui/admin.py
## À modifier
### webui/admin.py
~~~python
from django.contrib import admin
from .models import Port
......@@ -90,7 +98,7 @@ Créer les ports suivants:
- 389/tcp