Spaces:
Build error
Build error
| from database.setup_db import get_database | |
| from datetime import datetime | |
| import uuid | |
| from pymongo import DESCENDING | |
| def get_current_week_identifier(): | |
| """ | |
| Generate a week identifier based on the current year and week number. | |
| Example: '2023_45' for the 45th week of 2023. | |
| """ | |
| now = datetime.now() | |
| year = now.year | |
| week_number = now.isocalendar()[1] # ISO week number | |
| return f"{year}_Week_{week_number}" | |
| def insert_weekly_task_data(json_data,): | |
| """ | |
| Insert JSON data into the weekly_tasks collection. | |
| Args: | |
| json_data (dict): JSON object containing task data. | |
| """ | |
| db = get_database() | |
| collection = db["weekly_tasks"] | |
| # Generate the current week identifier | |
| week_identifier = get_current_week_identifier() | |
| unique_id = str(uuid.uuid4()) | |
| # Check if a document for the given week already exists | |
| existing_document = collection.find_one({"unique_id": unique_id}) | |
| if existing_document: | |
| print(f"Document with id: {unique_id} already exists. Skipping insert.") | |
| return | |
| # Insert the document if it doesn't already exist | |
| document = { | |
| "week": week_identifier, | |
| "unique_id": unique_id, | |
| "tasks": json_data, | |
| "created_at": datetime.now() | |
| } | |
| result = collection.insert_one(document) | |
| print(f"Inserted document with ID: {result.inserted_id}") | |
| def fetch_recent_two_entries(): | |
| """ | |
| Fetch the two most recent entries from the weekly_tasks collection | |
| based on the created_at timestamp. | |
| Returns: | |
| list: A list of the two most recent documents from the collection. | |
| """ | |
| db = get_database() | |
| collection = db["weekly_tasks"] | |
| # Query to fetch the two most recent entries | |
| recent_entries = list( | |
| collection.find().sort("created_at", DESCENDING).limit(2) | |
| ) | |
| return recent_entries | |