You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You must provide your Gmail credentials (``MAIL_USERNAME``and ``MAIL_PASSWORD``) to enable your application to send emails. For security purposes, we recommend that you generate an app password to use, rather than using your primary password. For more information, see the `App Password settings <https://myaccount.google.com/apppasswords>`__ in your Google Account.
153
+
You must provide your Gmail credentials and email (``MAIL_USERNAME``, ``MAIL_PASSWORD``, and ``MAIL_DEFAULT_SENDER``) to enable your application to send emails. For security purposes, we recommend that you generate an app password to use, rather than using your primary password. For more information, see the `App Password settings <https://myaccount.google.com/apppasswords>`__ in your Google Account.
153
154
154
-
You must also create a connection string to set into the ``MONGO_URI`` environment variable. For more information see the :ref:`Create a Connection String <pymongo-get-started-connection-string>` section of this guide.
155
+
You must also provide a connection string to set into the ``MONGO_URI`` environment variable. For more information see the :ref:`Create a Connection String <pymongo-get-started-connection-string>` section of this guide.
155
156
156
157
The provided Celery broker URL (``CELERY_BROKER_URL``) specifies RabbitMQ as its broker, but you can customize this URL to support other implementations. For more information, see the `Broker Settings <https://docs.celeryq.dev/en/stable/userguide/configuration.html#broker-settings>`__ section of the Celery documentation.
157
158
159
+
The ``ALLOWED_IPS`` list is used to control access to the :guilabel:`Send Newsletter` page. The rest of the variables configure the Flask and Celery components.
160
+
158
161
Initialize Flask, MongoDB, and Celery
159
162
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
160
163
@@ -241,11 +244,11 @@ Define your ``send_emails()`` method by adding the following code to your ``task
241
244
Define Your Routes
242
245
~~~~~~~~~~~~~~~~~~
243
246
244
-
In Flask, the ``@app.route()`` decorator assigns a URL path to a specific function. In the following code, it is used to define the root (``/``), ``/admin``, ``/subscribe``, and ``/send-newsletter`` routes. The optional ``methods`` parameter is used in some cases to define a list of allowable HTTP methods.
247
+
In Flask, the ``@app.route()`` decorator assigns a URL path to a specific function. In the following code, it is used to define the root (``/``), ``/admin``, ``/subscribe``, and ``/send-newsletters`` routes. The optional ``methods`` parameter is used in some cases to define a list of allowable HTTP methods.
245
248
246
249
The ``@app.before_request()`` method sets a function to run before every request. In this case, the function provides some basic security by limiting access to the ``admin`` page to IP addresses listed in the ``ALLOWED_IPS`` parameter defined in the ``config.py`` file. Specifically, access is only allowed for the ``localhost``.
247
250
248
-
The root and ``/admin`` routes render pages using the ``render_template()`` method. The ``/subscribe`` and ``/send-newsletter`` routes access request parameters in ``request.form[]`` to execute commands, and then return HTTP responses.
251
+
The root and ``/admin`` routes render pages using the ``render_template()`` method. The ``/subscribe`` and ``/send-newsletters`` routes access request parameters in ``request.form[]`` to execute commands, and then return HTTP responses.
249
252
250
253
Define your routes by adding the following code to your ``routes.py`` file:
251
254
@@ -313,7 +316,7 @@ Create Your Pages
313
316
314
317
The HTML files in the ``templates`` directory define the user interface, and are written using standard HTML. Since this application uses asynchronous HTTP requests, the scripts in these files use :wikipedia:`Fetch API calls <XMLHttpRequest#Fetch_alternative>`. These scripts also handle timeouts and errors.
315
318
316
-
Subscribe Pages
319
+
Subscribe Page
317
320
```````````````
318
321
319
322
Copy the following code into your ``subscribe.html`` file to create your :guilabel:`Subscribe to Newsletter` page.
@@ -516,7 +519,7 @@ You can apply a style sheet to your templates by adding the following code to th
516
519
color: #666;
517
520
}
518
521
519
-
Testing the Platform
522
+
Test the Application
520
523
~~~~~~~~~~~~~~~~~~~~
521
524
522
525
After you complete the previous steps, you have a working application that uses MongoDB, Flask, and Celery to manage a newsletter platform.
@@ -532,6 +535,24 @@ You can use the following steps to test your application:
532
535
documentation <https://www.rabbitmq.com/docs/platforms>`__ for your
533
536
operating system.
534
537
538
+
On MacOS:
539
+
540
+
.. code-block:: bash
541
+
542
+
brew services start rabbitmq
543
+
544
+
On Windows:
545
+
546
+
.. code-block:: bash
547
+
548
+
rabbitmq-service start
549
+
550
+
On Linux/Unix:
551
+
552
+
.. code-block:: bash
553
+
554
+
sudo systemctl start rabbitmq-server
555
+
535
556
.. step:: Start your application
536
557
537
558
Use the following code to start your application:
@@ -548,7 +569,7 @@ You can use the following steps to test your application:
548
569
549
570
.. step:: Create a subscriber
550
571
551
-
Navigate to ``localhost:5000`` in your browser to open the
572
+
Navigate to `<localhost:5000>`__ in your browser to open the
552
573
:guilabel:`Subscribe to our Newsletter` page.
553
574
554
575
Enter the subscriber information and click :guilabel:`Subscribe`.
@@ -559,7 +580,7 @@ You can use the following steps to test your application:
559
580
560
581
.. step:: Dispatch a newsletter
561
582
562
-
Navigate to ``localhost:5000/admin`` in your browser to open the
583
+
Navigate to `<localhost:5000/admin>`__ in your browser to open the
563
584
:guilabel:`Send Newsletter` page. Enter the newsletter details and click
564
585
:guilabel:`Send`.
565
586
@@ -579,16 +600,12 @@ You can use the following steps to test your application:
579
600
Next Steps
580
601
~~~~~~~~~~
581
602
582
-
This application demonstrates how to integrate with the Celery task queue to
583
-
manage subscriber data, and send batch emails. You can further enhance this
584
-
platform by integrating analytics, customizing email templates, and implementing
585
-
automated responses.
603
+
This application demonstrates how to integrate a Flask application with the Celery task queue to manage subscriber data, and send batch emails. You can further enhance this platform by integrating analytics, customizing email templates, and implementing automated responses.
586
604
587
605
More Resources
588
606
--------------
589
607
590
-
For more information about the components used in this tutorial, see the following
591
-
resources:
608
+
For more information about the components used in this tutorial, see the following resources:
592
609
593
610
- `Flask <https://flask.palletsprojects.com>`__
594
611
- `Flask Mail <https://pypi.org/project/Flask-Mail/#files>`__
0 commit comments