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 remove(self, package_id, list_id, confirm=None): """ Create a command to remove a package from the repository """ if not userinfo.is_admin(): controller.http_error(403) package_id = long(package_id) list_id = long(list_id) package = Package.query.filter_by(id = package_id).first() packagelist = PackageList.query.filter_by(id = list_id).first() if not package: return "Package %d not found" % package_id if not packagelist: return "List %d not found" % list_id if confirm != "Y": return template.render("package_remove.html" \ , package = package, packagelist = packagelist) source_package = package.source or package.package user = userinfo.find_user() action = "%s %s %s %s %s" % (user.email, 'remove' \ , packagelist.suite, source_package, package.version) time_now = time.strftime("%Y%m%d.%H%M%S", time.localtime()) filename = "%s_%s_%s" % (source_package, package.version, time_now) full_repos_cmd_dir = os.path.join(apt_portal.base_dir, '..', repos_commands_dir) if not os.path.isdir(full_repos_cmd_dir): return "%s directory is not available, " \ "repository commands are not supported" % \ full_repos_cmd_dir os.umask(002) f = open(os.path.join(full_repos_cmd_dir, filename), 'w') os.umask(022) f.write(action) f.close() return template.render("package_remove.html" \ , ticket_name = filename, confirm='Y')
def remove(self, package_id, list_id, confirm=None): """ Create a command to remove a package from the repository """ if not userinfo.is_admin(): controller.http_error(403) package_id = long(package_id) list_id = long(list_id) package = Package.query.filter_by(id=package_id).first() packagelist = PackageList.query.filter_by(id=list_id).first() if not package: return "Package %d not found" % package_id if not packagelist: return "List %d not found" % list_id if confirm != "Y": return template.render("package_remove.html" \ , package = package, packagelist = packagelist) source_package = package.source or package.package user = userinfo.find_user() action = "%s %s %s %s %s" % (user.email, 'remove' \ , packagelist.suite, source_package, package.version) time_now = time.strftime("%Y%m%d.%H%M%S", time.localtime()) filename = "%s_%s_%s" % (source_package, package.version, time_now) full_repos_cmd_dir = os.path.join(apt_portal.base_dir, '..', repos_commands_dir) if not os.path.isdir(full_repos_cmd_dir): return "%s directory is not available, " \ "repository commands are not supported" % \ full_repos_cmd_dir os.umask(002) f = open(os.path.join(full_repos_cmd_dir, filename), 'w') os.umask(022) f.write(action) f.close() return template.render("package_remove.html" \ , ticket_name = filename, confirm='Y')
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 copy(self, package_id, source_list_id, target_list): """ Create a command to copy a package to another repository """ if not userinfo.is_admin(): controller.http_error(403) package_id = long(package_id) source_list_id = long(source_list_id) package = Package.query.filter_by(id=package_id).first() source_packagelist = PackageList.query.filter_by( id=source_list_id).first() target_packagelist = PackageList.query.filter_by( suite=target_list).first() if not package: return "Package %d not found" % package_id if not source_packagelist: return "List %d not found" % list_id if target_list and not target_packagelist: return "Repository %s not found" % target_list repository_list = [] for plist in PackageList.query.all(): if plist.suite == source_packagelist.suite: continue if (plist.suite not in repository_list) and (plist.suite >= "t"): repository_list.append(plist.suite) if len(target_list) < 2: return template.render("package_copy.html" \ , package = package \ , source_packagelist = source_packagelist \ , target_packagelist = target_packagelist \ , repository_list = repository_list \ , package_id = package_id \ , source_list_id = source_list_id \ , target_list = target_list \ , asking = True \ ) source_package = package.source or package.package user = userinfo.find_user() action = "%s %s %s %s %s %s" % (user.email, 'copy' \ , target_packagelist.suite, source_packagelist.suite, source_package, package.version) time_now = time.strftime("%Y%m%d.%H%M%S", time.localtime()) filename = "%s_%s_%s" % (source_package, package.version, time_now) full_repos_cmd_dir = os.path.join(apt_portal.base_dir, '..', repos_commands_dir) if not os.path.isdir(full_repos_cmd_dir): return "%s directory is not available, " \ "repository commands are not supported" % \ full_repos_cmd_dir os.umask(002) f = open(os.path.join(full_repos_cmd_dir, filename), 'w') os.umask(022) f.write(action) f.close() return template.render("package_copy.html" \ , ticket_name = filename \ , asking = False \ )
def copy(self, package_id, source_list_id, target_list): """ Create a command to copy a package to another repository """ if not userinfo.is_admin(): controller.http_error(403) package_id = long(package_id) source_list_id = long(source_list_id) package = Package.query.filter_by(id = package_id).first() source_packagelist = PackageList.query.filter_by(id = source_list_id).first() target_packagelist = PackageList.query.filter_by(suite = target_list).first() if not package: return "Package %d not found" % package_id if not source_packagelist: return "List %d not found" % list_id if target_list and not target_packagelist: return "Repository %s not found" % target_list repository_list = [] for plist in PackageList.query.all(): if plist.suite == source_packagelist.suite: continue if ( plist.suite not in repository_list ) and ( plist.suite >= "t" ): repository_list.append(plist.suite) if len(target_list) < 2: return template.render("package_copy.html" \ , package = package \ , source_packagelist = source_packagelist \ , target_packagelist = target_packagelist \ , repository_list = repository_list \ , package_id = package_id \ , source_list_id = source_list_id \ , target_list = target_list \ , asking = True \ ) source_package = package.source or package.package user = userinfo.find_user() action = "%s %s %s %s %s %s" % (user.email, 'copy' \ , target_packagelist.suite, source_packagelist.suite, source_package, package.version) time_now = time.strftime("%Y%m%d.%H%M%S", time.localtime()) filename = "%s_%s_%s" % (source_package, package.version, time_now) full_repos_cmd_dir = os.path.join(apt_portal.base_dir, '..', repos_commands_dir) if not os.path.isdir(full_repos_cmd_dir): return "%s directory is not available, " \ "repository commands are not supported" % \ full_repos_cmd_dir os.umask(002) f = open(os.path.join(full_repos_cmd_dir, filename), 'w') os.umask(022) f.write(action) f.close() return template.render("package_copy.html" \ , ticket_name = filename \ , asking = False \ )
def edit(self, for_package_id = None, from_app_id = None): """ Add/Edit an application record, after submission the browser will be redirected to the referer page """ if not userinfo.is_admin(): controller.http_redirect(controller.base_url()+'/login/') # We need these to fill the combo box # do it here to avoid autoflush later categories = ApplicationsCategory.query.all() source_package = None application = None # the app info can be loaded from an app_id or for a package id if from_app_id: application = app_by_id(from_app_id) if not application: return "Application ID not found" elif for_package_id: package_id = for_package_id package = Package.query.filter_by(id = package_id).one() if not package: return "Package id %d not found" % package_id source_package = package.source or package.package if source_package: # we got a package hint application = Application.query.filter_by(\ source_package = source_package).first() if not application: # app was not found, create new application = Application() application.source_package = source_package # Automatically set app name to source package # usefull hint on "for_package_id" if not application.name: application.name = source_package # No changes here, save will be performed on edit_submit database.rollback() if sa_version.split(".") < ["0", "5", "0"]: database.clear() # Set the screenshot filename screenshot_filename = "" id = application.id if id: screenshot_filename = "/media/screens/%d/%d_t.png" % (id, id) return template.render("app_edit.html" \ , application = application \ , categories = categories \ , screenshot_filename = screenshot_filename \ )
def edit(self, for_package_id=None, from_app_id=None): """ Add/Edit an application record, after submission the browser will be redirected to the referer page """ if not userinfo.is_admin(): controller.http_redirect(controller.base_url() + '/login/') # We need these to fill the combo box # do it here to avoid autoflush later categories = ApplicationsCategory.query.all() source_package = None application = None # the app info can be loaded from an app_id or for a package id if from_app_id: application = app_by_id(from_app_id) if not application: return "Application ID not found" elif for_package_id: package_id = for_package_id package = Package.query.filter_by(id=package_id).one() if not package: return "Package id %d not found" % package_id source_package = package.source or package.package if source_package: # we got a package hint application = Application.query.filter_by(\ source_package = source_package).first() if not application: # app was not found, create new application = Application() application.source_package = source_package # Automatically set app name to source package # usefull hint on "for_package_id" if not application.name: application.name = source_package # No changes here, save will be performed on edit_submit database.rollback() if sa_version.split(".") < ["0", "5", "0"]: database.clear() # Set the screenshot filename screenshot_filename = "" id = application.id if id: screenshot_filename = "/media/screens/%d/%d_t.png" % (id, id) return template.render("app_edit.html" \ , application = application \ , categories = categories \ , screenshot_filename = screenshot_filename \ )
def set_class(self, package_id, install_class): """ Set the class_value for a package, select all packages which have a common name and version to the one identified by the package_id parameter """ if not userinfo.is_admin(): controller.http_error(403) package = Package.query.filter_by(id=package_id).one() package_list = Package.query.filter_by(\ package = package.package, version=package.version).all() for package in package_list: package.install_class = install_class return None
def index(self, q=None): """ The is the main page which presents the list of packages if no key (q) is specified only the packages needing an action are shown. """ if not userinfo.is_admin(): controller.http_redirect(controller.base_url() + '/login') class Stats(): total = 0 unclassified = 0 unlinked = 0 if q: db_packages = Package.query.filter_by(package=q).order_by(\ asc(Package.version), asc(Package.package)).all() else: db_packages = Package.query.order_by( \ asc(Package.version), asc(Package.package)).all() packages = {} last_package_version = None stats = Stats() # Walk the packages list, skip classified and linked for package in db_packages: package_version = package.package + "-" + package.version source_package = package.source or package.package if package_version == last_package_version: continue if len(package.lists) == 0: # package is not in a repos continue last_package_version = package_version stats.total += 1 app = None if package.install_class == None: stats.unclassified += 1 elif package.install_class == 'M': app = Application.query.filter_by( source_package=source_package).first() if not app: stats.unlinked += 1 if q or not package.install_class or \ (package.install_class=='M' and not app): if not source_package in packages.keys(): packages[source_package] = [] packages[source_package].append(package) ordered_packages = OrderedDict( sorted(packages.items(), key=lambda t: t[0])) return template.render("packages.html", packages=ordered_packages, stats=stats, q=q)
def set_class(self, package_id, install_class): """ Set the class_value for a package, select all packages which have a common name and version to the one identified by the package_id parameter """ if not userinfo.is_admin(): controller.http_error(403) package = Package.query.filter_by(id = package_id).one() package_list = Package.query.filter_by(\ package = package.package, version=package.version).all() for package in package_list: package.install_class = install_class return None
def index(self, q=None): """ The is the main page which presents the list of packages if no key (q) is specified only the packages needing an action are shown. """ if not userinfo.is_admin(): controller.http_redirect(controller.base_url()+'/login') class Stats(): total = 0 unclassified = 0 unlinked = 0 if q: db_packages = Package.query.filter_by(package=q).order_by(\ asc(Package.version), asc(Package.package)).all() else: db_packages = Package.query.order_by( \ asc(Package.version), asc(Package.package)).all() packages = {} last_package_version = None stats = Stats() # Walk the packages list, skip classified and linked for package in db_packages: package_version = package.package+"-"+package.version source_package = package.source or package.package if package_version == last_package_version: continue if len(package.lists) == 0: # package is not in a repos continue last_package_version = package_version stats.total += 1 app = None if package.install_class == None: stats.unclassified += 1 elif package.install_class == 'M': app = Application.query.filter_by( source_package = source_package ).first() if not app: stats.unlinked += 1 if q or not package.install_class or \ (package.install_class=='M' and not app): if not source_package in packages.keys(): packages[source_package] = [] packages[source_package].append(package) ordered_packages=OrderedDict(sorted(packages.items(), key=lambda t: t[0])) return template.render("packages.html", packages = ordered_packages , stats = stats, q=q )
def change_category(self, action, name): """ Add/Del a category """ if not userinfo.is_admin(): controller.http_error(403) if action == "Add": appcat = ApplicationsCategory(name = name) else: appcat = ApplicationsCategory.query.filter_by(name = name).first() if appcat: appcat.delete(); try: database.commit() except IntegrityError as e: pass
def search(self, q): """ Returns list of packages for the search box """ if not userinfo.is_admin(): controller.http_error(403) results = "" last_package = "" package_list = Package.query.filter(Package.package.like('%'+q+'%')).order_by(Package.package) for package in package_list: if package.package == last_package: continue last_package = package.package results += "%s|%d\n" % (package.package, package.id) return results
def change_category(self, action, name): """ Add/Del a category """ if not userinfo.is_admin(): controller.http_error(403) if action == "Add": appcat = ApplicationsCategory(name=name) else: appcat = ApplicationsCategory.query.filter_by(name=name).first() if appcat: appcat.delete() try: database.commit() except IntegrityError as e: pass
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 search(self, q): """ Returns list of packages for the search box """ if not userinfo.is_admin(): controller.http_error(403) results = "" last_package = "" package_list = Package.query.filter( Package.package.like('%' + q + '%')).order_by(Package.package) for package in package_list: if package.package == last_package: continue last_package = package.package results += "%s|%d\n" % (package.package, package.id) return results
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)