diff --git a/backend/temperature/migrations/0003_auto_20220315_2022.py b/backend/temperature/migrations/0003_auto_20220315_2022.py new file mode 100644 index 000000000..76cf7eb34 --- /dev/null +++ b/backend/temperature/migrations/0003_auto_20220315_2022.py @@ -0,0 +1,24 @@ +# Generated by Django 3.2.10 on 2022-03-15 11:22 + +from django.db import migrations, models +import django.utils.timezone + + +class Migration(migrations.Migration): + + dependencies = [ + ('temperature', '0002_auto_20220315_1749'), + ] + + operations = [ + migrations.AddField( + model_name='temperature', + name='date', + field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now), + preserve_default=False, + ), + migrations.AlterUniqueTogether( + name='temperature', + unique_together={('user_id', 'date')}, + ), + ] diff --git a/backend/temperature/migrations/0004_alter_temperature_date.py b/backend/temperature/migrations/0004_alter_temperature_date.py new file mode 100644 index 000000000..2d4fbb7b5 --- /dev/null +++ b/backend/temperature/migrations/0004_alter_temperature_date.py @@ -0,0 +1,18 @@ +# Generated by Django 3.2.10 on 2022-03-15 11:34 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('temperature', '0003_auto_20220315_2022'), + ] + + operations = [ + migrations.AlterField( + model_name='temperature', + name='date', + field=models.DateField(auto_now_add=True), + ), + ] diff --git a/backend/temperature/migrations/0005_auto_20220315_2047.py b/backend/temperature/migrations/0005_auto_20220315_2047.py new file mode 100644 index 000000000..475ed8fc8 --- /dev/null +++ b/backend/temperature/migrations/0005_auto_20220315_2047.py @@ -0,0 +1,40 @@ +# Generated by Django 3.2.10 on 2022-03-15 11:47 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('temperature', '0004_alter_temperature_date'), + ] + + operations = [ + migrations.AlterModelOptions( + name='solvedproblem', + options={'ordering': ('-solved_time',)}, + ), + migrations.AlterModelOptions( + name='temperature', + options={'ordering': ('-date',)}, + ), + migrations.AddField( + model_name='solvedproblem', + name='_id', + field=models.TextField(db_index=True, default=0), + ), + migrations.AlterField( + model_name='solvedproblem', + name='id', + field=models.TextField(primary_key=True, serialize=False), + ), + migrations.AlterField( + model_name='solvedproblem', + name='solved_time', + field=models.DateField(auto_now_add=True), + ), + migrations.AlterUniqueTogether( + name='solvedproblem', + unique_together={('user_id', '_id')}, + ), + ] diff --git a/backend/temperature/migrations/0006_alter_solvedproblem_id.py b/backend/temperature/migrations/0006_alter_solvedproblem_id.py new file mode 100644 index 000000000..8c75f7537 --- /dev/null +++ b/backend/temperature/migrations/0006_alter_solvedproblem_id.py @@ -0,0 +1,19 @@ +# Generated by Django 3.2.10 on 2022-03-15 12:18 + +from django.db import migrations, models +import utils.shortcuts + + +class Migration(migrations.Migration): + + dependencies = [ + ('temperature', '0005_auto_20220315_2047'), + ] + + operations = [ + migrations.AlterField( + model_name='solvedproblem', + name='id', + field=models.TextField(db_index=True, default=utils.shortcuts.rand_str, primary_key=True, serialize=False), + ), + ] diff --git a/backend/temperature/models.py b/backend/temperature/models.py index b13aa0f84..b9f548b2d 100644 --- a/backend/temperature/models.py +++ b/backend/temperature/models.py @@ -1,4 +1,5 @@ from django.db import models +from utils.shortcuts import rand_str class JudgeStatus: @@ -30,17 +31,22 @@ class ProblemScore: class Temperature(models.Model): user_id = models.IntegerField(unique=True) temperature = models.IntegerField(default=0) + date = models.DateField(auto_now_add=True) class Meta: db_table = "temperature" - ordering = ("-temperature",) + unique_together = (("user_id", "date"),) + ordering = ("-date",) class SolvedProblem(models.Model): + id = models.TextField(default=rand_str, primary_key=True, db_index=True) user_id = models.IntegerField(db_index=True) - id = models.TextField(primary_key=True, db_index=True) + _id = models.TextField(db_index=True, default=0) score = models.IntegerField() - solved_time = models.DateTimeField(auto_now_add=True) + solved_time = models.DateField(auto_now_add=True) class Meta: db_table = "solvedproblem" + unique_together = (("user_id", "_id"),) + ordering = ("-solved_time",) diff --git a/backend/temperature/urls.py b/backend/temperature/urls.py index fd4e508c9..8703f523c 100644 --- a/backend/temperature/urls.py +++ b/backend/temperature/urls.py @@ -1,7 +1,8 @@ from django.urls import path -from .views import TemperatureAPI +from .views import TemperatureAPI, RankAPI urlpatterns = [ - path("temperature/", TemperatureAPI.as_view(), name="temperature_api") + path("temperature/", TemperatureAPI.as_view(), name="temperature_api"), + path("rank/", RankAPI.as_view(), name="rank_api") ] diff --git a/backend/temperature/views.py b/backend/temperature/views.py index d898b3eaf..bde89c3b8 100644 --- a/backend/temperature/views.py +++ b/backend/temperature/views.py @@ -1,6 +1,7 @@ from utils.api import APIView, validate_serializer from .models import ProblemScore, Temperature, SolvedProblem from .serializers import CreateTemperatureSerializer +import datetime class TemperatureAPI(APIView): @@ -12,14 +13,15 @@ def post(self, request): return self.error("Parameter error, id is required") if not difficulty: return self.error("Parameter error, difficulty is required") - user_temp = Temperature.objects.get(user_id=request.user.id) - user_temp_qs = Temperature.objects.filter(user_id=request.user.id) - if not user_temp: - Temperature.objects.create(user_id=request.user.id, temperature=0) - solved_problem = SolvedProblem.objects.filter(user_id=request.user.id, id=id) + date = datetime.date.today() + if not Temperature.objects.filter(user_id=request.user.id, date=date): + Temperature.objects.create(user_id=request.user.id) + user_temp = Temperature.objects.get(user_id=request.user.id, date=date) + user_temp_qs = Temperature.objects.filter(user_id=request.user.id, date=date) + solved_problem = SolvedProblem.objects.filter(user_id=request.user.id, _id=id) if not solved_problem: SolvedProblem.objects.create(user_id=request.user.id, - id=id, + _id=id, score=ProblemScore.scores[difficulty]) user_temp_qs.update(temperature=user_temp.temperature+ProblemScore.scores[difficulty]) return self.success() @@ -36,5 +38,12 @@ def get(self, request): class RankAPI(APIView): def get(self, request): - data = "123" + date = datetime.date.today() + for idx, temp in enumerate(list(Temperature.objects.filter(date=date).order_by("-temperature"))): + if temp.user_id == request.user.id: + rank = idx + 1 + break + data = { + "rank": rank + } return self.success(data)