From 567e02a6cda2138bc666220145670029cd267c3b Mon Sep 17 00:00:00 2001
From: Bruno Rocha
Date: Wed, 11 May 2022 19:11:43 +0000
Subject: [PATCH 1/7] day 1
---
.vscode/settings.json | 3 +
exemplos/day1/blog/blog.db | Bin 0 -> 16384 bytes
exemplos/day1/blog/database.py | 53 ++++++++++++++++++
exemplos/day1/blog/list.template.html | 13 +++++
exemplos/day1/blog/post.template.html | 16 ++++++
exemplos/day1/blog/render.py | 40 +++++++++++++
...como-criar-um-blog-utilizando-python..html | 18 ++++++
exemplos/day1/blog/site/index.html | 14 +++++
...251-eleita-a-linguagem-mais-popular..html" | 18 ++++++
exemplos/day1/cgi-bin/envia.py | 16 ++++++
exemplos/day1/client.py | 14 +++++
exemplos/day1/contato.html | 16 ++++++
exemplos/day1/estilo.css | 20 +++++++
exemplos/day1/http_libs.py | 9 +++
exemplos/day1/pagina.html | 21 +++++++
exemplos/day1/script.js | 6 ++
exemplos/day1/urlopen.py | 3 +
17 files changed, 280 insertions(+)
create mode 100644 .vscode/settings.json
create mode 100644 exemplos/day1/blog/blog.db
create mode 100644 exemplos/day1/blog/database.py
create mode 100644 exemplos/day1/blog/list.template.html
create mode 100644 exemplos/day1/blog/post.template.html
create mode 100644 exemplos/day1/blog/render.py
create mode 100644 exemplos/day1/blog/site/como-criar-um-blog-utilizando-python..html
create mode 100644 exemplos/day1/blog/site/index.html
create mode 100644 "exemplos/day1/blog/site/python-\303\251-eleita-a-linguagem-mais-popular..html"
create mode 100755 exemplos/day1/cgi-bin/envia.py
create mode 100644 exemplos/day1/client.py
create mode 100644 exemplos/day1/contato.html
create mode 100644 exemplos/day1/estilo.css
create mode 100644 exemplos/day1/http_libs.py
create mode 100644 exemplos/day1/pagina.html
create mode 100644 exemplos/day1/script.js
create mode 100644 exemplos/day1/urlopen.py
diff --git a/.vscode/settings.json b/.vscode/settings.json
new file mode 100644
index 0000000..4d4e890
--- /dev/null
+++ b/.vscode/settings.json
@@ -0,0 +1,3 @@
+{
+ "livePreview.defaultPreviewPath": "/exemplos/day1/pagina.html"
+}
\ No newline at end of file
diff --git a/exemplos/day1/blog/blog.db b/exemplos/day1/blog/blog.db
new file mode 100644
index 0000000000000000000000000000000000000000..0bf5b9761dc2522a2dc0c555ed995e423a2be2f6
GIT binary patch
literal 16384
zcmeI(zfaph6bJCLgHk{Ow?mYn%i9X#M}<07ptb>76>2aLk|L()aE|0u+ZXIJYP(eM
zpLC=BH<&tA>ehdx>ey!kk)Wy?Rbr{Wr<@)8-n*PXo}R7j?KZVlbY#+4T6!Yx3J
zh=dR>_X+OT&EYS%kFUr6w0Fhw^Y1g>Pf?luBD{y*SKgsP00Izz00bZa0SG_<0uX=z
z1pZxMr&6siEI0>N9!4rVj<~ArX6iUsNqGBkVx#3Z+CH@#>rJ0-#m*%%R*RfDf4bt_
ztJiAInP1L1G?^{Bdn3CzLvz!_P4`GAR`pd%JFU&FM(coH`3KbKw6`|{PTKN=_F@@n
zwT%>=$TS?tlsduYZpWu!yG=o-*}M!14NsNWQP9m%ncIO$e;b^hf89-7orEZ?ZE?3N2ydt8i+;$G&Om-1RB)G}F?$EWK~
zRmigkOP8yoi%+B?rL81Mkxu%#?5mh3)EN!UFpp$9x`4)I01sc~9vS_&WE!eSQmRgL
zX60y6R)qup4faSgQk1EFuBc~Xos@8f;+z4ONAY{onrxsckg<%7HPd`wUGH3Y=iaxw
zxy6_j0uX=z1Rwwb2tWV=5P$##AOL}J1?DUJ&REY#+@*?~8vltz@&4cOehB`cK>z{}
efB*y_009U<00Izz00bcLUkOY)m6}ujnc)`&LJ_q9
literal 0
HcmV?d00001
diff --git a/exemplos/day1/blog/database.py b/exemplos/day1/blog/database.py
new file mode 100644
index 0000000..9c1fd2a
--- /dev/null
+++ b/exemplos/day1/blog/database.py
@@ -0,0 +1,53 @@
+# 1 - conectamos ao banco de dados
+from sqlite3 import connect
+conn = connect("blog.db")
+cursor = conn.cursor()
+
+# 2 - Criamos a tabela caso não exista
+conn.execute(
+ """\
+ CREATE TABLE if not exists post (
+ id integer PRIMARY KEY AUTOINCREMENT,
+ title varchar UNIQUE NOT NULL,
+ content varchar NOT NULL,
+ author varchar NOT NULL
+ );
+ """
+)
+
+# 3 - Criamos os posts iniciais para alimentar o banco de dados
+posts = [
+ {
+ "title": "Python é eleita a linguagem mais popular",
+ "content": """\
+ A linguem Python foi eleita a linguagem mais popular pela revista
+ tech masters e segue dominando o mundo.
+ """,
+ "author": "Satoshi Namamoto",
+ },
+ {
+ "title": "Como criar um blog utilizando Python",
+ "content": """\
+ Neste tutorial você aprenderá como criar um blog utilizando Python.
+
import make_a_blog
+ """,
+ "author": "Guido Van Rossum",
+ },
+]
+
+
+# 4 - Inserimos os posts caso o banco de dados esteja vazio
+count = cursor.execute("SELECT * FROM post;").fetchall()
+if not count:
+ cursor.executemany(
+ """\
+ INSERT INTO post (title, content, author)
+ VALUES (:title, :content, :author);
+ """,
+ posts,
+ )
+ conn.commit()
+
+# 5 - Verificamos que foi realmente inserido
+posts = cursor.execute("SELECT * FROM post;").fetchall()
+assert len(posts) == 2
diff --git a/exemplos/day1/blog/list.template.html b/exemplos/day1/blog/list.template.html
new file mode 100644
index 0000000..fb57226
--- /dev/null
+++ b/exemplos/day1/blog/list.template.html
@@ -0,0 +1,13 @@
+
+
+
+
+ Blog
+
+
+
Blog Posts:
+
+ {post_list}
+
+
+
\ No newline at end of file
diff --git a/exemplos/day1/blog/post.template.html b/exemplos/day1/blog/post.template.html
new file mode 100644
index 0000000..6aa3ac6
--- /dev/null
+++ b/exemplos/day1/blog/post.template.html
@@ -0,0 +1,16 @@
+
+
+
+
+ Blog
+
+
+
{post[title]}
+
+
+ {post[content]}
+
+
+ {post[author]}
+
+
\ No newline at end of file
diff --git a/exemplos/day1/blog/render.py b/exemplos/day1/blog/render.py
new file mode 100644
index 0000000..99180fb
--- /dev/null
+++ b/exemplos/day1/blog/render.py
@@ -0,0 +1,40 @@
+from pathlib import Path
+from database import conn
+
+# 1 - Obtemos os posts do banco de dados e deserializamos em um dict
+cursor = conn.cursor()
+fields = ("id", "title", "content", "author")
+results = cursor.execute(f"SELECT * FROM post;")
+posts = [dict(zip(fields, post)) for post in results]
+
+# 2 - Criamos a pasta de destino do site
+site_dir = Path("site")
+site_dir.mkdir(exist_ok=True)
+
+# 3 - Criamos uma função capaz de gerar a url de um post
+def get_post_url(post):
+ slug = post["title"].lower().replace(" ", "-")
+ return f"{slug}.html"
+
+
+# 3 - Renderizamos o a página `index.html` a partir do template.
+index_template = Path("list.template.html").read_text()
+index_page = site_dir / Path("index.html")
+post_list = [
+ f"