Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions backend/.env.example

This file was deleted.

24 changes: 23 additions & 1 deletion backend/backend/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"django_celery_beat",
"corsheaders",
"rest_framework",
"nested_admin",
Expand Down Expand Up @@ -84,13 +85,25 @@
# Database
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases

DATABASES = {
'''DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": BASE_DIR / "db.sqlite3",
}
}'''

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'mydatabase', # Your PostgreSQL database name
'USER': 'postgres', # Your PostgreSQL username
'PASSWORD': '1234', # Your PostgreSQL password
'HOST': 'localhost', # Leave as 'localhost' if PostgreSQL is installed locally
'PORT': '5432', # Default PostgreSQL port
}
}


# Password validation
# https://docs.djangoproject.com/en/4.2/ref/settings/#auth-password-validators

Expand Down Expand Up @@ -146,6 +159,15 @@
FRONTEND_URL,
]

# Celery Configuration
CELERY_BROKER_URL = 'redis://localhost:6379/0'
CELERY_BEAT_SCHEDULE = {
'cleanup-old-conversations': {
'task': 'chat.tasks.cleanup_old_conversations',
'schedule': 86400.0, # Runs every 24 hours
},
}

SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True
CSRF_COOKIE_SAMESITE = "None"
2 changes: 1 addition & 1 deletion backend/chat/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def queryset(self, request, queryset):
class ConversationAdmin(NestedModelAdmin):
actions = ["undelete_selected", "soft_delete_selected"]
inlines = [VersionInline]
list_display = ("title", "id", "created_at", "modified_at", "deleted_at", "version_count", "is_deleted", "user")
list_display = ("title", "id", "created_at", "modified_at", "deleted_at", "version_count", "is_deleted", "user", "summary")
list_filter = (DeletedListFilter,)
ordering = ("-modified_at",)

Expand Down
16 changes: 16 additions & 0 deletions backend/chat/management/commands/clean_old_conversations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from django.core.management.base import BaseCommand
from django.utils.timezone import now
from chat.models import Conversation
import datetime

class Command(BaseCommand):
help = "Deletes soft-deleted conversations older than 30 days."

def handle(self, *args, **kwargs):
threshold_date = now() - datetime.timedelta(days=30)
old_conversations = Conversation.objects.filter(deleted_at__lte=threshold_date)

count = old_conversations.count()
old_conversations.delete()

self.stdout.write(self.style.SUCCESS(f"Deleted {count} old conversations."))
17 changes: 17 additions & 0 deletions backend/chat/migrations/0002_conversation_summary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 5.0.2 on 2025-03-13 09:57

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("chat", "0001_initial"),
]

operations = [
migrations.AddField(
model_name="conversation",
name="summary",
field=models.TextField(blank=True, null=True),
),
]
17 changes: 16 additions & 1 deletion backend/chat/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,26 @@ class Conversation(models.Model):
deleted_at = models.DateTimeField(null=True, blank=True)
user = models.ForeignKey(CustomUser, on_delete=models.CASCADE)

# New field for storing conversation summaries
summary = models.TextField(blank=True, null=True)

def __str__(self):
return self.title

def version_count(self):
return self.versions.count()

def generate_summary(self):
messages = self.versions.prefetch_related("messages").values_list("messages__content", flat=True)
if messages:
self.summary = " | ".join(messages[:5]) # Simple summary of first 5 messages
else:
self.summary = "No messages yet."
self.save(update_fields=['summary']) # Avoids recursive save calls

def save(self, *args, **kwargs):
super().save(*args, **kwargs)
self.generate_summary()

version_count.short_description = "Number of versions"

Expand Down Expand Up @@ -58,8 +73,8 @@ class Meta:
ordering = ["created_at"]

def save(self, *args, **kwargs):
self.version.conversation.save()
super().save(*args, **kwargs)
self.version.conversation.generate_summary() # Directly update summary when message is saved

def __str__(self):
return f"{self.role}: {self.content[:20]}..."
Binary file added backend/data.json
Binary file not shown.
1 change: 0 additions & 1 deletion frontend/.env.local.example

This file was deleted.