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 send():
	"""Render sms sending page."""

	send_message_form = forms.SendMessageForm()

	if send_message_form.validate_on_submit():
		# check if there are any active senders first
		senders = sender_repo.get_all()
		if len(senders) == 0:
			# fail early if no senders
			flash('No senders available!', 'error')
			return redirect('/send')

		subscription_id = int(send_message_form.subscription.data)
		message_text = send_message_form.message_text.data

		# create Message entity
		new_message = message_repo.create_one(
			text=message_text,
			sent_at=datetime.utcnow(),
			subscription_id=subscription_id
		)

		twilio_dispatcher = TwilioDispatcher()
		failed = twilio_dispatcher.send_to_subscription(new_message)

		if len(failed) != 0:
			flash('Following numbers failed to be sent to:', 'error')
			for number, exception in failed.iteritems():
				flash('{num} : {reason}'.format(
					num=number,
					reason=exception
				), 'error')

		send_message_form.message_text.data = None
		send_message_form.subscription.data = None
		flash('Message sent!', 'success')

	messages = message_repo.get_sent_messages()

	return render_template(
		'send.html',
		messages=messages,
		send_message_form=send_message_form
	)
Exemplo n.º 3
0
def send_to_subscription_async(message_id, subscription_id, message_text):
    """Schedules a message to be sent at a later time by a Celery task."""
    senders = sender_repo.get_all()
    twilio_dispatcher = TwilioDispatcher()

    for sender in senders:

        numbers = number_repo.get_many_by_kwargs(
            subscription_id=subscription_id,
            sender_id=sender.id
        )

        for number in numbers:
            try:
                twilio_dispatcher.send_to_number(number.number, message_text)

            except (TwilioRestException, NotFoundError) as e:
                pass

    # update message entity's sent_at
    message_repo.update_by_id(message_id, sent_at=datetime.utcnow())
Exemplo n.º 4
0
def send():
    """Render sms sending page."""

    send_message_form = forms.SendMessageForm()

    if send_message_form.validate_on_submit():
        # check if there are any active senders first
        senders = sender_repo.get_all()
        if len(senders) == 0:
            # fail early if no senders
            flash('No senders available!', 'error')
            return redirect('/send')

        subscription_id = int(send_message_form.subscription.data)
        message_text = send_message_form.message_text.data

        # create Message entity
        new_message = message_repo.create_one(text=message_text,
                                              sent_at=datetime.utcnow(),
                                              subscription_id=subscription_id)

        twilio_dispatcher = TwilioDispatcher()
        failed = twilio_dispatcher.send_to_subscription(new_message)

        if len(failed) != 0:
            flash('Following numbers failed to be sent to:', 'error')
            for number, exception in failed.iteritems():
                flash('{num} : {reason}'.format(num=number, reason=exception),
                      'error')

        send_message_form.message_text.data = None
        send_message_form.subscription.data = None
        flash('Message sent!', 'success')

    messages = message_repo.get_sent_messages()

    return render_template('send.html',
                           messages=messages,
                           send_message_form=send_message_form)
Exemplo n.º 5
0
    def send_to_subscription(self, message):
        """Send one message to every number in a subscription."""
        senders = sender_repo.get_all()
        failed = {}

        for sender in senders:
            # get numbers associated with each sender
            # and has given subscription_id
            numbers = number_repo.get_many_by_kwargs(
                subscription_id=message.subscription_id,
                sender_id=sender.id
            )

            for number in numbers:
                try:
                    self.send_to_number(number.number, message.text)

                except (TwilioRestException, NotFoundError) as e:
                    failed[number.number] = str(e)
                    pass

        return failed
Exemplo n.º 6
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
	)