Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Back-end_Server
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Jobs
Commits
Open sidebar
Agendador
Back-end_Server
Commits
714521ee
Commit
714521ee
authored
Jul 31, 2018
by
Matheus Horstmann
🐴
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'develop' into 'master'
Develop See merge request
!74
parents
949c4b16
6793f3ff
Pipeline
#16914
passed with stages
in 1 minute and 41 seconds
Changes
9
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
139 additions
and
29 deletions
+139
-29
.env-example
.env-example
+2
-0
app/controllers/api/v1/accounts/passwords_controller.rb
app/controllers/api/v1/accounts/passwords_controller.rb
+5
-5
app/controllers/api/v1/citizen_uploads_controller.rb
app/controllers/api/v1/citizen_uploads_controller.rb
+28
-1
app/models/citizen_upload.rb
app/models/citizen_upload.rb
+1
-4
app/workers/citizen_upload_worker.rb
app/workers/citizen_upload_worker.rb
+97
-19
config/routes.rb
config/routes.rb
+3
-0
docker-compose.yml
docker-compose.yml
+3
-0
public/citizen_upload_example.ods
public/citizen_upload_example.ods
+0
-0
public/citizen_upload_example.xls
public/citizen_upload_example.xls
+0
-0
No files found.
.env-example
View file @
714521ee
...
...
@@ -2,3 +2,5 @@ AGENDADOR_SECRET_KEY_BASE=6e58e1f0e806642a44a504672e22665606100838a0c80c8b3b6766
AGENDADOR_API_DB_USER=agendador
AGENDADOR_API_DB_PASSWORD=123mudar
AGENDADOR_REDIS_PASSWORD=123mudar
MAIL_USERNAME: agendador
MAIL_PASSWORD: 123mudar
\ No newline at end of file
app/controllers/api/v1/accounts/passwords_controller.rb
View file @
714521ee
module
Api::V1
module
Api::V1
class
Accounts::PasswordsController
<
DeviseTokenAuth
::
PasswordsController
# this action is responsible for generating password reset tokens and
...
...
@@ -11,20 +11,20 @@ module Api::V1
# fall back to default value if provided
@redirect_url
||=
DeviseTokenAuth
.
default_password_reset_url
unless
@redirect_url
return
render_create_error_missing_redirect_url
end
@resource
=
Account
.
find_by
(
uid:
params
[
:cpf
])
if
@resource
.
citizen
.
email
.
nil?
or
@resource
.
citizen
.
email
.
empty?
@resource
=
Account
.
find_by
(
uid:
params
[
:cpf
])
if
@resource
.
nil?
or
@resource
.
citizen
.
email
.
nil?
or
@resource
.
citizen
.
email
.
empty?
render
json:
{
errors:
[
"User
#{
params
[
:cpf
]
}
does not have an email registered."
]
},
status:
422
return
end
@resource
.
email
=
@resource
.
citizen
.
email
@resource
.
email
=
@resource
.
citizen
.
email
@resource
.
save
@email
=
@resource
.
email
...
...
app/controllers/api/v1/citizen_uploads_controller.rb
View file @
714521ee
...
...
@@ -32,10 +32,21 @@ module Api::V1
# Sort uploads by date in descending order
@uploads
=
@uploads
.
order
(
"created_at DESC"
)
# Uploads with professional id
uploads_with_professional
=
@uploads
.
as_json
# Add professional id to uploads
uploads_with_professional
.
each
do
|
upload
|
upload_citizen
=
Citizen
.
find
(
upload
[
'citizen_id'
])
professional
=
Professional
.
where
(
account_id:
upload_citizen
.
account_id
).
first
upload
[
:professional_id
]
=
professional
.
id
end
# Create response object
response
=
Hash
.
new
response
[
:num_entries
]
=
@uploads
.
total_count
response
[
:entries
]
=
@uploads
.
as_json
response
[
:entries
]
=
uploads_with_professional
# Render uploads in JSON format
render
json:
response
.
to_json
...
...
@@ -133,6 +144,22 @@ module Api::V1
def
destroy
end
# GET /citizen_uploads/example_file_xls
def
example_xls
filename
=
"
#{
Rails
.
root
.
to_s
}
/public/citizen_upload_example.xls"
content_type
=
"application/xls"
send_file
filename
,
:type
=>
content_type
,
:x_sendfile
=>
true
end
# GET /citizen_uploads/example_file_ods
def
example_ods
filename
=
"
#{
Rails
.
root
.
to_s
}
/public/citizen_upload_example.ods"
content_type
=
"application/vnd.oasis.opendocument.spreadsheet"
send_file
filename
,
:type
=>
content_type
,
:x_sendfile
=>
true
end
private
# Use callbacks to share common setup or constraints between actions.
...
...
app/models/citizen_upload.rb
View file @
714521ee
...
...
@@ -7,10 +7,7 @@ class CitizenUpload < ApplicationRecord
has_attached_file
:log
,
path:
"/data/citizen_upload/:id/log.csv"
# Validates format of logs
# validates_attachment_content_type :log,
# :content_type => ['text/csv']
# Do not validate format of logs
do_not_validate_attachment_file_type
:log
# @params params [ActionController::Parameters] Parameters for searching
...
...
app/workers/citizen_upload_worker.rb
View file @
714521ee
...
...
@@ -31,15 +31,6 @@ class CitizenUploadWorker
status:
1
# parsing content
)
# Parse citizens from the CSV data
citizens
=
CSV
.
parse
(
content
).
map
{
|
row
|
Hash
[
columns
.
zip
(
row
)]
}
# Remove headers
citizens
=
citizens
.
drop
(
1
)
# Number of citizens to be uploaded
upload_size
=
citizens
.
length
# Line number starts with one
line_number
=
1
# Hash with errors
...
...
@@ -49,6 +40,41 @@ class CitizenUploadWorker
# Buffer containing accounts to create
account_to_create
=
Array
.
new
begin
# Parse citizens from the CSV data
citizens
=
CSV
.
parse
(
content
).
map
{
|
row
|
Hash
[
columns
.
zip
(
row
)]
}
# Remove headers
citizens
=
citizens
.
drop
(
1
)
# Number of citizens to be uploaded
upload_size
=
citizens
.
length
rescue
# Set citizens to nil
citizens
=
[]
# Upload size is zero
upload_size
=
1
# Add parsing error to log
errors
.
push
([
0
,
" "
,
" "
,
" "
,
" "
,
" "
,
" "
,
" "
,
" "
,
" "
,
" "
,
" "
,
" "
,
"Could not parse CSV file!"
])
end
# Update task status to in progress
CitizenUpload
.
update
(
upload_id
,
...
...
@@ -70,16 +96,34 @@ class CitizenUploadWorker
provider:
"cpf"
})
# Create default password for current citizen
account
.
password
=
citizen
.
birth_date
.
strftime
(
'%d%m%y'
)
# Citizen remaining info is added when .valid? method is called
if
citizen
.
valid?
and
account
.
valid?
if
citizen
.
valid?
# Create default password for current citizen
account
.
password
=
citizen
.
birth_date
.
strftime
(
'%d%m%y'
)
end
# Check if account is valid
if
account
.
valid?
# Check for permissions on the citizen to be added
if
permission
!=
"adm_c3sl"
and
citizen
.
city_id
!=
city_id
# If there was a permission error, store it in the errors hash
# errors.push("%d,Permission denied for this city" % [line_number])
errors
.
push
([
line_number
,
"Permission denied for this city"
])
errors
.
push
([
citizen_params
[
:name
],
citizen_params
[
:cpf
],
citizen_params
[
:rg
],
citizen_params
[
:birth_date
],
citizen_params
[
:cep
],
citizen_params
[
:address_number
],
citizen_params
[
:address_complement
],
citizen_params
[
:phone1
],
citizen_params
[
:phone2
],
citizen_params
[
:email
],
citizen_params
[
:pcd
],
citizen_params
[
:note
],
line_number
,
"Permission denied for this city"
])
else
# Add valid citizen with complete info to to_create array
inst
=
[
...
...
@@ -115,11 +159,37 @@ class CitizenUploadWorker
end
else
# Full messages string
full_messages_string
=
nil
# Go through error messages
citizen
.
errors
.
full_messages
.
each
do
|
message
|
# Add current error in the list of errors
# errors.push("%d,%s" % [line_number, message])
errors
.
push
([
line_number
,
message
])
if
full_messages_string
.
present?
full_messages_string
=
"
#{
full_messages_string
}
/
#{
message
}
"
else
full_messages_string
=
message
end
end
# If there are errors, insert them into the log
if
full_messages_string
.
present?
# Add current citizen in the list of errors
errors
.
push
([
citizen_params
[
:name
],
citizen_params
[
:cpf
],
citizen_params
[
:rg
],
citizen_params
[
:birth_date
],
citizen_params
[
:cep
],
citizen_params
[
:address_number
],
citizen_params
[
:address_complement
],
citizen_params
[
:phone1
],
citizen_params
[
:phone2
],
citizen_params
[
:email
],
citizen_params
[
:pcd
],
citizen_params
[
:note
],
line_number
,
full_messages_string
])
end
end
...
...
@@ -187,8 +257,16 @@ class CitizenUploadWorker
# Get CSV path
path
=
"
#{
Rails
.
root
.
to_s
}
/tmp/citizen_uploads/
#{
upload_id
}
.csv"
# Open log CSV file for writing the log
CSV
.
open
(
path
,
"wb"
)
do
|
csv
|
csv
<<
[
"Line"
,
"Error Message"
]
# Add headers to log
csv
<<
[
"Nome"
,
"CPF"
,
"RG"
,
"Data de Nascimento"
,
"CEP"
,
"Numero"
,
"Complemento"
,
"Telefone 1"
,
"Telefone 2"
,
"E-mail"
,
"Deficiencia"
,
"Observacao"
,
"Linha"
,
"Erros"
]
# Go through the errors to add them into the log
errors
.
each
do
|
error
|
csv
<<
error
end
...
...
config/routes.rb
View file @
714521ee
...
...
@@ -36,6 +36,9 @@ Rails.application.routes.draw do
end
end
get
"citizen_uploads/example_ods"
=>
"citizen_uploads#example_ods"
get
"citizen_uploads/example_xls"
=>
"citizen_uploads#example_xls"
resources
:citizen_uploads
resources
:occupations
...
...
docker-compose.yml
View file @
714521ee
...
...
@@ -38,6 +38,9 @@ services:
SECRET_KEY_BASE
:
${AGENDADOR_SECRET_KEY_BASE}
PGHOST
:
agendador-postgres
REDISHOST
:
agendador-redis
MAIL_USERNAME
:
${MAIL_USERNAME}
MAIL_PASSWORD
:
${MAIL_PASSWORD}
ports
:
-
'
3000:3000'
depends_on
:
...
...
public/citizen_upload_example.ods
0 → 100644
View file @
714521ee
File added
public/citizen_upload_example.xls
0 → 100644
View file @
714521ee
File added
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment