Skip to content

Commit

Permalink
Merge pull request #113 from snuhcs-course/feat/add-auto-backup
Browse files Browse the repository at this point in the history
설정 백업시 '자동백업' 여부 추가
  • Loading branch information
yjeong-k authored Dec 9, 2023
2 parents d2a371f + 70cf4a6 commit 4f9d1fc
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 7 deletions.
23 changes: 23 additions & 0 deletions backend/setup/migrations/0003_auto_20231209_1431.py
Original file line number Diff line number Diff line change
@@ -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),
),
]
10 changes: 8 additions & 2 deletions backend/setup/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


6 changes: 4 additions & 2 deletions backend/setup/serializers.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
7 changes: 5 additions & 2 deletions backend/setup/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)



Expand Down
5 changes: 4 additions & 1 deletion backend/setup/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -34,6 +36,7 @@ def get(self, request):
response_data = {
"display_mode": 0,
"default_menu": 1,
"auto_backup": 1,
"updated_at": ""
}
else:
Expand Down

0 comments on commit 4f9d1fc

Please sign in to comment.