Skip to content

Commit

Permalink
fix: minor fix & add: rankAPI
Browse files Browse the repository at this point in the history
  • Loading branch information
st42597 committed Mar 15, 2022
1 parent 70d4c67 commit c11fc4b
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 12 deletions.
24 changes: 24 additions & 0 deletions backend/temperature/migrations/0003_auto_20220315_2022.py
Original file line number Diff line number Diff line change
@@ -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')},
),
]
18 changes: 18 additions & 0 deletions backend/temperature/migrations/0004_alter_temperature_date.py
Original file line number Diff line number Diff line change
@@ -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),
),
]
40 changes: 40 additions & 0 deletions backend/temperature/migrations/0005_auto_20220315_2047.py
Original file line number Diff line number Diff line change
@@ -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')},
),
]
19 changes: 19 additions & 0 deletions backend/temperature/migrations/0006_alter_solvedproblem_id.py
Original file line number Diff line number Diff line change
@@ -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),
),
]
12 changes: 9 additions & 3 deletions backend/temperature/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.db import models
from utils.shortcuts import rand_str


class JudgeStatus:
Expand Down Expand Up @@ -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",)
5 changes: 3 additions & 2 deletions backend/temperature/urls.py
Original file line number Diff line number Diff line change
@@ -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")
]
23 changes: 16 additions & 7 deletions backend/temperature/views.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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()
Expand All @@ -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)

0 comments on commit c11fc4b

Please sign in to comment.