Example #1
0
    def index(self):
        if request.method == 'POST':
            sms = request.form['sms']
            group = request.form.getlist('group')
            msg = Message(content=sms, group=" ".join(group), info="Sending SMS")
            db.session.add(msg)
            db.session.commit()
            farmers = Farmer.query.all()
            groupNo, phone_no = groupFarmers(farmers, group)
            send_sms(text=sms, phone_no=phone_no, msg_id=msg.id)
            return redirect('/admin/message')

        farmers = Farmer.query.all()
        groupNo, phoneNos = groupFarmers(farmers)
        return self.render('admin/message.html', groupNo=groupNo)
Example #2
0
    def index(self):
        if request.method == 'POST':
            sms = request.form['sms']
            group = request.form.getlist('group')
            msg = Message(content=sms,
                          group=" ".join(group),
                          info="Sending SMS")
            db.session.add(msg)
            db.session.commit()
            farmers = Farmer.query.all()
            groupNo, phone_no = groupFarmers(farmers, group)
            send_sms(text=sms, phone_no=phone_no, msg_id=msg.id)
            return redirect('/admin/message')

        farmers = Farmer.query.all()
        groupNo, phoneNos = groupFarmers(farmers)
        return self.render('admin/message.html', groupNo=groupNo)
def process_partner_msgs(tw_client):
	positive_response_threshold = 2
	db, cur = util.get_db_conn()

	with open('fetch_positive_responses.sql', 'r') as queryfile:
		query=queryfile.read()

	cur.execute(query)
	results = cur.fetchall()
	logger.debug(positive_response_threshold)
	logger.debug(results)

	for res in results:
		partner_uuids_with_pos_response = res['partner_uuids_with_pos_response'].split(',')

		if len(partner_uuids_with_pos_response) >= positive_response_threshold:

			client_uuid, client_location_id = res['client_uuid'], res['location_id']
			first_msg_sent, last_msg_sent = res['first_msg_sent'], res['last_msg_sent']

			ids = ', '.join(map(lambda x: '%s', partner_uuids_with_pos_response))

			cur.execute("""
				SELECT phone_number
				FROM user
				LEFT JOIN location_user_association lua on lua.uuid = user.uuid
				WHERE lua.location_id = '%(client_location_id)s'
				AND user.type = 'partner'
				AND user.uuid not in (%(ids)s)
				""" % {'client_location_id': client_location_id, 'ids': ids}, [p for p in partner_uuids_with_pos_response])

			non_pos_respondent_numbers = [row['phone_number'] for row in cur.fetchall()]

			for num in non_pos_respondent_numbers:
				util.send_sms(tw_client, num, 'Client %s has received enough positive responses.' % client_uuid)

			cur.execute("""
				UPDATE message 
				SET response_threshold_hit_on = NOW() 
				WHERE sent_on 
				BETWEEN %s AND %s
				""", (first_msg_sent, last_msg_sent,))

	db.commit()
	cur.close()
Example #4
0
def farmer_registration(name=""):
    if request.method == 'POST':
        birthdate = request.form['birthdate']
        if birthdate:
            (day, month) = birthdate.split()
            birthdate = datetime.datetime(2000, month_map[month], int(day))
        else:
            birthdate = None
        name = request.form['name']
        village = request.form['village']
        area = int(request.form['area'])
        phone = request.form['phone']
        farmer = Farmer(name=name, village=village, area=area, \
                        phone=phone, birth_date=birthdate)
        db.session.add(farmer)
        db.session.commit()
        msg = Message.query.get(1)
        send_sms(text=msg.content, phone_no=[phone])
        return redirect('/u/transaction/%d' % farmer.id)
    farmers = Farmer.query.all()
    return render_template('registration.html', name=name, farmers=farmers)
Example #5
0
def farmer_registration(name=""):
    if request.method == 'POST':
        birthdate = request.form['birthdate']
        if birthdate:
            (day, month) = birthdate.split()
            birthdate = datetime.datetime(2000, month_map[month], int(day))
        else:
            birthdate = None
        name = request.form['name']
        village = request.form['village']
        area = int(request.form['area'])
        phone = request.form['phone']
        farmer = Farmer(name=name, village=village, area=area, \
                        phone=phone, birth_date=birthdate)
        db.session.add(farmer)
        db.session.commit()
        msg = Message.query.get(1)
        send_sms(text=msg.content, phone_no=[phone])
        return redirect('/u/transaction/%d' % farmer.id)
    farmers = Farmer.query.all()
    return render_template('registration.html', name=name, farmers=farmers)
def process_client_msgs(tw_client):
    db, cur = util.get_db_conn()

    with open('fetch_client_messages.sql', 'r') as queryfile:
        query = queryfile.read()

    cur.execute(query)
    new_msgs = cur.fetchall()

    forwarded_high_risk_msgs = {}
    for m in new_msgs:
        client_uuid = m['client_uuid']

        if not m['is_high_risk']:
            continue

        # TODO: truncate client message body to fit into max body char limit
        msg_template = 'Client {1} says: "{0}". Respond with "{1} omw" or "{1} noshow".'
        partner_numbers = m['partner_numbers'].split(',')
        forwarded_high_risk_msgs[m['id']] = [
            util.send_sms(tw_client, number,
                          msg_template.format(m['body'], client_uuid))
            for number in partner_numbers
        ]

    if len(new_msgs) == 0:
        return

    # TODO: check response codes on forwarded_high_risk_msgs
    # if there are errors, don't mark new messages as processed

    ids = ', '.join(map(lambda x: '%s', new_msgs))
    cur.execute(
        """
		UPDATE message
		SET processed_on = NOW()
		WHERE id in (%s)
		""" % ids, [m['id'] for m in new_msgs])

    db.commit()
    cur.close()