From 82c30cd529365153fc2db4f94e3ea92a3f8f1396 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Verg=C3=A9s?= Date: Wed, 17 Apr 2024 12:17:54 +0200 Subject: [PATCH] add pdf exporter --- app/models/decidim/anonymous_codes/group.rb | 6 +++- .../admin/export/_token_tr.html.erb | 7 +++++ .../admin/export/tokens_pdf.html.erb | 25 +++++++++++++++ .../anonymous_codes/admin/export/pdf.html.erb | 29 +++++++++++++++++ config/locales/en.yml | 16 ++++++++++ lib/decidim/anonymous_codes.rb | 7 ++++- lib/decidim/exporters/anonymous_tokens_pdf.rb | 31 +++++++++++++++++++ .../anonymous_tokens_pdf_controller_helper.rb | 12 +++++++ 8 files changed, 131 insertions(+), 2 deletions(-) create mode 100644 app/views/decidim/anonymous_codes/admin/export/_token_tr.html.erb create mode 100644 app/views/decidim/anonymous_codes/admin/export/tokens_pdf.html.erb create mode 100644 app/views/layouts/decidim/anonymous_codes/admin/export/pdf.html.erb create mode 100644 lib/decidim/exporters/anonymous_tokens_pdf.rb create mode 100644 lib/decidim/exporters/anonymous_tokens_pdf_controller_helper.rb diff --git a/app/models/decidim/anonymous_codes/group.rb b/app/models/decidim/anonymous_codes/group.rb index 210c9e6..7d63089 100644 --- a/app/models/decidim/anonymous_codes/group.rb +++ b/app/models/decidim/anonymous_codes/group.rb @@ -19,7 +19,11 @@ def self.for(organization) end def expired? - expires_at.present? && expires_at < Time.current + expires? && expires_at < Time.current + end + + def expires? + expires_at.present? end end end diff --git a/app/views/decidim/anonymous_codes/admin/export/_token_tr.html.erb b/app/views/decidim/anonymous_codes/admin/export/_token_tr.html.erb new file mode 100644 index 0000000..0f75985 --- /dev/null +++ b/app/views/decidim/anonymous_codes/admin/export/_token_tr.html.erb @@ -0,0 +1,7 @@ + + <%= token[:token] %> + <%= token[:resource_url] %> + <%= t("booleans.#{token[:available]}") %> + <%= t("booleans.#{token[:used]}") %> + <%= token[:usage_count] %> + diff --git a/app/views/decidim/anonymous_codes/admin/export/tokens_pdf.html.erb b/app/views/decidim/anonymous_codes/admin/export/tokens_pdf.html.erb new file mode 100644 index 0000000..40ef6a2 --- /dev/null +++ b/app/views/decidim/anonymous_codes/admin/export/tokens_pdf.html.erb @@ -0,0 +1,25 @@ +
+
+

<%= t(".tokens_for", group: translated_attribute(code_group.title)) %>

+ <% if collection.count > 1 %> +

<%= t(".totals", total: collection.count) %>

+

<%= t(".expires", expires: t("booleans.#{code_group.expires?}")) %>

+ <%= content_tag "h3", t(".expires_at", expires_at: l(code_group.expires_at, format: :decidim_short)) if code_group.expires? %> + <% end %> +
+ + + + + + + + + + + + + <%= render partial: "decidim/anonymous_codes/admin/export/token_tr", collection: collection, locals: { code_group: code_group }, as: :token %> + +
<%= t("token", scope: "decidim.anonymous_codes.tokens_serializer") %><%= t("resource_url", scope: "decidim.anonymous_codes.tokens_serializer") %><%= t("available", scope: "decidim.anonymous_codes.tokens_serializer") %><%= t("used", scope: "decidim.anonymous_codes.tokens_serializer") %><%= t("usage_count", scope: "decidim.anonymous_codes.tokens_serializer") %>
+
diff --git a/app/views/layouts/decidim/anonymous_codes/admin/export/pdf.html.erb b/app/views/layouts/decidim/anonymous_codes/admin/export/pdf.html.erb new file mode 100644 index 0000000..cdb3c3f --- /dev/null +++ b/app/views/layouts/decidim/anonymous_codes/admin/export/pdf.html.erb @@ -0,0 +1,29 @@ + + + + + + <%= @title %> + + + + <%= yield %> + + diff --git a/config/locales/en.yml b/config/locales/en.yml index 73e8381..302ebaa 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -8,6 +8,10 @@ en: max_uses_exceeded: The code has already been used the maximum number of times. decidim: + admin: + exports: + formats: + AnonymousTokensPDF: PDF anonymous_codes: admin: actions: @@ -51,6 +55,12 @@ en: back: Back to groups new_codes_button: Generate new codes title: Codes for group %{group} + export: + tokens_pdf: + expires: Expires? %{expires} + expires_at: 'Expires at: %{expires_at}' + tokens_for: Tokens for "%{group}" + totals: "%{total} total tokens" exports: all_tokens: All tokens menu_title: Access Codes @@ -75,4 +85,10 @@ en: label: Code submit: Continue title: Form restricted + tokens_serializer: + available: Available? + resource_url: Resource URL + token: Code + usage_count: Num. of uses + used: Used? used_code: The introduced code has already been used. Please try again. diff --git a/lib/decidim/anonymous_codes.rb b/lib/decidim/anonymous_codes.rb index 437e20b..bbc0c51 100644 --- a/lib/decidim/anonymous_codes.rb +++ b/lib/decidim/anonymous_codes.rb @@ -18,7 +18,7 @@ module AnonymousCodes end config_accessor :export_formats do - ENV.fetch("ANONYMOUS_CODES_EXPORT_FORMATS", "CSV JSON Excel").split + ENV.fetch("ANONYMOUS_CODES_EXPORT_FORMATS", "CSV JSON Excel AnonymousTokensPDF").split end def self.token_generator(length = nil) @@ -31,4 +31,9 @@ def self.token_generator(length = nil) end end end + + module Exporters + autoload :AnonymousTokensPDF, "decidim/exporters/anonymous_tokens_pdf" + autoload :AnonymousTokensPDFControllerHelper, "decidim/exporters/anonymous_tokens_pdf_controller_helper" + end end diff --git a/lib/decidim/exporters/anonymous_tokens_pdf.rb b/lib/decidim/exporters/anonymous_tokens_pdf.rb new file mode 100644 index 0000000..298585e --- /dev/null +++ b/lib/decidim/exporters/anonymous_tokens_pdf.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +require "wicked_pdf" + +module Decidim + module Exporters + # Inherits from abstract PDF exporter. This class is used to set + # the parameters used to create a PDF when exporting Survey Answers. + # + class AnonymousTokensPDF < PDF + def controller + @controller ||= AnonymousTokensPDFControllerHelper.new + end + + def template + "decidim/anonymous_codes/admin/export/tokens_pdf" + end + + def layout + "decidim/anonymous_codes/admin/export/pdf" + end + + def locals + { + code_group: collection&.first&.group, + collection: collection.map { |token| Decidim::AnonymousCodes::TokenSerializer.new(token).serialize } + } + end + end + end +end diff --git a/lib/decidim/exporters/anonymous_tokens_pdf_controller_helper.rb b/lib/decidim/exporters/anonymous_tokens_pdf_controller_helper.rb new file mode 100644 index 0000000..1ef37a9 --- /dev/null +++ b/lib/decidim/exporters/anonymous_tokens_pdf_controller_helper.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +module Decidim + module Exporters + # rubocop: disable Rails/ApplicationController + # A dummy controller to render views while exporting questionnaires + class AnonymousTokensPDFControllerHelper < ActionController::Base + # rubocop: enable Rails/ApplicationController + helper Decidim::TranslationsHelper + end + end +end