3
\$\begingroup\$

This code is built in flask, it allows a user to edit a title and a description of a div.

I have used the X-editable library which allows a user to edit text and use ajax requests.

I also process the value of the edited text and using python insert it into a mysql database.

The code works fine it is editing and saving to the db correctly. My question is my code set up correctly can or should I enhance it (considering the jQuery code)?

Also should I be performing more validation checks (i.e. should I be checking for http 200 repsonses within the python or jQuery?) or are my try/except blocks fine for this purpose.

Here is my html;

  <div class="container">
    {% for x in data %}
    <div class="row">
        <div class="col">
            <ul class="list-group">
                <li class="list-group-item">
                    <a href="#" class="pri_title" data-type="text" data-pk="{{ x.pri_id }}" data-url="/editTitle" data-title="Edit Title">{{ x.pri_title }}</a>
                </li>
                <li class="list-group-item">
                    <a href="#" class="pri_desc" data-type="text" data-pk="{{ x.pri_id }}" data-url="/editDesc" data-title="Edit Description">{{ x.pri_desc }}</a>
                </li>
                <li class="list-group-item"><p>{{ x.pri_link }}</p></li>
                <li class="list-group-item"><p>{{ x.pri_img_link }}</p></li>
                <li class="list-group-item"><p>{{ x.pri_source }}</p></li>
            </ul>
        </div>
    </div>
    {% endfor %}
</div>

Here is my jQuery;

$.fn.editable.defaults.mode = 'inline'; 

$(document).ready(function() { 
  $('.pri_title').editable(); 
  $('.pri_desc').editable(); 
});

and finally here is my python-flask code;

@app.route('/editTitle',methods=['GET','POST'])
def editTitle():
  if request.method == 'POST':
    updatedTitle = request.form['value']
    priId = request.form['pk']
    try:
        sql = "UPDATE pri_table SET pri_title = %s WHERE pri_id = %s"
        dbDets[1].execute(sql,(updatedTitle,priId),)
        dbDets[0].commit()
    except Exception as e:
        print("Error: "  + str(e))
  return redirect('/priority')

@app.route('/editDesc',methods=['GET','POST'])
def editDesc():
  if request.method == 'POST':
    updatedDesc = request.form['value']
    priId = request.form['pk']
    try:
        sql = "UPDATE pri_table SET pri_desc = %s WHERE pri_id = %s"
        dbDets[1].execute(sql,(updatedDesc,priId),)
        dbDets[0].commit()
    except Exception as e:
        print("Error: "  + str(e))
  return redirect('/priority')
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.