Skip to content

Commit

Permalink
Déplace les formulaires relatifs aux auteurs avec leurs vues
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnaud-D committed Mar 9, 2024
1 parent e6de79d commit 06f16f3
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 59 deletions.
58 changes: 0 additions & 58 deletions zds/tutorialv2/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from zds.tutorialv2.models import TYPE_CHOICES
from zds.tutorialv2.models.database import PublishableContent
from django.utils.translation import gettext_lazy as _
from zds.member.models import Profile
from zds.utils.forms import IncludeEasyMDE
from zds.utils.validators import with_svg_validator, slugify_raise_on_invalid, InvalidSlugError

Expand Down Expand Up @@ -47,63 +46,6 @@ def label_from_instance(self, obj):
return obj.title


class AuthorForm(forms.Form):
username = forms.CharField(label=_("Auteurs à ajouter séparés d'une virgule."), required=True)

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_class = "content-wrapper"
self.helper.form_method = "post"
self.helper.layout = Layout(
Field("username"),
ButtonHolder(
StrictButton(_("Ajouter"), type="submit"),
),
)

def clean_username(self):
"""Check every username and send it to the cleaned_data['user'] list
:return: a dictionary of all treated data with the users key added
"""
cleaned_data = super().clean()
users = []
if cleaned_data.get("username"):
for username in cleaned_data.get("username").split(","):
user = (
Profile.objects.contactable_members()
.filter(user__username__iexact=username.strip().lower())
.first()
)
if user is not None:
users.append(user.user)
if len(users) > 0:
cleaned_data["users"] = users
return cleaned_data

def is_valid(self):
return super().is_valid() and "users" in self.clean()


class RemoveAuthorForm(AuthorForm):
def clean_username(self):
"""Check every username and send it to the cleaned_data['user'] list
:return: a dictionary of all treated data with the users key added
"""
cleaned_data = super(AuthorForm, self).clean()
users = []
for username in cleaned_data.get("username").split(","):
# we can remove all users (bots inclued)
user = Profile.objects.filter(user__username__iexact=username.strip().lower()).first()
if user is not None:
users.append(user.user)
if len(users) > 0:
cleaned_data["users"] = users
return cleaned_data


class ContainerForm(FormWithTitle):
introduction = forms.CharField(
label=_("Introduction"),
Expand Down
61 changes: 60 additions & 1 deletion zds/tutorialv2/views/authors.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from crispy_forms.bootstrap import StrictButton
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Field
from django import forms
from django.contrib import messages
from django.contrib.auth.models import User
from django.shortcuts import redirect
Expand All @@ -8,16 +12,71 @@

from zds.gallery.models import UserGallery, GALLERY_WRITE
from zds.member.decorator import LoggedWithReadWriteHability
from zds.member.models import Profile
from zds.member.utils import get_bot_account
from zds.mp.models import is_reachable
from zds.tutorialv2 import signals

from zds.tutorialv2.forms import AuthorForm, RemoveAuthorForm
from zds.tutorialv2.mixins import SingleContentFormViewMixin
from zds.utils.models import get_hat_from_settings
from zds.mp.utils import send_mp


class AuthorForm(forms.Form):
username = forms.CharField(label=_("Auteurs à ajouter séparés d'une virgule."), required=True)

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_class = "content-wrapper"
self.helper.form_method = "post"
self.helper.layout = Layout(
Field("username"),
StrictButton(_("Ajouter"), type="submit"),
)

def clean_username(self):
"""Check every username and send it to the cleaned_data['user'] list
:return: a dictionary of all treated data with the users key added
"""
cleaned_data = super().clean()
users = []
if cleaned_data.get("username"):
for username in cleaned_data.get("username").split(","):
user = (
Profile.objects.contactable_members()
.filter(user__username__iexact=username.strip().lower())
.first()
)
if user is not None:
users.append(user.user)
if len(users) > 0:
cleaned_data["users"] = users
return cleaned_data

def is_valid(self):
return super().is_valid() and "users" in self.clean()


class RemoveAuthorForm(AuthorForm):
def clean_username(self):
"""Check every username and send it to the cleaned_data['user'] list
:return: a dictionary of all treated data with the users key added
"""
cleaned_data = super(AuthorForm, self).clean()
users = []
for username in cleaned_data.get("username").split(","):
# we can remove all users (bots inclued)
user = Profile.objects.filter(user__username__iexact=username.strip().lower()).first()
if user is not None:
users.append(user.user)
if len(users) > 0:
cleaned_data["users"] = users
return cleaned_data


class AddAuthorToContent(LoggedWithReadWriteHability, SingleContentFormViewMixin):
must_be_author = True
form_class = AuthorForm
Expand Down

0 comments on commit 06f16f3

Please sign in to comment.