Skip to content

Commit

Permalink
Import campaign firts version
Browse files Browse the repository at this point in the history
  • Loading branch information
ruby232 committed Dec 19, 2020
1 parent 11d140c commit 02d3d2b
Show file tree
Hide file tree
Showing 147 changed files with 347 additions and 33 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Gemfile.ci.lock

public/avatars/**/*
public/assets
public/importers/

tmp
Design
Expand All @@ -39,4 +40,5 @@ Design
.vagrant

docker-compose.override.yml

.bash_history
.local/
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,7 @@ gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw jruby]
gem 'activejob', '~> 5.2.0'
gem 'ransack_ui', path: 'vendor/gems/ransack_ui-1.3.4' # Vendored until our fix is merged and released
gem 'spreadsheet'

gem "roo", "~> 2.8"

gem "roo-xls", "~> 1.2"
13 changes: 12 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ GEM
jquery-ui-rails (6.0.1)
railties (>= 3.2.16)
libv8 (3.16.14.19)
libv8 (3.16.14.19-x86_64-linux)
listen (3.2.1)
rb-fsevent (~> 0.10, >= 0.10.3)
rb-inotify (~> 0.9, >= 0.9.10)
Expand Down Expand Up @@ -302,6 +303,13 @@ GEM
railties (>= 4.2.0, < 6.0)
responds_to_parent (2.0.0)
actionpack (>= 3.2.22, < 6.0)
roo (2.8.3)
nokogiri (~> 1)
rubyzip (>= 1.3.0, < 3.0.0)
roo-xls (1.2.0)
nokogiri
roo (>= 2.0.0, < 3)
spreadsheet (> 0.9.0)
rspec (3.9.0)
rspec-core (~> 3.9.0)
rspec-expectations (~> 3.9.0)
Expand Down Expand Up @@ -406,6 +414,7 @@ GEM

PLATFORMS
ruby
x86_64-linux

DEPENDENCIES
activejob (~> 5.2.0)
Expand Down Expand Up @@ -465,6 +474,8 @@ DEPENDENCIES
rb-inotify
responders (~> 2.0)
responds_to_parent
roo (~> 2.8)
roo-xls (~> 1.2)
rspec-activemodel-mocks
rspec-rails
rubocop (~> 0.76.0)
Expand All @@ -486,4 +497,4 @@ DEPENDENCIES
zeus

BUNDLED WITH
2.1.4
2.2.2
39 changes: 39 additions & 0 deletions app/controllers/entities/campaigns_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# Fat Free CRM is freely distributable under the terms of MIT license.
# See MIT-LICENSE file or http://www.opensource.org/licenses/mit-license.php
#------------------------------------------------------------------------------

class CampaignsController < EntitiesController
before_action :get_data_for_sidebar, only: :index

Expand Down Expand Up @@ -160,6 +161,37 @@ def filter
end
end

# get /campaigns/import AJAX
#----------------------------------------------------------------------------
def import
@importer = Importer.new
respond_with(@importer)
end

# patch /campaigns/import AJAX
#----------------------------------------------------------------------------
def import_upload
@error = false
@result = {
items: [],
errors: []
}

if params[:importer]
@importer = Importer.create(import_params)
@importer.entity_type = 'campaign'
if @importer.valid?
@importer.save
@result = FatFreeCRM::ImportHandle.process(@importer)
else
puts @importer.errors.full_messages
@result[:errors].push(@importer.errors.full_messages)
@error = true
end
end
respond_with(@error,@result)
end

private

#----------------------------------------------------------------------------
Expand Down Expand Up @@ -204,4 +236,11 @@ def get_data_for_sidebar
end
@campaign_status_total[:other] += @campaign_status_total[:all]
end

def import_params
return {} unless params[:importer]

params[:importer]
.permit(:attachment)
end
end
20 changes: 20 additions & 0 deletions app/models/entities/campaign.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,26 @@ def discard!(attachment)
end
end

# Save Campaign from row in xls
def self.import_from_xls(row,headers)
campaign = Campaign.new
campaign.name = row[headers['Name']]
campaign.access = row[headers['Access']]
campaign.status = row[headers['Status']]
campaign.budget =row[headers['Budget']]
campaign.target_leads =row[headers['target leads']]
campaign.target_conversion =row[headers['Target conversion']]
campaign.leads_count =row[headers['Number of leads']]
campaign.opportunities_count =row[headers['Total Opportunities']]
campaign.revenue =row[headers['target revenue']]
campaign.starts_on =row[headers['start date']]
campaign.ends_on =row[headers['end date']]
campaign.objectives =row[headers['Objectives']]
campaign.background_info =row[headers['Background']]
campaign.save
campaign
end

private

# Make sure end date > start date.
Expand Down
30 changes: 30 additions & 0 deletions app/models/polymorphic/importer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# == Schema Information
#
# Table name: importers
#
# id :integer not null, primary key
# entity_type :string
# attachment_file_size :integer
# attachment_file_name :string(255)
# attachment_content_type :string(255)
# status :string(255)
# created_at :datetime
# updated_at :datetime
#

class Importer < ActiveRecord::Base
attr_accessor :status, :entity_type

has_attached_file :attachment, :path => ":rails_root/public/importers/:id/:filename"

# validates_attachment :attachment, presence: true,
# content_type: { content_type: ['image/jpeg', 'image/jpg', 'image/png', 'image/gif'] }

validates_attachment_content_type :attachment,
:content_type => %w(text/xml application/xml application/vnd.ms-excel application/vnd.openxmlformats-officedocument.spreadsheetml.sheet application/x-ole-storage),
:message => 'Only EXCEL files are allowed.'
validates_attachment_file_name :attachment, matches: [/\.xls/, /\.xlsx?$/]


ActiveSupport.run_load_hooks(:fat_free_crm_importer, self)
end
35 changes: 35 additions & 0 deletions app/views/campaigns/_import.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
= form_for(@importer, url: import_upload_campaign_path(format: "js"), method: "patch", html: { multipart: true, target: "uploading", onsubmit: "$('#import_submit').disabled = true"}) do |f|
= link_to_close new_campaign_path
-# todo
-#= error_messages_for :avatar, object: @user.avatar, object_name: t('avatar')
%p
%small 'Descripcion aqui'

%div 'File':
= fields_for(Importer) do |a|
%div= a.file_field :attachment

.buttonbar
= f.submit t(:upload_picture), id: "import_submit"
#{t :or}
= link_to_cancel new_campaign_path

-#= simple_form_for(@campaign, html: one_submit_only, remote: true) do |f|
-# = link_to_close new_campaign_path
-# = f.hidden_field :user_id
-#
-# = f.error_messages object_name: t('campaign')
-#
-# .section
-# %table
-# %tr
-# %td(colspan="5")
-# .label.top.req 'File':
-# = f.file_field :file, autofocus: true, style: "width:500px"
-#
-# = hook(:entity_form, self, {f: f, entity: @campaign})
-#
-# .buttonbar
-# = f.submit 'Import campaign'
-# #{t :or}
-# = link_to_cancel new_campaign_path
14 changes: 14 additions & 0 deletions app/views/campaigns/_list_title_bar.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
- model_name = controller_name.singularize
- model_klass = model_name.camelcase.constantize

.title_tools
#buttons
= view_buttons
.create_asset
= link_to_inline("create_#{model_name}".to_sym, send("new_#{model_name}_path"), text: t("create_#{model_name}".to_sym))
.import_asset
= link_to_inline("import_#{model_name}".to_sym, send("import_#{model_name}_path"), text: t("import_#{model_name}".to_sym))

.title
%span{id: "create_#{model_name}_title"} #{t controller_name.to_sym}
= image_tag("loading.gif", size: :thumb, id: "loading", style: "display: none;")
6 changes: 6 additions & 0 deletions app/views/campaigns/import.js.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
- import_id = "campaign_import"

crm.flick('empty', 'toggle');
crm.flip_form('#{import_id}');
$('##{import_id}').html('#{ j render(partial: "import") }');
crm.set_title('#{import_id}', '#{ j t(import_id) }');
5 changes: 5 additions & 0 deletions app/views/campaigns/import_upload.js.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- if not @error
$('#campaign_import').html('#{ j render(partial: "import") }');
- else
$('#campaign_import').html('#{ j render(partial: "import") }');
$('#campaign_import').effect("shake", { duration:250, distance: 6 });
5 changes: 4 additions & 1 deletion app/views/campaigns/index.html.haml
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
= styles_for :campaign

= render 'entities/title_bar'
= render 'list_title_bar'

.remote#create_campaign{ hidden }
.remote#campaign_import{ hidden }

%iframe#uploading{ name: "uploading", style: "width:100px; height:10px; border:5px" }

= render 'search'

Expand Down
7 changes: 7 additions & 0 deletions config/initializers/paperclip.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Paperclip
class MediaTypeSpoofDetector
def spoofed?
false
end
end
end
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,7 @@
resources :settings, only: :index
resources :plugins, only: :index
end

get 'campaigns/import',to: 'campaigns#import', as: :import_campaign
match 'campaigns/import-upload',to: 'campaigns#import_upload', as: :import_upload_campaign, via: %i[put patch]
end
18 changes: 18 additions & 0 deletions db/migrate/20201217030615_create_importers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

class CreateImporters < ActiveRecord::Migration[4.2]
def self.up
create_table :importers do |t|
t.integer :attachment_file_size # Uploaded file size
t.string :attachment_file_name # Uploaded full file name
t.string :attachment_content_type # MIME content type
t.string :entity_type # led, campaign
t.string :status # new, success, error
t.timestamps
end
end

def self.down
drop_table :importers
end
end
Loading

0 comments on commit 02d3d2b

Please sign in to comment.