From e26eb505823b74850294f63cf14d40367f9c165d Mon Sep 17 00:00:00 2001 From: Andros Fenollosa Date: Sun, 6 Aug 2017 00:38:21 +0200 Subject: [PATCH] Add form and Add search --- README.md | 23 +++++++++++++++++-- app.py | 38 +++++++++++++++++++++++++++---- forms.py | 8 +++++++ requirements.txt | 3 +++ templates/items/buscador.html | 39 +++++++++++++++++++++++++++++--- templates/items/programadas.html | 6 +++++ templates/layouts/master.html | 4 ++++ 7 files changed, 112 insertions(+), 9 deletions(-) create mode 100644 forms.py create mode 100644 templates/items/programadas.html diff --git a/README.md b/README.md index 6e0ec52..b1bcedb 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ [wallaviso.com](http://wallaviso.com) -## Run +## Run (Ejecutar) [EN] For the impatient, you can play with the finished exercise. You should download the code and execute the following commands. @@ -29,4 +29,23 @@ python3 app.py [EN] Then open in your favorite browser, which will possibly be the fantastic Firefox, a new tab with [http://127.0.0.1:5000](http://127.0.0.1:5000) -[ES] Después abrir en tu navegador favorito, que posiblemente será el fantástico Firefox, una pestaña nueva con [http://127.0.0.1:5000](http://127.0.0.1:5000) \ No newline at end of file +[ES] Después abrir en tu navegador favorito, que posiblemente será el fantástico Firefox, una pestaña nueva con [http://127.0.0.1:5000](http://127.0.0.1:5000) + +## Workshop (Taller) + +### Part 1 - Flask Core y Search (Parte 1 - Nucleo de Flask y Buscador) 50 min + +### Break (Descanso) - 10 min + +[EN] We debug bugs and prepare for the next point. +[ES] Depuramos bugs y nos preparamos para el siguiente punto. + +### Part 2 - Databases and CRUD with Flask (Bases de datos y CRUD elementos con Flask) + +### Break (Descanso) - 10 min + +[EN] We take air for the last part. Otherwise, we make as we go to the bathroom and do not come back. +[ES] Cogemos aire para la última parte. En caso contrario, hacemos como que vamos al baño y nos piramos. + + +### Part 3 - Sending emails with new items (Envío de emails con nuevos elementos) \ No newline at end of file diff --git a/app.py b/app.py index 7e9f6df..e946f99 100644 --- a/app.py +++ b/app.py @@ -1,10 +1,40 @@ -from flask import Flask, render_template +from flask import Flask, render_template, request +from forms import SearchForm +# Get data Wallapop +import json +from urllib3 import PoolManager +import urllib.parse + +# Flask app = Flask(__name__) app.config['DEBUG'] = True +app.config['SECRET_KEY'] = 'mi secreto' + +@app.route('/', methods=['GET', 'POST']) +def buscador(): + form = SearchForm() + results = None + if form.validate_on_submit(): + name = form.name.data + price_max = form.price_max.data or '' + + # Search in Wallapop + http = PoolManager() + url_api = 'http://es.wallapop.com/rest/items?minPrice=&maxPrice={price_max}&dist=&order=creationDate-des&lat=41.398077&lng=2.170432&kws={kws}'.format( + kws=urllib.parse.quote(name, safe=''), + price_max=price_max + ) + results = http.request('GET', url_api) + results = json.loads( + results.data.decode('utf-8') + ) + results = results['items'] + return render_template('items/buscador.html', form=form, results=results) + +@app.route('/programadas') +def programadas(): + return render_template('items/programadas.html') -@app.route('/') -def index(): - return render_template('items/buscador.html') if __name__ == '__main__': app.run() \ No newline at end of file diff --git a/forms.py b/forms.py new file mode 100644 index 0000000..84489c2 --- /dev/null +++ b/forms.py @@ -0,0 +1,8 @@ +from flask_wtf import FlaskForm +from wtforms import StringField, IntegerField +from wtforms.validators import DataRequired, Length, NumberRange, Optional + + +class SearchForm(FlaskForm): + name = StringField('Nombre', [Length(min=1, max=100, message='Es demasiado largo'), DataRequired(message='Campo obligatorio')]) + price_max = IntegerField('Precio', [NumberRange(1, message='No puede ser inferior a 1'), Optional()]) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index e69de29..2437309 100644 --- a/requirements.txt +++ b/requirements.txt @@ -0,0 +1,3 @@ +Flask==0.12.2 +Flask-WTF==0.14.2 +urllib3==1.22 \ No newline at end of file diff --git a/templates/items/buscador.html b/templates/items/buscador.html index 6be7d84..e68e320 100644 --- a/templates/items/buscador.html +++ b/templates/items/buscador.html @@ -1,13 +1,46 @@ {% extends 'layouts/master.html' %} +{% set active_page = "buscador" %} {% block title %}Buscador{% endblock %} {% block body %}

Buscador

-
- - + + {{ form.csrf_token }} + {% for input in form %} + {% if input.type != 'CSRFTokenField' %} +
+ {# Label #} + {{ input.label }} + {# Input #} + {{ input(class="form-control") }} + {# Errors #} + {% if input.errors %} +
+ {% for error in input.errors %} + + {% endfor %} +
+ {% endif %} +
+ {% endif %} + {% endfor %} +
+{% if results %} + + {% for item in results %} + + + + + + + {% endfor %} +
{{ item.title }}{{ item.title }}{{ item.price }}+
+{% endif %} {% endblock %} \ No newline at end of file diff --git a/templates/items/programadas.html b/templates/items/programadas.html new file mode 100644 index 0000000..b7c4625 --- /dev/null +++ b/templates/items/programadas.html @@ -0,0 +1,6 @@ +{% extends 'layouts/master.html' %} +{% set active_page = "programadas" %} +{% block title %}Programadas{% endblock %} +{% block body %} +

Programadas

+{% endblock %} \ No newline at end of file diff --git a/templates/layouts/master.html b/templates/layouts/master.html index 73c8b13..7cd79f2 100644 --- a/templates/layouts/master.html +++ b/templates/layouts/master.html @@ -8,6 +8,10 @@
+ {% block body %}{% endblock %}