Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from flask import Flask
from flask import render_template

from forms import SearchForm
from flask.ext.elasticsearch import FlaskElasticsearch

from forms import SearchForm, AddForm


app = Flask(__name__)
es = FlaskElasticsearch(app)
app.config['DEBUG'] = True
app.config['SECRET_KEY'] = 'super_secret_key'

Expand All @@ -20,9 +23,22 @@ def search():
return render_template('search.html', form=form)


@app.route("/add/")
@app.route("/add/", methods=['GET', 'POST'])
def add():
return "Add"
form = AddForm()
if form.validate_on_submit():
data = {
'name': form.name.data,
'key': form.key.data,
'value': form.value.data,
}
response = es.index(index='resistor', doc_type='component',
body=data)
if not response['created']:
return 'Error'
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aqui tem que tratar o erro!

else:
return 'Success'
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

O que vamos fazer em caso de sucesso?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enviar as msgs de erro ou sucesso pro template.

return render_template('add.html', form=form)


if __name__ == "__main__":
Expand Down
7 changes: 7 additions & 0 deletions forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,10 @@

class SearchForm(Form):
search = TextField('Search', [validators.InputRequired()])


class AddForm(Form):
name = TextField('Name', [validators.InputRequired()])
key = TextField('Key', [validators.InputRequired()])
value = TextField('Value', [validators.InputRequired()])

19 changes: 19 additions & 0 deletions templates/add.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{% block body %}
<div class="add">
<h1>Add</h1>
<form method="post" role="form" action="{{ url_for('add') }}">
<div class="input-group">
<div class="form-group">
{{ form.name(class="form-control", placeholder="Name") }}
</div>
<div class="form-group">
{{ form.key(class="form-control", placeholder="Key") }}
{{ form.value(class="form-control", placeholder="Value") }}
<span class="input-group-btn">
<input type="submit" class="btn btn-primary " value="Add" />
</span>
</div>
{{ form.csrf_token }}
</form>
</div>
{% endblock %}