Commit 72398011 authored by Rafael Ravedutti's avatar Rafael Ravedutti Committed by Rafael Ravedutti

Add avatar to city halls

Signed-off-by: Rafael Ravedutti's avatarRafael Ravedutti <rrlmachado@inf.ufpr.br>
parent f998ebdb
Pipeline #15675 passed with stages
in 1 minute and 33 seconds
!/usr/bin/env bash
#!/usr/bin/env bash
set -e
# /app/bin/rails db:environment:set RAILS_ENV=$RAILS_ENV
......@@ -6,7 +6,7 @@ set -e
if [ "$1" = 'CREATE' ]; then
/app/bin/rake agendador:setup
fi
/app/bin/bundle exec sidekiq -C config/sidekiq.yml >> log/sidekiq.log
/app/bin/bundle exec sidekiq -C config/sidekiq.yml >> log/sidekiq.log &
/app/bin/bundle exec rails s -p 3000 -b '0.0.0.0'
exec "$@"
module Api::V1
class CityHallsController < ApplicationController
class CityHallsController < ApplicationController
include Authenticable
require "#{Rails.root}/lib/image_parser.rb"
before_action :set_city_hall, only: [:show, :update, :destroy]
before_action :set_city_hall, only: [ :show, :update, :destroy, :picture,
:upload_picture ]
# GET /city_halls
def index
......@@ -104,6 +106,78 @@ module Api::V1
# end
#end
# GET /city_hall/1/picture
def picture
if @city_hall.nil?
render json: {
errors: ["City hall #{params[:id]} does not exist."]
}, status: 404
else
# Allow request only if the citizen is reachable from current user
begin
authorize @city_hall, :picture?
rescue
render json: {
errors: ["You're not allowed to view this city hall."]
}, status: 403
return
end
path = @city_hall.avatar.path
if path.nil?
render json: {
errors: ["City hall #{params[:id]} does not have a picture."]
}, status: 404
else
if not params[:size].nil?
path.sub!('original', params[:size])
end
send_file path,
type: @city_hall.avatar_content_type,
disposition: 'inline'
end
end
end
# POST /city_hall/1/upload_picture
def upload_picture
if @city_hall.nil?
render json: {
errors: ["City hall #{params[:id]} does not exist."]
}, status: 404
else
# Allow request only if the citizen is reachable from current user
begin
authorize @city_hall, :update?
rescue
render json: {
errors: ["You're not allowed to update this city hall."]
}, status: 403
return
end
if params[:avatar]
begin
@city_hall.avatar = Agendador::Image::Parser.parse(params[:avatar])
ensure
Agendador::Image::Parser.clean_tempfile
end
@city_hall.save
render json: {
errors: ["City hall avatar uploaded!"]
}, status: 201
else
render json: {
errors: ["Avatar parameter undefined."]
}, status: 400
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
......@@ -120,20 +194,20 @@ module Api::V1
def city_hall_params
params.require(:city_hall).permit(
:address_number,
:address_complement,
:block_text,
:citizen_access,
:citizen_register,
:cep,
:description,
:email,
:name,
:neighborhood,
:phone1,
:phone2,
:schedule_period,
:show_professional,
:support_email,
:address_complement,
:block_text,
:citizen_access,
:citizen_register,
:cep,
:description,
:email,
:name,
:neighborhood,
:phone1,
:phone2,
:schedule_period,
:show_professional,
:support_email,
:url
)
end
......
......@@ -3,7 +3,7 @@ class CityHall < ApplicationRecord
# Associations #
has_many :service_places
has_many :resource_type
has_many :resource_type
belongs_to :city
# Validations #
......@@ -38,10 +38,20 @@ class CityHall < ApplicationRecord
validates_length_of :address_number, maximum: 10, allow_blank: true
# Specify location where the picture should be stored (default is public)
# and the formats (large, medium, thumb)
has_attached_file :avatar,
path: "images/city_halls/:id/avatar_:style.:extension",
styles: { large: '500x500', medium: '300x300', thumb: '100x100' }
# Validates format of pictures
validates_attachment_content_type :avatar,
:content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
before_validation :create_city_hall
scope :all_active, -> {
where(active: true)
scope :all_active, -> {
where(active: true)
}
......@@ -59,10 +69,10 @@ class CityHall < ApplicationRecord
address = Address.get_address(self.cep)
return self.as_json(only: [
:id, :active, :address_number, :block_text,
:citizen_access, :citizen_register, :name,
:schedule_period, :address_complement, :description,
:email, :phone1, :phone2, :support_email,
:id, :active, :address_number, :block_text,
:citizen_access, :citizen_register, :name,
:schedule_period, :address_complement, :description,
:email, :phone1, :phone2, :support_email,
:show_professional, :url
])
.merge({city: city.as_json(except: [
......@@ -85,7 +95,7 @@ class CityHall < ApplicationRecord
return search(search_params(params, permission), npage)
end
private
# Translates incoming search parameters to ransack patterns
......@@ -96,11 +106,11 @@ class CityHall < ApplicationRecord
case permission
when "adm_c3sl"
sortable = ["name", "cep", "active", "city_name", "city_state_name"]
filter = {"name" => "name_cont", "city" => "city_name_cont",
filter = {"name" => "name_cont", "city" => "city_name_cont",
"state" => "city_state_name_cont", "s" => "s"}
end
return filter_search_params(params, filter, sortable)
return filter_search_params(params, filter, sortable)
end
......@@ -110,7 +120,7 @@ class CityHall < ApplicationRecord
end
# Method surrounding create method for CityHall. It associates
# Method surrounding create method for CityHall. It associates
# the address to the city hall given a cep
def create_city_hall
address = Address.get_address(self.cep)
......
......@@ -14,7 +14,7 @@ class CityHallPolicy < ApplicationPolicy
.find(user[1]).service_place
city_hall_id = service_place.city_hall_id
return case permission
when "adm_c3sl"
scope.all
......@@ -60,11 +60,35 @@ class CityHallPolicy < ApplicationPolicy
end
end
def picture?
return picture_access_policy(user)
end
private
# Generic method for checking permissions when show/accessing/modifying
# city halls. It is used for avoiding code repetition in city
# Method for checking permissions when showing city hall picture
def picture_access_policy(user)
citizen = user[0]
permission = Professional.get_permission(user[1])
professional = citizen.professional
service_place = professional.professionals_service_places
.find(user[1]).service_place
city_hall_id = service_place.city_hall_id
return case
when permission == "adm_c3sl"
return true
else
return (record.id == city_hall_id)
end
end
# Generic method for checking permissions when show/accessing/modifying
# city halls. It is used for avoiding code repetition in city
# hall's policy methods.
#
# @param user [Array] current citizen and the permission provided
......
......@@ -32,7 +32,13 @@ Rails.application.routes.draw do
end
end
resources :city_halls
resources :city_halls do
member do
get 'picture'
post 'picture' => 'city_halls#upload_picture'
end
end
resources :occupations
resources :professionals
......
class AddAttachmentAvatarToCityHalls < ActiveRecord::Migration
def self.up
change_table :city_halls do |t|
t.attachment :avatar
end
end
def self.down
remove_attachment :city_halls, :avatar
end
end
......@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20180413130918) do
ActiveRecord::Schema.define(version: 20180511112959) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
......@@ -120,16 +120,16 @@ ActiveRecord::Schema.define(version: 20180413130918) do
end
create_table "city_halls", force: :cascade do |t|
t.boolean "active", null: false
t.string "address_number", limit: 10, null: false
t.string "address_street", null: false
t.text "block_text", null: false
t.string "cep", limit: 10, null: false
t.boolean "citizen_access", default: true, null: false
t.boolean "citizen_register", default: true, null: false
t.string "name", null: false
t.string "neighborhood", null: false
t.integer "schedule_period", default: 90, null: false
t.boolean "active", null: false
t.string "address_number", limit: 10, null: false
t.string "address_street", null: false
t.text "block_text", null: false
t.string "cep", limit: 10, null: false
t.boolean "citizen_access", default: true, null: false
t.boolean "citizen_register", default: true, null: false
t.string "name", null: false
t.string "neighborhood", null: false
t.integer "schedule_period", default: 90, null: false
t.string "address_complement"
t.text "description"
t.string "email"
......@@ -137,14 +137,18 @@ ActiveRecord::Schema.define(version: 20180413130918) do
t.string "logo_file_name"
t.integer "logo_file_size"
t.date "logo_updated_at"
t.string "phone1", limit: 14
t.string "phone2", limit: 14
t.string "phone1", limit: 14
t.string "phone2", limit: 14
t.string "support_email"
t.boolean "show_professional"
t.string "url"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "city_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "city_id", null: false
t.string "avatar_file_name"
t.string "avatar_content_type"
t.integer "avatar_file_size"
t.datetime "avatar_updated_at"
t.index ["city_id"], name: "index_city_halls_on_city_id", using: :btree
end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment