View group

This commit is contained in:
Gabriel Augendre 2022-11-03 00:02:28 +01:00
parent bb55490a91
commit 4e37472964
6 changed files with 44 additions and 3 deletions

View file

@ -3,6 +3,7 @@ from collections.abc import Iterable
import markdown import markdown
from django.db import models from django.db import models
from django.db.models import Q
from django.db.models.functions import Lower from django.db.models.functions import Lower
from django.urls import reverse from django.urls import reverse
from django_extensions.db.models import TimeStampedModel from django_extensions.db.models import TimeStampedModel
@ -75,7 +76,11 @@ class CharacterManager(models.Manager):
class CharacterQuerySet(models.QuerySet): class CharacterQuerySet(models.QuerySet):
def managed_by(self, user): def managed_by(self, user):
return self.filter(player=user) from party.models import Party
return self.filter(
Q(player=user) | Q(parties__in=Party.objects.managed_by(user))
)
def owned_by(self, user): def owned_by(self, user):
return self.filter(player=user) return self.filter(player=user)

View file

@ -1,4 +1,5 @@
from django.db import models from django.db import models
from django.db.models import Q
from django_extensions.db.models import TimeStampedModel from django_extensions.db.models import TimeStampedModel
from character.models import Character from character.models import Character
@ -12,6 +13,12 @@ class PartyQuerySet(models.QuerySet):
def played_by(self, user): def played_by(self, user):
return self.filter(characters__in=Character.objects.filter(player=user)) return self.filter(characters__in=Character.objects.filter(player=user))
def related_to(self, user):
return self.filter(
Q(game_master=user)
| Q(characters__in=Character.objects.filter(player=user))
).distinct()
class PartyManager(UniquelyNamedModelManager): class PartyManager(UniquelyNamedModelManager):
pass pass

View file

@ -0,0 +1,21 @@
{% extends "common/base.html" %}
{% load django_bootstrap5 %}
{% load character_extras %}
{% block title %}{{ party.name }} · Groupe{% endblock %}
{% block content %}
<h1>{{ party.name }}</h1>
<p>Personnages :</p>
<ul>
{% for character in party.characters.all %}
<li>
<a href="{% url "character:view" pk=character.pk %}">
{{ character.name }}
</a>
</li>
{% empty %}
<li>Aucun...</li>
{% endfor %}
</ul>
{% endblock %}

View file

@ -4,7 +4,7 @@
<h5 class="card-title"> <h5 class="card-title">
{{ party.name }} {{ party.name }}
</h5> </h5>
<a href="" class="btn btn-sm btn-success"> <a href="{% url "party:details" pk=party.pk %}" class="btn btn-sm btn-success">
<i class="fa-solid fa-users"></i> Accéder <i class="fa-solid fa-users"></i> Accéder
</a> </a>
</div> </div>

View file

@ -6,4 +6,5 @@ app_name = "party"
urlpatterns = [ urlpatterns = [
path("", views.parties_list, name="list"), path("", views.parties_list, name="list"),
path("create/", views.party_create, name="create"), path("create/", views.party_create, name="create"),
path("<int:pk>/", views.party_details, name="details"),
] ]

View file

@ -1,5 +1,5 @@
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.shortcuts import redirect, render from django.shortcuts import get_object_or_404, redirect, render
from party.forms import PartyForm from party.forms import PartyForm
from party.models import Party from party.models import Party
@ -28,3 +28,10 @@ def party_create(request):
return redirect("party:list") return redirect("party:list")
context = {"form": form} context = {"form": form}
return render(request, "party/party_create.html", context) return render(request, "party/party_create.html", context)
@login_required
def party_details(request, pk):
party = get_object_or_404(Party.objects.related_to(request.user), pk=pk)
context = {"party": party}
return render(request, "party/party_details.html", context)