1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-11-29 10:24:20 +01:00

[feature]async generation of users exports

This commit is contained in:
Sylvain 2016-07-27 17:00:06 +02:00
parent a9d92237df
commit a2066d8c09
11 changed files with 139 additions and 6 deletions

View File

@ -91,24 +91,51 @@ class API::MembersController < API::ApiController
# export subscriptions
def export_subscriptions
authorize :export
@subscriptions = Subscription.all.includes(:plan, :user => [:profile])
render xlsx: 'export_subscriptions.xlsx', filename: "export_subscriptions.xlsx"
export = Export.where({category:'users', export_type: 'subscriptions'}).where('created_at > ?', Subscription.maximum('updated_at')).last
if export.nil? || !FileTest.exist?(export.file)
@export = Export.new({category:'users', export_type: 'subscriptions', user: current_user})
if @export.save
render json: {export_id: @export.id}, status: :ok
else
render json: @export.errors, status: :unprocessable_entity
end
else
send_file File.join(Rails.root, export.file), :type => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', :disposition => 'attachment'
end
end
# export reservations
def export_reservations
authorize :export
@reservations = Reservation.all.includes(:slots, :reservable, :user => [:profile])
render xlsx: 'export_reservations.xlsx', filename: "export_reservations.xlsx"
export = Export.where({category:'users', export_type: 'reservations'}).where('created_at > ?', Reservation.maximum('updated_at')).last
if export.nil? || !FileTest.exist?(export.file)
@export = Export.new({category:'users', export_type: 'reservations', user: current_user})
if @export.save
render json: {export_id: @export.id}, status: :ok
else
render json: @export.errors, status: :unprocessable_entity
end
else
send_file File.join(Rails.root, export.file), :type => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', :disposition => 'attachment'
end
end
def export_members
authorize :export
@members = User.with_role(:member).includes(:group, :trainings, :tags, :invoices, :projects, :subscriptions => [:plan], :profile => [:address])
render xlsx: 'export_members.xlsx', filename: "export_members.xlsx"
export = Export.where({category:'users', export_type: 'members'}).where('created_at > ?', User.with_role(:member).maximum('updated_at')).last
if export.nil? || !FileTest.exist?(export.file)
@export = Export.new({category:'users', export_type: 'members', user: current_user})
if @export.save
render json: {export_id: @export.id}, status: :ok
else
render json: @export.errors, status: :unprocessable_entity
end
else
send_file File.join(Rails.root, export.file), :type => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', :disposition => 'attachment'
end
end
def merge

View File

@ -26,6 +26,8 @@ class Export < ActiveRecord::Base
case category
when 'statistics'
StatisticsExportWorker.perform_async(self.id)
when 'users'
UsersExportWorker.perform_async(self.id)
else
raise NoMethodError, "Unknown export service for #{category}/#{export_type}"
end

View File

@ -0,0 +1,65 @@
require 'abstract_controller'
require 'action_controller'
require 'action_view'
require 'active_record'
# require any helpers
require './app/helpers/application_helper'
class UsersExportService
# export subscriptions
def export_subscriptions(export)
@subscriptions = Subscription.all.includes(:plan, :user => [:profile])
ActionController::Base.prepend_view_path './app/views/'
# place data in view_assigns
view_assigns = {subscriptions: @subscriptions}
av = ActionView::Base.new(ActionController::Base.view_paths, view_assigns)
av.class_eval do
# include any needed helpers (for the view)
include ApplicationHelper
end
content = av.render template: 'exports/users_subscriptions.xlsx.axlsx'
# write content to file
File.open(export.file,"w+b") {|f| f.puts content }
end
# export reservations
def export_reservations(export)
@reservations = Reservation.all.includes(:slots, :reservable, :user => [:profile])
ActionController::Base.prepend_view_path './app/views/'
# place data in view_assigns
view_assigns = {reservations: @reservations}
av = ActionView::Base.new(ActionController::Base.view_paths, view_assigns)
av.class_eval do
# include any needed helpers (for the view)
include ApplicationHelper
end
content = av.render template: 'exports/users_reservations.xlsx.axlsx'
# write content to file
File.open(export.file,"w+b") {|f| f.puts content }
end
# export members
def export_members(export)
@members = User.with_role(:member).includes(:group, :trainings, :tags, :invoices, :projects, :subscriptions => [:plan], :profile => [:address])
ActionController::Base.prepend_view_path './app/views/'
# place data in view_assigns
view_assigns = {members: @members}
av = ActionView::Base.new(ActionController::Base.view_paths, view_assigns)
av.class_eval do
# include any needed helpers (for the view)
include ApplicationHelper
end
content = av.render template: 'exports/users_members.xlsx.axlsx'
# write content to file
File.open(export.file,"w+b") {|f| f.puts content }
end
end

View File

@ -0,0 +1,27 @@
class UsersExportWorker
include Sidekiq::Worker
def perform(export_id)
export = Export.find(export_id)
unless export.user.is_admin?
raise SecurityError, 'Not allowed to export'
end
unless export.category == 'users'
raise KeyError, 'Wrong worker called'
end
service = UsersExportService.new
method_name = "export_#{export.export_type}"
if %w(members subscriptions reservations).include?(export.export_type) and service.respond_to?(method_name)
service.public_send(method_name, export)
NotificationCenter.call type: :notify_admin_export_complete,
receiver: export.user,
attached_object: export
end
end
end

View File

@ -259,6 +259,9 @@ en:
statistics_project: "of statistics about projects"
statistics_subscription: "of subscription statistics"
statistics_training: "of statistics about trainings"
users_members: "of the members' list"
users_subscriptions: "of the subscriptions' list"
users_reservations: "of the reservations' list"
is_over: "is over."
download_here: "Download here"

View File

@ -259,6 +259,9 @@ fr:
statistics_project: "des statistiques sur les projets"
statistics_subscription: "des statistiques d'abonnements"
statistics_training: "des statistiques sur les formations"
users_members: "de la liste des membres"
users_subscriptions: "de la liste des abonnements"
users_reservations: "de la liste des réservations"
is_over: "est terminé."
download_here: "Téléchargez ici"

View File

@ -261,6 +261,9 @@ en:
statistics_project: "of statistics about projects"
statistics_subscription: "of subscription statistics"
statistics_training: "of statistics about trainings"
users_members: "of the members' list"
users_subscriptions: "of the subscriptions' list"
users_reservations: "of the reservations' list"
click_to_download: "Excel file generated successfully. To download it, click"
here: "here"

View File

@ -261,6 +261,9 @@ fr:
statistics_project: "des statistiques sur les projets"
statistics_subscription: "des statistiques d'abonnements"
statistics_training: "des statistiques sur les formations"
users_members: "de la liste des membres"
users_subscriptions: "de la liste des abonnements"
users_reservations: "de la liste des réservations"
click_to_download: "La génération est terminée. Pour télécharger le fichier Excel, cliquez"
here: "ici"