cours1.html 31.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<!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 1/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 1/7</h1>
  <p class="author">Sébastien Bonnegent</p>
</section>

<section><section id="django" class="title-slide slide level1"><h1>Django</h1></section><section id="quest-ce-que-cest" class="slide level2">
<h2>Qu’est ce que c’est ?</h2>
<blockquote>
<p>The web framework for perfectionists with deadlines.<a href="#/fn1" class="footnote-ref" id="fnref1"><sup>1</sup></a></p>
</blockquote>
<ul>
<li>framework web python de haut niveau</li>
<li>rapide à mettre en place</li>
<li>flexible</li>
</ul>
</section><section id="avantages" class="slide level2">
<h2>Avantages</h2>
<ul>
<li>factorisation du code</li>
<li>interface d’administration automatique</li>
<li>architecture MVC (Model-Template-View)</li>
<li>ORM: postgresql, mysql, sqlite, oracle, …</li>
<li>serveur de développement intégré</li>
<li>gestion des migrations</li>
<li></li>
</ul>
</section><section id="inconvénients" class="slide level2">
<h2>Inconvénients</h2>
<ul>
<li>?</li>
</ul>
</section><section id="utiliser-par" class="slide level2">
<h2>Utiliser par</h2>
<ul>
<li>disqus</li>
<li>dropbox (storage)</li>
<li>instagram</li>
<li>mozilla</li>
<li>open stack</li>
<li>pinterest</li>
<li>spotify</li>
<li>youtube</li>
<li></li>
</ul>
</section></section>
<section><section id="prérequis" class="title-slide slide level1"><h1>Prérequis</h1></section><section id="lenvironnement-pipenvpipenv" class="slide level2">
<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
Bonnegent Sebastien's avatar
Bonnegent Sebastien committed
147
$ mkdir fwm1
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
$ cd !$
$ pipenv --python 3.6
$ pipenv install django django-extensions ipython</code></pre>
</section><section id="projet-ou-application" class="slide level2">
<h2>Projet ou application ?</h2>
<blockquote>
<p>Une application est une application Web qui fait quelque chose – par exemple un système de blog, une base de données publique ou une application de sondage. Un projet est un ensemble de réglages et d’applications pour un site Web particulier. Un projet peut contenir plusieurs applications. Une application peut apparaître dans plusieurs projets.<a href="#/fn3" class="footnote-ref" id="fnref3"><sup>3</sup></a></p>
</blockquote>
</section><section id="création-du-projet" class="slide level2">
<h2>Création du projet</h2>
<pre><code>$ pipenv run django-admin startproject conf .</code></pre>
<p><img data-src="images/tree-conf.png" /></p>
</section><section id="création-dune-application" class="slide level2">
<h2>Création d’une application</h2>
<pre><code>$ pipenv run django-admin startapp webui</code></pre>
<p><img data-src="images/tree-webui.png" /></p>
</section><section id="activation-de-lapplication" class="slide level2">
<h2>Activation de l’application</h2>
<h3 id="confsettings.py">conf/settings.py</h3>
<div class="sourceCode" id="cb4"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb4-1" title="1">INSTALLED_APPS <span class="op">=</span> [    </a>
<a class="sourceLine" id="cb4-2" title="2">    <span class="st">&#39;django.contrib.admin&#39;</span>,    </a>
<a class="sourceLine" id="cb4-3" title="3">    <span class="st">&#39;django.contrib.auth&#39;</span>,    </a>
<a class="sourceLine" id="cb4-4" title="4">    <span class="st">&#39;django.contrib.contenttypes&#39;</span>,    </a>
<a class="sourceLine" id="cb4-5" title="5">    <span class="st">&#39;django.contrib.sessions&#39;</span>,    </a>
<a class="sourceLine" id="cb4-6" title="6">    <span class="st">&#39;django.contrib.messages&#39;</span>,    </a>
<a class="sourceLine" id="cb4-7" title="7">    <span class="st">&#39;django.contrib.staticfiles&#39;</span>,    </a>
<a class="sourceLine" id="cb4-8" title="8">    <span class="st">&#39;webui&#39;</span>,</a>
<a class="sourceLine" id="cb4-9" title="9">    <span class="st">&#39;django_extensions&#39;</span>,</a>
<a class="sourceLine" id="cb4-10" title="10">]</a></code></pre></div>
</section><section id="configuration" class="slide level2">
<h2>Configuration</h2>
<h3 id="confsettings.py-1">conf/settings.py</h3>
<div class="sourceCode" id="cb5"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb5-1" title="1">DEBUG <span class="op">=</span> <span class="va">True</span></a>
<a class="sourceLine" id="cb5-2" title="2">ALLOWED_HOSTS <span class="op">=</span> []</a>
<a class="sourceLine" id="cb5-3" title="3">ROOT_URLCONF <span class="op">=</span> <span class="st">&#39;conf.urls&#39;</span></a>
<a class="sourceLine" id="cb5-4" title="4">LANGUAGE_CODE <span class="op">=</span> <span class="st">&#39;fr-fr&#39;</span></a>
<a class="sourceLine" id="cb5-5" title="5">TIME_ZONE <span class="op">=</span> <span class="st">&#39;Europe/Paris&#39;</span></a>
<a class="sourceLine" id="cb5-6" title="6">SECRET_KEY <span class="op">=</span> <span class="st">&#39;Une_clef_secrete_et_longue!&#39;</span></a></code></pre></div>
<h3 id="pour-générer-une-nouvelle-clé-secrête">Pour générer une nouvelle clé secrête</h3>
<pre><code>$ pipenv run ./manage.py generate_secret_key
f#wgrz$@#wh@*x5)_+(*$$^aw(c!q#a4)c0230bwp!iy</code></pre>
</section><section id="gestion-des-migrations" class="slide level2">
<h2>Gestion des migrations</h2>
<h3 id="création-si-nécessaire">Création (si nécessaire)</h3>
<pre><code>$ pipenv run ./manage.py makemigrations</code></pre>
<h3 id="application">Application</h3>
<pre><code>$ pipenv run ./manage.py migrate</code></pre>
<p><img data-src="images/tree-migrate-initial.png" /></p>
</section><section id="quelques-commandes-utiles" class="slide level2">
<h2>Quelques commandes utiles</h2>
<pre><code>$ pipenv run ./manage.py shell
$ # shell avec chargement automatique des classes
$ pipenv run ./manage.py shell_plus</code></pre>
<pre><code>$ pipenv run ./manage.py createsuperuser</code></pre>
<h3 id="serveur-de-développement">Serveur de développement</h3>
<pre><code>$ pipenv run ./manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).
September 28, 2017 - 13:29:48
Django version 1.11, using settings &#39;conf.settings&#39;
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.</code></pre>
</section></section>
<section><section id="notre-projet" class="title-slide slide level1"><h1>Notre projet</h1></section><section id="but" class="slide level2">
<h2>But</h2>
<p>Gestion des règles des parefeux à travers une interface web. On pourra créer des groupes de ports / services et gérer plusieurs parefeux différents.</p>
</section><section id="diagramme-des-classes" class="slide level2">
<h2>Diagramme des classes</h2>
<p><img data-src="images/diagramme_classe.png" /></p>
<p>Note: pas de clé primaire !</p>
</section></section>
<section><section id="à-vous-de-jouer" class="title-slide slide level1"><h1>À vous de jouer !</h1></section><section id="ports" class="slide level2">
<h2>Ports</h2>
<ul>
<li>protocole: any (défaut), tcp, udp, icmp</li>
<li>début: entier positif, 0 par défaut</li>
<li>fin: entier positif, 0 par défaut</li>
</ul>
</section><section id="webuimodels.py" class="slide level2">
<h2>webui/models.py</h2>
<div class="sourceCode" id="cb12"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb12-1" title="1">PROTO <span class="op">=</span> ((<span class="st">&#39;a&#39;</span>, <span class="st">&#39;any&#39;</span>), (<span class="st">&#39;t&#39;</span>, <span class="st">&#39;tcp&#39;</span>),</a>
<a class="sourceLine" id="cb12-2" title="2">         (<span class="st">&#39;u&#39;</span>, <span class="st">&#39;udp&#39;</span>), (<span class="st">&#39;i&#39;</span>, <span class="st">&#39;icmp&#39;</span>))</a>
<a class="sourceLine" id="cb12-3" title="3"><span class="kw">class</span> Port(models.Model):</a>
<a class="sourceLine" id="cb12-4" title="4">    protocole <span class="op">=</span> models.CharField(max_length<span class="op">=</span><span class="dv">1</span>,</a>
<a class="sourceLine" id="cb12-5" title="5">                                 choices<span class="op">=</span>PROTO,</a>
<a class="sourceLine" id="cb12-6" title="6">                                 default<span class="op">=</span><span class="st">&#39;a&#39;</span>)</a>
<a class="sourceLine" id="cb12-7" title="7">    debut <span class="op">=</span> models.PositiveIntegerField(default<span class="op">=</span><span class="dv">0</span>)</a>
<a class="sourceLine" id="cb12-8" title="8">    fin <span class="op">=</span> models.PositiveIntegerField(default<span class="op">=</span><span class="dv">0</span>)</a></code></pre></div>
</section><section id="appliquer-les-modifications" class="slide level2">
<h2>Appliquer les modifications</h2>
<pre><code>$ pipenv run ./manage.py makemigrations
Migrations for &#39;webui&#39;:
  webui/migrations/0001_initial.py
    - Create model Port
$ pipenv run ./manage.py migrate
...</code></pre>
</section><section id="contenu-dune-migration" class="slide level2">
<h2>Contenu d’une migration</h2>
<pre><code>$ cat webui/migrations/0001_initial.py</code></pre>
<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> django.db <span class="im">import</span> migrations, models</a>
<a class="sourceLine" id="cb15-2" title="2"></a>
<a class="sourceLine" id="cb15-3" title="3"><span class="kw">class</span> Migration(migrations.Migration):</a>
<a class="sourceLine" id="cb15-4" title="4">    initial <span class="op">=</span> <span class="va">True</span></a>
<a class="sourceLine" id="cb15-5" title="5">    dependencies <span class="op">=</span> [ ]</a>
<a class="sourceLine" id="cb15-6" title="6"></a>
<a class="sourceLine" id="cb15-7" title="7">    operations <span class="op">=</span> [</a>
<a class="sourceLine" id="cb15-8" title="8">        migrations.CreateModel(</a>
<a class="sourceLine" id="cb15-9" title="9">            name<span class="op">=</span><span class="st">&#39;Port&#39;</span>,</a>
<a class="sourceLine" id="cb15-10" title="10">            fields<span class="op">=</span>[</a>
<a class="sourceLine" id="cb15-11" title="11">                (<span class="st">&#39;id&#39;</span>, models.AutoField(auto_created<span class="op">=</span><span class="va">True</span>, primary_key<span class="op">=</span><span class="va">True</span>, serialize<span class="op">=</span><span class="va">False</span>, verbose_name<span class="op">=</span><span class="st">&#39;ID&#39;</span>)),</a>
<a class="sourceLine" id="cb15-12" title="12">                (<span class="st">&#39;protocole&#39;</span>, models.CharField(choices<span class="op">=</span>[(<span class="st">&#39;a&#39;</span>, <span class="st">&#39;any&#39;</span>), (<span class="st">&#39;t&#39;</span>, <span class="st">&#39;tcp&#39;</span>), (<span class="st">&#39;u&#39;</span>, <span class="st">&#39;udp&#39;</span>), (<span class="st">&#39;i&#39;</span>, <span class="st">&#39;icmp&#39;</span>)], default<span class="op">=</span><span class="st">&#39;a&#39;</span>, max_length<span class="op">=</span><span class="dv">1</span>)),</a>
<a class="sourceLine" id="cb15-13" title="13">                (<span class="st">&#39;debut&#39;</span>, models.PositiveIntegerField(default<span class="op">=</span><span class="dv">0</span>)),</a>
<a class="sourceLine" id="cb15-14" title="14">                (<span class="st">&#39;fin&#39;</span>, models.PositiveIntegerField(default<span class="op">=</span><span class="dv">0</span>)),</a>
<a class="sourceLine" id="cb15-15" title="15">            ],</a>
<a class="sourceLine" id="cb15-16" title="16">        ),</a>
<a class="sourceLine" id="cb15-17" title="17">    ]</a></code></pre></div>
</section><section id="shell-django" class="slide level2">
<h2>Shell django</h2>
<h3 id="pipenv-run-.manage.py-shell">pipenv run ./manage.py shell</h3>
<div class="sourceCode" id="cb16"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb16-1" title="1"><span class="op">&gt;&gt;&gt;</span> <span class="im">from</span> webui.models <span class="im">import</span> Port</a>
<a class="sourceLine" id="cb16-2" title="2"><span class="op">&gt;&gt;&gt;</span> p <span class="op">=</span> Port()</a>
<a class="sourceLine" id="cb16-3" title="3"><span class="op">&gt;&gt;&gt;</span> p.protocole</a>
<a class="sourceLine" id="cb16-4" title="4"><span class="co">&#39;a&#39;</span></a>
<a class="sourceLine" id="cb16-5" title="5"><span class="op">&gt;&gt;&gt;</span> p.get_protocole_display()</a>
<a class="sourceLine" id="cb16-6" title="6"><span class="co">&#39;any&#39;</span></a>
<a class="sourceLine" id="cb16-7" title="7"><span class="op">&gt;&gt;&gt;</span> p.debut</a>
<a class="sourceLine" id="cb16-8" title="8"><span class="dv">0</span></a>
<a class="sourceLine" id="cb16-9" title="9"><span class="op">&gt;&gt;&gt;</span> p.fin</a>
<a class="sourceLine" id="cb16-10" title="10"><span class="dv">0</span></a>
<a class="sourceLine" id="cb16-11" title="11"><span class="op">&gt;&gt;&gt;</span> p.debut <span class="op">=</span> <span class="dv">22</span></a>
<a class="sourceLine" id="cb16-12" title="12"><span class="op">&gt;&gt;&gt;</span> p.save()</a>
<a class="sourceLine" id="cb16-13" title="13"><span class="op">&gt;&gt;&gt;</span> p</a>
<a class="sourceLine" id="cb16-14" title="14"><span class="op">&lt;</span>Port: Port <span class="bu">object</span> (<span class="dv">1</span>)<span class="op">&gt;</span></a></code></pre></div>
</section><section id="webuimodels.py-1" class="slide level2">
<h2>webui/models.py</h2>
<p>Modifier l’affichage des instances.</p>
<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> Port(models.Model):</a>
<a class="sourceLine" id="cb17-2" title="2"></a>
<a class="sourceLine" id="cb17-3" title="3">    <span class="kw">def</span> <span class="fu">__str__</span>(<span class="va">self</span>):</a>
<a class="sourceLine" id="cb17-4" title="4">        ports <span class="op">=</span> <span class="ss">f&quot;</span><span class="sc">{</span><span class="va">self</span><span class="sc">.</span>debut<span class="sc">}</span><span class="ss">&quot;</span></a>
<a class="sourceLine" id="cb17-5" title="5">        <span class="cf">if</span> <span class="va">self</span>.debut <span class="op">&lt;</span> <span class="va">self</span>.fin:</a>
<a class="sourceLine" id="cb17-6" title="6">            ports <span class="op">+=</span> <span class="ss">f&quot;-</span><span class="sc">{</span><span class="va">self</span><span class="sc">.</span>fin<span class="sc">}</span><span class="ss">&quot;</span></a>
<a class="sourceLine" id="cb17-7" title="7">        <span class="cf">return</span> <span class="ss">f&quot;</span><span class="sc">{</span>ports<span class="sc">}</span><span class="ss">(</span><span class="sc">{</span><span class="va">self</span><span class="sc">.</span>get_protocole_display()<span class="sc">}</span><span class="ss">)&quot;</span></a></code></pre></div>
</section><section id="shell-plus" class="slide level2">
<h2>Shell plus</h2>
<h3 id="pipenv-run-.manage.py-shell_plus">pipenv run ./manage.py shell_plus</h3>
<div class="sourceCode" id="cb18"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb18-1" title="1"><span class="op">&gt;&gt;&gt;</span> p <span class="op">=</span> Port(debut<span class="op">=</span><span class="dv">80</span>, protocole<span class="op">=</span><span class="st">&quot;tcp&quot;</span>, fin<span class="op">=</span><span class="dv">88</span>)</a>
<a class="sourceLine" id="cb18-2" title="2"><span class="op">&gt;&gt;&gt;</span> p</a>
<a class="sourceLine" id="cb18-3" title="3"><span class="op">&lt;</span>Port: <span class="dv">80-88</span>(tcp)<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb18-4" title="4"><span class="op">&gt;&gt;&gt;</span> p.save()</a>
<a class="sourceLine" id="cb18-5" title="5"><span class="op">&gt;&gt;&gt;</span> p <span class="op">=</span> Port(debut<span class="op">=</span><span class="dv">80</span>, protocole<span class="op">=</span><span class="st">&quot;tcp&quot;</span>)</a>
<a class="sourceLine" id="cb18-6" title="6"><span class="op">&gt;&gt;&gt;</span> p</a>
<a class="sourceLine" id="cb18-7" title="7"><span class="op">&lt;</span>Port: <span class="dv">80</span>(tcp)<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb18-8" title="8"><span class="op">&gt;&gt;&gt;</span> p.save()</a>
<a class="sourceLine" id="cb18-9" title="9"><span class="op">&gt;&gt;&gt;</span> Port(protocole<span class="op">=</span><span class="st">&quot;tcp&quot;</span>, debut<span class="op">=</span><span class="dv">25</span>).save()</a>
<a class="sourceLine" id="cb18-10" title="10"><span class="op">&gt;&gt;&gt;</span> Port.objects.<span class="bu">all</span>()</a>
<a class="sourceLine" id="cb18-11" title="11"><span class="op">&lt;</span>QuerySet [<span class="op">&lt;</span>Port: <span class="dv">22</span>(<span class="bu">any</span>)<span class="op">&gt;</span>, <span class="op">&lt;</span>Port: <span class="dv">80-88</span>(tcp)<span class="op">&gt;</span>,</a>
<a class="sourceLine" id="cb18-12" title="12">           <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">25</span>(tcp)<span class="op">&gt;</span>]<span class="op">&gt;</span></a></code></pre></div>
</section><section id="webuimodels.py-2" class="slide level2">
<h2>webui/models.py</h2>
<p>Modifier l’ordre des instances.</p>
<div class="sourceCode" id="cb19"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb19-1" title="1"><span class="kw">class</span> Port(models.Model):                                                       </a>
<a class="sourceLine" id="cb19-2" title="2">                                                                                </a>
<a class="sourceLine" id="cb19-3" title="3">    <span class="kw">class</span> Meta(<span class="bu">object</span>):                                                         </a>
<a class="sourceLine" id="cb19-4" title="4">        ordering <span class="op">=</span> (<span class="st">&#39;debut&#39;</span>, <span class="st">&#39;protocole&#39;</span>, <span class="st">&#39;fin&#39;</span>)</a></code></pre></div>
<h3 id="shell">shell</h3>
<div class="sourceCode" id="cb20"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb20-1" title="1"><span class="op">&gt;&gt;&gt;</span> Port.objects.<span class="bu">all</span>()</a>
<a class="sourceLine" id="cb20-2" title="2"><span class="op">&lt;</span>QuerySet [<span class="op">&lt;</span>Port: <span class="dv">22</span>(<span class="bu">any</span>)<span class="op">&gt;</span>, <span class="op">&lt;</span>Port: <span class="dv">25</span>(tcp)<span class="op">&gt;</span>,</a>
<a class="sourceLine" id="cb20-3" title="3">           <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">80-88</span>(tcp)<span class="op">&gt;</span>]<span class="op">&gt;</span></a></code></pre></div>
</section><section id="récupération" class="slide level2">
<h2>Récupération</h2>
<div class="sourceCode" id="cb21"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb21-1" title="1"><span class="op">&gt;&gt;&gt;</span> Port.objects.get(debut<span class="op">=</span><span class="dv">22</span>)</a>
<a class="sourceLine" id="cb21-2" title="2"><span class="op">&lt;</span>Port: <span class="dv">22</span>(<span class="bu">any</span>)<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb21-3" title="3"><span class="op">&gt;&gt;&gt;</span> Port.objects.get(debut<span class="op">=</span><span class="dv">80</span>)</a>
<a class="sourceLine" id="cb21-4" title="4">webui.models.Port.MultipleObjectsReturned: get() returned more than one Port</a></code></pre></div>
<h3 id="création-si-besoin">Création si besoin</h3>
<div class="sourceCode" id="cb22"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb22-1" title="1">port, flag <span class="op">=</span> Port.objects.get_or_create(debut<span class="op">=</span><span class="dv">25</span>)</a></code></pre></div>
</section><section id="sélection" class="slide level2">
<h2>Sélection</h2>
<div class="sourceCode" id="cb23"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb23-1" title="1">In [<span class="dv">1</span>]: Port.objects.<span class="bu">filter</span>(debut<span class="op">=</span><span class="dv">80</span>)</a>
<a class="sourceLine" id="cb23-2" title="2">Out[<span class="dv">1</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">80-88</span>(tcp)<span class="op">&gt;</span>]<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb23-3" title="3"></a>
<a class="sourceLine" id="cb23-4" title="4">In [<span class="dv">2</span>]: Port.objects.<span class="bu">filter</span>(debut<span class="op">=</span><span class="dv">80</span>).count()</a>
<a class="sourceLine" id="cb23-5" title="5">Out[<span class="dv">2</span>]: <span class="dv">2</span></a>
<a class="sourceLine" id="cb23-6" title="6"></a>
<a class="sourceLine" id="cb23-7" title="7">In [<span class="dv">3</span>]: Port.objects.<span class="bu">filter</span>(debut<span class="op">=</span><span class="dv">80</span>)[<span class="dv">1</span>]</a>
<a class="sourceLine" id="cb23-8" title="8">Out[<span class="dv">3</span>]: <span class="op">&lt;</span>Port: <span class="dv">80-88</span>(tcp)<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb23-9" title="9"></a>
<a class="sourceLine" id="cb23-10" title="10">In [<span class="dv">4</span>]: Port.objects.<span class="bu">filter</span>(debut__gt<span class="op">=</span><span class="dv">20</span>)</a>
<a class="sourceLine" id="cb23-11" title="11">Out[<span class="dv">4</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">25</span>(tcp)<span class="op">&gt;</span>,</a>
<a class="sourceLine" id="cb23-12" title="12">                   <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">80-88</span>(tcp)<span class="op">&gt;</span>]<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb23-13" title="13"></a>
<a class="sourceLine" id="cb23-14" title="14">In [<span class="dv">5</span>]: Port.objects.<span class="bu">filter</span>(debut__gt<span class="op">=</span><span class="dv">20</span>).<span class="bu">filter</span>(debut__lte<span class="op">=</span><span class="dv">25</span>)</a>
<a class="sourceLine" id="cb23-15" title="15">Out[<span class="dv">5</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">25</span>(tcp)<span class="op">&gt;</span>]<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb23-16" title="16"></a>
<a class="sourceLine" id="cb23-17" title="17">In [<span class="dv">6</span>]: Port.objects.<span class="bu">filter</span>(debut__gt<span class="op">=</span><span class="dv">20</span>, debut__lte<span class="op">=</span><span class="dv">25</span>)</a>
<a class="sourceLine" id="cb23-18" title="18">Out[<span class="dv">6</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">25</span>(tcp)<span class="op">&gt;</span>]<span class="op">&gt;</span></a></code></pre></div>
</section><section id="suppression" class="slide level2">
<h2>Suppression</h2>
<div class="sourceCode" id="cb24"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb24-1" title="1">In [<span class="dv">1</span>]: Port.objects.<span class="bu">all</span>()</a>
<a class="sourceLine" id="cb24-2" title="2">Out[<span class="dv">1</span>]: <span class="op">&lt;</span>QuerySet [<span class="op">&lt;</span>Port: <span class="dv">22</span>(<span class="bu">any</span>)<span class="op">&gt;</span>, <span class="op">&lt;</span>Port: <span class="dv">25</span>(tcp)<span class="op">&gt;</span>,</a>
<a class="sourceLine" id="cb24-3" title="3">                   <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">80-88</span>(tcp)<span class="op">&gt;</span>]<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb24-4" title="4"></a>
<a class="sourceLine" id="cb24-5" title="5">In [<span class="dv">2</span>]: Port.objects.get(fin<span class="op">=</span><span class="dv">88</span>)</a>
<a class="sourceLine" id="cb24-6" title="6">Out[<span class="dv">2</span>]: <span class="op">&lt;</span>Port: <span class="dv">80-88</span>(tcp)<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb24-7" title="7"></a>
<a class="sourceLine" id="cb24-8" title="8">In [<span class="dv">3</span>]: Port.objects.get(fin<span class="op">=</span><span class="dv">88</span>).delete()</a>
<a class="sourceLine" id="cb24-9" title="9">Out[<span class="dv">3</span>]: (<span class="dv">1</span>, {<span class="st">&#39;webui.Port&#39;</span>: <span class="dv">1</span>})</a>
<a class="sourceLine" id="cb24-10" title="10"></a>
<a class="sourceLine" id="cb24-11" title="11">In [<span class="dv">4</span>]: Port.objects.<span class="bu">filter</span>(debut__gt<span class="op">=</span><span class="dv">22</span>,</a>
<a class="sourceLine" id="cb24-12" title="12">                            debut__lte<span class="op">=</span><span class="dv">25</span>).delete()</a>
<a class="sourceLine" id="cb24-13" title="13">Out[<span class="dv">4</span>]: (<span class="dv">1</span>, {<span class="st">&#39;webui.Port&#39;</span>: <span class="dv">1</span>})</a>
<a class="sourceLine" id="cb24-14" title="14"></a>
<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>
Bonnegent Sebastien's avatar
Bonnegent Sebastien committed
365
<p>Faire la classe suivante</p>
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
<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>
</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>
<ul>
<li>https://docs.djangoproject.com/fr/
<ul>
<li>partie modèle</li>
</ul></li>
<li>https://docs.djangoproject.com/fr/2.2/intro/tutorial01/
<ul>
<li>tutoriel officiel</li>
</ul></li>
</ul>
</section></section>
<section class="footnotes">
<hr />
<ol>
<li id="fn1"><p>https://www.djangoproject.com/<a href="#/fnref1" class="footnote-back"></a></p></li>
<li id="fn2"><p>https://gitlab.insa-rouen.fr/bonnegent/cours_django/wikis/pipenv<a href="#/fnref2" class="footnote-back"></a></p></li>
<li id="fn3"><p>https://docs.djangoproject.com/fr/2.2/intro/tutorial01/<a href="#/fnref3" class="footnote-back"></a></p></li>
</ol>
</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>