Skip to content

Commit

Permalink
Merge pull request #2396 from internetee/2391-domain-pdf
Browse files Browse the repository at this point in the history
Admin: download pdf with domain data on show
  • Loading branch information
vohmar authored Sep 29, 2022
2 parents c2cea06 + 74ab6fd commit c488085
Show file tree
Hide file tree
Showing 7 changed files with 276 additions and 3 deletions.
7 changes: 6 additions & 1 deletion app/controllers/admin/domains_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Admin
class DomainsController < BaseController
DEFAULT_VERSIONS_PER_PAGE = 10

before_action :set_domain, only: %i[show edit update keep]
before_action :set_domain, only: %i[show edit update download keep]
authorize_resource

# rubocop:disable Metrics/MethodLength
Expand Down Expand Up @@ -65,6 +65,11 @@ def versions
.per(DEFAULT_VERSIONS_PER_PAGE)
end

def download
filename = "#{@domain.name}.pdf"
send_data @domain.as_pdf, filename: filename
end

def keep
@domain.keep
redirect_to edit_admin_domain_url(@domain), notice: t('.kept')
Expand Down
6 changes: 6 additions & 0 deletions app/models/domain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,12 @@ def as_csv_row
]
end

def as_pdf
domain_html = ApplicationController.render(template: 'domain/pdf', assigns: { domain: self })
generator = PDFKit.new(domain_html)
generator.to_pdf
end

def registrant_name
return registrant.name if registrant

Expand Down
5 changes: 3 additions & 2 deletions app/views/admin/domains/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@

<div class="page-header">
<div class="row">
<div class="col-sm-8">
<div class="col-sm-7">
<h1><%= domain.name_with_status %></h1>
</div>

<div class="col-sm-4 text-right">
<div class="col-sm-5 text-right">
<%= link_to t('.download_btn'), download_admin_domain_path(@domain), class: 'btn btn-primary' %>
<%= link_to t('.edit_btn'), edit_admin_domain_path(@domain), class: 'btn btn-primary' %>
<%= link_to t('.history_btn'), admin_domain_domain_versions_path(@domain),
class: 'btn btn-primary' %>
Expand Down
241 changes: 241 additions & 0 deletions app/views/domain/pdf.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
%html{lang: 'et'}
%head
%meta{charset: "utf-8"}
:css
.container {
margin: auto;
font-size: 12px;
}
.col-md-12 {
}
.col-xs-4 {
width: 33%;
}
.left {
float: left;
}
.left {
padding-right: 5px;
}
.right {
float: right;
}
dt {
float: left;
width: 100px;
clear: left;
text-align: right;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
font-weight: bold;
line-height: 1.42857;
}
dd {
margin-left: 120px;
line-height: 1.42857;
}
table {
width: 100%;
border-collapse: collapse;
font-size: 12px;
}
th {
text-align: left;
border: 0px;
border-top: 1px solid #DDD;
padding: 6px;
}
thead th {
border-bottom: 2px solid #DDD;
border-top: 0px;
}
td {
border-top: 1px solid #DDD;
}
td {
padding: 6px;
}
hr {
height: 1px;
border: 0;
color: #DDD;
background-color: #DDD;
}
.clear {
clear: both;
}
.pull-down {
margin-top: 30px;
}
#header {
position: relative;
min-height: 100px;
}
img {
width: 106px;
height: 102px;
}
#header-content {
position: absolute;
bottom: 0;
}
h1 {
margin-bottom: 5px;
}
#footer {
position: absolute;
bottom: 0px;
width: 99%;
}
.blue {
color: #0098da;
}
%body
.container
#header.row
.col-sm-6.left
#header-content
%h1= "#{@domain.name} (#{Time.zone.now.to_date.strftime("%d/%m/%Y")})"
.col-sm-6.right
%img{src: "#{Rails.root}/public/eis-logo-black-et.png"}
.clear
%hr
.row
.col-sm-8.left
%h3 Üldine
%hr
%dl.dl-horizontal
%dt Nimi
%dd= @domain.name
%dt Registreeritud
%dd= @domain.registered_at.to_date.strftime("%d/%m/%Y")
%dt Aegub
%dd= @domain.valid_to.to_date.strftime("%d/%m/%Y")
%dt Registripidaja
%dd= "#{@domain.registrar} #{@domain.registrar.website}"

.col-sm-4.right
%h3 Registreerija
%hr
%dl.dl-horizontal
%dt.left_25 Nimi
%dd.left_25= @domain.registrant

%dt.left_25 Isikukood
%dd.left_25= @domain.registrant.ident_human_description

%dt.left_25 E-mail
%dd.left_25= @domain.registrant.email

%dt.left_25 Telefon
%dd.left_25= @domain.registrant.phone

.clear
.row.pull-down
.col-md-12
%h3 Halduskontaktid
%hr
.table-responsive
%table.table.table-hover.table-condensed
%thead
%tr
%th{class: 'col-xs-3'} Nimi
%th{class: 'col-xs-3'} Isikukood
%th{class: 'col-xs-3'} E-mail
%th{class: 'col-xs-3'} Telefon
%tbody
- @domain.admin_contacts.each do |ac|
%tr
%td= ac.name
%td= ac.ident_human_description
%td= ac.email
%td= ac.phone

.clear
.row.pull-down
.col-md-12
%h3 Tehnilised kontaktid
%hr
.table-responsive
%table.table.table-hover.table-condensed
%thead
%tr
%th{class: 'col-xs-3'} Nimi
%th{class: 'col-xs-3'} Isikukood
%th{class: 'col-xs-3'} E-mail
%th{class: 'col-xs-3'} Telefon
%tbody
- @domain.tech_contacts.each do |tc|
%tr
%td= tc.name
%td= tc.ident_human_description
%td= tc.email
%td= tc.phone

.clear
.row.pull-down
.col-md-12
%h3 Nimeserverid
%hr
.table-responsive
%table.table.table-hover.table-condensed
%thead
%tr
%th{class: 'col-xs-4'} Hostinimi
%th{class: 'col-xs-4'}= t(:ipv4)
%th{class: 'col-xs-4'}= t(:ipv6)
%tbody
- @domain.nameservers.each do |x|
%tr
%td= x
%td= x.ipv4
%td= x.ipv6
.clear
.row.pull-down
.col-md-12
%h3= t(:dnskeys)
%hr
.table-responsive
%table.table.table-hover.table-condensed
%thead
%tr
%th{class: 'col-xs-1'} Lipp
%th{class: 'col-xs-1'} Protokolli
%th{class: 'col-xs-1'} Algoritm
%th{class: 'col-xs-9'} Avalik võti
%tbody
- @domain.dnskeys.each do |x|
%tr
%td= x.flags
%td= x.protocol
%td= x.alg
%td= x.public_key
#footer
%hr
%p{class: 'blue'} Eesti Interneti SA
%p Paldiski mnt 80, 10617 Tallinn, Estonia T +372 727 1000 E info@internet.ee www.internet.ee
%p Reg. nr 90010019 KMKR EE101286464
1 change: 1 addition & 0 deletions config/locales/admin/domains.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ en:
registrant: Registrant

show:
download_btn: Download PDF
edit_btn: Edit statuses
history_btn: History

Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@
resource :registry_lock, controller: 'domains/registry_lock', only: :destroy

member do
get :download
patch :keep
end
end
Expand Down
18 changes: 18 additions & 0 deletions test/integration/admin_area/domain_show_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'test_helper'

class DomainShowTest < ApplicationIntegrationTest
setup do
@domain = domains(:shop)
sign_in users(:admin)
end

def test_downloads_domain
filename = "#{@domain.name}.pdf"
get download_admin_domain_path(@domain)

assert_response :ok
assert_equal 'application/pdf', response.headers['Content-Type']
assert_equal "attachment; filename=\"#{filename}\"; filename*=UTF-8''#{filename}", response.headers['Content-Disposition']
assert_not_empty response.body
end
end

0 comments on commit c488085

Please sign in to comment.