pwa: only accept JPEG and PNG for application icon files (#41211)

This commit is contained in:
Frédéric Péters 2020-04-01 10:35:52 +02:00
parent e6ff7d66f7
commit b561b38199
4 changed files with 20 additions and 2 deletions

View File

@ -15,6 +15,8 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from django import forms
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _
from .models import PwaSettings
@ -23,3 +25,13 @@ class PwaSettingsForm(forms.ModelForm):
class Meta:
model = PwaSettings
exclude = ('push_notifications_infos',)
def __init__(self, *args, **kwargs):
super(PwaSettingsForm, self).__init__(*args, **kwargs)
self.fields['application_icon'].widget.attrs = {'accept': 'image/jpeg,image/png'}
def clean_application_icon(self):
value = self.cleaned_data.get('application_icon')
if hasattr(value, 'content_type') and value.content_type not in ('image/jpeg', 'image/png'):
raise ValidationError(_('The application icon must be in JPEG or PNG format.'))
return value

View File

@ -15,6 +15,6 @@ class Migration(migrations.Migration):
migrations.AddField(
model_name='pwasettings',
name='application_icon',
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'),
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'),
),
]

View File

@ -44,7 +44,7 @@ class PwaSettings(models.Model):
blank=True)
application_icon = models.FileField(
verbose_name=_('Application Icon'),
help_text=_(u'Should be a square of at least 512×512 pixels.'),
help_text=_(u'Icon file must be in JPEG or PNG format, and should be a square of at least 512×512 pixels.'),
upload_to='pwa',
blank=True,
null=True)

View File

@ -194,6 +194,12 @@ def test_pwa_manager(app, admin_user):
resp = resp.form.submit().follow()
assert not PwaSettings.singleton().application_name
# try an icon in an invalid format
resp.form['application_icon'] = Upload('test.txt', b'hello', 'text/plain')
resp = resp.form.submit()
assert 'The application icon must be in JPEG or PNG format' in resp
assert PwaSettings.singleton().application_icon.name == 'pwa/test.png'
def test_pwa_offline_page(app):
PwaSettings.objects.all().delete()