-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from snuhcs-course/feat/backup-backend
웨이트테이블 백업 구현(백엔드)
- Loading branch information
Showing
18 changed files
with
155 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.contrib import admin | ||
|
||
# Register your models here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.test import TestCase | ||
|
||
# Create your tests here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |