From b4fd13a8970b731b9886a9c619ce13501adb450e Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Thu, 28 Jan 2021 22:12:35 +0100 Subject: [PATCH] stash Signed-off-by: Yohann D'ANELLO --- lg/forms.py | 17 ++++++++++++++++- lg/models.py | 15 ++++++++++++++- lg/urls.py | 8 ++++++++ lg/views.py | 14 ++++++++++++-- lglog/templates/index.html | 2 +- lglog/templates/lg/new_payer.html | 11 +++++++++++ lglog/urls.py | 2 ++ 7 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 lg/urls.py create mode 100644 lglog/templates/lg/new_payer.html diff --git a/lg/forms.py b/lg/forms.py index cd09c07..5917edd 100644 --- a/lg/forms.py +++ b/lg/forms.py @@ -1,6 +1,21 @@ from django import forms +from django.utils.translation import gettext_lazy as _ -from .models import Action, CupidonAction, DoveAction, HackerAction, RavenAction, WerewolfAction, WitchAction +from .models import Action, Cupidon, CupidonAction, DiscordUser, Dove, DoveAction, Hacker, HackerAction, \ + Raven, RavenAction, Werewolf, WerewolfAction, Witch, WitchAction + + +class PlayerForm(forms.Form): + user = forms.ModelChoiceField( + queryset=DiscordUser.objects.filter(is_superuser=False, player__isnull=True), + label=lambda: _("user").capitalize(), + ) + + player_type = forms.ChoiceField( + choices=[(model.__name__, model._meta.verbose_name) + for model in [Werewolf, Cupidon, Witch, Raven, Dove, Hacker]], + label=lambda: _("player type").capitalize(), + ) class ActionForm(forms.ModelForm): diff --git a/lg/models.py b/lg/models.py index 101aef9..28732dc 100644 --- a/lg/models.py +++ b/lg/models.py @@ -22,9 +22,22 @@ class DiscordUser(AbstractUser): ) @property - def avatar_url(self): + def avatar_url(self) -> str: return f"https://cdn.discordapp.com/avatars/{self.discord_id}/{self.avatar_id}.png" + @property + def plays(self) -> bool: + return Player.objects.filter(user=self).exists() + + @property + def role(self): + if self.plays: + return self.player._meta.verbose_name + elif self.is_superuser: + return _("admin") + else: + return _("spectator") + class Player(PolymorphicModel): user = models.OneToOneField( diff --git a/lg/urls.py b/lg/urls.py new file mode 100644 index 0000000..dc4f6d8 --- /dev/null +++ b/lg/urls.py @@ -0,0 +1,8 @@ +from django.urls import path + +from lg import views + +app_name = 'lg' +urlpatterns = [ + path('new_player', views.NewPlayerView.as_view()), +] diff --git a/lg/views.py b/lg/views.py index 91ea44a..4e8fc9f 100644 --- a/lg/views.py +++ b/lg/views.py @@ -1,3 +1,13 @@ -from django.shortcuts import render +from django.contrib.auth.mixins import LoginRequiredMixin +from django.views.generic import FormView -# Create your views here. +from lg.forms import PlayerForm + + +class NewPlayerView(LoginRequiredMixin, FormView): + form_class = PlayerForm + template_name = "lg/new_payer.html" + + def form_valid(self, form): + user = form.cleaned_data["user"] + class_name diff --git a/lglog/templates/index.html b/lglog/templates/index.html index b49cc26..2646713 100644 --- a/lglog/templates/index.html +++ b/lglog/templates/index.html @@ -5,5 +5,5 @@ {% block title %}{% trans "Index" %}{% endblock %} {% block content %} - Historique de loup-garou + Votre rĂ´le : {{ user.role }} {% endblock %} diff --git a/lglog/templates/lg/new_payer.html b/lglog/templates/lg/new_payer.html new file mode 100644 index 0000000..38c9a76 --- /dev/null +++ b/lglog/templates/lg/new_payer.html @@ -0,0 +1,11 @@ +{% extends "base.html" %} + +{% load crispy_forms_filters %} + +{% block content %} +
+ {% csrf_token %} + {{ form|crispy }} + +
+{% endblock %} diff --git a/lglog/urls.py b/lglog/urls.py index 4c45a70..d18d3e3 100644 --- a/lglog/urls.py +++ b/lglog/urls.py @@ -10,4 +10,6 @@ urlpatterns = [ path('i18n/', include('django.conf.urls.i18n')), path('accounts/login/', LoginView.as_view()), path('accounts/', include('django.contrib.auth.urls')), + + path('lg/', include('lg.urls')) ]