def notification():
    if request.method == 'POST':
        notification = Notification()
        notification.message = request.form['message']
        notification.subject = request.form['subject']
        notification.status = 'Notifications submitted'
        notification.submitted_date = datetime.utcnow()

        try:
            db.session.add(notification)
            db.session.commit()

            queue = QueueClient.from_connection_string(
                app.config.get('SERVICE_BUS_CONNECTION_STRING'),
                app.config.get('SERVICE_BUS_QUEUE_NAME'))
            logging.error('msg str: %s', Message(int(notification.id)))
            queue.send(Message('{}'.format(notification.id)))

            return redirect('/Notifications')

        except:
            logging.error('log unable to save notification')

    else:
        return render_template('notification.html')
Beispiel #2
0
def notification():
    if request.method == 'POST':
        notification = Notification()
        notification.message = request.form['message']
        notification.subject = request.form['subject']
        notification.status = 'Notifications submitted'
        notification.submitted_date = datetime.utcnow()
        logging.info("Record just created")

        try:

            db.session.add(notification)
            db.session.commit()

            ##################################################
            ## TODO: Refactor This logic into an Azure Function
            ## Code below will be replaced by a message queue
            #################################################

            notification_id = notification.id

            msg = Message(str(notification_id))
            queue_client.send(msg)

            #################################################
            ## END of TODO
            #################################################

            return redirect('/Notifications')
        except :
            logging.error('log unable to save notification')

    else:
        return render_template('notification.html')
Beispiel #3
0
def notification():
    if request.method == 'POST':
        notification = Notification()
        notification.message = request.form['message']
        notification.subject = request.form['subject']
        notification.status = 'Notifications submitted'
        notification.submitted_date = datetime.utcnow()

        try:
            db.session.add(notification)
            db.session.commit()

            notification_id = notification.id

            print('Enqueueing notification_id: {} to queue: {}'.format(
                notification_id, app.config.get('SERVICE_BUS_QUEUE_NAME')))

            sb_queue_client = QueueClient.from_connection_string(app.config.get('SERVICE_BUS_CONNECTION_STRING'), app.config.get('SERVICE_BUS_QUEUE_NAME'))

            msg = Message(str(notification_id))

            print('notification ID to send $s', str(notification_id))
            
            sb_queue_client.send(msg)   

            print('notification_id: {} enqueued to queue: {}'.format(
                notification_id, app.config.get('SERVICE_BUS_QUEUE_NAME')))

            return redirect('/Notifications')
        except :
            logging.error('log unable to save notification')

    else:
        return render_template('notification.html')
Beispiel #4
0
def notification():
    if request.method == 'POST':
        notification = Notification()
        notification.message = request.form['message']
        notification.subject = request.form['subject']
        notification.status = 'Notifications submitted'
        notification.submitted_date = datetime.utcnow()

        try:
            db.session.add(notification)
            db.session.commit()
            notification_id = notification.id
            msg = Message(str(notification_id))
            # Call servicebus queue_client to enqueue notification ID
            queue_client.send(msg)

            # attendees = Attendee.query.all()

            # for attendee in attendees:
            #     subject = '{}: {}'.format(attendee.first_name, notification.subject)
            #     send_email(attendee.email, subject, notification.message)

            # notification.completed_date = datetime.utcnow()
            # notification.status = 'Notified {} attendees'.format(len(attendees))
            # db.session.commit()

            return redirect('/Notifications')
        except Exception as e:
            logging.error(e)
            logging.error('log unable to save notification')
    else:
        return render_template('notification.html')
Beispiel #5
0
def notification():
    if request.method == 'POST':
        notification = Notification()
        notification.message = request.form['message']
        notification.subject = request.form['subject']
        notification.status = 'Notifications submitted'
        notification.submitted_date = datetime.utcnow()

        try:
            db.session.add(notification)
            db.session.commit()
            db.session.refresh(notification)
            notid = '{}'.format(notification.id)

            try:
                msg = Message(notid)
                sentResult = queue_client.send(msg)

                # logging.info(msg)

                print(msg)
            except Exception as e:
                logging.error(
                    'Error occurred while sending message to queue {}', str(e))

            return redirect('/Notifications')
        except:
            logging.error('Unable to save notification')

    else:
        return render_template('notification.html')
def notification():
    if request.method == 'POST':
        notification = Notification()
        notification.message = request.form['message']
        notification.subject = request.form['subject']
        notification.status = 'Notifications submitted'
        notification.submitted_date = datetime.utcnow()
        try:
            db.session.add(notification)
            db.session.commit()

            ##################################################
            ## Refactor This logic into an Azure Function
            ## Code below will be replaced by a message queue
            #################################################
            # Call servicebus queue_client to enqueue notification ID
            db.session.flush()
            msg = Message(notification.id)
            queue_client.send(msg)

            #################################################
            ## END of TODO
            #################################################

            return redirect('/Notifications')
        except:
            logging.error('log unable to save notification')

    else:
        return render_template('notification.html')
Beispiel #7
0
def notification():
    if request.method == 'POST':
        notification = Notification()
        notification.message = request.form['message']
        notification.subject = request.form['subject']
        notification.status = 'Notifications submitted'
        notification.submitted_date = datetime.utcnow()

        try:
            db.session.add(notification)
            db.session.commit()

            print('Notification added with message: {} and text: {}'.format(
                request.form['message'], request.form['subject']))

            # Call servicebus queue_client to enqueue notification ID
            # create queue client
            notification_id = str(notification.id)
            print('Adding notification_id: {} to queue: {}'.format(
                notification_id, app.config.get('SERVICE_BUS_QUEUE_NAME')))
            print('Queue client is {}'.format(queue_client))
            msg = Message(notification_id)
            queue_client.send(msg)

            return redirect('/Notifications')
        except:
            logging.error('log unable to save notification')

    else:
        return render_template('notification.html')
Beispiel #8
0
def notification():
    if request.method == 'POST':
        notification = Notification()
        notification.message = request.form['message']
        notification.subject = request.form['subject']
        notification.status = 'Notifications submitted'
        notification.submitted_date = datetime.utcnow()

        try:
            db.session.add(notification)
            db.session.commit()

            print("notification")
            print(notification.id)
            print(notification.message)

            CONNECTION_STR = app.config.get('SERVICE_BUS_CONNECTION_STRING')
            print("CONNECTION STRING --> : " + CONNECTION_STR)

            QUEUE_NAME = app.config.get('SERVICE_BUS_QUEUE_NAME')
            print("SERVICE_BUS_QUEUE_NAME --> : " + QUEUE_NAME)

            sb_client = ServiceBusClient.from_connection_string(CONNECTION_STR)
            queue_client = sb_client.get_queue(QUEUE_NAME)

            message = Message(str(notification.id))
            queue_client.send(message)

            return redirect('/Notifications')

        except:
            logging.error('log unable to save notification')

    else:
        return render_template('notification.html')
Beispiel #9
0
def notification():
    if request.method == 'POST':
        notification = Notification()
        notification.message = request.form['message']
        notification.subject = request.form['subject']
        notification.status = 'Notifications submitted'
        notification.submitted_date = datetime.utcnow()

        try:
            db.session.add(notification)
            db.session.commit()

            print('New notification Inserted successfully with message: {} and subject: {}'.format(
                request.form['message'], request.form['subject']
            ))

            # get id of inserted notification
            notification_id = notification.id
            print('Enqueueing notification_id: {} to queue: {}'.format(
                notification_id, app.config.get('SERVICE_BUS_QUEUE_NAME')))

            # Create the QueueClient
            sb_queue_client = QueueClient.from_connection_string(
                app.config.get('SERVICE_BUS_CONNECTION_STRING'), app.config.get('SERVICE_BUS_QUEUE_NAME')
            )

            # Enqueue notification_id to the service bus queue
            msg = Message(str(notification_id))
            sb_queue_client.send(msg)
            print('notification_id: {} enqueued to queue: {}'.format(
                notification_id, app.config.get('SERVICE_BUS_QUEUE_NAME')))

            '''
            ##################################################
            ## TODO: Refactor This logic into an Azure Function
            ## Code below will be replaced by a message queue
            #################################################
            attendees = Attendee.query.all()

            for attendee in attendees:
                subject = '{}: {}'.format(attendee.first_name, notification.subject)
                send_email(attendee.email, subject, notification.message)

            notification.completed_date = datetime.utcnow()
            notification.status = 'Notified {} attendees'.format(len(attendees))
            db.session.commit()
            # TODO Call servicebus queue_client to enqueue notification ID

            #################################################
            ## END of TODO
            #################################################
            '''

            return redirect('/Notifications')
        except :
            logging.error('log unable to save notification')

    else:
        return render_template('notification.html')
Beispiel #10
0
def notification():
    if request.method == 'POST':
        notification = Notification()
        notification.message = request.form['message']
        notification.subject = request.form['subject']
        notification.status = 'Notifications submitted'
        notification.submitted_date = datetime.utcnow()

        try:
            notification.completed_date = datetime.utcnow()
            notification.status = 'Notification submitted'
            db.session.add(notification)
            db.session.commit()

            ##################################################
            ## TODO: Refactor This logic into an Azure Function
            ## Code below will be replaced by a message queue
            #################################################
            # attendees = Attendee.query.all()
            #
            # for attendee in attendees:
            #     subject = '{}: {}'.format(attendee.first_name, notification.subject)
            #     send_email(attendee.email, subject, notification.message)

            # notification.completed_date = datetime.utcnow()
            # notification.status = 'Notification submitted'
            # db.session.commit()
            # TODO Call servicebus queue_client to enqueue notification ID
            notification_id = Notification.query.order_by(
                Notification.id.desc()).first()
            msg = Message(notification_id)
            queue_client.send(msg)

            #################################################
            ## END of TODO
            #################################################

            return redirect('/Notifications')
        except:
            logging.error('log unable to save notification')

    else:
        return render_template('notification.html')
def notification():
    if request.method == 'POST':
        notification = Notification()
        notification.message = request.form['message']
        notification.subject = request.form['subject']
        notification.status = 'Notifications submitted'
        notification.submitted_date = datetime.utcnow()

        try:
            db.session.add(notification)
            db.session.commit()

            ##################################################
            # TODO: Refactor This logic into an Azure Function
            # Code below will be replaced by a message queue
            #################################################
            # attendees = Attendee.query.all()

            # for attendee in attendees:
            #     subject = '{}: {}'.format(attendee.first_name, notification.subject)
            #     send_email(attendee.email, subject, notification.message)

            # notification.completed_date = datetime.utcnow()
            # notification.status = 'Notified {} attendees'.format(len(attendees))
            # db.session.commit()
            # TODO Call servicebus queue_client to enqueue notification ID
            notification_id = notification.id
            servicebus_client = ServiceBusClient.from_connection_string(
                conn_str=app.config.get("SERVICE_BUS_CONNECTION_STRING"),
                logging_enable=True)

            with servicebus_client:
                sender = servicebus_client.get_queue_sender(
                    queue_name=app.config.get("SERVICE_BUS_QUEUE_NAME"))
                with sender:
                    message = ServiceBusMessage(str(notification_id))
                    sender.send_messages(message)
                    logging.info("MSG Queue sent")

            #################################################
            # END of TODO
            #################################################

            return redirect('/Notifications')
        except:
            logging.error('log unable to save notification')

    else:
        return render_template('notification.html')
Beispiel #12
0
def notification():
    if request.method == 'POST':
        notification = Notification()
        notification.message = request.form['message']
        notification.subject = request.form['subject']
        notification.status = 'Notifications submitted'
        notification.submitted_date = datetime.utcnow()

        try:
            db.session.add(notification)
            db.session.commit()

            queue_client.send(Message('{}'.format(notification.id)))

            return redirect('/Notifications')
        except:
            logging.error('log unable to save notification')

    else:
        return render_template('notification.html')
Beispiel #13
0
def notification():
    if request.method == 'POST':
        notification = Notification()
        notification.message = request.form['message']
        notification.subject = request.form['subject']
        notification.status = 'Notifications submitted'
        notification.submitted_date = datetime.utcnow()

        try:
            db.session.add(notification)
            db.session.commit()

            ##################################################
            ## TODO: Refactor This logic into an Azure Function
            ## Code below will be replaced by a message queue
            #################################################
            queue_client = QueueClient.from_connection_string(
                app.config.get("SERVICE_BUS_CONNECTION_STRING"),
                app.config.get("SERVICE_BUS_QUEUE_NAME"))
            msg = Message(f'{notification.id}'.encode())

            logging.info(queue_client)
            logging.info(msg)

            # TODO Call servicebus queue_client to enqueue notification ID
            queue_client.send(msg)

            #################################################
            ## END of TODO
            #################################################

            return redirect('/Notifications')
        except:
            logging.error('log unable to save notification')

    else:
        return render_template('notification.html')