class Persons(db.Model):
__tablename__ = 'persons'
id = db.Column( db.Integer, primary_key=True)
pname = db.Column(db.String(80), nullable=False)
_phone_number = db.Column(db.String(80))
phone_country_code = db.Column(db.String(20))
income = db.Column(db.Integer)
expenditure = db.Column(db.Integer)
I have performed all the tasks as services provided by a web application. Flask is the framework used.
One of our clients wanted to search for slangs (in local language) for an answer to a text question
on the basis of cities (which was the answer to a different MCQ question).
- Since this is an mcq questions answer will be pre-defined , so common slangs will get noted upon being selected by a user.
A market research agency wanted to validate responses coming in against a set of business rules
(eg. monthly savings cannot be more than monthly income) and send the response back to the data collector
to fix it when the rules generate a flag.
There can be two approached :
1 . Checking during insertion: After the data gets collected from html form, at server side simple logics can validate the incoming data and on wrong data it can notify.
@app.route("/personadd", methods=['POST'])
def personadd():
errors = {}
pname=request.form["name"]
_phone_number = request.form["_phone_number"]
phone_country_code = request.form["phone_country_code"]
income = request.form["income"]
expenditure = request.form["expenditure"]
#add income and expenditure as well and check this at server side
#phonenumber validation at client side
if int(income)<int(expenditure):
errors["err"] = ["The expenditure can't be greater then income!"]
return render_template("base.html" , errors = errors)
- Checking after data is collected : We can loop over the data in database and false data can be reported accordingly.
@app.route('/mistakes')
def mistakes():
result = db.session.query(Persons).filter()
person = {"details":[]}
for row in result:
if(row.income<row.expenditure):
person["details"].append( "{} has incorerect entry income={} and expenditure={}".format(row.pname , row.income, row.expenditure))
return render_template("index.html" , person = person)
A very common need for organizations is wanting all their data onto Google Sheets, wherein they could
connect their CRM, and also generate graphs and charts offered by Sheets out of the box. In such cases,
each response to the form becomes a row in the sheet, and questions in the form become columns.
Looping over the data from the database we can xlwt workbook object and append row wise data to it. At the end simply generate and download an excel file.
@app.route('/download/report/excel')
def download_report():
result = db.session.query(Persons).filter()
#output in bytes
output = io.BytesIO()
#create WorkBook object
workbook = xlwt.Workbook()
#add a sheet
sh = workbook.add_sheet('Student Report')
#add headers
sh.write(0, 0, 'id')
sh.write(0, 1, 'pname')
sh.write(0, 2, '_phone_number')
sh.write(0, 3, 'phone_country_code')
sh.write(0, 4, 'income')
sh.write(0, 5, 'expenditure')
idx = 0
for row in result:
sh.write(idx+1, 0, row.id)
sh.write(idx+1, 1, row.pname)
sh.write(idx+1, 2, row._phone_number)
sh.write(idx+1, 3, row.phone_country_code)
sh.write(idx+1, 4, row.income)
sh.write(idx+1, 5, row.expenditure)
idx += 1
workbook.save(output)
output.seek(0)
return Response(output, mimetype="application/ms-excel", headers={"Content-Disposition":"attachment;filename=student_report.xls"})
A recent client partner wanted us to send an SMS to the customer whose details are
collected in the response as soon as the ingestion was complete reliably. The content
of the SMS consists of details of the customer, which were a part of the answers in
the response. This customer was supposed to use this as a βreceiptβ for them having
participated in the exercise
For this i have used twilio api. So as soon as the form gets submitted, one sms is sent to the user on successfull submissions containing filled details by him to his/her phone number. Here the free/trial version only sends sms to owner's personal number.
#Twilio Config
account_sid = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
auth_token = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
client = Client(account_sid, auth_token)
message = client.messages.create(
body='Hello {} This is a sample message !! It means you have succefully registerd'.format(pname),
from_='+16075363224',
to=phone_country_code+_phone_number
)
xlwt , Twilio , flask , flask_sqlalchemy , io , os
Licensed under the MIT License.
Thanks to all the smart people at Atlan for reviewing my project.