Compare commits

..

4 Commits

Author SHA1 Message Date
Lukáš Kucharczyk 44c70a5ee7
Formatting
Django CI/CD / test (push) Successful in 1m21s Details
Django CI/CD / build-and-push (push) Successful in 1m57s Details
2024-06-03 18:19:11 +02:00
Lukáš Kucharczyk cd804f2c77
Sort url paths 2024-06-03 18:18:58 +02:00
Lukáš Kucharczyk 15997bd5af
Re-enable delete session delete view 2024-06-03 18:07:10 +02:00
Lukáš Kucharczyk 880ea93424
Unify url path names 2024-06-03 18:05:34 +02:00
5 changed files with 60 additions and 56 deletions

View File

@ -8,6 +8,7 @@ def safe_division(numerator: int | float, denominator: int | float) -> int | flo
except ZeroDivisionError: except ZeroDivisionError:
return 0 return 0
def safe_getattr(obj, attr_chain, default=None): def safe_getattr(obj, attr_chain, default=None):
""" """
Safely get the nested attribute from an object. Safely get the nested attribute from an object.
@ -20,7 +21,7 @@ def safe_getattr(obj, attr_chain, default=None):
Returns: Returns:
The value of the nested attribute if it exists, otherwise the default value. The value of the nested attribute if it exists, otherwise the default value.
""" """
attrs = attr_chain.split('.') attrs = attr_chain.split(".")
for attr in attrs: for attr in attrs:
try: try:
obj = getattr(obj, attr) obj = getattr(obj, attr)

View File

@ -46,7 +46,7 @@ class EditionChoiceField(forms.ModelChoiceField):
class IncludePlatformSelect(forms.Select): class IncludePlatformSelect(forms.Select):
def create_option(self, name, value, *args, **kwargs): def create_option(self, name, value, *args, **kwargs):
option = super().create_option(name, value, *args, **kwargs) option = super().create_option(name, value, *args, **kwargs)
if platform_id := safe_getattr(value, 'instance.platform.id'): if platform_id := safe_getattr(value, "instance.platform.id"):
option["attrs"]["data-platform"] = platform_id option["attrs"]["data-platform"] = platform_id
return option return option

View File

@ -4,32 +4,60 @@ from games import views
urlpatterns = [ urlpatterns = [
path("", views.index, name="index"), path("", views.index, name="index"),
path("device/add", views.add_device, name="add_device"),
path("edition/add", views.add_edition, name="add_edition"),
path( path(
"list-sessions/recent", "edition/add/for-game/<int:game_id>",
views.list_sessions, views.add_edition,
{"filter": "recent"}, name="add_edition_for_game",
name="list_sessions_recent",
), ),
path("add-game/", views.add_game, name="add_game"), path("edition/<int:edition_id>/edit", views.edit_edition, name="edit_edition"),
path("add-platform/", views.add_platform, name="add_platform"), path("game/add", views.add_game, name="add_game"),
path("add-session/", views.add_session, name="add_session"), path("game/<int:game_id>/edit", views.edit_game, name="edit_game"),
path("game/<int:game_id>/view", views.view_game, name="view_game"),
path("platform/add", views.add_platform, name="add_platform"),
path("platform/<int:platform_id>/edit", views.edit_platform, name="edit_platform"),
path("purchase/add", views.add_purchase, name="add_purchase"),
path("purchase/<int:purchase_id>/edit", views.edit_purchase, name="edit_purchase"),
path( path(
"add-session-for-purchase/<int:purchase_id>", "purchase/<int:purchase_id>/delete",
views.delete_purchase,
name="delete_purchase",
),
path(
"purchase/related-purchase-by-edition",
views.related_purchase_by_edition,
name="related_purchase_by_edition",
),
path(
"purchase/add/for-edition/<int:edition_id>",
views.add_purchase,
name="add_purchase_for_edition",
),
path("session/add", views.add_session, name="add_session"),
path(
"session/add/for-purchase/<int:purchase_id>",
views.add_session, views.add_session,
name="add_session_for_purchase", name="add_session_for_purchase",
), ),
path( path(
"session/clone/from-game/<int:session_id>", "session/add/from-game/<int:session_id>",
views.new_session_from_existing_session, views.new_session_from_existing_session,
{"template": "view_game.html#session-info"}, {"template": "view_game.html#session-info"},
name="view_game_start_session_from_session", name="view_game_start_session_from_session",
), ),
path( path(
"session/clone/from-list/<int:session_id>", "session/add/from-list/<int:session_id>",
views.new_session_from_existing_session, views.new_session_from_existing_session,
{"template": "list_sessions.html#session-row"}, {"template": "list_sessions.html#session-row"},
name="list_sessions_start_session_from_session", name="list_sessions_start_session_from_session",
), ),
path("session/<int:session_id>/edit", views.edit_session, name="edit_session"),
path(
"session/<int:session_id>/delete",
views.delete_session,
name="delete_session",
),
path( path(
"session/end/from-game/<int:session_id>", "session/end/from-game/<int:session_id>",
views.end_session, views.end_session,
@ -42,67 +70,39 @@ urlpatterns = [
{"template": "list_sessions.html#session-row"}, {"template": "list_sessions.html#session-row"},
name="list_sessions_end_session", name="list_sessions_end_session",
), ),
# path( path("session/list", views.list_sessions, name="list_sessions"),
# "delete_session/by-id/<int:session_id>",
# views.delete_session,
# name="delete_session",
# ),
path( path(
"purchase/<int:purchase_id>/delete", "session/list/recent",
views.delete_purchase, views.list_sessions,
name="delete_purchase", {"filter": "recent"},
), name="list_sessions_recent",
path("add-purchase/", views.add_purchase, name="add_purchase"),
path(
"add-purchase-for-edition/<int:edition_id>",
views.add_purchase,
name="add_purchase_for_edition",
), ),
path( path(
"related-purchase-by-edition", "session/list/by-purchase/<int:purchase_id>",
views.related_purchase_by_edition,
name="related_purchase_by_edition",
),
path("add-edition/", views.add_edition, name="add_edition"),
path(
"add-edition-for-game/<int:game_id>",
views.add_edition,
name="add_edition_for_game",
),
path("edit-edition/<int:edition_id>", views.edit_edition, name="edit_edition"),
path("game/<int:game_id>/view", views.view_game, name="view_game"),
path("game/<int:game_id>/edit", views.edit_game, name="edit_game"),
path("edit-platform/<int:platform_id>", views.edit_platform, name="edit_platform"),
path("add-device/", views.add_device, name="add_device"),
path("edit-session/<int:session_id>", views.edit_session, name="edit_session"),
path("edit-purchase/<int:purchase_id>", views.edit_purchase, name="edit_purchase"),
path("list-sessions/", views.list_sessions, name="list_sessions"),
path(
"list-sessions/by-purchase/<int:purchase_id>",
views.list_sessions, views.list_sessions,
{"filter": "purchase"}, {"filter": "purchase"},
name="list_sessions_by_purchase", name="list_sessions_by_purchase",
), ),
path( path(
"list-sessions/by-platform/<int:platform_id>", "session/list/by-platform/<int:platform_id>",
views.list_sessions, views.list_sessions,
{"filter": "platform"}, {"filter": "platform"},
name="list_sessions_by_platform", name="list_sessions_by_platform",
), ),
path( path(
"list-sessions/by-game/<int:game_id>", "session/list/by-game/<int:game_id>",
views.list_sessions, views.list_sessions,
{"filter": "game"}, {"filter": "game"},
name="list_sessions_by_game", name="list_sessions_by_game",
), ),
path( path(
"list-sessions/by-edition/<int:edition_id>", "session/list/by-edition/<int:edition_id>",
views.list_sessions, views.list_sessions,
{"filter": "edition"}, {"filter": "edition"},
name="list_sessions_by_edition", name="list_sessions_by_edition",
), ),
path( path(
"list-sessions/by-ownership/<str:ownership_type>", "session/list/by-ownership/<str:ownership_type>",
views.list_sessions, views.list_sessions,
{"filter": "ownership_type"}, {"filter": "ownership_type"},
name="list_sessions_by_ownership_type", name="list_sessions_by_ownership_type",

View File

@ -284,10 +284,11 @@ def end_session(request, session_id: int, template: str = ""):
return redirect("list_sessions") return redirect("list_sessions")
# def delete_session(request, session_id=None): @login_required
# session = Session.objects.get(id=session_id) def delete_session(request, session_id=None):
# session.delete() session = get_object_or_404(Session, id=session_id)
# return redirect("list_sessions") session.delete()
return redirect("list_sessions")
@login_required @login_required
@ -591,12 +592,14 @@ def stats(request, year: int = 0):
request.session["return_path"] = request.path request.session["return_path"] = request.path
return render(request, "stats.html", context) return render(request, "stats.html", context)
@login_required @login_required
def delete_purchase(request, purchase_id=None): def delete_purchase(request, purchase_id=None):
purchase = get_object_or_404(Purchase, id=purchase_id) purchase = get_object_or_404(Purchase, id=purchase_id)
purchase.delete() purchase.delete()
return redirect("list_sessions") return redirect("list_sessions")
@login_required @login_required
def add_purchase(request, edition_id=None): def add_purchase(request, edition_id=None):
context = {} context = {}

View File

@ -23,10 +23,10 @@ from graphene_django.views import GraphQLView
urlpatterns = [ urlpatterns = [
path("", RedirectView.as_view(url="/tracker")), path("", RedirectView.as_view(url="/tracker")),
path("graphql", csrf_exempt(GraphQLView.as_view(graphiql=True))),
path("login/", auth_views.LoginView.as_view(), name="login"), path("login/", auth_views.LoginView.as_view(), name="login"),
path("logout/", auth_views.LogoutView.as_view(), name="logout"), path("logout/", auth_views.LogoutView.as_view(), name="logout"),
path("tracker/", include("games.urls")), path("tracker/", include("games.urls")),
path("graphql", csrf_exempt(GraphQLView.as_view(graphiql=True))),
] ]
if settings.DEBUG: if settings.DEBUG: