89
89
90
90
- ``app.py``: The main entry point for your Flask application
91
91
- ``config.py``: Configuration settings for your application, including
92
- MongoDB connection details , mail server configuration, Celery broker
92
+ the MongoDB connection URI , mail server configuration, Celery broker
93
93
connection, and any other environment-specific variables
94
94
- ``tasks.py``: Defines background tasks to send emails asynchronously
95
95
- ``routes.py``: Defines the routes (URLs) that your application responds
@@ -115,11 +115,11 @@ Setup
115
115
116
116
.. step:: Install the required Python packages
117
117
118
- Your application depends on the following libraries:
118
+ Your application uses the following libraries:
119
119
120
120
- `Flask <https://flask.palletsprojects.com/en/stable/>`__ for handling
121
121
the web server and routing
122
- - `Flask Mail <https://pypi.org/project/Flask-Mail/>`__ for sending emails
122
+ - `Flask- Mail <https://pypi.org/project/Flask-Mail/>`__ for sending emails
123
123
from your application
124
124
- :ref:`{+driver-short+} <pymongo-get-started-download-and-install>`
125
125
- `Celery <https://docs.celeryq.dev/en/stable/>`__ to manage tasks, such
@@ -142,12 +142,12 @@ Setup
142
142
Configure Your Application
143
143
~~~~~~~~~~~~~~~~~~~~~~~~~~
144
144
145
- The ``config.py`` file contains the settings and credentials to perform the
145
+ The ``config.py`` file contains settings and credentials to perform the
146
146
following actions:
147
147
148
148
- Connect Celery to RabbitMQ as its message broker
149
149
- Configure Flask-Mail to use Gmail as its SMTP server
150
- - Connect your application to your MongoDB server
150
+ - Connect your application to your MongoDB deployment
151
151
152
152
Define the necessary configurations by adding the following code to your
153
153
``config.py`` file:
@@ -175,8 +175,8 @@ password to use, rather than using your primary password. For more information,
175
175
see the `App Password settings <https://myaccount.google.com/apppasswords>`__ in
176
176
your Google Account.
177
177
178
- You must also provide a connection string to set into the ``MONGO_URI``
179
- environment variable. For more information see the :ref:`Create a Connection
178
+ You must also provide a connection string to set as the ``MONGO_URI``
179
+ environment variable. For more information, see the :ref:`Create a Connection
180
180
String <pymongo-get-started-connection-string>` section of this guide.
181
181
182
182
The provided Celery broker URL (``CELERY_BROKER_URL``) specifies RabbitMQ as its
@@ -192,7 +192,7 @@ components.
192
192
Initialize Flask, MongoDB, and Celery
193
193
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
194
194
195
- The ``app.py`` file initializes and configures the core components of the Flask
195
+ The ``app.py`` file initializes and configures the core components of your
196
196
application. It performs the following tasks:
197
197
198
198
- Creates a Flask application and loads configuration constants
@@ -251,15 +251,15 @@ app.app_context()`` block. This gives Flask access to other components like the
251
251
Flask-Mail ``mail`` instance and the {+driver-short+} connection to your
252
252
``newsletter`` MongoDB database.
253
253
254
- This method loops through the list of ``subscribers``, creates an email using
254
+ This function loops through the list of ``subscribers``, creates an email using
255
255
the Flask-Mail ``Message`` class, and then sends it to each user by using the
256
256
``mail`` object. After each email is sent, it logs the delivery by inserting a
257
257
document into your MongoDB ``deliveries`` collection to record that the message
258
- was sent. Each email operation is wrapped in a ``try`` block to ensure that in
258
+ was sent. Each email operation is wrapped in a ``try`` block to ensure that, in
259
259
the case of an error, the failure is logged and the database is not updated with
260
260
a false delivery record.
261
261
262
- Define your ``send_emails()`` method by adding the following code to your
262
+ Define your ``send_emails()`` function by adding the following code to your
263
263
``tasks.py`` file:
264
264
265
265
.. code-block:: python
@@ -297,10 +297,10 @@ Define Your Routes
297
297
In Flask, the ``@app.route()`` decorator assigns a URL path to a specific
298
298
function. In the following code, it is used to define the root (``/``),
299
299
``/admin``, ``/subscribe``, and ``/send-newsletters`` routes. The optional
300
- ``methods`` parameter is used in some cases to define a list of allowable HTTP
301
- methods.
300
+ ``methods`` parameter is used in some instances to define a list of allowable
301
+ HTTP methods.
302
302
303
- The ``@app.before_request()`` method sets a function to run before every
303
+ The ``@app.before_request()`` decorator sets a function to run before every
304
304
request. In this case, the function provides some basic security by limiting
305
305
access to the ``admin`` page to IP addresses listed in the ``ALLOWED_IPS``
306
306
parameter defined in the ``config.py`` file. Specifically, access is only
@@ -372,14 +372,14 @@ Define your routes by adding the following code to your ``routes.py`` file:
372
372
You can add more security protections or customize user-facing alerts for your
373
373
application in this file.
374
374
375
- Create Your Pages
376
- ~~~~~~~~~~~~~~~~~
375
+ Create Your Page Templates
376
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
377
377
378
378
The HTML files in the ``templates`` directory define the user interface, and are
379
- written using standard HTML. Since this application uses asynchronous HTTP
380
- requests, the scripts in these files use :wikipedia: `Fetch API calls
381
- <XMLHttpRequest#Fetch_alternative>` . These scripts also handle timeouts and
382
- errors.
379
+ written using standard HTML. Because this application uses asynchronous HTTP
380
+ requests, the scripts in these files use `Fetch API calls
381
+ <https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API>`__ . These scripts
382
+ also handle timeouts and errors.
383
383
384
384
Subscribe Page
385
385
```````````````
@@ -446,7 +446,7 @@ Copy the following code into your ``subscribe.html`` file to create your
446
446
Admin Page
447
447
```````````
448
448
449
- The script for the admin page displays an alert to the user that depends on the
449
+ The admin page script displays an alert to the user that indicates the
450
450
success of the ``send_newsletter`` call.
451
451
452
452
Copy the following code into your ``admin.html`` file to create your
@@ -644,7 +644,7 @@ You can use the following steps to test your application:
644
644
645
645
To confirm that you created a new subscriber, open `Atlas
646
646
<https://account.mongodb.com/account/login>`__ and navigate to the
647
- ``users`` collection in your ``newletter `` database.
647
+ ``users`` collection in your ``newsletter `` database.
648
648
649
649
.. step:: Dispatch a newsletter
650
650
@@ -669,17 +669,23 @@ Next Steps
669
669
~~~~~~~~~~
670
670
671
671
This application demonstrates how to integrate a Flask application with the
672
- Celery task queue to manage subscriber data, and send batch emails. You can
673
- further enhance this platform by integrating analytics, customizing email
674
- templates, and implementing automated responses.
672
+ Celery task queue to manage subscriber data and send batch emails. You can
673
+ build on this application to experiment with Flask or Celery. Some possible
674
+ improvements include the following changes:
675
+
676
+ - Add `retries <https://docs.celeryq.dev/en/stable/userguide/calling
677
+ html#message-sending-retry>`__ to your ``send_emails`` function
678
+ - `Format your newsletter <https://flask-mail.readthedocs.io/en/latest/#sending-messages>`__
679
+ - Implement more rigorous `security features <https://docs.celeryq.dev/en/stable/userguide/security.html>`__
675
680
676
681
More Resources
677
682
--------------
678
683
679
- For more information about the components used in this tutorial, see the following resources:
684
+ For more information about the components used in this tutorial, see the
685
+ following resources:
680
686
681
687
- `Flask <https://flask.palletsprojects.com>`__
682
- - `Flask Mail <https://pypi.org/project/Flask-Mail/#files>`__
688
+ - `Flask- Mail <https://pypi.org/project/Flask-Mail/#files>`__
683
689
- `Celery <https://docs.celeryq.dev/en/stable/>`__
684
690
- `RabbitMQ <https://www.rabbitmq.com/docs/download>`__
685
691
0 commit comments