Skip to content

Commit

Permalink
Fix for SVG upload in sponsorship application (#2352)
Browse files Browse the repository at this point in the history
* changing imagefield to file field on sponsorupdateform and sponsorshipapplicationform. adding django file validator to these two and the sponsor model

* allow png upload

* adding test to assert that svg can be uploaded

* fix migration
  • Loading branch information
jessiebelle authored Jan 2, 2024
1 parent cbc6c14 commit 56d2c85
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
7 changes: 5 additions & 2 deletions sponsors/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django import forms
from django.conf import settings
from django.contrib.admin.widgets import AdminDateWidget
from django.core.validators import FileExtensionValidator
from django.db.models import Q
from django.utils import timezone
from django.utils.functional import cached_property
Expand Down Expand Up @@ -225,10 +226,11 @@ class SponsorshipApplicationForm(forms.Form):
help_text="For display on our sponsor webpage. High resolution PNG or JPG, smallest dimension no less than 256px",
required=False,
)
print_logo = forms.ImageField(
print_logo = forms.FileField(
label="Sponsor print logo",
help_text="For printed materials, signage, and projection. SVG or EPS",
required=False,
validators=[FileExtensionValidator(['eps', 'epsf' 'epsi', 'svg', 'png'])],
)

primary_phone = forms.CharField(
Expand Down Expand Up @@ -557,10 +559,11 @@ class SponsorUpdateForm(forms.ModelForm):
help_text="For display on our sponsor webpage. High resolution PNG or JPG, smallest dimension no less than 256px",
required=False,
)
print_logo = forms.ImageField(
print_logo = forms.FileField(
widget=forms.widgets.FileInput,
help_text="For printed materials, signage, and projection. SVG or EPS",
required=False,
validators=[FileExtensionValidator(['eps', 'epsf' 'epsi', 'svg', 'png'])],
)

def __init__(self, *args, **kwargs):
Expand Down
19 changes: 19 additions & 0 deletions sponsors/migrations/0099_auto_20231224_1854.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 2.2.24 on 2023-12-24 18:54

import django.core.validators
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('sponsors', '0098_auto_20231219_1910'),
]

operations = [
migrations.AlterField(
model_name='sponsor',
name='print_logo',
field=models.FileField(blank=True, help_text='For printed materials, signage, and projection. SVG or EPS', null=True, upload_to='sponsor_print_logos', validators=[django.core.validators.FileExtensionValidator(['eps', 'epsfepsi', 'svg', 'png'])], verbose_name='Print logo'),
),
]
2 changes: 2 additions & 0 deletions sponsors/models/sponsors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""
from allauth.account.models import EmailAddress
from django.conf import settings
from django.core.validators import FileExtensionValidator
from django.db import models
from django.core.exceptions import ObjectDoesNotExist
from django.template.defaultfilters import slugify
Expand Down Expand Up @@ -51,6 +52,7 @@ class Sponsor(ContentManageable):
)
print_logo = models.FileField(
upload_to="sponsor_print_logos",
validators=[FileExtensionValidator(['eps', 'epsf' 'epsi', 'svg', 'png'])],
blank=True,
null=True,
verbose_name="Print logo",
Expand Down
18 changes: 18 additions & 0 deletions sponsors/tests/test_forms.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from pathlib import Path

from django.core.files.uploadedfile import SimpleUploadedFile
from model_bakery import baker

from django.conf import settings
Expand Down Expand Up @@ -438,6 +441,21 @@ def test_create_sponsor_with_valid_data_for_non_required_inputs(
self.assertEqual(sponsor.landing_page_url, "https://companyx.com")
self.assertEqual(sponsor.twitter_handle, "@companyx")

def test_create_sponsor_with_svg_for_print_logo(
self,
):
tick_svg = Path(settings.STATICFILES_DIRS[0]) / "img"/"sponsors"/"tick.svg"
with tick_svg.open("rb") as fd:
uploaded_svg = SimpleUploadedFile("tick.svg", fd.read())
self.files["print_logo"] = uploaded_svg

form = SponsorshipApplicationForm(self.data, self.files)
self.assertTrue(form.is_valid(), form.errors)

sponsor = form.save()

self.assertTrue(sponsor.print_logo)

def test_use_previous_user_sponsor(self):
contact = baker.make(SponsorContact, user__email="foo@foo.com")
self.data = {"sponsor": contact.sponsor.id}
Expand Down

0 comments on commit 56d2c85

Please sign in to comment.