devnamdev2003 commited on
Commit
ced18b6
·
1 Parent(s): 1d8dc03

add data backup key

Browse files
api/migrations/0001_initial.py CHANGED
@@ -1,4 +1,4 @@
1
- # Generated by Django 5.2.4 on 2025-07-27 10:12
2
 
3
  from django.db import migrations, models
4
 
@@ -11,15 +11,50 @@ class Migration(migrations.Migration):
11
  ]
12
 
13
  operations = [
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  migrations.CreateModel(
15
  name='UserData',
16
  fields=[
17
  ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
18
  ('user_id', models.CharField(max_length=100, unique=True)),
19
  ('expenses', models.JSONField(blank=True, default=dict)),
20
- ('budget', models.JSONField(blank=True, default=dict)),
21
  ('category', models.JSONField(blank=True, default=dict)),
22
  ('user_data', models.JSONField(blank=True, default=dict)),
 
 
 
23
  ],
24
  ),
25
  ]
 
1
+ # Generated by Django 5.2.4 on 2026-02-22 07:41
2
 
3
  from django.db import migrations, models
4
 
 
11
  ]
12
 
13
  operations = [
14
+ migrations.CreateModel(
15
+ name='AIKey',
16
+ fields=[
17
+ ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
18
+ ('aikey', models.CharField(max_length=100, unique=True)),
19
+ ('isActive', models.BooleanField(default=False)),
20
+ ('keySource', models.CharField(max_length=100, unique=True)),
21
+ ],
22
+ ),
23
+ migrations.CreateModel(
24
+ name='AppVersion',
25
+ fields=[
26
+ ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
27
+ ('version', models.CharField(max_length=20)),
28
+ ('release_notes', models.TextField(blank=True, null=True)),
29
+ ('is_force_update', models.BooleanField(default=False)),
30
+ ('created_at', models.DateTimeField(auto_now_add=True)),
31
+ ('isActive', models.BooleanField(default=False)),
32
+ ],
33
+ options={
34
+ 'db_table': 'app_version',
35
+ },
36
+ ),
37
+ migrations.CreateModel(
38
+ name='Contact',
39
+ fields=[
40
+ ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
41
+ ('name', models.CharField(max_length=20)),
42
+ ('email', models.EmailField(max_length=50)),
43
+ ('message', models.TextField(max_length=500)),
44
+ ],
45
+ ),
46
  migrations.CreateModel(
47
  name='UserData',
48
  fields=[
49
  ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
50
  ('user_id', models.CharField(max_length=100, unique=True)),
51
  ('expenses', models.JSONField(blank=True, default=dict)),
52
+ ('salary', models.JSONField(blank=True, default=dict)),
53
  ('category', models.JSONField(blank=True, default=dict)),
54
  ('user_data', models.JSONField(blank=True, default=dict)),
55
+ ('has_music_url_access', models.BooleanField(default=False)),
56
+ ('has_ai_access', models.BooleanField(default=False)),
57
+ ('data_backup_key', models.CharField(blank=True, max_length=100, unique=True)),
58
  ],
59
  ),
60
  ]
api/migrations/0002_userdata_has_music_url_access.py DELETED
@@ -1,18 +0,0 @@
1
- # Generated by Django 5.2.4 on 2025-09-29 17:25
2
-
3
- from django.db import migrations, models
4
-
5
-
6
- class Migration(migrations.Migration):
7
-
8
- dependencies = [
9
- ('api', '0001_initial'),
10
- ]
11
-
12
- operations = [
13
- migrations.AddField(
14
- model_name='userdata',
15
- name='has_music_url_access',
16
- field=models.BooleanField(default=False),
17
- ),
18
- ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
api/migrations/0003_userdata_has_ai_access.py DELETED
@@ -1,18 +0,0 @@
1
- # Generated by Django 5.2.4 on 2025-09-30 18:01
2
-
3
- from django.db import migrations, models
4
-
5
-
6
- class Migration(migrations.Migration):
7
-
8
- dependencies = [
9
- ('api', '0002_userdata_has_music_url_access'),
10
- ]
11
-
12
- operations = [
13
- migrations.AddField(
14
- model_name='userdata',
15
- name='has_ai_access',
16
- field=models.BooleanField(default=False),
17
- ),
18
- ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
api/migrations/0004_aikey.py DELETED
@@ -1,22 +0,0 @@
1
- # Generated by Django 5.2.4 on 2026-01-25 13:00
2
-
3
- from django.db import migrations, models
4
-
5
-
6
- class Migration(migrations.Migration):
7
-
8
- dependencies = [
9
- ('api', '0003_userdata_has_ai_access'),
10
- ]
11
-
12
- operations = [
13
- migrations.CreateModel(
14
- name='AIKey',
15
- fields=[
16
- ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
17
- ('aikey', models.CharField(max_length=100, unique=True)),
18
- ('isActive', models.BooleanField(default=False)),
19
- ('keySource', models.CharField(max_length=100, unique=True)),
20
- ],
21
- ),
22
- ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
api/migrations/0005_appversion.py DELETED
@@ -1,27 +0,0 @@
1
- # Generated by Django 5.2.4 on 2026-01-25 13:17
2
-
3
- from django.db import migrations, models
4
-
5
-
6
- class Migration(migrations.Migration):
7
-
8
- dependencies = [
9
- ('api', '0004_aikey'),
10
- ]
11
-
12
- operations = [
13
- migrations.CreateModel(
14
- name='AppVersion',
15
- fields=[
16
- ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
17
- ('version', models.CharField(max_length=20)),
18
- ('release_notes', models.TextField(blank=True, null=True)),
19
- ('is_force_update', models.BooleanField(default=False)),
20
- ('created_at', models.DateTimeField(auto_now_add=True)),
21
- ('isActive', models.BooleanField(default=False)),
22
- ],
23
- options={
24
- 'db_table': 'app_version',
25
- },
26
- ),
27
- ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
api/migrations/0006_contact.py DELETED
@@ -1,22 +0,0 @@
1
- # Generated by Django 5.2.4 on 2026-01-26 15:54
2
-
3
- from django.db import migrations, models
4
-
5
-
6
- class Migration(migrations.Migration):
7
-
8
- dependencies = [
9
- ('api', '0005_appversion'),
10
- ]
11
-
12
- operations = [
13
- migrations.CreateModel(
14
- name='Contact',
15
- fields=[
16
- ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
17
- ('name', models.CharField(max_length=20)),
18
- ('email', models.EmailField(max_length=50)),
19
- ('message', models.TextField(max_length=500)),
20
- ],
21
- ),
22
- ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
api/migrations/0007_rename_budget_userdata_salary.py DELETED
@@ -1,18 +0,0 @@
1
- # Generated by Django 5.2.4 on 2026-01-29 12:36
2
-
3
- from django.db import migrations
4
-
5
-
6
- class Migration(migrations.Migration):
7
-
8
- dependencies = [
9
- ('api', '0006_contact'),
10
- ]
11
-
12
- operations = [
13
- migrations.RenameField(
14
- model_name='userdata',
15
- old_name='budget',
16
- new_name='salary',
17
- ),
18
- ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
api/models.py CHANGED
@@ -1,6 +1,6 @@
1
  from django.db import models
2
- from django.contrib.postgres.fields import JSONField # If using PostgreSQL
3
-
4
 
5
  class UserData(models.Model):
6
  user_id = models.CharField(max_length=100, unique=True)
@@ -10,7 +10,8 @@ class UserData(models.Model):
10
  user_data = models.JSONField(default=dict, blank=True)
11
  has_music_url_access = models.BooleanField(default=False)
12
  has_ai_access = models.BooleanField(default=False)
13
-
 
14
  def __str__(self):
15
  return f"Data for User {self.user_id}"
16
 
@@ -34,6 +35,13 @@ class AppVersion(models.Model):
34
  class Meta:
35
  db_table = "app_version"
36
 
 
 
 
 
 
 
 
37
  def __str__(self):
38
  return self.version
39
 
 
1
  from django.db import models
2
+ from django.contrib.postgres.fields import JSONField
3
+ import uuid
4
 
5
  class UserData(models.Model):
6
  user_id = models.CharField(max_length=100, unique=True)
 
10
  user_data = models.JSONField(default=dict, blank=True)
11
  has_music_url_access = models.BooleanField(default=False)
12
  has_ai_access = models.BooleanField(default=False)
13
+ data_backup_key = models.CharField(max_length=100, unique=True, blank=True)
14
+
15
  def __str__(self):
16
  return f"Data for User {self.user_id}"
17
 
 
35
  class Meta:
36
  db_table = "app_version"
37
 
38
+ def save(self, *args, **kwargs):
39
+ # If this version is active → deactivate others
40
+ if self.isActive:
41
+ AppVersion.objects.filter(isActive=True).exclude(id=self.id).update(isActive=False)
42
+
43
+ super().save(*args, **kwargs)
44
+
45
  def __str__(self):
46
  return self.version
47
 
api/views.py CHANGED
@@ -5,77 +5,55 @@ from .models import UserData, AIKey, AppVersion, Contact
5
  from django.shortcuts import get_object_or_404
6
  from .serializers import ContactSerializer
7
  from rest_framework.generics import CreateAPIView
8
-
9
 
10
  class UserDataPostView(APIView):
11
  def post(self, request):
12
  user_id = request.data.get("user_id")
13
  if not user_id:
14
  return Response(
15
- {"error": "user_id is required"}, status=status.HTTP_400_BAD_REQUEST
 
16
  )
17
 
18
  obj, created = UserData.objects.get_or_create(user_id=user_id)
19
 
20
- # Track if updates were actually made
 
 
21
  updated = False
22
  for key in ["expenses", "salary", "category", "user_data"]:
23
  if key in request.data and getattr(obj, key) != request.data[key]:
24
  setattr(obj, key, request.data[key])
25
  updated = True
26
 
27
- if updated:
28
- obj.save()
29
 
30
- if created:
31
- obj = get_object_or_404(UserData, user_id=user_id)
32
- if obj.has_ai_access:
33
- AI_KEY = get_object_or_404(AIKey, isActive=True).aikey
34
- else:
35
- AI_KEY = None
36
- return Response(
37
- {
38
- "message": "Data created successfull",
39
- "app_version": get_object_or_404(AppVersion, isActive=True).version,
40
- "has_music_url_access": obj.has_music_url_access,
41
- "has_ai_access": obj.has_ai_access,
42
- "ai_key": AI_KEY,
43
- },
44
- status=status.HTTP_201_CREATED,
45
- )
46
- elif updated:
47
- obj = get_object_or_404(UserData, user_id=user_id)
48
- print(obj.has_ai_access)
49
- if obj.has_ai_access:
50
- AI_KEY = get_object_or_404(AIKey, isActive=True).aikey
51
- else:
52
- AI_KEY = None
53
- return Response(
54
- {
55
- "message": "Data updated successfull",
56
- "app_version": get_object_or_404(AppVersion, isActive=True).version,
57
- "has_music_url_access": obj.has_music_url_access,
58
- "has_ai_access": obj.has_ai_access,
59
- "ai_key": AI_KEY,
60
- },
61
- status=status.HTTP_200_OK,
62
- )
63
  else:
64
- obj = get_object_or_404(UserData, user_id=user_id)
65
- if obj.has_ai_access:
66
- AI_KEY = get_object_or_404(AIKey, isActive=True).aikey
67
- else:
68
- AI_KEY = None
69
- return Response(
70
- {
71
- "message": "No changes made",
72
- "app_version": get_object_or_404(AppVersion, isActive=True).version,
73
- "has_music_url_access": obj.has_music_url_access,
74
- "has_ai_access": obj.has_ai_access,
75
- "ai_key": AI_KEY,
76
- },
77
- status=status.HTTP_200_OK,
78
- )
 
 
 
 
 
 
79
 
80
 
81
  class GetFieldView(APIView):
 
5
  from django.shortcuts import get_object_or_404
6
  from .serializers import ContactSerializer
7
  from rest_framework.generics import CreateAPIView
8
+ import uuid
9
 
10
  class UserDataPostView(APIView):
11
  def post(self, request):
12
  user_id = request.data.get("user_id")
13
  if not user_id:
14
  return Response(
15
+ {"error": "user_id is required"},
16
+ status=status.HTTP_400_BAD_REQUEST,
17
  )
18
 
19
  obj, created = UserData.objects.get_or_create(user_id=user_id)
20
 
21
+ # Always regenerate backup key
22
+ obj.data_backup_key = str(uuid.uuid4())
23
+
24
  updated = False
25
  for key in ["expenses", "salary", "category", "user_data"]:
26
  if key in request.data and getattr(obj, key) != request.data[key]:
27
  setattr(obj, key, request.data[key])
28
  updated = True
29
 
30
+ obj.save() # Save once after changes
 
31
 
32
+ # AI key logic
33
+ if obj.has_ai_access:
34
+ AI_KEY = get_object_or_404(AIKey, isActive=True).aikey
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  else:
36
+ AI_KEY = None
37
+
38
+ return Response(
39
+ {
40
+ "message": (
41
+ "Data created successfully"
42
+ if created
43
+ else "Data updated successfully"
44
+ if updated
45
+ else "No changes made"
46
+ ),
47
+ "app_version": get_object_or_404(
48
+ AppVersion, isActive=True
49
+ ).version,
50
+ "has_music_url_access": obj.has_music_url_access,
51
+ "has_ai_access": obj.has_ai_access,
52
+ "ai_key": AI_KEY,
53
+ "data_backup_key": obj.data_backup_key,
54
+ },
55
+ status=status.HTTP_201_CREATED if created else status.HTTP_200_OK,
56
+ )
57
 
58
 
59
  class GetFieldView(APIView):
run_live.bat CHANGED
@@ -1,6 +1,6 @@
1
  @echo off
2
  REM Activate virtual environment
3
- call env\Scripts\activate.bat
4
 
5
  REM Set environment variable to live
6
  set DEVELOPMENT=live
 
1
  @echo off
2
  REM Activate virtual environment
3
+ call venv\Scripts\activate.bat
4
 
5
  REM Set environment variable to live
6
  set DEVELOPMENT=live
run_local.bat CHANGED
@@ -1,7 +1,7 @@
1
  @echo off
2
 
3
  REM Activate virtual environment
4
- call env\Scripts\activate.bat
5
 
6
  REM Set environment variable to live
7
  set DEVELOPMENT=local
 
1
  @echo off
2
 
3
  REM Activate virtual environment
4
+ call venv\Scripts\activate.bat
5
 
6
  REM Set environment variable to live
7
  set DEVELOPMENT=local
run_migrate_live.bat CHANGED
@@ -1,7 +1,7 @@
1
  @echo off
2
 
3
  REM Activate the virtual environment
4
- call env\Scripts\activate.bat
5
 
6
  REM Set environment variable to live
7
  set DEVELOPMENT=live
 
1
  @echo off
2
 
3
  REM Activate the virtual environment
4
+ call venv\Scripts\activate.bat
5
 
6
  REM Set environment variable to live
7
  set DEVELOPMENT=live
run_migrate.bat → run_migrate_local.bat RENAMED
@@ -1,14 +1,13 @@
1
  @echo off
2
 
3
  REM Activate the virtual environment
4
- call env\Scripts\activate.bat
5
 
6
  REM Set environment variable to live
7
  set DEVELOPMENT=local
8
 
9
 
10
  REM Make and apply migrations
11
- python manage.py makemigrations
12
  python manage.py migrate
13
 
14
  REM Wait for user to press Enter
 
1
  @echo off
2
 
3
  REM Activate the virtual environment
4
+ call venv\Scripts\activate.bat
5
 
6
  REM Set environment variable to live
7
  set DEVELOPMENT=local
8
 
9
 
10
  REM Make and apply migrations
 
11
  python manage.py migrate
12
 
13
  REM Wait for user to press Enter
run_migrations.bat CHANGED
@@ -1,7 +1,7 @@
1
  @echo off
2
 
3
  REM Activate the virtual environment
4
- call env\Scripts\activate.bat
5
 
6
  REM Set environment variable to live
7
  set DEVELOPMENT=local
 
1
  @echo off
2
 
3
  REM Activate the virtual environment
4
+ call venv\Scripts\activate.bat
5
 
6
  REM Set environment variable to live
7
  set DEVELOPMENT=local