Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 3 additions & 21 deletions zds/member/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,27 +277,9 @@ def __init__(self, *args, **kwargs):
# to get initial value form checkbox show email
initial = kwargs.get("initial", {})
self.fields["options"].initial = ""

if "show_sign" in initial and initial["show_sign"]:
self.fields["options"].initial += "show_sign"

if "is_hover_enabled" in initial and initial["is_hover_enabled"]:
self.fields["options"].initial += "is_hover_enabled"

if "allow_temp_visual_changes" in initial and initial["allow_temp_visual_changes"]:
self.fields["options"].initial += "allow_temp_visual_changes"

if "show_markdown_help" in initial and initial["show_markdown_help"]:
self.fields["options"].initial += "show_markdown_help"

if "email_for_answer" in initial and initial["email_for_answer"]:
self.fields["options"].initial += "email_for_answer"

if "email_for_new_mp" in initial and initial["email_for_new_mp"]:
self.fields["options"].initial += "email_for_new_mp"

if "hide_forum_activity" in initial and initial["hide_forum_activity"]:
self.fields["options"].initial += "hide_forum_activity"
for option in [m[0] for m in self.multi_choices]:
if option in initial and initial[option]:
self.fields["options"].initial += option

layout = Layout(
IncludeEasyMDE(),
Expand Down
5 changes: 5 additions & 0 deletions zds/member/tests/tests_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,11 @@ def test_too_long_signature_miniprofile_form(self):
form = MiniProfileForm(data=data)
self.assertFalse(form.is_valid())

def test_missing_non_required_fields_miniprofile_form(self):
data = {}
form = MiniProfileForm(data=data)
self.assertTrue(form.is_valid())


class ProfileFormTest(TestCase):
"""
Expand Down
75 changes: 75 additions & 0 deletions zds/member/tests/tests_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
from django.test import TestCase
from django.urls import reverse

from zds.member.tests.factories import ProfileFactory


class UpdateMemberViewTest(TestCase):
def setUp(self):
self.profile = ProfileFactory()

def test_full_form(self):
self.client.force_login(self.profile.user)

new_biography = "Biography"
new_site = "https://perdu.com"
new_avatar_url = "https://site.foo/img.png"
new_sign = "My signature"

response = self.client.post(
reverse("update-member"),
{"biography": new_biography, "site": new_site, "avatar_url": new_avatar_url, "sign": new_sign},
)
self.assertEqual(302, response.status_code)

self.profile.refresh_from_db()

self.assertEqual(self.profile.biography, new_biography)
self.assertEqual(self.profile.site, new_site)
self.assertEqual(self.profile.avatar_url, new_avatar_url)
self.assertEqual(self.profile.sign, new_sign)

def test_blank_form(self):
self.client.force_login(self.profile.user)

self.profile.biography = "Biography"
self.profile.site = "https://perdu.com"
self.profile.avatar_url = "https://site.foo/img.png"
self.profile.sign = "My signature"
self.profile.save()

response = self.client.post(
reverse("update-member"), {"biography": "", "site": "", "avatar_url": "", "sign": ""}
)
self.assertEqual(302, response.status_code)

self.profile.refresh_from_db()

self.assertEqual(self.profile.biography, "")
self.assertEqual(self.profile.site, "")
self.assertEqual(self.profile.avatar_url, "")
self.assertEqual(self.profile.sign, "")

def test_empty_form(self):
self.client.force_login(self.profile.user)

initial_biography = "Biography"
initial_site = "https://perdu.com"
initial_avatar_url = "https://site.foo/img.png"
initial_sign = "My signature"

self.profile.biography = initial_biography
self.profile.site = initial_site
self.profile.avatar_url = initial_avatar_url
self.profile.sign = initial_sign
self.profile.save()

response = self.client.post(reverse("update-member"), {})
self.assertEqual(302, response.status_code)

self.profile.refresh_from_db()

self.assertEqual(self.profile.biography, initial_biography)
self.assertEqual(self.profile.site, initial_site)
self.assertEqual(self.profile.avatar_url, initial_avatar_url)
self.assertEqual(self.profile.sign, initial_sign)
18 changes: 6 additions & 12 deletions zds/member/views/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,19 +267,13 @@ def form_valid(self, form):
return response

def update_profile(self, profile, form):
for field in ["biography", "site", "avatar_url", "sign", "licence"]:
if field in form.data:
profile.__setattr__(field, form.cleaned_data.get(field, ""))

cleaned_data_options = form.cleaned_data.get("options")
profile.biography = form.data["biography"]
profile.site = form.data["site"]
profile.show_sign = "show_sign" in cleaned_data_options
profile.is_hover_enabled = "is_hover_enabled" in cleaned_data_options
profile.allow_temp_visual_changes = "allow_temp_visual_changes" in cleaned_data_options
profile.show_markdown_help = "show_markdown_help" in cleaned_data_options
profile.email_for_answer = "email_for_answer" in cleaned_data_options
profile.email_for_new_mp = "email_for_new_mp" in cleaned_data_options
profile.hide_forum_activity = "hide_forum_activity" in cleaned_data_options
profile.avatar_url = form.data["avatar_url"]
profile.sign = form.data["sign"]
profile.licence = form.cleaned_data["licence"]
for option in [m[0] for m in self.form_class.multi_choices]:
profile.__setattr__(option, option in cleaned_data_options)

def get_success_url(self):
return reverse("update-member")
Expand Down