def create(self): """ Create a new badge in the system """ # Check if user is allowed to create a badge require.badge.create() import shutil label = request.params['badge-label'] description = request.params['badge-description'] image = request.POST['badge-image'] try: # Get upload directory for Badge and generate a random filename upload_dir = h.get_object_upload_dir(Badge) random_filename = h.get_uuid_filename(image.filename) # Open the filename and copy the uploaded image permanent_filename = os.path.join(upload_dir, random_filename) permanent_image = open(permanent_filename, 'w') shutil.copyfileobj(image.file, permanent_image) upload_image_path = h.upload(random_filename, Badge) # Close image files image.file.close() permanent_image.close() except OSError: upload_image_path = '' h.flash_error(_('Uploading files not supported at the moment.')) badge = Badge(label, upload_image_path, description, c.account) db.session.add(badge) db.session.commit() redirect( h.url_for(controller='badge', action='information', id=badge.id))