Commit 51fd4f29 authored by Bonnegent Sebastien's avatar Bonnegent Sebastien
Browse files

cours 5 ok

parent e630d244
all: cours1.html cours2.html cours3.html cours4.html
all: cours1.html cours2.html cours3.html cours4.html cours5.html
%.html: %.md
pandoc -s -t revealjs -V revealjs-url=./reveal.js -V theme=beige -o $@ $<
......@@ -31,14 +31,10 @@ séance.
- formulaire simple (contact)
## Cours 5
- frontend: manipulation des objets
- script des gestions
- logging
- messages
- template générique
- formulaire
- envoi de mails
- hijack et restrictions d'accès
- créer
- modifier
- les menus
- supprimer
## Cours 6
- utilisation de black
......
......@@ -247,43 +247,43 @@ $ pipenv install</code></pre>
<a class="sourceLine" id="cb13-4" title="4"> context_object_name <span class="op">=</span> <span class="st">&#39;parefeu&#39;</span></a>
<a class="sourceLine" id="cb13-5" title="5"> model <span class="op">=</span> Parefeu</a>
<a class="sourceLine" id="cb13-6" title="6"> <span class="co"># queryset = Parefeu.objects.all()</span></a></code></pre></div>
</section><section id="routage-1" class="slide level2">
<h2>Routage</h2>
<h3 id="confurls.py-3">conf/urls.py</h3>
<div class="sourceCode" id="cb14"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb14-1" title="1">path(<span class="st">&#39;parefeu/&lt;int:pk&gt;/&#39;</span>,</a>
<a class="sourceLine" id="cb14-2" title="2"> login_required(views.ParefeuDetail.as_view()),</a>
<a class="sourceLine" id="cb14-3" title="3"> name<span class="op">=</span><span class="st">&#39;parefeu-detail&#39;</span>),</a></code></pre></div>
</section><section id="templates-2" class="slide level2">
<h2>Templates</h2>
<h3 id="webuitemplateswebuiparefeu_detail.html">webui/templates/webui/parefeu_detail.html</h3>
<div class="sourceCode" id="cb15"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb15-1" title="1">{<span class="op">%</span> extends <span class="st">&quot;webui/home.html&quot;</span> <span class="op">%</span>}</a>
<a class="sourceLine" id="cb15-2" title="2">{<span class="op">%</span> block title <span class="op">%</span>}Parefeu: {{parefeu.nom}}{<span class="op">%</span> endblock <span class="op">%</span>}</a>
<a class="sourceLine" id="cb15-3" title="3">{<span class="op">%</span> block content <span class="op">%</span>}</a>
<a class="sourceLine" id="cb15-4" title="4"><span class="op">&lt;</span>h2<span class="op">&gt;</span>{{parefeu.nom}}<span class="op">&lt;/</span>h2<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb15-5" title="5"><span class="op">&lt;</span>table <span class="kw">class</span><span class="op">=</span><span class="st">&quot;table table-striped&quot;</span><span class="op">&gt;</span></a>
<a class="sourceLine" id="cb15-6" title="6"> <span class="op">&lt;</span>tr<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb15-7" title="7"> <span class="op">&lt;</span>th<span class="op">&gt;</span>Nombre de règles total<span class="op">&lt;/</span>th<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb15-8" title="8"> <span class="op">&lt;</span>td<span class="op">&gt;</span>{{parefeu.nombre_de_regles}}<span class="op">&lt;/</span>td<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb15-9" title="9"> <span class="op">&lt;/</span>tr<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb15-10" title="10"> <span class="op">&lt;</span>tr<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb15-11" title="11"> <span class="op">&lt;</span>th<span class="op">&gt;</span>Administrateur<span class="op">&lt;/</span>th<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb15-12" title="12"> <span class="op">&lt;</span>td<span class="op">&gt;</span>{{parefeu.admin}}<span class="op">&lt;/</span>td<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb15-13" title="13"> <span class="op">&lt;/</span>tr<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb15-14" title="14"> <span class="op">&lt;</span>tr<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb15-15" title="15"> <span class="op">&lt;</span>th<span class="op">&gt;</span>Dernière modification<span class="op">&lt;/</span>th<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb15-16" title="16"> <span class="op">&lt;</span>td<span class="op">&gt;</span>{{parefeu.modification}}<span class="op">&lt;/</span>td<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb15-17" title="17"> <span class="op">&lt;/</span>tr<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb15-18" title="18"> {<span class="op">%</span> <span class="cf">if</span> parefeu.parefeu_set.count <span class="op">%</span>}</a>
<a class="sourceLine" id="cb15-19" title="19"> <span class="op">&lt;</span>tr<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb15-20" title="20"> <span class="op">&lt;</span>th<span class="op">&gt;</span>Parefeu(x) utilisant ce parefeu comme modèle<span class="op">&lt;/</span>th<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb15-21" title="21"> <span class="op">&lt;</span>td<span class="op">&gt;</span>{<span class="op">%</span> <span class="cf">for</span> fw <span class="kw">in</span> parefeu.parefeu_set.<span class="bu">all</span> <span class="op">%</span>}</a>
<a class="sourceLine" id="cb15-22" title="22"> <span class="op">&lt;</span>a href<span class="op">=</span><span class="st">&quot;{</span><span class="sc">% u</span><span class="st">rl &#39;parefeu&#39; fw.id %}&quot;</span><span class="op">&gt;</span>{{fw}}<span class="op">&lt;/</span>a<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb15-23" title="23"> {<span class="op">%</span> endfor <span class="op">%</span>}</a>
<a class="sourceLine" id="cb15-24" title="24"> <span class="op">&lt;/</span>td<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb15-25" title="25"> <span class="op">&lt;/</span>tr<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb15-26" title="26"> {<span class="op">%</span> endif <span class="op">%</span>}</a>
<a class="sourceLine" id="cb15-27" title="27"><span class="op">&lt;/</span>table<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb15-28" title="28">{<span class="op">%</span> endblock <span class="op">%</span>}</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">{<span class="op">%</span> extends <span class="st">&quot;webui/home.html&quot;</span> <span class="op">%</span>}</a>
<a class="sourceLine" id="cb14-2" title="2">{<span class="op">%</span> block title <span class="op">%</span>}Parefeu: {{parefeu.nom}}{<span class="op">%</span> endblock <span class="op">%</span>}</a>
<a class="sourceLine" id="cb14-3" title="3">{<span class="op">%</span> block content <span class="op">%</span>}</a>
<a class="sourceLine" id="cb14-4" title="4"><span class="op">&lt;</span>h2<span class="op">&gt;</span>{{parefeu.nom}}<span class="op">&lt;/</span>h2<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb14-5" title="5"><span class="op">&lt;</span>table <span class="kw">class</span><span class="op">=</span><span class="st">&quot;table table-striped&quot;</span><span class="op">&gt;</span></a>
<a class="sourceLine" id="cb14-6" title="6"> <span class="op">&lt;</span>tr<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb14-7" title="7"> <span class="op">&lt;</span>th<span class="op">&gt;</span>Nombre de règles total<span class="op">&lt;/</span>th<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb14-8" title="8"> <span class="op">&lt;</span>td<span class="op">&gt;</span>{{parefeu.nombre_de_regles}}<span class="op">&lt;/</span>td<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb14-9" title="9"> <span class="op">&lt;/</span>tr<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb14-10" title="10"> <span class="op">&lt;</span>tr<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb14-11" title="11"> <span class="op">&lt;</span>th<span class="op">&gt;</span>Administrateur<span class="op">&lt;/</span>th<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb14-12" title="12"> <span class="op">&lt;</span>td<span class="op">&gt;</span>{{parefeu.admin}}<span class="op">&lt;/</span>td<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb14-13" title="13"> <span class="op">&lt;/</span>tr<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb14-14" title="14"> <span class="op">&lt;</span>tr<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb14-15" title="15"> <span class="op">&lt;</span>th<span class="op">&gt;</span>Dernière modification<span class="op">&lt;/</span>th<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb14-16" title="16"> <span class="op">&lt;</span>td<span class="op">&gt;</span>{{parefeu.modification}}<span class="op">&lt;/</span>td<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb14-17" title="17"> <span class="op">&lt;/</span>tr<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb14-18" title="18"> {<span class="op">%</span> <span class="cf">if</span> parefeu.parefeu_set.count <span class="op">%</span>}</a>
<a class="sourceLine" id="cb14-19" title="19"> <span class="op">&lt;</span>tr<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb14-20" title="20"> <span class="op">&lt;</span>th<span class="op">&gt;</span>Parefeu(x) utilisant ce parefeu comme modèle<span class="op">&lt;/</span>th<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb14-21" title="21"> <span class="op">&lt;</span>td<span class="op">&gt;</span>{<span class="op">%</span> <span class="cf">for</span> fw <span class="kw">in</span> parefeu.parefeu_set.<span class="bu">all</span> <span class="op">%</span>}</a>
<a class="sourceLine" id="cb14-22" title="22"> <span class="op">&lt;</span>a href<span class="op">=</span><span class="st">&quot;{</span><span class="sc">% u</span><span class="st">rl &#39;parefeu&#39; fw.id %}&quot;</span><span class="op">&gt;</span>{{fw}}<span class="op">&lt;/</span>a<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb14-23" title="23"> {<span class="op">%</span> endfor <span class="op">%</span>}</a>
<a class="sourceLine" id="cb14-24" title="24"> <span class="op">&lt;/</span>td<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb14-25" title="25"> <span class="op">&lt;/</span>tr<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb14-26" title="26"> {<span class="op">%</span> endif <span class="op">%</span>}</a>
<a class="sourceLine" id="cb14-27" title="27"><span class="op">&lt;/</span>table<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb14-28" title="28">{<span class="op">%</span> endblock <span class="op">%</span>}</a></code></pre></div>
</section><section id="routage-1" class="slide level2">
<h2>Routage</h2>
<h3 id="confurls.py-3">conf/urls.py</h3>
<div class="sourceCode" id="cb15"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb15-1" title="1">path(<span class="st">&#39;parefeu/&lt;int:pk&gt;/&#39;</span>,</a>
<a class="sourceLine" id="cb15-2" title="2"> login_required(views.ParefeuDetail.as_view()),</a>
<a class="sourceLine" id="cb15-3" title="3"> name<span class="op">=</span><span class="st">&#39;parefeu-detail&#39;</span>),</a></code></pre></div>
</section><section id="accés-facile" class="slide level2">
<h2>Accés facile</h2>
<h3 id="webuitemplateswebuiparefeu_list.html-1">webui/templates/webui/parefeu_list.html</h3>
......
......@@ -170,14 +170,6 @@ class ParefeuDetail(DetailView):
# queryset = Parefeu.objects.all()
~~~
## Routage
### conf/urls.py
~~~python
path('parefeu/<int:pk>/',
login_required(views.ParefeuDetail.as_view()),
name='parefeu-detail'),
~~~
## Templates
### webui/templates/webui/parefeu_detail.html
~~~python
......@@ -211,6 +203,14 @@ path('parefeu/<int:pk>/',
{% endblock %}
~~~
## Routage
### conf/urls.py
~~~python
path('parefeu/<int:pk>/',
login_required(views.ParefeuDetail.as_view()),
name='parefeu-detail'),
~~~
## Accés facile
### webui/templates/webui/parefeu_list.html
~~~python
......
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="generator" content="pandoc">
<meta name="author" content="Sébastien Bonnegent">
<title>Django par la pratique 5/7</title>
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui">
<link rel="stylesheet" href="./reveal.js/css/reveal.css">
<style type="text/css">
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
span.underline{text-decoration: underline;}
div.column{display: inline-block; vertical-align: top; width: 50%;}
</style>
<style type="text/css">
a.sourceLine { display: inline-block; line-height: 1.25; }
a.sourceLine { pointer-events: none; color: inherit; text-decoration: inherit; }
a.sourceLine:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode { white-space: pre; position: relative; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
code.sourceCode { white-space: pre-wrap; }
a.sourceLine { text-indent: -1em; padding-left: 1em; }
}
pre.numberSource a.sourceLine
{ position: relative; left: -4em; }
pre.numberSource a.sourceLine::before
{ content: attr(title);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; pointer-events: all; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
a.sourceLine::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>
<link rel="stylesheet" href="./reveal.js/css/theme/beige.css" id="theme">
<!-- Printing and PDF exports -->
<script>
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match( /print-pdf/gi ) ? './reveal.js/css/print/pdf.css' : './reveal.js/css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
<!--[if lt IE 9]>
<script src="./reveal.js/lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<div class="slides">
<section id="title-slide">
<h1 class="title">Django par la pratique 5/7</h1>
<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" class="slide level2">
<h2>Support</h2>
<ul>
<li>cours5.html</li>
</ul>
</section><section id="préparation" class="slide level2">
<h2>Préparation</h2>
<h3 id="lancien">L’ancien</h3>
<pre><code>$ git pull
$ cd fwm4
$ pipenv --rm</code></pre>
<h3 id="le-nouveau">Le nouveau</h3>
<pre><code>$ cd fwm5
$ pipenv install</code></pre>
</section><section id="au-programme" class="slide level2">
<h2>Au programme</h2>
<ul>
<li>créer</li>
<li>modifier</li>
<li>les menus</li>
<li>supprimer</li>
</ul>
</section></section>
<section><section id="créer" class="title-slide slide level1"><h1>Créer</h1></section><section id="parefeu" class="slide level2">
<h2>Parefeu</h2>
<h3 id="webuiviews.py">webui/views.py</h3>
<div class="sourceCode" id="cb3"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb3-1" title="1"><span class="kw">class</span> ParefeuCreate(CreateView):</a>
<a class="sourceLine" id="cb3-2" title="2"> model <span class="op">=</span> Parefeu</a>
<a class="sourceLine" id="cb3-3" title="3"> fields <span class="op">=</span> [<span class="st">&#39;nom&#39;</span>, <span class="st">&#39;modele&#39;</span>]</a>
<a class="sourceLine" id="cb3-4" title="4"> template_name <span class="op">=</span> <span class="st">&quot;webui/object_create.html&quot;</span></a>
<a class="sourceLine" id="cb3-5" title="5"></a>
<a class="sourceLine" id="cb3-6" title="6"> <span class="kw">def</span> get_context_data(<span class="va">self</span>, <span class="op">**</span>kwargs):</a>
<a class="sourceLine" id="cb3-7" title="7"> context <span class="op">=</span> <span class="bu">super</span>().get_context_data(<span class="op">**</span>kwargs)</a>
<a class="sourceLine" id="cb3-8" title="8"> context[<span class="st">&#39;menu_parefeu&#39;</span>] <span class="op">=</span> <span class="st">&#39;active&#39;</span></a>
<a class="sourceLine" id="cb3-9" title="9"> <span class="cf">return</span> context</a>
<a class="sourceLine" id="cb3-10" title="10"></a>
<a class="sourceLine" id="cb3-11" title="11"> <span class="kw">def</span> form_valid(<span class="va">self</span>, form):</a>
<a class="sourceLine" id="cb3-12" title="12"> form.instance.admin <span class="op">=</span> <span class="va">self</span>.request.user</a>
<a class="sourceLine" id="cb3-13" title="13"> <span class="cf">return</span> <span class="bu">super</span>(ParefeuCreate, <span class="va">self</span>).form_valid(form)</a></code></pre></div>
</section><section id="parefeu-1" class="slide level2">
<h2>Parefeu</h2>
<h3 id="webuitemplateswebuiobject_create.html">webui/templates/webui/object_create.html</h3>
<div class="sourceCode" id="cb4"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb4-1" title="1">{<span class="op">%</span> extends <span class="st">&quot;webui/home.html&quot;</span> <span class="op">%</span>}</a>
<a class="sourceLine" id="cb4-2" title="2">{<span class="op">%</span> block content <span class="op">%</span>}</a>
<a class="sourceLine" id="cb4-3" title="3"><span class="op">&lt;</span>h1<span class="op">&gt;</span>Création<span class="op">&lt;/</span>h1<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb4-4" title="4"><span class="op">&lt;</span>form enctype<span class="op">=</span><span class="st">&quot;multipart/form-data&quot;</span> method<span class="op">=</span><span class="st">&quot;post&quot;</span><span class="op">&gt;</span></a>
<a class="sourceLine" id="cb4-5" title="5"> {<span class="op">%</span> csrf_token <span class="op">%</span>}</a>
<a class="sourceLine" id="cb4-6" title="6"> <span class="op">&lt;</span>table <span class="kw">class</span><span class="op">=</span><span class="st">&quot;table table-striped&quot;</span><span class="op">&gt;</span></a>
<a class="sourceLine" id="cb4-7" title="7"> {{form.as_table}}</a>
<a class="sourceLine" id="cb4-8" title="8"> <span class="op">&lt;</span>tr<span class="op">&gt;&lt;</span>td colspan<span class="op">=</span><span class="dv">2</span><span class="op">&gt;</span></a>
<a class="sourceLine" id="cb4-9" title="9"> <span class="op">&lt;</span>button <span class="kw">class</span><span class="op">=</span><span class="st">&quot;btn btn-primary btn-block&quot;</span></a>
<a class="sourceLine" id="cb4-10" title="10"> <span class="bu">type</span><span class="op">=</span><span class="st">&quot;submit&quot;</span><span class="op">/&gt;</span>Enregistrer<span class="op">&lt;/</span>button<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb4-11" title="11"> <span class="op">&lt;/</span>td<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb4-12" title="12"> <span class="op">&lt;/</span>tr<span class="op">&gt;&lt;/</span>table<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb4-13" title="13"><span class="op">&lt;/</span>form<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb4-14" title="14">{<span class="op">%</span> endblock <span class="op">%</span>}</a></code></pre></div>
</section><section id="parefeu-2" class="slide level2">
<h2>Parefeu</h2>
<h3 id="webuimodels.py">webui/models.py</h3>
<div class="sourceCode" id="cb5"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb5-1" title="1"><span class="im">from</span> django.urls <span class="im">import</span> reverse</a>
<a class="sourceLine" id="cb5-2" title="2"></a>
<a class="sourceLine" id="cb5-3" title="3"><span class="kw">class</span> Parefeu(AvecNom):</a>
<a class="sourceLine" id="cb5-4" title="4"> <span class="co"># ...</span></a>
<a class="sourceLine" id="cb5-5" title="5"></a>
<a class="sourceLine" id="cb5-6" title="6"> <span class="kw">def</span> get_absolute_url(<span class="va">self</span>):</a>
<a class="sourceLine" id="cb5-7" title="7"> <span class="co"># return reverse(&#39;parefeu-detail&#39;,</span></a>
<a class="sourceLine" id="cb5-8" title="8"> <span class="co"># kwargs={&#39;pk&#39;: self.pk})</span></a>
<a class="sourceLine" id="cb5-9" title="9"> <span class="cf">return</span> reverse(<span class="st">&#39;parefeux&#39;</span>)</a></code></pre></div>
</section><section id="parefeu-3" class="slide level2">
<h2>Parefeu</h2>
<h3 id="confurls.py">conf/urls.py</h3>
<div class="sourceCode" id="cb6"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb6-1" title="1">path(<span class="st">&#39;parefeu/add/&#39;</span>, </a>
<a class="sourceLine" id="cb6-2" title="2"> login_required(views.ParefeuCreate.as_view()),</a>
<a class="sourceLine" id="cb6-3" title="3"> name<span class="op">=</span><span class="st">&#39;parefeu-create&#39;</span>),</a></code></pre></div>
</section><section id="parefeu-4" class="slide level2">
<h2>Parefeu</h2>
<h3 id="webuitemplateswebuiparefeu_list.html">webui/templates/webui/parefeu_list.html</h3>
<div class="sourceCode" id="cb7"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb7-1" title="1"><span class="op">&lt;</span>a href<span class="op">=</span><span class="st">&quot;{</span><span class="sc">% u</span><span class="st">rl &#39;parefeu-create&#39; %}&quot;</span></a>
<a class="sourceLine" id="cb7-2" title="2"> <span class="kw">class</span><span class="op">=</span><span class="st">&quot;btn btn-success&quot;</span> role<span class="op">=</span><span class="st">&quot;button&quot;</span><span class="op">&gt;</span>Ajouter<span class="op">&lt;/</span>a<span class="op">&gt;</span></a></code></pre></div>
</section><section id="parefeu-5" class="slide level2">
<h2>Parefeu</h2>
<h3 id="à-faire">À faire</h3>
<ul>
<li>ajouter un parefeu avec modèle <strong>Serveur Web</strong></li>
<li>afficher le modèle dans <strong>parefeu_detail.html</strong></li>
</ul>
</section></section>
<section><section id="modifier" class="title-slide slide level1"><h1>Modifier</h1></section><section id="parefeu-6" class="slide level2">
<h2>Parefeu</h2>
<h3 id="webuiviews.py-1">webui/views.py</h3>
<div class="sourceCode" id="cb8"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb8-1" title="1"><span class="im">from</span> django.views.generic.edit <span class="im">import</span> UpdateView</a>
<a class="sourceLine" id="cb8-2" title="2"><span class="kw">class</span> ParefeuUpdate(UpdateView):</a>
<a class="sourceLine" id="cb8-3" title="3"> model <span class="op">=</span> Parefeu</a>
<a class="sourceLine" id="cb8-4" title="4"> fields <span class="op">=</span> [<span class="st">&#39;nom&#39;</span>, <span class="st">&#39;modele&#39;</span>]</a>
<a class="sourceLine" id="cb8-5" title="5"> template_name <span class="op">=</span> <span class="st">&quot;webui/object_update.html&quot;</span></a></code></pre></div>
</section><section id="parefeu-7" class="slide level2">
<h2>Parefeu</h2>
<h3 id="webuitemplateswebuiobject_update.html">webui/templates/webui/object_update.html</h3>
<pre><code>{% extends &quot;webui/home.html&quot; %}
{% block content %}
&lt;h1&gt;Modification&lt;/h1&gt;
&lt;form enctype=&quot;multipart/form-data&quot; method=&quot;post&quot;&gt;
{% csrf_token %}
&lt;table class=&quot;table table-striped&quot;&gt;
{{form.as_table}}
&lt;tr&gt;&lt;td colspan=2&gt;
&lt;button class=&quot;btn btn-primary btn-block&quot;
type=&quot;submit&quot;/&gt;Modifier&lt;/button&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;
&lt;/form&gt;
{% endblock %}</code></pre>
</section><section id="parefeu-8" class="slide level2">
<h2>Parefeu</h2>
<h3 id="confurls.py-1">conf/urls.py</h3>
<div class="sourceCode" id="cb10"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb10-1" title="1">path(<span class="st">&#39;parefeu/&lt;int:pk&gt;/update/&#39;</span>,</a>
<a class="sourceLine" id="cb10-2" title="2"> login_required(views.ParefeuUpdate.as_view()),</a>
<a class="sourceLine" id="cb10-3" title="3"> name<span class="op">=</span><span class="st">&#39;parefeu-update&#39;</span>),</a></code></pre></div>
</section><section id="parefeu-9" class="slide level2">
<h2>Parefeu</h2>
<h3 id="à-faire-1">À faire</h3>
<ul>
<li>ajouter un bouton de suppression sur la page de détail d’un parefeu</li>
</ul>
</section></section>
<section><section id="menu" class="title-slide slide level1"><h1>Menu</h1></section><section id="factorisation" class="slide level2">
<h2>Factorisation</h2>
<h3 id="webuiviews.py-2">webui/views.py</h3>
<div class="sourceCode" id="cb11"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb11-1" title="1"><span class="kw">class</span> ParefeuMenu(<span class="bu">object</span>):</a>
<a class="sourceLine" id="cb11-2" title="2"> <span class="kw">def</span> get_context_data(<span class="va">self</span>, <span class="op">**</span>kwargs):</a>
<a class="sourceLine" id="cb11-3" title="3"> context <span class="op">=</span> <span class="bu">super</span>().get_context_data(<span class="op">**</span>kwargs)</a>
<a class="sourceLine" id="cb11-4" title="4"> context[<span class="st">&#39;menu_parefeu&#39;</span>] <span class="op">=</span> <span class="st">&#39;active&#39;</span></a>
<a class="sourceLine" id="cb11-5" title="5"> <span class="cf">return</span> context</a></code></pre></div>
</section><section id="factorisation-1" class="slide level2">
<h2>Factorisation</h2>
<h3 id="webuiviews.py-3">webui/views.py</h3>
<div class="sourceCode" id="cb12"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb12-1" title="1"><span class="kw">class</span> ParefeuCreate(ParefeuMenu, CreateView):</a>
<a class="sourceLine" id="cb12-2" title="2"><span class="co"># supprimer get_context_data()</span></a>
<a class="sourceLine" id="cb12-3" title="3"></a>
<a class="sourceLine" id="cb12-4" title="4"><span class="kw">class</span> ParefeuUpdate(ParefeuMenu, UpdateView):</a>
<a class="sourceLine" id="cb12-5" title="5"><span class="co"># supprimer get_context_data()</span></a>
<a class="sourceLine" id="cb12-6" title="6"></a>
<a class="sourceLine" id="cb12-7" title="7"><span class="kw">class</span> ParefeuDetail(ParefeuMenu, DetailView):</a>
<a class="sourceLine" id="cb12-8" title="8"><span class="co"># supprimer get_context_data()</span></a>
<a class="sourceLine" id="cb12-9" title="9"></a>
<a class="sourceLine" id="cb12-10" title="10"><span class="kw">class</span> ParefeuList(ParefeuMenu, ListView):</a>
<a class="sourceLine" id="cb12-11" title="11"><span class="co"># supprimer get_context_data()</span></a></code></pre></div>
</section></section>
<section><section id="supprimer" class="title-slide slide level1"><h1>Supprimer</h1></section></section>
<section><section id="fin" class="title-slide slide level1"><h1>FIN !</h1></section></section>
</div>
</div>
<script src="./reveal.js/lib/js/head.min.js"></script>
<script src="./reveal.js/js/reveal.js"></script>
<script>
// Full list of configuration options available at:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
// Push each slide change to the browser history
history: true,
// Optional reveal.js plugins
dependencies: [
{ src: './reveal.js/lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: './reveal.js/plugin/zoom-js/zoom.js', async: true },
{ src: './reveal.js/plugin/notes/notes.js', async: true }
]
});
</script>
</body>
</html>
---
author:
- Sébastien Bonnegent
title: Django par la pratique 5/7
---
# Départ
## Support
- cours5.html
## Préparation
### L'ancien
~~~
$ git pull
$ cd fwm4
$ pipenv --rm
~~~
### Le nouveau
~~~
$ cd fwm5
$ pipenv install
~~~
## Au programme
- créer
- modifier
- les menus
- supprimer
- debugger
# Créer
## Parefeu
### webui/views.py
~~~python
class ParefeuCreate(CreateView):
model = Parefeu
fields = ['nom', 'modele']
template_name = "webui/object_create.html"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['menu_parefeu'] = 'active'
return context
def form_valid(self, form):
form.instance.admin = self.request.user
return super(ParefeuCreate, self).form_valid(form)
~~~
## Parefeu
### webui/templates/webui/object_create.html
~~~python
{% extends "webui/home.html" %}
{% block content %}
<h1>Création</h1>
<form enctype="multipart/form-data" method="post">
{% csrf_token %}
<table class="table table-striped">
{{form.as_table}}
<tr><td colspan=2>
<button class="btn btn-primary btn-block"
type="submit"/>Enregistrer</button>
</td>
</tr></table>
</form>
{% endblock %}
~~~
## Parefeu
### webui/models.py
~~~python
from django.urls import reverse
class Parefeu(AvecNom):
# ...
def get_absolute_url(self):
# return reverse('parefeu-detail',
# kwargs={'pk': self.pk})
return reverse('parefeux')
~~~
## Parefeu
### conf/urls.py
~~~python
path('parefeu/add/',
login_required(views.ParefeuCreate.as_view()),
name='parefeu-create'),
~~~
## Parefeu
### webui/templates/webui/parefeu_list.html
~~~python
<a href="{% url 'parefeu-create' %}"
class="btn btn-success" role="button">Ajouter</a>
~~~
## Parefeu
### À faire
* ajouter un parefeu avec modèle **Serveur Web**
* afficher le modèle dans **parefeu_detail.html**
# Modifier
## Parefeu
### webui/views.py
~~~python
from django.views.generic.edit import UpdateView
class ParefeuUpdate(UpdateView):
model = Parefeu
fields = ['nom', 'modele']
template_name = "webui/object_update.html"
~~~
## Parefeu
### webui/templates/webui/object_update.html
~~~
{% extends "webui/home.html" %}
{% block content %}
<h1>Modification</h1>
<form enctype="multipart/form-data" method="post">
{% csrf_token %}
<table class="table table-striped">
{{form.as_table}}
<tr><td colspan=2>
<button class="btn btn-primary btn-block"
type="submit"/>Modifier</button>
</td></tr>
</table>
</form>
{% endblock %}
~~~
## Parefeu
### conf/urls.py
~~~python
path('parefeu/<int:pk>/update/',
login_required(views.ParefeuUpdate.as_view()),
name='parefeu-update'),
~~~
## Parefeu
### À faire
* ajouter un bouton de modification sur la page de détail d'un parefeu
# Menu
## Factorisation
### webui/views.py
~~~python
class ParefeuMenu(object):
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['menu_parefeu'] = 'active'
return context
~~~
## Factorisation
### webui/views.py
~~~python
class ParefeuCreate(ParefeuMenu, CreateView):
# supprimer get_context_data()