示例#1
0
# Define the hubs for specific types of event
user_hub = notifications.create_hub("UserHub")
system_hub = notifications.create_hub("EventHub")
user_hub_id = user_hub.hub_id
system_hub_id = system_hub.hub_id


# Add a new consumer to the user_hub
@user_hub.register_consumer(name="app.write_to_file")
def write_to_file(event, *args, **kwargs):
    with open("events.log", "a+w") as f:
        f.write(str(event))


# Register manually a push consumer
backend = notifications.create_backend()
push_consumer = PushConsumer(backend, user_hub_id)
user_hub.register_consumer(push_consumer)

# Create two independent email consumers
mail_consumer = FlaskMailConsumer.from_app(
    app, default_email_account, [default_email_account]
)

email_consumer = FlaskEmailConsumer.from_app(
    app, default_email_account, [default_email_account]
)

# Register one or more predefined consumers
for consumer in (mail_consumer, email_consumer):
    system_hub.register_consumer(consumer)
示例#2
0
# Define the hubs for specific types of event
user_hub = notifications.create_hub("UserHub")
system_hub = notifications.create_hub("EventHub")
user_hub_id = user_hub.hub_id
system_hub_id = system_hub.hub_id


# Add a new consumer to the user_hub
@user_hub.register_consumer(name="app.write_to_file")
def write_to_file(event, *args, **kwargs):
    with open("events.log", "a+w") as f:
        f.write(str(event))


# Register manually a push consumer
backend = notifications.create_backend()
push_consumer = PushConsumer(backend, user_hub_id)
user_hub.register_consumer(push_consumer)

# Create two independent email consumers
mail_consumer = FlaskMailConsumer.from_app(app, default_email_account,
                                           [default_email_account])

email_consumer = FlaskEmailConsumer.from_app(app, default_email_account,
                                             [default_email_account])

# Register one or more predefined consumers
for consumer in (mail_consumer, email_consumer):
    system_hub.register_consumer(consumer)

# Register filters for the hubs
class NotificationsFlaskTestCase(unittest.TestCase):

    """Base test class for Flask-Notifications."""

    def setUp(self):
        """Set up the environment before the tests."""
        self.app = Flask(__name__)
        self.test_app = self.app.test_client()

        self.config = {
            "DEBUG": True,
            "TESTING": True,
            "CELERY_BROKER_URL": "redis://*****:*****@gmail.com"

        # Time variables
        self.tomorrow = datetime.now() + timedelta(days=1)
        self.next_to_tomorrow = datetime.now() + timedelta(days=2)
        self.next_to_next_to_tomorrow = datetime.now() + timedelta(days=3)
        self.next_to_tomorrow_tm = float(self.next_to_tomorrow.strftime("%s"))
        self.next_to_next_to_tomorrow_tm = \
            float(self.next_to_next_to_tomorrow.strftime("%s"))

        # Create basic event to use in the tests, id randomized
        self.event = Event("1234",
                           event_type="user",
                           title="This is a test",
                           body="This is the body of a test",
                           sender="system",
                           recipients=["jvican"],
                           expiration_datetime=self.tomorrow)
        self.event_json = self.event.to_json()

    def tearDown(self):
        """Destroy environment."""
        self.app = None
        self.celery = None
        self.redis = None