Exemplo n.º 1
0
def sender():
    """Render show senders/ add sender page."""

    add_sender_form = forms.AddSenderForm()

    if add_sender_form.validate_on_submit():
        number = utils.normalize_number(add_sender_form.sender_number.data)
        try:
            sender_repo.create_one(number=number)

            # check for numbers with no sender_id
            sender = sender_repo.get_min_sender()
            numbers = number_repo.get_many_by_kwargs(sender_id=None)
            for number in numbers:
                number.sender_id = sender.id
                db.session.add(number)
            db.session.commit()

            flash('Sender added!', 'success')
        except DuplicateError as e:
            flash('Sender already exists!', 'error')
            print e
            pass

        # reset form
        add_sender_form.sender_number.data = None

    senders = sender_repo.get_all()

    return render_template('sender.html',
                           senders=senders,
                           add_sender_form=add_sender_form)
Exemplo n.º 2
0
    def handle_response():
        # See: https://www.twilio.com/docs/sms/tutorials/how-to-receive-and-reply-python
        # Webhook for the user texting us / twilio hits this
        print("Request values", request.values)
        body = request.values.get('Body', None)
        phone_number = request.values.get('From')
        phone_number = normalize_number(phone_number)

        resp = MessagingResponse()

        if body.lower() == 'confirm':
            print("Looking for user with phone number {}".format(phone_number))
            user = DB.session.query(User).filter(
                User.phone_number == phone_number,
                User.confirmed == False).one_or_none()
            print("query result: {}".format(user))

            if user:
                user.confirmed = True
                DB.session.add(user)
                DB.session.commit()
                resp.message("Thank you! Your path to happiness begins NOW!")
                return str(resp)
            else:
                return ''

        else:
            # check if user confirmed
            # if so do the regular response flow: store in db, thank the user
            user = DB.session.query(User).filter(
                User.phone_number == phone_number,
                User.confirmed == True).one_or_none()
            print("found user: {}".format(user))
            if user:
                message = body.lower()
                try:
                    score = parse(message)
                    print("Scoring message {} as happiness level {}".format(
                        message, score))
                except Exception as e:
                    print("Error in parsing message: {}".format(e))
                    score = None

                response = UserResponse(user_id=user.id,
                                        raw=body.lower(),
                                        happiness=score)
                DB.session.add(response)
                DB.session.commit()

                response_text = respond_to(int(score))
                resp.message(
                    "{}\nThanks for sharing. Your response has been recorded.".
                    format(response_text))
            else:
                resp.message("Please sign up and confirm first.")
            return str(resp)
Exemplo n.º 3
0
def number():
	"""Render add number/ add by file page."""

	add_number_form = forms.AddNumberForm()
	upload_file_form = forms.UploadFileForm()

	if add_number_form.validate_on_submit():
		normalized_number = utils.normalize_number(add_number_form.number.data)
		try:
			number_repo.create_one(
				number=normalized_number,
				subscription_id=int(add_number_form.subscription.data)
			)
			flash('Number added!', 'success')
		except DuplicateError as e:
			print e
			flash('Number already exists!', 'error')
			pass

		# reset form
		add_number_form.number.data = None
		add_number_form.subscription.data = '1'

	if upload_file_form.validate_on_submit():
		f = upload_file_form.number_file.data
		subscription_id = int(upload_file_form.subscription.data)
		extension = os.path.splitext(f.filename)[1]

		if extension == '.csv':
			numbers = utils.read_from_csv_file(f)
		elif extension == '.txt':
			numbers = utils.read_from_txt_file(f)

		for num in numbers:
			try:
				number_repo.create_one(
					number=num,
					subscription_id=subscription_id
				)
			except DuplicateError as e:
				flash(
					'Number {number} already exists!'.format(number=e.number),
					'error'
				)
				print e
				pass

		flash('Numbers added!', 'success')

	return render_template(
		'number.html',
		add_number_form=add_number_form,
		upload_file_form=upload_file_form
	)
Exemplo n.º 4
0
def number():
    """Render add number/ add by file page."""

    add_number_form = forms.AddNumberForm()
    upload_file_form = forms.UploadFileForm()

    if add_number_form.validate_on_submit():
        normalized_number = utils.normalize_number(add_number_form.number.data)
        try:
            number_repo.create_one(number=normalized_number,
                                   subscription_id=int(
                                       add_number_form.subscription.data))
            flash('Number added!', 'success')
        except DuplicateError as e:
            print e
            flash('Number already exists!', 'error')
            pass

        # reset form
        add_number_form.number.data = None
        add_number_form.subscription.data = '1'

    if upload_file_form.validate_on_submit():
        f = upload_file_form.number_file.data
        subscription_id = int(upload_file_form.subscription.data)
        extension = os.path.splitext(f.filename)[1]

        if extension == '.csv':
            numbers = utils.read_from_csv_file(f)
        elif extension == '.txt':
            numbers = utils.read_from_txt_file(f)

        for num in numbers:
            try:
                number_repo.create_one(number=num,
                                       subscription_id=subscription_id)
            except DuplicateError as e:
                flash(
                    'Number {number} already exists!'.format(number=e.number),
                    'error')
                print e
                pass

        flash('Numbers added!', 'success')

    return render_template('number.html',
                           add_number_form=add_number_form,
                           upload_file_form=upload_file_form)
Exemplo n.º 5
0
def sender():
	"""Render show senders/ add sender page."""

	add_sender_form = forms.AddSenderForm()

	if add_sender_form.validate_on_submit():
		number = utils.normalize_number(add_sender_form.sender_number.data)
		try:
			sender_repo.create_one(
				number=number
			)

			# check for numbers with no sender_id
			sender = sender_repo.get_min_sender()
			numbers = number_repo.get_many_by_kwargs(
				sender_id=None
			)
			for number in numbers:
				number.sender_id = sender.id
				db.session.add(number)
			db.session.commit()

			flash('Sender added!', 'success')
		except DuplicateError as e:
			flash('Sender already exists!', 'error')
			print e
			pass

		# reset form
		add_sender_form.sender_number.data = None

	senders = sender_repo.get_all()

	return render_template(
		'sender.html',
		senders=senders,
		add_sender_form=add_sender_form
	)
Exemplo n.º 6
0
 def validate_phone_number(self, _, phone_number):
     return normalize_number(phone_number)
Exemplo n.º 7
0
def get_by_number(num):
    """Return Number entity with given number."""
    normalized_number = utils.normalize_number(num)
    number = models.Number.query.filter_by(number=normalized_number).first()
    return number