jeudi 24 septembre 2015

Flask - Submit Button Actioning Database Update

I think I may have been at it too long today and I can't wrap my head around this very well. I am fairly new to python and have been playing with flask for about a week, this is my first 'real' app that I have built so please be gentle.

The app allows an internal customer (datatechs) to enter their ticket number and login into a form. That form is submitted and kept in a sqlite DB for now, with an ID, ticket#, login, create_time and an active flag.

On another page I have what could be called a management list, this renders all the tickets that are currently in the database and marked as active. I have populated this with a jinja2 for loop and have a button that resides inline.

For example:

[ 1 ]  [ ticket number ]  [ login ]  [ button ]
[ 2 ]  [ ticket number ]  [ login ]  [ button ]

The button is my issue. I am very unsure about how I can have the specific button clicked cause an action against that entry in the database. I am looking to flip the status field from an int of 1 to 0 (true and false works too), so these can be essentially marked as complete but still available for analytics.

Thanks for reading, here are some snippets.

views.py

@app.route('/', methods=['GET', 'POST'])
def index():
    form = TicketForm()
    ticket = Ticket.query.filter_by(status=1)    

    if request.method == 'GET':
        return render_template('index.html', form=form, ticket=ticket)    

    elif request.method == 'POST':
        if form.validate() == False:
            flash('All fields are required, please update and try again.')
            return render_template('index.html', form=form, ticket=ticket)
        else:
            remedy = Ticket(request.form['tt'], request.form['login'], create=datetime.datetime.utcnow(), status=1)
            db.session.add(remedy)
            db.session.commit()
            flash('Your ticket has been added to the queue')
            return redirect(url_for('index'))    


@app.route('/manage', methods=['GET'])
def manage():
    if request.method == 'GET':
        ticket = Ticket.query.all()
        return render_template('manage.html', ticket=ticket)

models.py

class Ticket(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    tt = db.Column(db.String(10))
    user = db.Column(db.String(20))
    create = db.Column(DateTime, default=datetime.datetime.utcnow)
    status = db.Column(db.Integer)    

    def __init__(self, tt, user, create, status):
        self.tt = tt
        self.user = user
        self.create = create
        self.status = status

manage.html

{% extends "base.html" %}    

{% block content %}
<table class="table" width=50%>
  <thead>
     <tr>
        <th>#</th>
        <th>Ticket Number</th>
        <th>Requester Login</th>
        <th>Time Requested (UTC)</th>
        <th>Cancel Request</td>
     </tr>
  </thead>
  <tbody>
{% for ticket in ticket %}
     <tr>
        <td>{{ ticket.id }}</td>
        <td>{{ ticket.tt }}</td>
        <td>{{ ticket.user }}</td>
        <td>{{ ticket.create }}</td>
        <td><button type="button" class="btn btn-xs btn-danger">Kill</button></td>
     </tr>
{% endfor %}
  </tbody>
  </table>    

{% endblock %}

Aucun commentaire:

Enregistrer un commentaire