Skip to content

Commit

Permalink
Add game deletion and LibraryCategory model
Browse files Browse the repository at this point in the history
  • Loading branch information
strycore committed Feb 19, 2024
1 parent 6a7e287 commit fabedbc
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 3 deletions.
38 changes: 36 additions & 2 deletions accounts/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Module for user account views"""
# pylint: disable=too-many-ancestors,raise-missing-from
import json
import logging
from datetime import datetime
from collections import defaultdict
Expand Down Expand Up @@ -488,11 +489,22 @@ def post(self, request, *args, **kwargs):
client_library[game["slug"]].append(game)
stored_library = self.get_queryset(ignore_since=True)
updated_games = set()
stats = {"user": request.user.username, "unchanged": 0, "updated": 0, "created": 0, "errors": 0}
stats = {
"user": request.user.username,
"unchanged": 0,
"updated": 0,
"created": 0,
"errors": 0,
}
for game in stored_library:
if game.get_slug() in client_library:
client_games = client_library[game.get_slug()]
stored_key = (game.get_slug(), game.runner or "", game.platform or "", game.service or "")
stored_key = (
game.get_slug(),
game.runner or "",
game.platform or "",
game.service or "",
)
for client_game in client_games:
client_key = (
client_game["slug"],
Expand Down Expand Up @@ -568,3 +580,25 @@ def post(self, request, *args, **kwargs):
LOGGER.info(stats)
return self.get(request)

def delete_game(self, game):
library_games = models.LibraryGame.objects.filter(
gamelibrary__user=self.request.user, slug=game["slug"]
)
if len(library_games) == 1:
return library_games[0].delete()

for library_game in library_games:
if library_game["lastplayed"] == game["lastplayed"]:
return library_game.delete()
return [""]

def delete(self, request):
stats = {"delete_results": {}}
for game in request.data:
slug = game["slug"]
if not slug:
LOGGER.warning("No slug provided")
return HttpResponseBadRequest("Missing slug")
result = self.delete_game(game)
stats["delete_results"][slug] = result[0]
return HttpResponse(json.dumps(stats))
36 changes: 36 additions & 0 deletions games/migrations/0080_librarycategory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Generated by Django 4.2.1 on 2024-02-19 05:35

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):
dependencies = [
("games", "0079_librarygame_updated_at"),
]

operations = [
migrations.CreateModel(
name="LibraryCategory",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("name", models.CharField(max_length=256)),
(
"gamelibrary",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="categories",
to="games.gamelibrary",
),
),
],
),
]
6 changes: 6 additions & 0 deletions games/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1161,6 +1161,12 @@ class Meta:
def __str__(self):
return "%s's library" % self.user.username

class LibraryCategory(models.Model):
"""Model to represent a user defined category"""
gamelibrary = models.ForeignKey(GameLibrary, on_delete=models.CASCADE, related_name="categories")
name = models.CharField(max_length=256)


class LibraryGame(models.Model):
game = models.ForeignKey(Game, on_delete=models.CASCADE, null=True)
name = models.CharField(max_length=256, null=True)
Expand Down
1 change: 0 additions & 1 deletion templates/accounts/library_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ <h1>My Library</h1>
{% include 'includes/library_paginator.html' %}
</div>
{% endif %}

</div>
</div>
{% endblock %}
Expand Down

0 comments on commit fabedbc

Please sign in to comment.