Skip to content
This repository was archived by the owner on Apr 29, 2022. It is now read-only.

Commit 7c3c9f7

Browse files
authored
Return proper exceptions when uploading big profile pictures (#1324)
* Return proper exceptions when uploading big profile pictures * Handle Attributerror since images are not required * Add alert in the html page * Update limit to FILE_UPLOAD_MAX_MEMORY_SIZE
1 parent d01f5e4 commit 7c3c9f7

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

conference/user_panel.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from model_utils import Choices
77

88
from django import forms
9+
from django.conf import settings
910
from django.conf.urls import url as re_path
1011
from django.contrib import messages
1112
from django.contrib.auth import views as auth_views
@@ -474,6 +475,15 @@ def clean_twitter(self):
474475

475476
return data
476477

478+
def clean_image(self):
479+
data = self.cleaned_data.get("image")
480+
try:
481+
if data.size > settings.PROFILE_PICTURE_MAX_SIZE:
482+
raise forms.ValidationError("Profile picture too large ( > 2.5mb )")
483+
except AttributeError:
484+
pass
485+
return data
486+
477487
def resolve_image_settings(self, selected_option, image_url, image):
478488
if selected_option == PICTURE_CHOICES.gravatar:
479489
image = None

pycon/settings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ def _(x):
5959

6060
SITE_DATA_ROOT = DATA_DIR + "/site"
6161

62+
PROFILE_PICTURE_MAX_SIZE = 2621440
63+
6264
# DATABASE
6365
# --------
6466
DATABASES = {

templates/conference/user_panel/profile_settings.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@
1313
<div class='row' style='margin-top: 3em'>
1414

1515
<div class="col-md-12">
16+
{% if profile_form.errors %}
17+
{% for field in profile_form %}
18+
{% for error in field.errors %}
19+
<div class="alert alert-danger">
20+
{{ error|escape }}
21+
</div>
22+
{% endfor %}
23+
{% endfor %}
24+
{% endif %}
1625
<form method="POST" action="." enctype="multipart/form-data">
1726
{% csrf_token %}
1827
{% crispy profile_form %}

0 commit comments

Comments
 (0)