Skip to content

Commit

Permalink
Corrige la gestion des unes (#4450)
Browse files Browse the repository at this point in the history
* Corrige la gestion des unes (fix #4444)

* CBV that!

* Ajoute un input type="datetime-local"
  • Loading branch information
pierre-24 authored and sandhose committed Sep 18, 2017
1 parent 4705a55 commit 884cd5e
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 124 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
41 changes: 29 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,48 @@ class Meta:
required=False
)

pubdate = forms.DateTimeField(
label=_(u'Date de publication (exemple: 25/12/2015 15:00 ou 2015-12-25T15:00)'),
input_formats=[
'%d/%m/%Y %H:%M:%S', '%Y-%m-%d %H:%M:%S', # full format with second
'%Y-%m-%dT%H:%M', # datetime field format
'%Y-%m-%d %H:%M', '%d/%m/%Y %H:%M', # without second
'%Y-%m-%d', '%d/%m/%Y' # day only
],
widget=forms.DateTimeInput(
attrs={'placeholder': _(u'Exemple : 25/12/2016 10:00'), 'type': 'datetime-local'},
format='%Y-%m-%dT%H:%M' # datetime field format
)
)

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
Loading

0 comments on commit 884cd5e

Please sign in to comment.