def altercontact(contact_id): if (request.method == 'PUT'): firstname = request.form.get('firstname') lastname = request.form.get('lastname') phone_no = request.form.get('phone_no') contact_id = contact_id cur.execute( "update contacts SET firstname = %s , lastname = %s , phone_no = %s WHERE contact_id = %s", (firstname, lastname, phone_no, contact_id)) conn.commit() return jsonify({'Message': 'Contact Updated Successfully'}), 200 elif (request.method == 'DELETE'): contact_id = contact_id cur.execute("DELETE FROM contacts WHERE contact_id = " + (contact_id)) conn.commit() return jsonify({'Message': 'Contact Deleted Successfully'}), 200 elif (request.method == 'GET'): contact_id = contact_id sql = "select * from contacts where contact_id = " + contact_id contacts = get_dict_resultset(sql) return jsonify({ 'Message': 'Contact Retreived Successfully', 'Contact_list': contacts }), 200 else: return jsonify({'Message': 'Method Not Allowed'}), 405
def list_option(): sql = "select * from contacts" ans = get_dict_resultset(sql) print '#######################################' print 'Contacts Retrieved succesfully' print '#######################################' print ans print '#######################################' calling_loop = looper()
def contacts(contact_id=None, method=None): contact_id = contact_id or '' method = method or '' if (contact_id == ''): if (request.method == 'GET'): sql = "select * from contacts" contacts = get_dict_resultset(sql) return render_template('contacts.html', contacts=contacts) elif (request.method == 'POST'): firstname = request.form['firstname'] lastname = request.form['lastname'] phone_no = request.form['phone_no'] cur.execute( "insert into contacts (firstname,lastname,phone_no) values(%s,%s,%s)", (firstname, lastname, phone_no)) conn.commit() return redirect(url_for('contacts')) else: return redirect(url_for('contacts')) else: if (method == 'edit'): sql = "select * from contacts" contacts = get_dict_resultset(sql) for i in contacts: if int(contact_id) == int(i['contact_id']): contact = i return render_template('contacts.html', contacts=contacts, contact=contact) elif (method == 'delete'): cur.execute("DELETE FROM contacts WHERE contact_id = " + contact_id) conn.commit() return redirect(url_for('contacts')) else: return redirect(url_for('contacts')) return redirect(url_for('contacts'))
def createcontact(): if (request.method == 'POST'): firstname = request.form.get('firstname') lastname = request.form.get('lastname') phone_no = request.form.get('phone_no') cur.execute( "insert into contacts (firstname,lastname,phone_no) values(%s,%s,%s)", (firstname, lastname, phone_no)) conn.commit() return jsonify({'Message': 'Contact Added Successfully'}), 200 elif (request.method == 'GET'): sql = "select * from contacts" contacts = get_dict_resultset(sql) return jsonify({ 'Message': 'Contacts Retreived Successfully', 'Contact_list': contacts }), 200 else: return jsonify({'Message': 'Method Not Allowed'}), 405
def export_data(): sql = "select firstname,lastname,phone_no from contacts" contacts = get_dict_resultset(sql) df = pd.DataFrame(contacts) df.to_excel(r'exportdbdata.xlsx', index=False, header=True) return redirect(url_for('contacts'))