I haven't had my coffee yet :(
- Fork and clone this repository.
- Install the requirements using
pip install -r requirements/dev.lock
.
-
Create a
signals.py
file inside ofcoffeeshops
. -
Go to
coffeeshops/apps.py
and add aready
method toCoffeeshopsConfig
as follows:class CoffeeshopsConfig(AppConfig): default_auto_field = "django.db.models.BigAutoField" name = "coffeeshops" def ready(self) -> None: import coffeeshops.signals
- This will allow our signals to work when the app starts. You can read about this method here.
We want to be notified every time a new cafe owner joins our platform. To do that, we need to send an email each time a CafeOwner
has been created.
-
Go to
coffeeshops/signals.py
and importsend_mail
fromdjango.core.mail
(we will need this function later). -
Create a function called
send_new_owner_email
, which takes in asender
,instance
,created
, and**kwargs
.- Use the
send_mail
function (read about it here) ifcreated
isTrue
. - Send an email with the subject as
New Cafe Owner
and the body of the email asA new cafe owner has joined named <FULL_NAME_OF_CAFE_OWNER>
. For thefrom_email
you can use"sender@test.com"
andrecipient_list
can be["receiver@test.com"]
.
- Use the
-
Decorate your
send_new_owner_email
withregister
, which is imported fromdjango.dispatch
(read about decorators here). -
Add
post_save
(imported fromdjango.db.models.signals
) to yourregister
decorator as the first argument and set thesender
parameter equal toCafeOwner
(imported fromcoffeeshops.models
).- Read about the
post_save
signal here.
- Read about the
-
Test out your signal by creating an admin account (using
./manage.py createsuperuser
), starting the server, logging in to the admin site, and creating a newCafeOwner
.-
You should see a new login
TASK-Django-M13-Signals/emails/TIMESTAMP.log
(wheretimestamp
is hyphenated numbers) that should look like the following:Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: New Cafe Owner From: sender@test.com To: receiver@test.com Date: Sat, 23 Jul 2022 22:26:35 -0000 Message-ID: <165861519536.9692.1816094537173382016@YOUR_COMPUTER_USERNAME> A new cafe owner has joined named Guido van Rossum. -------------------------------------------------------------------------------
-
Slug is a newspaper term and a short label for something. It contains only letters, numbers, underscores, or hyphens. They’re generally used in URLs. We want to auto-generate slugs for our CoffeeShop
instances.
- Create a function called
slugify_coffee_shop
incoffeeshops/signals.py
and decorate it with apre_save
signal that hasCoffeeShop
as its sender (read aboutpre_save
signals here). - Check if
instance
does not have aslug
. - If our
instance
does not have a slug then just setinstance.slug
equal tocreate_slug(instance)
wherecreate_slug
is imported fromutils
(i.e.,from utils import create_slug
). - Test out your signal on the admin site, by creating a new
CoffeeShop
, you should see aslug
assigned to yourCoffeeShop
after saving.
BONUS: if you can tell an instructor why a pre_save
signal must be used for our slugify_coffee_shop
receiver and not a post_save
signal, you get extra points.
We want to make sure that an address object always exists for a CoffeeShop
even if one was never created or if it is deleted.
- Create a function called
add_default_address
incoffeeshops/signals.py
and decorate it with apost_save
signal that hasCoffeeShop
as its sender. - Check if the
instance
has beencreated
, and ifinstance.location
is empty.- Create a
CoffeeShopAddress
object. - Set
instance.location
equal to the newCoffeeShopAddress
object you've created. - Save your instance.
- Create a
- Test out your signal on the admin site by creating a new
CoffeeShop
and leaving thelocation
field empty, you should see a defaultlocation
assigned to yourCoffeeShop
after saving.
- Create a function called
restore_default_address
and decorate it with apost_delete
signal that hasCoffeeShopAddress
as its sender (read aboutpost_delete
signals here). - Get the coffee shop from the deleted
instance
(hint: use the related namecoffee_shop
) and store it in a variable calledcoffee_shop
. - Create a new
CoffeeShopAddress
instance. - Set
coffee_shop.location
equal to the newCoffeeShopAddress
object you've created. - Save your
coffee_shop
instance. - Test out your signal on the admin site, by deleting a location from any
CoffeeShop
, you should see a defaultlocation
assigned to theCoffeeShop
.
BONUS: if you can tell an instructor why a post_delete
signal must be used for our restore_default_address
receiver and not a pre_delete
signal, you get extra points.
We want to manage our Drink
inventory and mark is_out_of_stock
if our drinks go out of stock.
- Create a function called
slugify_coffee_shop
incoffeeshops/signals.py
and decorate it with apre_save
signal that hasDrink
as its sender. - Set
instance.is_out_of_stock
toFalse
ifinstance.stock_count
is greater than zero and vice-versa. - Test out your signal on the admin site, by setting a stock count greater than zero to a drink, you should see
is_out_of_stock
asFalse
after saving.