Building an Automated Task Scheduler with Python and MongoDB

Introduction

Efficient task management is critical for any business. Automating task assignments and reminders ensures that teams stay productive and deadlines are met. In this guide, we’ll build a task scheduler using Python, MongoDB, and Pandas to store tasks, schedule assignments, track progress, and send notifications.


1. Task Management Overview

  • The goal is to automate task assignments based on workload and deadlines.
  • Tasks are stored in MongoDB and processed with Python.
  • Pandas is used for tracking task completion and generating reports.
  • Notifications are sent via email or messaging platforms.

2. MongoDB Task Storage

Setting Up the Database

First, install pymongo and connect to the MongoDB database:

from pymongo import MongoClient

client = MongoClient("mongodb://localhost:27017/")
db = client["task_management"]
tasks_collection = db["tasks"]

Defining the Task Schema

Each task will have the following structure:

task = {
    "title": "Update financial report",
    "assigned_to": "John Doe",
    "priority": "High",
    "deadline": "2025-03-01",
    "status": "Pending"
}

tasks_collection.insert_one(task)

3. Python Scheduling Logic

To automate task assignments, we use the schedule library:

import schedule
import time

def assign_tasks():
    pending_tasks = tasks_collection.find({"status": "Pending"})
    for task in pending_tasks:
        print(f"Assigning task: {task['title']} to {task['assigned_to']}")

# Run the scheduler every day at 8 AM
schedule.every().day.at("08:00").do(assign_tasks)

while True:
    schedule.run_pending()
    time.sleep(60)

4. Pandas Progress Tracking

We use Pandas to generate a report on task completion:

import pandas as pd

tasks = list(tasks_collection.find({}, {"_id": 0}))  # Exclude MongoDB ObjectID
df = pd.DataFrame(tasks)

# Generate progress summary
summary = df.groupby("status").size()
print(summary)

# Save report to Excel
df.to_excel("task_report.xlsx", index=False)

5. Notifications

To notify employees of their assigned tasks, we send emails using smtplib:

import smtplib
from email.message import EmailMessage

def send_email(to, subject, body):
    msg = EmailMessage()
    msg.set_content(body)
    msg["Subject"] = subject
    msg["From"] = "noreply@company.com"
    msg["To"] = to

    with smtplib.SMTP("smtp.example.com", 587) as server:
        server.starttls()
        server.login("your_email", "your_password")
        server.send_message(msg)

send_email("john.doe@example.com", "New Task Assigned", "Please complete your task: Update financial report")

Next steps

This automated task scheduler ensures efficient task tracking, reduces manual work, and keeps teams organized. Lillqvist Strata can help you implement a fully customized version of this solution, tailored to your business needs, maximizing efficiency and profitability.

Let’s automate your workflow today!

Leave a comment

Your email address will not be published. Required fields are marked *