def upload_screenshot(self, userfile): if not userinfo.is_admin(): controller.http_error(403) username = controller.session('login_username') data = userfile.file.read() filename = os.path.join(apt_portal.base_dir, '../media/screens/%s_upload.png' % username) thumb_filename = os.path.join( apt_portal.base_dir, '../media/screens/%s_upload_t.png' % username) return_thumb = '../media/screens/%s_upload_t.png' % username f = open(filename, 'wb') f.write(data) f.close() img_type = imghdr.what(filename) if img_type != 'png': os.unlink(filename) return "ERROR: File format is not PNG!" # Create the thumbnail to present on the form # Calculate size to maintain aspect ratio size = 260, 205 im = Image.open(filename) width = im.size[0] height = im.size[1] newwidth = int(size[0]) newheight = int(height * (newwidth / float(width))) if newheight > int(size[1]): newheight = int(size[1]) newwidth = int(width * (newheight / float(height))) size = newwidth, newheight # Resize and save the image im.thumbnail(size, Image.ANTIALIAS) im.save(thumb_filename) return "/" + return_thumb
def upload_screenshot(self, userfile): if not userinfo.is_admin(): controller.http_error(403) username = controller.session('login_username') data = userfile.file.read() filename = os.path.join(apt_portal.base_dir , '../media/screens/%s_upload.png' % username) thumb_filename = os.path.join(apt_portal.base_dir , '../media/screens/%s_upload_t.png' % username) return_thumb = '../media/screens/%s_upload_t.png' % username f = open(filename, 'wb') f.write(data) f.close() img_type = imghdr.what(filename) if img_type != 'png': os.unlink(filename) return "ERROR: File format is not PNG!" # Create the thumbnail to present on the form # Calculate size to maintain aspect ratio size = 260, 205 im = Image.open(filename) width = im.size[0] height = im.size[1] newwidth = int(size[0]) newheight = int(height*(newwidth/float(width))) if newheight > int(size[1]): newheight = int(size[1]) newwidth = int(width*(newheight/float(height))) size = newwidth, newheight # Resize and save the image im.thumbnail(size, Image.ANTIALIAS) im.save(thumb_filename) return "/"+return_thumb
def edit_submit(self, id, source_package, name, homepage, license, \ descr, video_link, category): """ Add/Edit an application record - submit """ if not userinfo.is_admin(): controller.http_redirect(controller.base_url() + '/login/') # Keep category query on top to avoid autoflush cat = ApplicationsCategory.query.filter_by(name=category).first() application = app_by_id(id) or Application() application.source_package = source_package or None application.name = name application.homepage = homepage application.descr = descr.decode('utf-8') application.license = license application.video_link = video_link if cat: application.category = cat else: return "ERROR: Could not find category", category try: database.commit() except IntegrityError as e: session.rollback() return "ERROR: Unable to update" id = application.id # db operation was succesful, update the screenshot if submited username = controller.session('login_username') filename = os.path.join(apt_portal.base_dir, '../media/screens/%s_upload.png' % username) thumb_filename = os.path.join( apt_portal.base_dir, '../media/screens/%s_upload_t.png' % username) # There is a screenshot image to be uploaded if os.path.exists(filename): screen_img_dir = os.path.join(apt_portal.base_dir, "../media/screens/%d" % id) if not os.path.isdir(screen_img_dir): os.makedirs(screen_img_dir, 0755) dest = "%s/%s.png" % (screen_img_dir, id) thumb_dest = "%s/%d_t.png" % (screen_img_dir, id) os.rename(filename, dest) os.chmod(dest, 0644) os.rename(thumb_filename, thumb_dest) os.chmod(thumb_dest, 0644) return "OK " + str(application.id)
def edit_submit(self, id, source_package, name, homepage, license, \ descr, video_link, category): """ Add/Edit an application record - submit """ if not userinfo.is_admin(): controller.http_redirect(controller.base_url()+'/login/') # Keep category query on top to avoid autoflush cat = ApplicationsCategory.query.filter_by(name=category).first() application = app_by_id(id) or Application() application.source_package = source_package or None application.name = name application.homepage = homepage application.descr = descr.decode('utf-8') application.license = license application.video_link = video_link if cat: application.category = cat else: return "ERROR: Could not find category", category try: database.commit() except IntegrityError as e: session.rollback() return "ERROR: Unable to update" id = application.id # db operation was succesful, update the screenshot if submited username = controller.session('login_username') filename = os.path.join(apt_portal.base_dir , '../media/screens/%s_upload.png' % username) thumb_filename = os.path.join(apt_portal.base_dir, '../media/screens/%s_upload_t.png' % username) # There is a screenshot image to be uploaded if os.path.exists(filename): screen_img_dir = os.path.join(apt_portal.base_dir, "../media/screens/%d" % id) if not os.path.isdir(screen_img_dir): os.makedirs(screen_img_dir, 0755) dest = "%s/%s.png" % (screen_img_dir, id) thumb_dest = "%s/%d_t.png" % (screen_img_dir, id) os.rename(filename, dest) os.chmod(dest, 0644) os.rename(thumb_filename, thumb_dest) os.chmod(thumb_dest, 0644) return "OK "+str(application.id)
def render(template_name, **kwargs): global _template_lookup, _media_base_url mytemplate = _template_lookup.get_template(template_name) # Global functions kwargs["_"] = _ kwargs["session"] = controller.session # Check if we need to use a lang prefix kwargs["pagename"] = pagename() kwargs["base_url"] = controller.base_url() kwargs["self_url"] = controller.self_url() kwargs["release"] = controller.selected_release kwargs["media_base_url"] = _media_base_url kwargs["current_release"] = controller.current_release kwargs["login_username"] = controller.session('login_username') start_t = time.time() template_output = mytemplate.render(**kwargs) stop_t = time.time() if not template_name.endswith('.mail'): template_output += '\n<!-- Template %s rendering took %0.3f ms -->\n' % \ (template_name, (stop_t-start_t)*1000.0) return template_output