Explorar el Código

Mark games as featured

Colin Powell hace 3 años
padre
commit
ed91508ac9
Se han modificado 3 ficheros con 31 adiciones y 2 borrados
  1. 18 0
      games/migrations/0017_game_featured_on.py
  2. 4 0
      games/models.py
  3. 9 2
      games/views.py

+ 18 - 0
games/migrations/0017_game_featured_on.py

@@ -0,0 +1,18 @@
+# Generated by Django 4.0.4 on 2022-04-21 15:24
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('games', '0016_game_source_game_tags_and_more'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='game',
+            name='featured_on',
+            field=models.DateField(blank=True, null=True),
+        ),
+    ]

+ 4 - 0
games/models.py

@@ -214,6 +214,10 @@ class Game(BaseModel):
         blank=True,
         null=True,
     )
+    featured_on = models.DateField(
+        blank=True,
+        null=True,
+    )
 
     tags = TaggableManager()
 

+ 9 - 2
games/views.py

@@ -1,11 +1,12 @@
 import json
 import logging
+from datetime import datetime
 
 from celery import states
-from django.core.cache import cache
 from django.conf import settings
 from django.contrib.auth.decorators import login_required
 from django.contrib.auth.mixins import LoginRequiredMixin
+from django.core.cache import cache
 from django.db.models import Avg, Count, F
 from django.http import HttpResponse
 from django.views.generic import DetailView, ListView
@@ -30,10 +31,16 @@ class RecentGameList(LoginRequiredMixin, ListView):
     def get_context_data(self, **kwargs):
         cached_game_id = cache.get("todays_game_id", None)
         if not cached_game_id:
-            todays_game = Game.objects.filter(rating__gte=0.7).order_by("?").first()
+            todays_game = (
+                Game.objects.filter(rating__gte=0.7, featured_on__isnull=True)
+                .order_by("?")
+                .first()
+            )
             cache.set("todays_game_id", todays_game.id, settings.FEATURED_GAME_DURATION)
         else:
             todays_game = Game.objects.get(id=cached_game_id)
+            todays_game.featured_on = datetime.now().date
+            todays_game.save(update_fields=["featured_on"])
 
         return super(RecentGameList, self).get_context_data(
             todays_game=todays_game,