-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #82 from it3s/import
Import
- Loading branch information
Showing
40 changed files
with
876 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
|
||
App.components.importEdit = -> | ||
errMessage: """ | ||
<div class="err"><i class="fa fa-exclamation-triangle"></i><%= msg %></div> | ||
""" | ||
|
||
attributes: -> | ||
submitBtn: @container.find('.submit-btn') | ||
mapInput: @container.find('#map-autocomplete') | ||
errsDiv: @container.find('.validation-errors') | ||
|
||
initialize: -> | ||
@on @attr.submitBtn, 'click', @onSubmit | ||
|
||
onSubmit: (evt) -> | ||
@cleanErrors() | ||
unless @isValid() | ||
@showErrors() | ||
evt.preventDefault() | ||
|
||
isValid: -> | ||
@hasMap() && @allEntriesAreValid() | ||
|
||
hasMap: -> | ||
_val = @attr.mapInput.val() | ||
_val && _val.length > 0 | ||
|
||
allEntriesAreValid: -> | ||
@container.find('li.parsed-data.invalid').length is 0 | ||
|
||
cleanErrors: -> | ||
@attr.errsDiv.html "" | ||
|
||
showErrors: -> | ||
if !@hasMap() | ||
_tpl = _.template(@errMessage, msg: I18n.imports.errors.map) | ||
@attr.errsDiv.append _tpl | ||
|
||
if !@allEntriesAreValid() | ||
_tpl = _.template(@errMessage, msg: I18n.imports.errors.invalid) | ||
@attr.errsDiv.append _tpl |
16 changes: 16 additions & 0 deletions
16
app/assets/javascripts/components/importUploader.js.coffee
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
|
||
App.components.importUploader = -> | ||
_.extend App.components.uploader(), { | ||
|
||
buttonLabel: I18n.uploader.import | ||
|
||
afterInitialize: -> | ||
@method = if @attr.update then 'PATCH' else 'POST' | ||
|
||
onDone: (e, data) -> | ||
setTimeout( => | ||
window.location = data.result.redirect if data.result.redirect | ||
@reset() | ||
, 200) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,3 +61,4 @@ | |
@import "activities"; | ||
@import "notifications"; | ||
@import "pictures"; | ||
@import "import"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
.import-modal { | ||
@include modal; | ||
|
||
h2 { | ||
text-align: center; | ||
margin-bottom: 20px; | ||
} | ||
|
||
.explanation { | ||
margin-bottom: 20px; | ||
} | ||
|
||
.example { | ||
margin: 20px 0 30px; | ||
} | ||
|
||
.upload-file-wrapper { | ||
text-align: center; | ||
|
||
.error { | ||
display: block; | ||
color: $dark-red; | ||
} | ||
} | ||
|
||
.upload-button { | ||
@include blue-button-with-border; | ||
padding: 15px 80px; | ||
} | ||
} | ||
|
||
.imports { | ||
.title { | ||
margin-bottom: 40px; | ||
|
||
h1 { text-align: center; } | ||
} | ||
.import-file, .import-action { | ||
margin-bottom: 20px; | ||
|
||
label { | ||
font-weight: bold; | ||
display: inline-block; | ||
width: 250px; | ||
} | ||
} | ||
.uploader { | ||
display: inline-block; | ||
|
||
.upload-button { | ||
@include blue-button-with-border; | ||
padding: 5px 10px; | ||
width: auto; | ||
} | ||
} | ||
|
||
.import-action { | ||
position: relative; | ||
|
||
.ui-autocomplete-input { width: 250px; } | ||
|
||
.submit-btn { | ||
@include blue-button-with-border; | ||
display: inline-block; | ||
position: absolute; | ||
right: 0; | ||
top: -5px; | ||
} | ||
} | ||
|
||
.validation-errors { | ||
margin: 10px 0; | ||
|
||
.err { | ||
color: $dark-red; | ||
padding: 10px; | ||
background: lighten($soft-red, 25%); | ||
border-radius: 4px; | ||
margin-bottom: 5px; | ||
|
||
i { margin-right: 10px; } | ||
} | ||
} | ||
|
||
.already-imported { | ||
font-weight: bold; | ||
padding: 20px; | ||
text-align: center; | ||
color: $dark-green; | ||
background-color: lighten($soft-green, 35%); | ||
border-radius: 4px; | ||
margin-bottom: 20px; | ||
|
||
i { margin-right: 10px; } | ||
} | ||
|
||
.parsed-data-list { | ||
|
||
.parsed-data { | ||
position: relative; | ||
padding: 10px; | ||
margin-bottom: 10px; | ||
border-radius: 4px; | ||
width: 100%; | ||
background-color: lighten($meppit-light-blue, 5%); | ||
|
||
.icon { | ||
margin-right: 5px; | ||
} | ||
|
||
.number { | ||
font-weight: bold; | ||
margin-right: 10px; | ||
color: $grey; | ||
} | ||
|
||
&.valid { | ||
border: solid 1px $dark-green; | ||
.icon { color: $dark-green } | ||
} | ||
&.invalid { | ||
border: solid 1px $dark-red; | ||
.icon { color: $dark-red } | ||
} | ||
|
||
.see-table-button { | ||
@include button; | ||
display: inline-block; | ||
position: absolute; | ||
right: 10px; | ||
top: 5px; | ||
padding: 4px 8px; | ||
} | ||
|
||
.data-preview { | ||
display: none; | ||
} | ||
|
||
.section-label { | ||
display: block; | ||
margin: 20px 0 10px; | ||
font-weight: bold; | ||
} | ||
|
||
.raw-line .line, .json-table { | ||
padding: 10px; | ||
background: $main-color-bg; | ||
border-radius: 4px; | ||
border: solid 1px $ultra-soft-grey; | ||
} | ||
|
||
.invalid-attrs { | ||
margin-top: 10px; | ||
margin-left: -2px; | ||
font-size: 0.9em; | ||
color: $dark-red; | ||
opacity: 0.8; | ||
|
||
.err-key { font-weight: bold; } | ||
} | ||
} | ||
} | ||
} | ||
.list-item.import { | ||
.item-avatar { | ||
font-size: 30px; | ||
width: 32px; | ||
height: 32px; | ||
border-radius: 5px; | ||
} | ||
|
||
.import-status { | ||
float: right; | ||
padding: 5px; | ||
color: white; | ||
border-radius: 4px; | ||
width: 150px; | ||
text-align: center; | ||
} | ||
|
||
&.imported-true { | ||
.item-avatar, .import-status { background-color: $dark-green; } | ||
} | ||
&.imported-false { | ||
.item-avatar, .import-status { background-color: $dark-red; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
class ImportsController < ApplicationController | ||
|
||
before_action :require_login, except: [:example] | ||
before_action :find_import, only: [:edit, :update, :load] | ||
before_action :is_current_user, only: [:edit, :update, :load] | ||
|
||
|
||
def create | ||
@import = Import.new import_params | ||
save_import | ||
end | ||
|
||
def edit | ||
@parsed_data = @import.parse_source | ||
end | ||
|
||
def update | ||
@import.source = params[:import][:source] | ||
save_import | ||
end | ||
|
||
def example | ||
send_data example_csv , filename: t("import.example_filename") | ||
end | ||
|
||
def load | ||
map_id = params[:map].to_i | ||
loaded = @import.load_to_map! map_id | ||
flash[:notice] = t("import.edit.load.#{ loaded ? 'success' : 'error' }") | ||
redirect_to map_path(map_id) | ||
end | ||
|
||
private | ||
|
||
def example_csv | ||
values = Import.csv_headers.map { |key| t "import.example_value.#{key}" } | ||
"#{ Import.csv_headers.join(',') }\n#{ values.join(',') }" | ||
end | ||
|
||
def import_params | ||
{source: params[:import][:source], user: current_user} | ||
end | ||
|
||
def find_import | ||
@import = Import.find params[:id] | ||
end | ||
|
||
def save_import | ||
if @import.valid? && @import.save | ||
render json: {redirect: edit_import_path(@import)} | ||
else | ||
render json: {errors: @import.errors.messages}, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
def is_current_user | ||
redirect_to(root_path, notice: t('access_denied')) if @import.user != current_user | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.