Skip to content

Commit

Permalink
Corrige la gestion des unes (fix #4444)
Browse files Browse the repository at this point in the history
  • Loading branch information
pierre-24 committed Aug 4, 2017
1 parent a7ef93e commit 2ba83b0
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 55 deletions.
2 changes: 2 additions & 0 deletions templates/featured/index.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{% extends "featured/base.html" %}
{% load i18n %}
{% load remove_url_protocole %}
{% load date %}

{% block title %}
{% trans "Liste des unes" %}
Expand Down Expand Up @@ -45,6 +46,7 @@
class="topic-image"
>
<span class="topic-title">{{ featured_resource.title }}</span>
<span class="topic-subtitle">{% trans 'Publiée' %} {{ featured_resource.pubdate|format_date:True|lower }}</span>
</a>
</div>
</div>
Expand Down
37 changes: 25 additions & 12 deletions zds/featured/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class FeaturedResourceForm(forms.ModelForm):
class Meta:
model = FeaturedResource

fields = ['title', 'type', 'authors', 'image_url', 'url', 'pubdate']
fields = ['title', 'type', 'authors', 'image_url', 'url']

widgets = {
'title': forms.TextInput(
Expand Down Expand Up @@ -44,12 +44,6 @@ class Meta:
attrs={
'placeholder': _(u'Lien vers la ressource.')
}
),

'pubdate': forms.DateTimeInput(
attrs={
'placeholder': _(u'Exemple : 2016-12-25 00:00:00')
}
)
}

Expand All @@ -59,25 +53,44 @@ class Meta:
required=False
)

pubdate = forms.DateTimeField(
label=_(u'Date de publication'),
input_formats=[
'%d/%m/%Y %H:%M:%S', '%Y-%m-%d %H:%M:%S', '%Y-%m-%d %H:%M', '%d/%m/%Y %H:%M', '%Y-%m-%d', '%d/%m/%Y'],
widget=forms.DateTimeInput(
attrs={'placeholder': _(u'Exemple : 25/12/2016 10:00')},
format='%d/%m/%Y %H:%M:%S'
)
)

def __init__(self, *args, **kwargs):
hide_major_update_field = kwargs.pop('hide_major_update_field', False)

super(FeaturedResourceForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_class = 'content-wrapper'
self.helper.form_method = 'post'
self.helper.form_action = reverse('featured-resource-create')

self.helper.layout = Layout(
fields = [
Field('title'),
Field('type'),
Field('authors'),
Field('image_url'),
Field('url'),
Field('major_update'),
Field('url')
]

if not hide_major_update_field:
fields.append(Field('major_update'))

fields.extend([
Field('pubdate'),
ButtonHolder(
StrictButton(_(u'Enregistrer'), type='submit'),
),
)
)
])

self.helper.layout = Layout(*fields)


class FeaturedMessageForm(forms.ModelForm):
Expand Down
102 changes: 73 additions & 29 deletions zds/featured/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from zds.member.factories import StaffProfileFactory, ProfileFactory
from zds.featured.factories import FeaturedResourceFactory
from zds.featured.models import FeaturedResource, FeaturedMessage
from datetime import datetime, date


stringof2001chars = 'http://url.com/'
Expand Down Expand Up @@ -52,25 +53,44 @@ def test_success_create_featured(self):
username=staff.user.username,
password='hostel77'
)
self.assertTrue(login_check)

self.assertTrue(login_check)
self.assertEqual(0, FeaturedResource.objects.all().count())
response = self.client.post(
reverse('featured-resource-create'),
{
'title': 'title',
'type': 'type',
'image_url': 'image_url',
'url': 'url',
'authors': staff.user.username,
'pubdate': '2016-12-25 00:00:00'
},
follow=True
)

pubdate = date(2016, 1, 1).strftime('%d/%m/%Y %H:%M:%S')

fields = {
'title': 'title',
'type': 'type',
'image_url': 'http://test.com/image.png',
'url': 'http://test.com',
'authors': staff.user.username,
'pubdate': pubdate
}

response = self.client.post(reverse('featured-resource-create'), fields, follow=True)

self.assertEqual(200, response.status_code)
self.assertEqual(1, FeaturedResource.objects.all().count())

featured = FeaturedResource.objects.first()

for field, value in fields.items():
if field != 'pubdate':
self.assertEqual(value, getattr(featured, field), msg='Error on {}'.format(field))
else:
self.assertEqual(value, featured.pubdate.strftime('%d/%m/%Y %H:%M:%S'))

# now with major_update
fields['major_update'] = 'on'

response = self.client.post(reverse('featured-resource-create'), fields, follow=True)
self.assertEqual(200, response.status_code)
self.assertEqual(2, FeaturedResource.objects.all().count())

featured = FeaturedResource.objects.last()
self.assertTrue((datetime.now() - featured.pubdate).total_seconds() < 10)

def test_failure_create_featured_with_unauthenticated_user(self):
response = self.client.get(reverse('featured-resource-create'))

Expand Down Expand Up @@ -103,7 +123,7 @@ def test_failure_too_long_url(self):
'title': 'title',
'type': 'type',
'image_url': stringof2001chars,
'url': 'url',
'url': 'http://test.com',
'authors': staff.user.username
},
follow=True
Expand All @@ -117,7 +137,7 @@ def test_failure_too_long_url(self):
{
'title': 'title',
'type': 'type',
'image_url': 'url',
'image_url': 'http://test.com/image.png',
'url': stringof2001chars,
'authors': staff.user.username
},
Expand All @@ -139,21 +159,45 @@ def test_success_update_featured(self):

news = FeaturedResourceFactory()
self.assertEqual(1, FeaturedResource.objects.all().count())
response = self.client.post(
reverse('featured-resource-update', args=[news.pk]),
{
'title': 'title',
'type': 'type',
'image_url': 'image_url',
'url': 'url',
'authors': staff.user.username
},
follow=True
)

old_featured = FeaturedResource.objects.first()

pubdate = date(2016, 1, 1).strftime('%d/%m/%Y %H:%M:%S')

fields = {
'title': 'title',
'type': 'type',
'image_url': 'http://test.com/image.png',
'url': 'http://test.com',
'authors': staff.user.username,
'pubdate': pubdate
}

response = self.client.post(reverse('featured-resource-update', args=[news.pk]), fields, follow=True)

self.assertEqual(200, response.status_code)
self.assertEqual(1, FeaturedResource.objects.all().count())

featured = FeaturedResource.objects.first()

for field, value in fields.items():
self.assertNotEqual(getattr(featured, field), getattr(old_featured, field))

if field != 'pubdate':
self.assertEqual(value, getattr(featured, field), msg='Error on {}'.format(field))
else:
self.assertEqual(value, featured.pubdate.strftime('%d/%m/%Y %H:%M:%S'))

# now with major_update
self.assertFalse((datetime.now() - featured.pubdate).total_seconds() < 10)

fields['major_update'] = 'on'

response = self.client.post(reverse('featured-resource-update', args=[news.pk]), fields, follow=True)
self.assertEqual(200, response.status_code)
featured = FeaturedResource.objects.first()
self.assertTrue((datetime.now() - featured.pubdate).total_seconds() < 10)

def test_failure_create_featured_with_unauthenticated_user(self):
response = self.client.get(reverse('featured-resource-update', args=[42]))

Expand Down Expand Up @@ -266,7 +310,7 @@ def test_success_list_create_message(self):
reverse('featured-message-create'),
{
'message': 'message',
'url': 'url',
'url': 'http://test.com',
},
follow=True
)
Expand All @@ -286,7 +330,7 @@ def test_create_only_one_message_in_system(self):
reverse('featured-message-create'),
{
'message': 'message',
'url': 'url',
'url': 'http://test.com',
},
follow=True
)
Expand All @@ -298,7 +342,7 @@ def test_create_only_one_message_in_system(self):
reverse('featured-message-create'),
{
'message': 'message',
'url': 'url',
'url': 'http://test.com',
},
follow=True
)
Expand Down
36 changes: 22 additions & 14 deletions zds/featured/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,21 @@ def post(self, request, *args, **kwargs):

return render(request, self.template_name, {'form': form})

def form_valid(self, form):
def get_form_kwargs(self):
return {'hide_major_update_field': True}

def form_valid(self, form):
featured_resource = FeaturedResource()
featured_resource.title = form.data.get('title')
featured_resource.type = form.data.get('type')
featured_resource.authors = form.data.get('authors')
featured_resource.image_url = form.data.get('image_url')
featured_resource.url = form.data.get('url')
featured_resource.pubdate = form.data.get('pubdate')
featured_resource.title = form.cleaned_data.get('title')
featured_resource.type = form.cleaned_data.get('type')
featured_resource.authors = form.cleaned_data.get('authors')
featured_resource.image_url = form.cleaned_data.get('image_url')
featured_resource.url = form.cleaned_data.get('url')
if form.cleaned_data.get('major_update', False):
featured_resource.pubdate = datetime.now()
else:
featured_resource.pubdate = form.cleaned_data.get('pubdate')

featured_resource.save()

return redirect(reverse('featured-resource-list'))
Expand Down Expand Up @@ -108,16 +114,18 @@ def post(self, request, *args, **kwargs):

def form_valid(self, form):

self.featured_resource.title = form.data.get('title')
self.featured_resource.type = form.data.get('type')
self.featured_resource.authors = form.data.get('authors')
self.featured_resource.image_url = form.data.get('image_url')
self.featured_resource.url = form.data.get('url')
if form.data.get('major_update') is not None:
self.featured_resource.title = form.cleaned_data.get('title')
self.featured_resource.type = form.cleaned_data.get('type')
self.featured_resource.authors = form.cleaned_data.get('authors')
self.featured_resource.image_url = form.cleaned_data.get('image_url')
self.featured_resource.url = form.cleaned_data.get('url')
if form.cleaned_data.get('major_update', False):
self.featured_resource.pubdate = datetime.now()
else:
self.featured_resource.pubdate = form.cleaned_data.get('pubdate')

self.featured_resource.save()
return redirect(reverse('homepage'))
return redirect(reverse('featured-resource-list'))

def get_form(self, form_class=FeaturedResourceForm):
form = self.form_class(self.request.POST)
Expand Down

0 comments on commit 2ba83b0

Please sign in to comment.