summaryrefslogtreecommitdiff
path: root/app/presenters/packages/pypi/simple_package_versions_presenter.rb
blob: 2bccaf4db72d8a2ae342cbe7cddaabe45c8bd953 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 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 |relation|
          batch = relation.preload_files
                          .preload_pypi_metadatum

          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