Skip to content

Commit

Permalink
fix auto update with poor solution
Browse files Browse the repository at this point in the history
  • Loading branch information
phattd15 committed Nov 15, 2021
1 parent d163d82 commit 7c52942
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 1 deletion.
20 changes: 20 additions & 0 deletions base/migrations/0007_fetchdata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 3.2.9 on 2021-11-15 17:27

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('base', '0006_profile_history'),
]

operations = [
migrations.CreateModel(
name='FetchData',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('last_update', models.DateTimeField(auto_now_add=True)),
],
),
]
Binary file not shown.
9 changes: 9 additions & 0 deletions base/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,12 @@ class AuthQuery(models.Model):
contest_id = models.IntegerField(default=1400)
index = models.CharField(max_length=4)
valid = models.BooleanField(default=False)


"""
TODO: Fix this with async / celery later
"""


class FetchData(models.Model):
last_update = models.DateTimeField(auto_now_add=True)
6 changes: 5 additions & 1 deletion base/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.contrib.auth.models import User
import json
from urllib.request import urlopen
from .models import Problem
from .models import Problem, FetchData
from random import randint
from datetime import datetime, timedelta
from django.utils import timezone
Expand Down Expand Up @@ -31,8 +31,12 @@ def read_data(url):
def fetch_problemset():
URL = "https://codeforces.com/api/problemset.problems"

FetchData.objects.all().delete()
Problem.objects.all().delete()

fd = FetchData()
fd.save()

data = read_data(URL)
problemset = data["problems"]

Expand Down
7 changes: 7 additions & 0 deletions base/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import time
from django.shortcuts import redirect, render
from .models import Profile, Problem, AuthQuery
from django.contrib.auth.models import User
Expand Down Expand Up @@ -53,6 +54,12 @@ def challenge_list(request):
if profile.in_progress:
return redirect("challenge")

fd = FetchData.objects.all()

if fd[0].last_update + timedelta(hours=12) < timezone.now():
update_problemset()
fd[0].last_update = timezone.now()

if request.method == "POST":
contest_id = request.POST.get("contest_id")
index = request.POST.get("index")
Expand Down

0 comments on commit 7c52942

Please sign in to comment.