-
Notifications
You must be signed in to change notification settings - Fork 0
/
DemonstratingDbAsCaller.py
44 lines (33 loc) · 2.48 KB
/
DemonstratingDbAsCaller.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#Code Snippet and Logic how Database as caller work. Let's divide into the part to understand logic and the whole concept.
#imported all necessary libaries which is required to perform this activity
import logging
from django.db import transaction
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.apps import apps
#Defined a model for User accounts for performing database operation
class UserAccount(models.Model):
#the UserAccount model has two parameters as username and email. The email field is unique, meaning that no two users can have the same email address.
username = models.CharField(max_length=255)
email = models.EmailField(unique=True)
#Defined the signal handler to send a welcome email
@receiver(post_save, sender=UserAccount)
#The signal handler will be sendiing the data as welcome email by creating a new EmailLog entry in the database.
def send_welcome_email(sender, instance, **kwargs):
# using try and exception to handle a database error while sending the email
try:
#the with transaction.atomic() block confirms that the creation of the EmailLog object/reference is atomic, meaning that if any errors occur during the creation process, the entire transaction will be rolled back.
with transaction.atomic():
# Create a new email log entry
EmailLog = apps.get_model('myapp', 'EmailLog')
email_log = EmailLog(user=instance, email_type='welcome')
email_log.save()
# Raise an exception to simulate a database error
raise ValueError('Simulated database error while sending email') #it handles the database error
except ValueError as e:
logging.error(f"Error sending welcome email: {e}")
#And the except block catches the ValueError exception and logs an error message using the logging module.
#So it has been shown in the code and logic as below explained final output -------
#Django signals, by default, do not run in the same database transaction as the code that calls them.
#This means that if someone creates a new user account, the account gets saved to the database successfully. Even if something goes wrong in the signal handler (like an error while trying to send a welcome email), that error won't rollback the account creation. The account is saved, but the email sending fails separately, without affecting the account creation.
#So, the account is created, but the email doesn’t get sent because of the error.