def portfolio_add(site, host, netloc, csrf, logged_in, user_id_logged_in): """ Add portfolio post url """ # POST parameters site_id = int(request.POST.get('site')) user_id = int(request.POST.get('user')) title = request.POST.get('title') description = request.POST.get('description') # Upload portfolio image start_index = 1 try: portfolios = site.portfolios.order_by(Portfolio.id.desc()) last_portfolio = portfolios.get() start_index = last_portfolio.id + 1 except Portfolio.DoesNotExist: pass img_path = site.get_image_path(IMAGE_PATH) img_url = '%s/%s/' % (host, IMAGE_DIR) upload_response = upload_new_image(request, 'portfolio', start_index, img_path, img_url, ORIG_SIZE, NORM_SIZE, THUMB_SIZE) if not upload_response['status']: return upload_response # Upload data original_image = upload_response['original_filename'] normalized_image = upload_response['normalized_filename'] thumbnail_image = upload_response['thumbnail_filename'] original_url = upload_response['original_url'] normalized_url = upload_response['normalized_url'] thumbnail_url = upload_response['thumbnail_url'] # Create Portfolio portfolio_created = None with db.transaction(): try: portfolio_created = Portfolio.create( site=site, title=title, description=description, original_image=original_image, normalized_image=normalized_image, thumbnail_image=thumbnail_image) except IntegrityError as exp: if portfolio_created: portfolio_created.delete_instance(IMAGE_PATH) else: site_img_path = site.get_image_path(IMAGE_PATH) if original_image: original_file = os.path.join(site_img_path, original_image) if os.path.exists(original_file): os.remove(original_file) if normalized_image: normalized_file = os.path.join(site_img_path, normalized_image) if os.path.exists(normalized_file): os.remove(normalized_file) if thumbnail_image: thumbnail_file = os.path.join(site_img_path, thumbnail_image) if os.path.exists(thumbnail_file): os.remove(thumbnail_file) # Return error return dict(status=False, info='%s' % exp) # Lista de portfolios atualizada try: portfolio_list = template('portfolios_admin_list.html', site=site, host=host, csrf=csrf, img_url=img_url) except Exception as exp: return dict(status=False, info='%s' % exp) # Return OK return dict(status=True, info='Adicionado com sucesso', portfolio_id=portfolio_created.get_id(), original=original_url, normalized=normalized_url, thumbnail=thumbnail_url, portfolio_list=portfolio_list)