diff options
Diffstat (limited to 'app/presenters/packages/pypi')
4 files changed, 161 insertions, 96 deletions
diff --git a/app/presenters/packages/pypi/package_presenter.rb b/app/presenters/packages/pypi/package_presenter.rb deleted file mode 100644 index a779ce41cf9..00000000000 --- a/app/presenters/packages/pypi/package_presenter.rb +++ /dev/null @@ -1,96 +0,0 @@ -# frozen_string_literal: true - -# Display package version data acording to PyPI -# Simple API: https://warehouse.pypa.io/api-reference/legacy/#simple-project-api -module Packages - module Pypi - class PackagePresenter - include API::Helpers::RelatedResourcesHelpers - - def initialize(packages, project_or_group) - @packages = packages - @project_or_group = project_or_group - end - - # Returns the HTML body for PyPI simple API. - # Basically a list of package download links for a specific - # package - def body - <<-HTML - <!DOCTYPE html> - <html> - <head> - <title>Links for #{escape(name)}</title> - </head> - <body> - <h1>Links for #{escape(name)}</h1> - #{links} - </body> - </html> - HTML - end - - private - - def links - refs = [] - - @packages.map do |package| - package_files = package.installable_package_files - - package_files.each do |file| - url = build_pypi_package_path(file) - - refs << package_link(url, package.pypi_metadatum.required_python, file.file_name) - end - end - - refs.join - end - - def package_link(url, required_python, filename) - "<a href=\"#{url}\" data-requires-python=\"#{escape(required_python)}\">#{filename}</a><br>" - end - - def build_pypi_package_path(file) - params = { - id: @project_or_group.id, - sha256: file.file_sha256, - file_identifier: file.file_name - } - - if project? - expose_url( - api_v4_projects_packages_pypi_files_file_identifier_path( - params, true - ) - ) + "#sha256=#{file.file_sha256}" - elsif group? - expose_url( - api_v4_groups___packages_pypi_files_file_identifier_path( - params, true - ) - ) + "#sha256=#{file.file_sha256}" - else - '' - end - end - - def name - @packages.first.name - end - - def escape(str) - ERB::Util.html_escape(str) - end - - def project? - @project_or_group.is_a?(::Project) - end - - def group? - @project_or_group.is_a?(::Group) - end - end - end -end diff --git a/app/presenters/packages/pypi/simple_index_presenter.rb b/app/presenters/packages/pypi/simple_index_presenter.rb new file mode 100644 index 00000000000..ffe4eeb9585 --- /dev/null +++ b/app/presenters/packages/pypi/simple_index_presenter.rb @@ -0,0 +1,50 @@ +# frozen_string_literal: true + +# Display package repository index acording to PyPI +# Simple API: https://peps.python.org/pep-0503/ +module Packages + module Pypi + class SimpleIndexPresenter < SimplePresenterBase + private + + def links + refs = [] + + available_packages.each_batch do |batch| + batch.each do |package| + url = build_pypi_package_path(package) + + refs << package_link(url, package.pypi_metadatum.required_python, package.name) + end + end + + refs.join + end + + def build_pypi_package_path(package) + params = { + id: @project_or_group.id, + package_name: package.normalized_pypi_name + } + + if project? + expose_url( + api_v4_projects_packages_pypi_simple_package_name_path( + params, true + ) + ) + elsif group? + expose_url( + api_v4_groups___packages_pypi_simple_package_name_path( + params, true + ) + ) + end + end + + def body_name + @project_or_group.name + end + end + end +end diff --git a/app/presenters/packages/pypi/simple_package_versions_presenter.rb b/app/presenters/packages/pypi/simple_package_versions_presenter.rb new file mode 100644 index 00000000000..0baa0714463 --- /dev/null +++ b/app/presenters/packages/pypi/simple_package_versions_presenter.rb @@ -0,0 +1,58 @@ +# frozen_string_literal: true + +# Display package version data acording to PyPI +# Simple API: https://warehouse.pypa.io/api-reference/legacy/#simple-project-api +# Generates the HTML body for PyPI simple API. +# Basically a list of package download links for a specific +# package +module Packages + module Pypi + class SimplePackageVersionsPresenter < SimplePresenterBase + private + + def links + refs = [] + + available_packages.each_batch do |batch| + batch.each do |package| + package_files = package.installable_package_files + + package_files.each do |file| + url = build_pypi_package_file_path(file) + + refs << package_link(url, package.pypi_metadatum.required_python, file.file_name) + end + end + end + + refs.join + end + + def build_pypi_package_file_path(file) + params = { + id: @project_or_group.id, + sha256: file.file_sha256, + file_identifier: file.file_name + } + + if project? + expose_url( + api_v4_projects_packages_pypi_files_file_identifier_path( + params, true + ) + ) + "#sha256=#{file.file_sha256}" + elsif group? + expose_url( + api_v4_groups___packages_pypi_files_file_identifier_path( + params, true + ) + ) + "#sha256=#{file.file_sha256}" + end + end + + def body_name + @packages.first.name + end + end + end +end diff --git a/app/presenters/packages/pypi/simple_presenter_base.rb b/app/presenters/packages/pypi/simple_presenter_base.rb new file mode 100644 index 00000000000..a459319539c --- /dev/null +++ b/app/presenters/packages/pypi/simple_presenter_base.rb @@ -0,0 +1,53 @@ +# frozen_string_literal: true + +# Display package version data acording to PyPI +# Simple API: https://warehouse.pypa.io/api-reference/legacy/#simple-project-api +module Packages + module Pypi + class SimplePresenterBase + include API::Helpers::RelatedResourcesHelpers + + def initialize(packages, project_or_group) + @packages = packages + @project_or_group = project_or_group + end + + def body + <<-HTML + <!DOCTYPE html> + <html> + <head> + <title>Links for #{escape(body_name)}</title> + </head> + <body> + <h1>Links for #{escape(body_name)}</h1> + #{links} + </body> + </html> + HTML + end + + private + + def package_link(url, required_python, name) + "<a href=\"#{url}\" data-requires-python=\"#{escape(required_python)}\">#{name}</a>" + end + + def escape(str) + ERB::Util.html_escape(str) + end + + def project? + @project_or_group.is_a?(::Project) + end + + def group? + @project_or_group.is_a?(::Group) + end + + def available_packages + @packages.not_pending_destruction + end + end + end +end |