2020-07-28 23:09:34 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
# Display package version data acording to PyPI
|
2020-07-28 23:09:34 +05:30
|
|
|
# Simple API: https://warehouse.pypa.io/api-reference/legacy/#simple-project-api
|
|
|
|
module Packages
|
|
|
|
module Pypi
|
|
|
|
class PackagePresenter
|
|
|
|
include API::Helpers::RelatedResourcesHelpers
|
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
def initialize(packages, project_or_group)
|
2020-07-28 23:09:34 +05:30
|
|
|
@packages = packages
|
2021-09-04 01:27:46 +05:30
|
|
|
@project_or_group = project_or_group
|
2020-07-28 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
# Returns the HTML body for PyPI simple API.
|
2020-07-28 23:09:34 +05:30
|
|
|
# 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|
|
2022-04-04 11:22:00 +05:30
|
|
|
package_files = package.installable_package_files
|
2022-03-02 08:16:31 +05:30
|
|
|
|
|
|
|
package_files.each do |file|
|
2020-07-28 23:09:34 +05:30
|
|
|
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)
|
2021-09-04 01:27:46 +05:30
|
|
|
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
|
2020-07-28 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def name
|
|
|
|
@packages.first.name
|
|
|
|
end
|
|
|
|
|
|
|
|
def escape(str)
|
|
|
|
ERB::Util.html_escape(str)
|
|
|
|
end
|
2021-09-04 01:27:46 +05:30
|
|
|
|
|
|
|
def project?
|
|
|
|
@project_or_group.is_a?(::Project)
|
|
|
|
end
|
|
|
|
|
|
|
|
def group?
|
|
|
|
@project_or_group.is_a?(::Group)
|
|
|
|
end
|
2020-07-28 23:09:34 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|