Update REAME with new save system

This commit is contained in:
Andros Fenollosa 2017-08-27 12:51:01 +02:00
parent ce2097e39a
commit 1dc9dc6d58
7 changed files with 65 additions and 87 deletions

View File

@ -88,7 +88,6 @@ app = Flask(__name__)
app.config['DEBUG'] = True app.config['DEBUG'] = True
app.config['SECRET_KEY'] = 'mi secreto' app.config['SECRET_KEY'] = 'mi secreto'
@app.route('/') @app.route('/')
def buscador(): def buscador():
return 'Hello PyConES17 !!!' return 'Hello PyConES17 !!!'
@ -463,10 +462,7 @@ db = SQLAlchemy(app)
class Programado(db.Model): class Programado(db.Model):
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
item_id = db.Column(db.Integer)
title = db.Column(db.String(128)) title = db.Column(db.String(128))
picture_URL = db.Column(db.String(300))
price = db.Column(db.String(10))
``` ```
@ -490,10 +486,7 @@ manager.add_command('db', MigrateCommand)
class Programado(db.Model): class Programado(db.Model):
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
item_id = db.Column(db.Integer)
title = db.Column(db.String(128)) title = db.Column(db.String(128))
picture_URL = db.Column(db.String(300))
price = db.Column(db.String(10))
if __name__ == "__main__": if __name__ == "__main__":
@ -519,7 +512,7 @@ sqlite3 database.sqlite
--- ---
#### 2.2 Save item #### 2.2 Save item
[ES] Para guarda un elemento necesitamos modificar nuestra plantilla **buscador.html** para enviar la información que queremos guardar usando *POST*. Qué sencillamente será un **<form>** con las variables ocultas. [ES] Para guarda un elemento necesitamos modificar nuestra plantilla **buscador.html** para enviar la información que queremos guardar usando *POST*. Qué sencillamente será un **<form>** con las variables ocultas. En este caso lo que haremos será mostrar un botón solo visible cuando recibamos una petición *POST*. Su finalidad será realizar una petición a la página *programadas_nuevo()* con el nombre que hemos buscado.
```jinja2 ```jinja2
{% extends 'layouts/master.html' %} {% extends 'layouts/master.html' %}
@ -552,7 +545,13 @@ sqlite3 database.sqlite
{% endif %} {% endif %}
{% endfor %} {% endfor %}
<input type="submit" class="btn btn-primary" value="Buscar"> <input type="submit" class="btn btn-primary" value="Buscar">
</form> </form>
{% if results %}
<form action="{{ url_for('programadas_nuevo') }}" method="post">
<input type="hidden" name="title" value="{{ form.name.data }}">
<input type="submit" class="btn btn-success" value="Programar">
</form>
{% endif %}
</div> </div>
</div> </div>
{% if results %} {% if results %}
@ -562,15 +561,6 @@ sqlite3 database.sqlite
<td><img class="img-responsive" src="{{ item.pictureURL }}" alt="{{ item.title }}"></td> <td><img class="img-responsive" src="{{ item.pictureURL }}" alt="{{ item.title }}"></td>
<td>{{ item.title }}</td> <td>{{ item.title }}</td>
<td>{{ item.price }}</td> <td>{{ item.price }}</td>
<td>
<form action="{{ url_for('programadas_nuevo') }}" method="post">
<input type="hidden" name="itemId" value="{{ item.itemId }}">
<input type="hidden" name="title" value="{{ item.title }}">
<input type="hidden" name="price" value="{{ item.price }}">
<input type="hidden" name="pictureURL" value="{{ item.pictureURL }}">
<input type="submit" class="btn btn-success" value="+">
</form>
</td>
</tr> </tr>
{% endfor %} {% endfor %}
</table> </table>
@ -578,7 +568,7 @@ sqlite3 database.sqlite
{% endblock %} {% endblock %}
``` ```
[ES] Ahora, tendremos que crear la función para **programadas_nuevo** en **app.py**. Lo primero que le decimos es que solo puede aceptar peticiones *POST*. A continuación creamos variables para guardar la información del formulario. Después creamos el registro en la base de datos. Por último redireccionamos a la anterior página para ver el nuevo elemento. [ES] Ahora, tendremos que crear la función para *programadas_nuevo()* en **app.py**. Lo primero que le decimos es que solo puede aceptar peticiones *POST*. A continuación creamos variables para guardar la información del formulario. Después creamos el registro en la base de datos. Por último redireccionamos a la anterior página para ver el nuevo elemento.
[ES] Por partes. Importamos **db** que será nuestro *ORM*, y **Programado** que será la tabla a manipular. [ES] Por partes. Importamos **db** que será nuestro *ORM*, y **Programado** que será la tabla a manipular.
@ -590,10 +580,7 @@ from models import db, Programado
```python3 ```python3
my_program = Programado( my_program = Programado(
item_id=itemId, title=title
title=title,
picture_URL=pictureURL,
price=price
) )
``` ```
@ -656,16 +643,10 @@ def get_resultados(name='', price_max=''):
@app.route('/programadas/nuevo', methods=('POST',)) @app.route('/programadas/nuevo', methods=('POST',))
def programadas_nuevo(): def programadas_nuevo():
itemId = int(request.form['itemId'])
title = request.form['title'] title = request.form['title']
pictureURL = request.form['pictureURL']
price = request.form['price']
# We saved in the database # We saved in the database
my_program = Programado( my_program = Programado(
item_id=itemId, title=title
title=title,
picture_URL=pictureURL,
price=price
) )
db.session.add(my_program) db.session.add(my_program)
try: try:
@ -732,16 +713,11 @@ def get_resultados(name='', price_max=''):
@app.route('/programadas/nuevo', methods=('POST',)) @app.route('/programadas/nuevo', methods=('POST',))
def programadas_nuevo(): def programadas_nuevo():
itemId = int(request.form['itemId'])
title = request.form['title'] title = request.form['title']
pictureURL = request.form['pictureURL']
price = request.form['price']
# We saved in the database # We saved in the database
my_program = Programado( my_program = Programado(
item_id=itemId, title=title
title=title,
picture_URL=pictureURL,
price=price
) )
db.session.add(my_program) db.session.add(my_program)
try: try:
@ -767,19 +743,13 @@ if __name__ == '__main__':
<table class="table"> <table class="table">
<thead> <thead>
<tr> <tr>
<th scope="col">Imagen</th>
<th scope="col">Titulo</th> <th scope="col">Titulo</th>
<th scope="col">Precio</th>
<th></th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{% for item in programado_all %} {% for item in programado_all %}
<tr> <tr>
<td><img src="{{ item.picture_URL }}" alt="{{ item.title }}"></td>
<td>{{ item.title }}</td> <td>{{ item.title }}</td>
<td>{{ item.price }}</td>
<td></td>
</tr> </tr>
{% endfor %} {% endfor %}
</tbody> </tbody>
@ -801,23 +771,20 @@ if __name__ == '__main__':
<table class="table"> <table class="table">
<thead> <thead>
<tr> <tr>
<th scope="col">Imagen</th>
<th scope="col">Titulo</th> <th scope="col">Titulo</th>
<th scope="col">Precio</th>
<th></th> <th></th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{% for item in programado_all %} {% for item in programado_all %}
<tr> <tr>
<td><img src="{{ item.picture_URL }}" alt="{{ item.title }}"></td>
<td>{{ item.title }}</td> <td>{{ item.title }}</td>
<td>{{ item.price }}</td>
<td> <td>
<form action="{{ url_for('programadas_borrar') }}" method="post"> <form action="{{ url_for('programadas_borrar') }}" method="post">
<input type="hidden" name="id" value="{{ item.id }}"> <input type="hidden" name="id" value="{{ item.id }}">
<input type="submit" class="btn btn-danger" value="-"> <input type="submit" class="btn btn-danger" value="-">
</form> </form>
</td>
</tr> </tr>
{% endfor %} {% endfor %}
</tbody> </tbody>
@ -886,16 +853,11 @@ def get_resultados(name='', price_max=''):
@app.route('/programadas/nuevo', methods=('POST',)) @app.route('/programadas/nuevo', methods=('POST',))
def programadas_nuevo(): def programadas_nuevo():
itemId = int(request.form['itemId']) title = request.form['title']
title = request.form['title']
pictureURL = request.form['pictureURL']
price = request.form['price']
# We saved in the database # We saved in the database
my_program = Programado( my_program = Programado(
item_id=itemId, title=title
title=title,
picture_URL=pictureURL,
price=price
) )
db.session.add(my_program) db.session.add(my_program)
try: try:
@ -1023,16 +985,11 @@ def get_resultados(name='', price_max=''):
@app.route('/programadas/nuevo', methods=('POST',)) @app.route('/programadas/nuevo', methods=('POST',))
def programadas_nuevo(): def programadas_nuevo():
itemId = int(request.form['itemId']) title = request.form['title']
title = request.form['title']
pictureURL = request.form['pictureURL']
price = request.form['price']
# We saved in the database # We saved in the database
my_program = Programado( my_program = Programado(
item_id=itemId, title=title
title=title,
picture_URL=pictureURL,
price=price
) )
db.session.add(my_program) db.session.add(my_program)
try: try:

8
app.py
View File

@ -45,16 +45,10 @@ def get_resultados(name='', price_max=''):
@app.route('/programadas/nuevo', methods=('POST',)) @app.route('/programadas/nuevo', methods=('POST',))
def programadas_nuevo(): def programadas_nuevo():
itemId = int(request.form['itemId'])
title = request.form['title'] title = request.form['title']
pictureURL = request.form['pictureURL']
price = request.form['price']
# We saved in the database # We saved in the database
my_program = Programado( my_program = Programado(
item_id=itemId, title=title
title=title,
picture_URL=pictureURL,
price=price
) )
db.session.add(my_program) db.session.add(my_program)
try: try:

35
avisador.py Normal file
View File

@ -0,0 +1,35 @@
from flask_script import Manager
from flask_mail import Mail, Message
from models import db, Programado
from app import app
app.config.update(
MAIL_SERVER='smtp.mailgun.org',
MAIL_PORT=587,
MAIL_USERNAME='postmaster@sandboxcbf540cc1c514f818bce4f566e6a1477.mailgun.org',
MAIL_PASSWORD='9176ea5fc4c32da768f334c33f6c8b20'
)
mail = Mail(app)
manager = Manager(app)
@manager.command
def buscar_y_notificar():
msg = Message(
"Nuevo aviso",
sender="no-reply@pycon17.es",
recipients=["andros@fenollosa.email"]
)
msg.body = "testing"
msg.html = "<b>testing</b>"
mail.send(msg)
@manager.command
def test():
programado_all = Programado.query.all()
app.get_resultados()
if __name__ == "__main__":
manager.run()

View File

@ -14,10 +14,7 @@ manager.add_command('db', MigrateCommand)
class Programado(db.Model): class Programado(db.Model):
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
item_id = db.Column(db.Integer)
title = db.Column(db.String(128)) title = db.Column(db.String(128))
picture_URL = db.Column(db.String(300))
price = db.Column(db.String(10))
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -3,4 +3,5 @@ Flask-Migrate==2.1.0
Flask-Script==2.0.5 Flask-Script==2.0.5
Flask-SQLAlchemy==2.1 Flask-SQLAlchemy==2.1
Flask-WTF==0.14.2 Flask-WTF==0.14.2
Flask-Mail==0.9.1
urllib3==1.22 urllib3==1.22

View File

@ -29,6 +29,12 @@
{% endfor %} {% endfor %}
<input type="submit" class="btn btn-primary" value="Buscar"> <input type="submit" class="btn btn-primary" value="Buscar">
</form> </form>
{% if results %}
<form action="{{ url_for('programadas_nuevo') }}" method="post">
<input type="hidden" name="title" value="{{ form.name.data }}">
<input type="submit" class="btn btn-success" value="Programar">
</form>
{% endif %}
</div> </div>
</div> </div>
{% if results %} {% if results %}
@ -38,15 +44,6 @@
<td><img class="img-responsive" src="{{ item.pictureURL }}" alt="{{ item.title }}"></td> <td><img class="img-responsive" src="{{ item.pictureURL }}" alt="{{ item.title }}"></td>
<td>{{ item.title }}</td> <td>{{ item.title }}</td>
<td>{{ item.price }}</td> <td>{{ item.price }}</td>
<td>
<form action="{{ url_for('programadas_nuevo') }}" method="post">
<input type="hidden" name="itemId" value="{{ item.itemId }}">
<input type="hidden" name="title" value="{{ item.title }}">
<input type="hidden" name="price" value="{{ item.price }}">
<input type="hidden" name="pictureURL" value="{{ item.pictureURL }}">
<input type="submit" class="btn btn-success" value="+">
</form>
</td>
</tr> </tr>
{% endfor %} {% endfor %}
</table> </table>

View File

@ -6,23 +6,20 @@
<table class="table"> <table class="table">
<thead> <thead>
<tr> <tr>
<th scope="col">Imagen</th>
<th scope="col">Titulo</th> <th scope="col">Titulo</th>
<th scope="col">Precio</th>
<th></th> <th></th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{% for item in programado_all %} {% for item in programado_all %}
<tr> <tr>
<td><img src="{{ item.picture_URL }}" alt="{{ item.title }}"></td>
<td>{{ item.title }}</td> <td>{{ item.title }}</td>
<td>{{ item.price }}</td>
<td> <td>
<form action="{{ url_for('programadas_borrar') }}" method="post"> <form action="{{ url_for('programadas_borrar') }}" method="post">
<input type="hidden" name="id" value="{{ item.id }}"> <input type="hidden" name="id" value="{{ item.id }}">
<input type="submit" class="btn btn-danger" value="-"> <input type="submit" class="btn btn-danger" value="Borrar">
</form> </form>
</td>
</tr> </tr>
{% endfor %} {% endfor %}
</tbody> </tbody>