Skip to content

Commit

Permalink
Merge pull request #51 from snuhcs-course/feat/backup-backend
Browse files Browse the repository at this point in the history
웨이트테이블 백업 구현(백엔드)
  • Loading branch information
89645321 authored Nov 21, 2023
2 parents 7ff5081 + 6fd77fd commit 489289a
Show file tree
Hide file tree
Showing 18 changed files with 155 additions and 3 deletions.
1 change: 1 addition & 0 deletions backend/config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ def get_secret(setting, secrets=secrets):
'entry',
'setup',
'storages',
'weight_table'
]

MIDDLEWARE = [
Expand Down
1 change: 1 addition & 0 deletions backend/config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
path('user/', include('user.urls')),
path('setting/', include('setup.urls', namespace='default')),
path('symbol/', include('entry.urls')),
path('weight/', include('weight_table.urls'))
]
2 changes: 1 addition & 1 deletion backend/entry/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ class Symbol(models.Model):
class FavoriteSymbol(models.Model):
symbol = models.ForeignKey('Symbol', related_name='favorites', on_delete=models.CASCADE)
user = models.ForeignKey('user.User', related_name='favorites', on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)
created_at = models.DateTimeField(auto_now_add=True)
1 change: 0 additions & 1 deletion backend/entry/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import boto3
import botocore
from rest_framework import permissions, status
from rest_framework.response import Response
from rest_framework.views import APIView
Expand Down
18 changes: 18 additions & 0 deletions backend/setup/migrations/0002_setting_updated_at.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.6 on 2023-11-20 12:35

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('setup', '0001_initial'),
]

operations = [
migrations.AddField(
model_name='setting',
name='updated_at',
field=models.DateTimeField(auto_now=True),
),
]
1 change: 1 addition & 0 deletions backend/setup/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ class Setting(models.Model):
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)
updated_at = models.DateTimeField(auto_now=True)


6 changes: 6 additions & 0 deletions backend/setup/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
class SettingBackupSerializer(serializers.Serializer):
display_mode = serializers.IntegerField(required=True)
default_menu = serializers.IntegerField(required=True)
updated_at = serializers.DateTimeField(read_only=True)

def validate(self, data):
display_mode = data.get('display_mode')
Expand All @@ -16,3 +17,8 @@ def validate(self, data):
if default_menu not in [0, 1]:
raise ValidationError({"default_menu": ["invalid default_menu (not 0 or 1)"]})
return data

def to_representation(self, instance):
representation = super(SettingBackupSerializer, self).to_representation(instance)
representation['updated_at'] = instance.updated_at.strftime('%Y-%m-%d')
return representation
3 changes: 2 additions & 1 deletion backend/setup/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ def get(self, request):
if not Setting.objects.filter(user=user).exists():
response_data = {
"display_mode": 0,
"default_menu": 0
"default_menu": 0,
"updated_at": ""
}
else:
curr_setting = Setting.objects.get(user=user)
Expand Down
Empty file.
3 changes: 3 additions & 0 deletions backend/weight_table/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
6 changes: 6 additions & 0 deletions backend/weight_table/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class WeightTableConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'weight_table'
27 changes: 27 additions & 0 deletions backend/weight_table/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Generated by Django 3.2.6 on 2023-11-20 13:13

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

initial = True

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.CreateModel(
name='WeightTable',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('symbol_id', models.IntegerField()),
('weight', models.TextField()),
('updated_at', models.DateTimeField(auto_now=True)),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='weight_table', to=settings.AUTH_USER_MODEL)),
],
),
]
Empty file.
10 changes: 10 additions & 0 deletions backend/weight_table/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django.db import models


# Create your models here.

class WeightTable(models.Model):
symbol_id = models.IntegerField(null=False, blank=False)
weight = models.TextField(null=False, blank=False)
user = models.ForeignKey('user.User', related_name='weight_table', on_delete=models.CASCADE)
updated_at = models.DateTimeField(auto_now=True)
39 changes: 39 additions & 0 deletions backend/weight_table/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from rest_framework import serializers
from rest_framework.exceptions import ValidationError

from entry.models import Symbol
from weight_table.models import WeightTable


class WeightTableBackupSerializer(serializers.Serializer):
# symbol_id
id = serializers.IntegerField(source='symbol_id', required=True)
weight = serializers.CharField(required=True)

def validate(self, data):
id = data.get('symbol_id')
user = self.context['user']

all_symbols = list(Symbol.objects.values_list('id', flat=True))
if id not in all_symbols:
raise ValidationError({"id": ["Invalid symbol (no such symbol)"]})

# Default symbol이 아니라면
if id > 500:
symbol = Symbol.objects.get(id=id)
if symbol.created_by != user:
raise ValidationError({"not_mine": ["the requested symbol is created by another user"]})

return data

def create(self, validated_data):
user = self.context['user']
symbol_id = validated_data.get('symbol_id')
weight = validated_data.get('weight')

if not WeightTable.objects.filter(user=user, symbol_id=symbol_id).exists():
WeightTable.objects.create(user=user, symbol_id=symbol_id, weight=weight)
else:
weight_row = WeightTable.objects.get(user=user, symbol_id=symbol_id)
weight_row.weight = weight
weight_row.save()
3 changes: 3 additions & 0 deletions backend/weight_table/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
8 changes: 8 additions & 0 deletions backend/weight_table/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.urls import path
from . import views


app_name = 'weight_table'
urlpatterns = [
path('backup/', views.WeightTableBackupView.as_view(), name='backup weight table'),
]
29 changes: 29 additions & 0 deletions backend/weight_table/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from rest_framework import permissions, status
from rest_framework.response import Response
from rest_framework.views import APIView

from weight_table.models import WeightTable
from weight_table.serializers import WeightTableBackupSerializer


class WeightTableBackupView(APIView):
permission_classes = (permissions.IsAuthenticated,)

def post(self, request):
user = request.user
data = request.data.get('weight_table')

serializer = WeightTableBackupSerializer(data=data, context={'user': user}, many=True)
serializer.is_valid(raise_exception=True)
serializer.save()

return Response(status=status.HTTP_200_OK)

def get(self, request):
user = request.user
weight_table = WeightTable.objects.filter(user=user)
response_data = WeightTableBackupSerializer(weight_table, many=True).data
response_data = {
"weight_table": response_data
}
return Response(response_data, status=status.HTTP_200_OK)

0 comments on commit 489289a

Please sign in to comment.