You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by br...@apache.org on 2021/10/15 16:08:44 UTC

[allura] 01/02: updated the flash message if the picture upload raises an exception

This is an automated email from the ASF dual-hosted git repository.

brondsem pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/allura.git

commit 24f5b496ff21a2e91e476b8db352018b89b4762d
Author: Guillermo Cruz <gu...@slashdotmedia.com>
AuthorDate: Tue Oct 12 14:19:21 2021 -0600

    updated the flash message if the picture upload raises an exception
---
 Allura/allura/controllers/project.py  |  8 ++++++--
 Allura/allura/ext/admin/admin_main.py | 14 +++++++++++---
 Allura/allura/model/project.py        | 25 ++++++++++++++-----------
 3 files changed, 31 insertions(+), 16 deletions(-)

diff --git a/Allura/allura/controllers/project.py b/Allura/allura/controllers/project.py
index 9d01eae..3e8a4b4 100644
--- a/Allura/allura/controllers/project.py
+++ b/Allura/allura/controllers/project.py
@@ -679,8 +679,12 @@ class NeighborhoodAdminController(object):
             if self.neighborhood.icon:
                 self.neighborhood.icon.delete()
                 M.ProjectFile.query.remove(dict(project_id=c.project._id, category=re.compile(r'^icon')))
-            M.AuditLog.log('update neighborhood icon')
-            c.project.save_icon(icon.filename, icon.file, content_type=icon.type)
+            save_icon = c.project.save_icon(icon.filename, icon.file, content_type=icon.type)
+            if save_icon:
+                M.AuditLog.log('update neighborhood icon')
+            else:
+                M.AuditLog.log('could not update neighborhood icon')
+                flash("There's a problem with the uploaded image", 'error')
         redirect('overview')
 
     @expose('jinja:allura:templates/neighborhood_help.html')
diff --git a/Allura/allura/ext/admin/admin_main.py b/Allura/allura/ext/admin/admin_main.py
index 064108e..079f60f 100644
--- a/Allura/allura/ext/admin/admin_main.py
+++ b/Allura/allura/ext/admin/admin_main.py
@@ -328,6 +328,8 @@ class ProjectAdminController(BaseController):
                features=None,
                **kw):
         require_access(c.project, 'update')
+        flash_status = 'success'
+        flash_message = 'Form values saved'
 
         if removal != c.project.removal:
             M.AuditLog.log('change project removal status to %s', removal)
@@ -405,10 +407,16 @@ class ProjectAdminController(BaseController):
         if icon is not None and icon != b'':
             if c.project.icon:
                 M.ProjectFile.query.remove(dict(project_id=c.project._id, category=re.compile(r'^icon')))
-            M.AuditLog.log('update project icon')
-            c.project.save_icon(icon.filename, icon.file, content_type=icon.type)
+            save_icon = c.project.save_icon(icon.filename, icon.file, content_type=icon.type)
+            if not save_icon:
+                M.AuditLog.log('could not update project icon')
+                flash_message = f'{flash_message}, but image upload failed'
+                flash_status = 'warning'
+            else:
+                M.AuditLog.log('update project icon')
+
         g.post_event('project_updated')
-        flash('Saved', 'success')
+        flash(flash_message, flash_status)
         redirect('overview')
 
     def _add_trove(self, type, new_trove):
diff --git a/Allura/allura/model/project.py b/Allura/allura/model/project.py
index 694e0eb..1ef6973 100644
--- a/Allura/allura/model/project.py
+++ b/Allura/allura/model/project.py
@@ -392,19 +392,22 @@ class Project(SearchIndexable, MappedClass, ActivityNode, ActivityObject):
             original_meta=dict(project_id=self._id, category='icon_original'),
             convert_bmp=True,
         )
-        # store the dimensions so we don't have to read the whole image each time we need to know
-        icon_orig_img = PIL.Image.open(icon_orig.rfile())
+        if icon_orig:
+            # store the dimensions so we don't have to read the whole image each time we need to know
+            icon_orig_img = PIL.Image.open(icon_orig.rfile())
 
-        self.set_tool_data('allura', icon_original_size=icon_orig_img.size)
+            self.set_tool_data('allura', icon_original_size=icon_orig_img.size)
 
-        try:
-            # calc and save icon file hash, for better cache busting purposes
-            file_input.seek(0)
-            file_bytes = file_input.read()
-            file_sha256 = sha256(file_bytes).hexdigest()
-            self.set_tool_data('allura', icon_sha256=file_sha256)
-        except Exception as ex:
-            log.exception('Failed to calculate sha256 for icon file for {}'.format(self.shortname))
+            try:
+                # calc and save icon file hash, for better cache busting purposes
+                file_input.seek(0)
+                file_bytes = file_input.read()
+                file_sha256 = sha256(file_bytes).hexdigest()
+                self.set_tool_data('allura', icon_sha256=file_sha256)
+            except Exception as ex:
+                log.exception('Failed to calculate sha256 for icon file for {}'.format(self.shortname))
+            return True
+        return False
 
     @property
     def icon(self):