Projet

Général

Profil

0001-pwa-only-accept-JPEG-and-PNG-for-application-icon-fi.patch

Frédéric Péters, 01 avril 2020 10:36

Télécharger (3,79 ko)

Voir les différences:

Subject: [PATCH] pwa: only accept JPEG and PNG for application icon files
 (#41211)

 combo/apps/pwa/forms.py                              | 12 ++++++++++++
 .../migrations/0004_pwasettings_application_icon.py  |  2 +-
 combo/apps/pwa/models.py                             |  2 +-
 tests/test_pwa.py                                    |  6 ++++++
 4 files changed, 20 insertions(+), 2 deletions(-)
combo/apps/pwa/forms.py
15 15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 16

  
17 17
from django import forms
18
from django.core.exceptions import ValidationError
19
from django.utils.translation import ugettext_lazy as _
18 20

  
19 21
from .models import PwaSettings
20 22

  
......
23 25
    class Meta:
24 26
        model = PwaSettings
25 27
        exclude = ('push_notifications_infos',)
28

  
29
    def __init__(self, *args, **kwargs):
30
        super(PwaSettingsForm, self).__init__(*args, **kwargs)
31
        self.fields['application_icon'].widget.attrs = {'accept': 'image/jpeg,image/png'}
32

  
33
    def clean_application_icon(self):
34
        value = self.cleaned_data.get('application_icon')
35
        if hasattr(value, 'content_type') and value.content_type not in ('image/jpeg', 'image/png'):
36
            raise ValidationError(_('The application icon must be in JPEG or PNG format.'))
37
        return value
combo/apps/pwa/migrations/0004_pwasettings_application_icon.py
15 15
        migrations.AddField(
16 16
            model_name='pwasettings',
17 17
            name='application_icon',
18
            field=models.FileField(blank=True, help_text='Should be a square of at least 512\xd7512 pixels.', null=True, upload_to=b'pwa', verbose_name='Application Icon'),
18
            field=models.FileField(blank=True, help_text='Icon file must be in JPEG or PNG format, and should be a square of at least 512\xd7512 pixels.', null=True, upload_to=b'pwa', verbose_name='Application Icon'),
19 19
        ),
20 20
    ]
combo/apps/pwa/models.py
44 44
            blank=True)
45 45
    application_icon = models.FileField(
46 46
            verbose_name=_('Application Icon'),
47
            help_text=_(u'Should be a square of at least 512×512 pixels.'),
47
            help_text=_(u'Icon file must be in JPEG or PNG format, and should be a square of at least 512×512 pixels.'),
48 48
            upload_to='pwa',
49 49
            blank=True,
50 50
            null=True)
tests/test_pwa.py
194 194
        resp = resp.form.submit().follow()
195 195
        assert not PwaSettings.singleton().application_name
196 196

  
197
        # try an icon in an invalid format
198
        resp.form['application_icon'] = Upload('test.txt', b'hello', 'text/plain')
199
        resp = resp.form.submit()
200
        assert 'The application icon must be in JPEG or PNG format' in resp
201
        assert PwaSettings.singleton().application_icon.name == 'pwa/test.png'
202

  
197 203

  
198 204
def test_pwa_offline_page(app):
199 205
    PwaSettings.objects.all().delete()
200
-