Skip to content

Commit

Permalink
Ajoute le message de commit aux formulaires
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnaud-D committed Sep 23, 2024
1 parent edfb494 commit 9900318
Showing 1 changed file with 45 additions and 13 deletions.
58 changes: 45 additions & 13 deletions zds/tutorialv2/views/contents.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

from crispy_forms.bootstrap import StrictButton
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Field, HTML, Div
from crispy_forms.layout import Layout, Field, HTML, ButtonHolder
from django.conf import settings
from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin
from django.db import transaction
from django.forms import forms, CharField, Textarea
from django.forms import forms, CharField, Textarea, TextInput
from django.shortcuts import redirect
from django.template.loader import render_to_string
from django.urls import reverse
Expand Down Expand Up @@ -248,6 +248,13 @@ class EditIntroductionForm(forms.Form):
),
)

commit_message = CharField(
label=_("Message de suivi"),
max_length=400,
required=False,
widget=TextInput(attrs={"placeholder": _("Un résumé de vos ajouts et modifications.")}),
)

def __init__(self, versioned_content, *args, **kwargs):
super().__init__(*args, **kwargs)

Expand All @@ -258,13 +265,14 @@ def __init__(self, versioned_content, *args, **kwargs):
self.helper.layout = Layout(
IncludeEasyMDE(),
Field("introduction"),
Div(
StrictButton(_("Modifier"), type="submit"),
ButtonHolder(
StrictButton(_("Aperçu"), type="preview", name="preview", css_class="btn btn-grey preview-btn"),
),
HTML(
"""{% if form.introduction.value %}{% include "misc/preview.part.html" with text=form.introduction.value %}{% endif %}"""
),
Field("commit_message"),
ButtonHolder(StrictButton(_("Modifier"), type="submit")),
)


Expand Down Expand Up @@ -294,22 +302,30 @@ def get_form_kwargs(self):
def form_valid(self, form):
publishable = self.object
versioned = self.versioned_object
sha = self.update_introduction_in_repository(publishable, versioned, form.cleaned_data["introduction"])

commit_message = "Modification de l'introduction"
if form.cleaned_data["commit_message"] != "":
commit_message = form.cleaned_data["commit_message"]

sha = self.update_introduction_in_repository(
publishable, versioned, form.cleaned_data["introduction"], commit_message
)
self.update_sha_draft(publishable, sha)

messages.success(self.request, self.success_message)
return super().form_valid(form)

def get_success_url(self):
return reverse("content:view", args=[self.object.pk, self.object.slug])

@staticmethod
def update_introduction_in_repository(publishable_content, versioned_content, introduction):
def update_introduction_in_repository(publishable_content, versioned_content, introduction, commit_message):
sha = versioned_content.repo_update_top_container(
publishable_content.title,
publishable_content.slug,
introduction,
versioned_content.get_conclusion(),
"Modification de l'introduction",
commit_message,
)
return sha

Expand All @@ -328,6 +344,13 @@ class EditConclusionForm(forms.Form):
),
)

commit_message = CharField(
label=_("Message de suivi"),
max_length=400,
required=False,
widget=TextInput(attrs={"placeholder": _("Un résumé de vos ajouts et modifications.")}),
)

def __init__(self, versioned_content, *args, **kwargs):
super().__init__(*args, **kwargs)

Expand All @@ -338,13 +361,14 @@ def __init__(self, versioned_content, *args, **kwargs):
self.helper.layout = Layout(
IncludeEasyMDE(),
Field("conclusion"),
Div(
StrictButton(_("Modifier"), type="submit"),
StrictButton(_("Aperçu"), type="preview", name="preview", css_class="btn btn-grey preview-btn"),
ButtonHolder(
StrictButton(_("Aperçu"), type="preview", name="preview", css_class="btn btn-grey preview-btn")
),
HTML(
"""{% if form.conclusion.value %}{% include "misc/preview.part.html" with text=form.conclusion.value %}{% endif %}"""
),
Field("commit_message"),
ButtonHolder(StrictButton(_("Modifier"), type="submit")),
)


Expand Down Expand Up @@ -374,22 +398,30 @@ def get_form_kwargs(self):
def form_valid(self, form):
publishable = self.object
versioned = self.versioned_object
sha = self.update_conclusion_in_repository(publishable, versioned, form.cleaned_data["conclusion"])

commit_message = "Modification de la conclusion"
if form.cleaned_data["commit_message"] != "":
commit_message = form.cleaned_data["commit_message"]

sha = self.update_conclusion_in_repository(
publishable, versioned, form.cleaned_data["conclusion"], commit_message
)
self.update_sha_draft(publishable, sha)

messages.success(self.request, self.success_message)
return super().form_valid(form)

def get_success_url(self):
return reverse("content:view", args=[self.object.pk, self.object.slug])

@staticmethod
def update_conclusion_in_repository(publishable_content, versioned_content, conclusion):
def update_conclusion_in_repository(publishable_content, versioned_content, conclusion, commit_message):
sha = versioned_content.repo_update_top_container(
publishable_content.title,
publishable_content.slug,
versioned_content.get_introduction(),
conclusion,
"Modification de la conclusion",
commit_message,
)
return sha

Expand Down

0 comments on commit 9900318

Please sign in to comment.