diff --git a/backend/setup/migrations/0003_auto_20231209_1431.py b/backend/setup/migrations/0003_auto_20231209_1431.py new file mode 100644 index 00000000..65fd8e9e --- /dev/null +++ b/backend/setup/migrations/0003_auto_20231209_1431.py @@ -0,0 +1,23 @@ +# Generated by Django 3.2.6 on 2023-12-09 05:31 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('setup', '0002_setting_updated_at'), + ] + + operations = [ + migrations.AddField( + model_name='setting', + name='auto_backup', + field=models.IntegerField(choices=[(0, 'non-auto'), (1, 'auto')], default=1), + ), + migrations.AlterField( + model_name='setting', + name='default_menu', + field=models.IntegerField(choices=[(0, 'tts'), (1, 'symbol')], default=0), + ), + ] diff --git a/backend/setup/models.py b/backend/setup/models.py index f976ffcf..598b1a96 100644 --- a/backend/setup/models.py +++ b/backend/setup/models.py @@ -9,13 +9,19 @@ class Setting(models.Model): ) MENU_CHOICE = ( - (0, 'symbol'), - (1, 'tts'), + (0, 'tts'), + (1, 'symbol'), + ) + + BACKUP_CHOICE = ( + (0, 'non-auto'), + (1, 'auto'), ) user = models.OneToOneField('user.User', related_name='settings', on_delete=models.CASCADE) display_mode = models.IntegerField(null=False, default=0, choices=DISPLAY_CHOICE) default_menu = models.IntegerField(null=False, default=0, choices=MENU_CHOICE) + auto_backup = models.IntegerField(null=False, default=1, choices=BACKUP_CHOICE) updated_at = models.DateTimeField(auto_now=True) diff --git a/backend/setup/serializers.py b/backend/setup/serializers.py index 7c1f4090..05d12ff6 100644 --- a/backend/setup/serializers.py +++ b/backend/setup/serializers.py @@ -1,21 +1,23 @@ from rest_framework import serializers from rest_framework.exceptions import ValidationError -from setup.models import Setting - class SettingBackupSerializer(serializers.Serializer): display_mode = serializers.IntegerField(required=True) default_menu = serializers.IntegerField(required=True) + auto_backup = serializers.IntegerField(required=True) updated_at = serializers.DateTimeField(read_only=True) def validate(self, data): display_mode = data.get('display_mode') default_menu = data.get('default_menu') + auto_backup = data.get('auto_backup') if display_mode not in [0, 1]: raise ValidationError({"display_mode": ["invalid display_mode (not 0 or 1)"]}) if default_menu not in [0, 1]: raise ValidationError({"default_menu": ["invalid default_menu (not 0 or 1)"]}) + if auto_backup not in [0, 1]: + raise ValidationError({"auto_backup": ["invalid auto_backup (not 0 or 1)"]}) return data def to_representation(self, instance): diff --git a/backend/setup/tests.py b/backend/setup/tests.py index 530ce57e..5f14f2e1 100644 --- a/backend/setup/tests.py +++ b/backend/setup/tests.py @@ -33,7 +33,8 @@ def test_settings_backup_success(self): } data = { 'display_mode': 0, - 'default_menu': 1 + 'default_menu': 1, + 'auto_backup': 1, } # when attempting backup for the first time response = self.client.post('/setting/backup/', data, **headers) @@ -42,7 +43,8 @@ def test_settings_backup_success(self): # when attempting backup repetitively data = { 'display_mode': 1, - 'default_menu': 0 + 'default_menu': 0, + 'auto_backup': 1, } response = self.client.post('/setting/backup/', data, **headers) self.assertEqual(response.status_code, status.HTTP_200_OK) @@ -56,6 +58,7 @@ def test_get_settings_success(self): data = response.json() self.assertEqual(data['display_mode'], 0) # 0 is the default value self.assertEqual(data['default_menu'], 1) # 1 is the default value + self.assertEqual(data['auto_backup'], 1) diff --git a/backend/setup/views.py b/backend/setup/views.py index f8db9a9f..aafd60de 100644 --- a/backend/setup/views.py +++ b/backend/setup/views.py @@ -16,13 +16,15 @@ def post(self, request): serializer.is_valid(raise_exception=True) display_mode = serializer.validated_data['display_mode'] default_menu = serializer.validated_data['default_menu'] + auto_backup = serializer.validated_data['auto_backup'] if not Setting.objects.filter(user=user).exists(): - Setting.objects.create(display_mode=display_mode, default_menu=default_menu, user=user) + Setting.objects.create(display_mode=display_mode, default_menu=default_menu, auto_backup=auto_backup, user=user) else: curr_setting = Setting.objects.get(user=user) curr_setting.display_mode = display_mode curr_setting.default_menu = default_menu + curr_setting.auto_backup = auto_backup curr_setting.save() return Response(status=status.HTTP_200_OK) @@ -34,6 +36,7 @@ def get(self, request): response_data = { "display_mode": 0, "default_menu": 1, + "auto_backup": 1, "updated_at": "" } else: