diff options
author | Sunil Shetye | 2025-03-03 17:39:53 +0530 |
---|---|---|
committer | Sunil Shetye | 2025-03-04 12:16:50 +0530 |
commit | 0bed06e26220b28020f6e124c761bd0dc527e388 (patch) | |
tree | 3109b2756ddadd5aa899b83dff5ff3bcdc6dcdf2 | |
parent | ac5bf4b93b74ab502a1a966e51a6e2829daa53bf (diff) | |
download | Common-Interface-Project-0bed06e26220b28020f6e124c761bd0dc527e388.tar.gz Common-Interface-Project-0bed06e26220b28020f6e124c761bd0dc527e388.tar.bz2 Common-Interface-Project-0bed06e26220b28020f6e124c761bd0dc527e388.zip |
save app name also
add session count and return expire_at time
store expire_at also
-rw-r--r-- | blocks/eda-frontend/src/App.js | 5 | ||||
-rw-r--r-- | blocks/simulationAPI/models.py | 28 | ||||
-rw-r--r-- | blocks/simulationAPI/views.py | 6 |
3 files changed, 33 insertions, 6 deletions
diff --git a/blocks/eda-frontend/src/App.js b/blocks/eda-frontend/src/App.js index dd4bbe3a..edc5b138 100644 --- a/blocks/eda-frontend/src/App.js +++ b/blocks/eda-frontend/src/App.js @@ -81,13 +81,16 @@ const App = () => { const fetchSession = async () => { // Check if session ID already exists let sessionId = localStorage.getItem('session_id') + let expireAt = localStorage.getItem('expire_at') - if (!sessionId) { + if (!sessionId || !expireAt || new Date(expireAt) < new Date()) { // Generate a new session ID api.get('simulation/get_session?app_name=' + process.env.REACT_APP_NAME) .then(res => { sessionId = res.data.session_id + expireAt = res.data.expire_at localStorage.setItem('session_id', sessionId) + localStorage.setItem('expire_at', expireAt) console.log('new sessionId', sessionId) }) } diff --git a/blocks/simulationAPI/models.py b/blocks/simulationAPI/models.py index 2de65687..63be2eed 100644 --- a/blocks/simulationAPI/models.py +++ b/blocks/simulationAPI/models.py @@ -1,4 +1,5 @@ from django.db import models +from django.core.exceptions import ValidationError from django.core.files.storage import FileSystemStorage from django.conf import settings from django.utils import timezone @@ -9,17 +10,38 @@ import uuid # session class Session(models.Model): session_id = models.CharField(primary_key=True, max_length=40, null=False, editable=False) - app_name = models.CharField(max_length=100, blank=True, null=True) + app_name = models.CharField(max_length=40, blank=False, null=False, default='') created_at = models.DateTimeField(auto_now_add=True) expire_at = models.DateTimeField(default=timezone.now() + timedelta(days=1)) + count = models.IntegerField(null=False, default=0) + + def save(self, *args, **kwargs): + if self.pk is None: # New entry + self.count = 1 + super().save(*args, **kwargs) + return + + old_instances = Session.objects.filter(pk=self.pk) + if not old_instances.exists(): # New entry + self.count = 1 + super().save(*args, **kwargs) + return + + old_instance = old_instances.first() + if old_instance.app_name == self.app_name: + self.count += 1 + kwargs['update_fields'] = ['count'] + super().save(*args, **kwargs) + return + + raise ValidationError("mismatch: Cannot update app name.") def __str__(self): return self.session_id class Task(models.Model): - task_id = models.UUIDField( - primary_key=True, default=uuid.uuid4, editable=False) + task_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) file = models.FileField(storage=FileSystemStorage(location=settings.MEDIA_ROOT), default='default_file.txt') parameters = models.TextField(blank=True, null=True) diff --git a/blocks/simulationAPI/views.py b/blocks/simulationAPI/views.py index 413a8d3f..1c1b5416 100644 --- a/blocks/simulationAPI/views.py +++ b/blocks/simulationAPI/views.py @@ -345,10 +345,12 @@ def get_session(request): request.session.save() # Create a new session if not already exists session_id = request.session.session_key + app_name = request.GET.get('app_name', None) # Save or update session in the custom table session, created = Session.objects.update_or_create( - session_id=session_id + session_id=session_id, + app_name=app_name ) - return JsonResponse({'session_id': session_id, 'stored': not created}) + return JsonResponse({'session_id': session_id, 'expire_at': session.expire_at}) |