Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

Commit

Permalink
feat: archive game on delete plus data
Browse files Browse the repository at this point in the history
  • Loading branch information
dionizh committed Jan 17, 2022
1 parent 791a567 commit 3c40309
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 10 deletions.
24 changes: 24 additions & 0 deletions aimmo/migrations/0027_add_game_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 2.2.25 on 2022-01-14 15:50

from django.db import migrations, models
import django.utils.timezone


class Migration(migrations.Migration):

dependencies = [
('aimmo', '0026_remove_worksheets'),
]

operations = [
migrations.AddField(
model_name='game',
name='creation_time',
field=models.DateTimeField(default=django.utils.timezone.now, null=True),
),
migrations.AddField(
model_name='game',
name='is_archived',
field=models.BooleanField(default=False),
),
]
3 changes: 3 additions & 0 deletions aimmo/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from common.models import Class
from django.contrib.auth.models import User
from django.db import models
from django.utils import timezone

from aimmo import app_settings
from aimmo.worksheets import WORKSHEETS
Expand Down Expand Up @@ -71,6 +72,8 @@ class Game(models.Model):
start_width = models.IntegerField(default=31)
status = models.CharField(max_length=1, choices=STATUS_CHOICES, default=RUNNING)
worksheet_id = models.IntegerField(default=DEFAULT_WORKSHEET_ID)
is_archived = models.BooleanField(default=False)
creation_time = models.DateTimeField(default=timezone.now, null=True)

@property
def is_active(self):
Expand Down
2 changes: 1 addition & 1 deletion aimmo/templatetags/players_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def get_user_playable_games(context, base_url):
playable_games = user.userprofile.student.class_field.game
elif logged_in_as_teacher(user):
playable_games = Game.objects.filter(
game_class__teacher=user.userprofile.teacher
game_class__teacher=user.userprofile.teacher, is_archived=False
)
else:
playable_games = Game.objects.none()
Expand Down
16 changes: 11 additions & 5 deletions aimmo/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,9 +354,13 @@ def test_delete_game(self):
game2 = models.Game(id=2, name="test", worksheet_id=2)
game2.save()

response = client.delete(reverse("game-detail", kwargs={"pk": self.game.id}))
self.assertEquals(response.status_code, 204)
self.assertEquals(len(models.Game.objects.all()), 1)
data = {"game_ids": [self.game.id]}
response = client.post(reverse("game-delete-games"), data)

assert response.status_code == 204
assert models.Game.objects.all().count() == 2
assert models.Game.objects.filter(is_archived=True).count() == 1
assert models.Game.objects.filter(is_archived=False).count() == 1

def test_delete_non_existent_game(self):
c = self.login()
Expand Down Expand Up @@ -505,13 +509,15 @@ def test_delete_games(self):
)
response = client.post(reverse("game-delete-games"), data)
assert response.status_code == 403
assert Game.objects.count() == 3
assert Game.objects.filter(is_archived=False).count() == 3
assert Game.objects.filter(is_archived=True).count() == 0

# Login as initial teacher and delete games - only his games should be deleted
client = self.login()
response = client.post(reverse("game-delete-games"), data)
assert response.status_code == 204
assert Game.objects.count() == 1
assert Game.objects.filter(is_archived=False).count() == 1
assert Game.objects.filter(is_archived=True).count() == 2
assert Game.objects.get(pk=new_game.id)

def test_list_running_games(self):
Expand Down
13 changes: 9 additions & 4 deletions aimmo/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class GameViewSet(

def list(self, request):
response = {}
for game in Game.objects.all():
for game in Game.objects.filter(is_archived=False):
serializer = GameSerializer(game)
response[game.pk] = serializer.data
return Response(response)
Expand All @@ -113,9 +113,14 @@ def running(self, request):
)
def delete_games(self, request):
game_ids = request.data.getlist("game_ids")
Game.objects.filter(
pk__in=game_ids, game_class__teacher__new_user=request.user
).delete()
games = Game.objects.filter(
pk__in=game_ids,
game_class__teacher__new_user=request.user,
is_archived=False,
)
for game in games:
game.is_archived = True # mark as deleted/archived
game.save()
return Response(status=status.HTTP_204_NO_CONTENT)


Expand Down

0 comments on commit 3c40309

Please sign in to comment.