Migrate to postgres, add memcache and vastly improve available equipment instance query time from 1min 30 seconds to 12 seconds

This commit is contained in:
Keannu Christian Bernasol 2024-01-07 02:28:14 +08:00
parent 0af8efa793
commit b0b1f4db86
24 changed files with 1253 additions and 292 deletions

View file

@ -0,0 +1,18 @@
# Generated by Django 5.0.1 on 2024-01-06 16:49
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('transactions', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='transaction',
name='transaction_status',
field=models.CharField(choices=[('Pending Approval', 'Pending Approval'), ('Approved', 'Approved'), ('Rejected', 'Rejected'), ('Cancelled', 'Cancelled'), ('Borrowed', 'Borrowed'), ('Returned: Pending Checking', 'Returned: Pending Checking'), ('With Breakages: Pending Resolution', 'With Breakages: Pending Resolution'), ('Finalized', 'Finalized')], db_index=True, default='Pending', max_length=40),
),
]

View file

@ -2,6 +2,7 @@ from django.db import models
from accounts.models import CustomUser
from equipments.models import EquipmentInstance
from django.utils.timezone import now
from django.core.cache import cache
class Transaction(models.Model):
@ -34,9 +35,13 @@ class Transaction(models.Model):
CustomUser, on_delete=models.SET_NULL, null=True, related_name='teacher_transactions')
equipments = models.ManyToManyField(EquipmentInstance)
transaction_status = models.CharField(
max_length=40, choices=TRANSACTION_STATUS_CHOICES, default='Pending')
max_length=40, choices=TRANSACTION_STATUS_CHOICES, default='Pending', db_index=True)
subject = models.TextField(max_length=128)
timestamp = models.DateTimeField(default=now, editable=False)
def __str__(self):
return f"Transaction #{self.id} under {self.teacher} by {self.borrower}"
def save(self, *args, **kwargs):
cache.delete('non_finalized_transactions')
return super().save(*args, **kwargs)