DEV Community

Mgboawaji ikpaiko
Mgboawaji ikpaiko

Posted on

How I approach new problems as backend engineer

I was thrilled when I received the email confirming my acceptance into the HNG Internship (HNG 11) 2024 edition. Enrolling in the backend track, my first task was to write an article about a difficult problem I’ve solved in my journey as a backend developer. Let me share a recent challenge I faced and how I tackled it.

A friend recently approached me with a project idea: a system to send him daily email notifications reminding him of upcoming contract deadlines. To tackle this challenge, I opted for Django as the backend framework and leveraged Celery and Celery Beat for asynchronous task handling and scheduling.

The testdriven.io blog post on handling periodic tasks with Django (Handling periodic tasks with django) provided a clear walkthrough of integrating Celery and Celery Beat. After integrating Celery and setting up the backend, I moved on to creating a view for users to save their contracts. Below are some code snippets

#models.py
from django import models


class Contract(models.Model):
    document_type = models.CharField(max_length=50, choices=DOCUMENT_TYPES)
    document_name = models.CharField(max_length=255)
    duration = models.CharField(max_length=50)
    start_date = models.DateField(null=True, blank=True)
    expiry_date = models.DateField(null=True, blank=True)
    issuing_body = models.CharField(max_length=255)
    process_owner = models.CharField(max_length=255)

    def __str__(self):
        return self.document_name
Enter fullscreen mode Exit fullscreen mode
#tasks.py
from celery import shared_task
from datetime import datetime, timedelta
from django.core.mail import send_mail
from .models import Contract

@shared_task(bind=True)
def send_contract_expiry_notification(self):

    # Calculate the date one month from now
    one_month_from_now = datetime.now() + timedelta(days=30)

    # Find contracts that are expiring within one month
    expiring_contracts = Contract.objects.filter(expiry_date__lte=one_month_from_now)
    print(expiring_contracts)

    # If there's no contract then don't send any email
    if expiring_contracts:
        # Initialize the message body
        message_body = "Dear user,\n\nThe following contracts are expiring soon:\n\n"

        for contract in expiring_contracts:
            # Calculate the number of days until the expiry date
            days_until_expiry = (contract.expiry_date - datetime.now().date()).days

            # Add contract details to the message body
            message_body += (
                f"Contract: {contract.document_name}\n"
                f"Expiry Date: {contract.expiry_date}\n"
                f"Days Until Expiry: {days_until_expiry}\n\n"
            )

        # Finalize the message body
        message_body += "Please take the necessary actions."

        # Customize your notification message here
        subject = "Contract Expiry Notification"
        recipient_list = ['***@gmail.com', '***@gmail.com']  # Change this to your desired recipient email(s)

        # Send email notification
        send_mail(subject, message_body, '***@gmail.com', recipient_list)
Enter fullscreen mode Exit fullscreen mode

Through this project, I learned how to integrate Celery and Celery Beat with Django, overcoming the challenges of unfamiliar tools by leveraging quality online resources. This experience not only strengthened my technical skills but also underscored the importance of continuous learning and adaptability—qualities I am eager to bring to the HNG Internship.

I'm excited about the HNG Internship and the opportunities it offers to work on real-world projects, collaborate with talented individuals, and further refine my backend development skills. This journey is just beginning, and I look forward to sharing more of it with you.

If you're passionate about technology and eager to push your boundaries, I highly recommend applying for the HNG Internship. It’s an incredible platform for learning, growth, and making meaningful connections in the tech community. Visit the HNG Internship website to learn more and join this transformative experience. Let's innovate and grow together!

Join HNG Premium

Top comments (0)