Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR for #813 #826

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ def denguechatorg

def index
@user = User.new

# We're manually ordering this to display the diversity of
# our cities.
@citiesThatUsed = City.find_by_sql("SELECT cities.* from cities join
Expand Down
1 change: 1 addition & 0 deletions app/models/breeding_site.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class BreedingSite < ActiveRecord::Base
#----------------------------------------------------------------------------

has_many :elimination_methods, :dependent => :destroy
has_many :organizations_breeding_sites, class_name: "OrganizationsBreedingSites"
accepts_nested_attributes_for :elimination_methods, :allow_destroy => true

#----------------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions app/models/csv_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ def self.clean_breeding_site_codes
end

def self.accepted_breeding_site_codes
return ["a", "b", "l", "m", "p", "t", "x"] + self.clean_breeding_site_codes
end
return ["a", "b", "l", "m", "p", "t", "x"] + self.clean_breeding_site_codes
end

end
1 change: 1 addition & 0 deletions app/models/inspection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ module Types
belongs_to :breeding_site
belongs_to :elimination_method

belongs_to :organizations_breeding_sites, class_name: "OrganizationBreedingSite", foreign_key: "organization_breeding_site_id"
belongs_to :visit
belongs_to :report
belongs_to :spreadsheet, :foreign_key => "csv_id"
Expand Down
1 change: 1 addition & 0 deletions app/models/organization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class Organization < ActiveRecord::Base
has_many :memberships
has_many :users, :through => :memberships
has_many :teams
has_many :organizations_breeding_sites, class_name: "OrganizationBreedingSite"

validates_presence_of :name
end
15 changes: 15 additions & 0 deletions app/models/organization_breeding_site.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class OrganizationBreedingSite < ActiveRecord::Base

self.table_name = "organizations_breeding_sites"
attr_accessible :organization_id, :breeding_site_id

belongs_to :organization
belongs_to :breeding_site

validates_presence_of :organization_id
validates_presence_of :breeding_site_id

scope :by_organization_code, -> organization_id, code { where("organization_id = ? AND code LIKE ?", "#{organization_id}", "#{code}") }


end
32 changes: 24 additions & 8 deletions app/models/spreadsheet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,12 @@ def self.extract_content_from_row(row)
}
end

def self.extract_breeding_site_from_row(row_content)
def extract_breeding_site_from_row(row_content)
type = row_content[:breeding_site].strip.downcase

organizationBreedingSite = OrganizationBreedingSite.by_organization_code(self.user.selected_membership.organization_id,type)
if !organizationBreedingSite.empty?
breeding_site = organizationBreedingSite.breeding_site
else
if type.include?("a")
breeding_site = BreedingSite.find_by_string_id(BreedingSite::Types::DISH)
elsif type.include?("b")
Expand All @@ -114,10 +117,18 @@ def self.extract_breeding_site_from_row(row_content)
elsif type.include?("t")
breeding_site = BreedingSite.find_by_string_id(BreedingSite::Types::SMALL_CONTAINER)
end

end
return breeding_site
end

def extract_organization_breeding_site_from_row(row_content)
type = row_content[:breeding_site].strip.upcase
organizationBreedingSite = OrganizationBreedingSite.by_organization_code(self.user.selected_membership.organization_id,type)
if !organizationBreedingSite.empty?
return organizationBreedingSite.first
end
end

#----------------------------------------------------------------------------

def self.generate_description_from_row_content(row_content)
Expand Down Expand Up @@ -170,7 +181,7 @@ def check_for_breeding_site_errors(rows)
next if row_content[:breeding_site].blank?

type = row_content[:breeding_site].strip.downcase
if Spreadsheet.accepted_breeding_site_codes.exclude?(type[0])
if self.accepted_breeding_site_codes.exclude?(type)
CsvError.create(:csv_id => self.id, :error_type => CsvError::Types::UNKNOWN_CODE)
end
end
Expand Down Expand Up @@ -233,10 +244,10 @@ def self.load_spreadsheet(file)
# TODO: This should technically be abstracted into paperclip_defaults.
# See https://github.com/thoughtbot/paperclip#uri-obfuscation
# See http://stackoverflow.com/questions/22416990/paperclip-unable-to-change-default-path
file_location = (Rails.env.production? || Rails.env.staging?) ? file.url : file.path
# file_location = (Rails.env.production? || Rails.env.staging?) ? file.url : file.path

if File.extname( file.original_filename ) == ".xlsx"
spreadsheet = Roo::Excelx.new(file_location, :file_warning => :ignore)
spreadsheet = Roo::Excelx.new(file.url, :file_warning => :ignore)
end

return spreadsheet
Expand All @@ -246,8 +257,13 @@ def self.clean_breeding_site_codes
return ["n"]
end

def self.accepted_breeding_site_codes
return ["a", "b", "l", "m", "p", "t", "x"] + self.clean_breeding_site_codes
def accepted_breeding_site_codes
breedingSitesCodes = self.user.selected_membership.organization.organizations_breeding_sites
if breedingSitesCodes
return breedingSitesCodes.map {|t| t.code.downcase } + ["a", "b", "l", "m", "p", "t", "x"] + Spreadsheet.clean_breeding_site_codes
else
return ["a", "b", "l", "m", "p", "t", "x"] + Spreadsheet.clean_breeding_site_codes
end
end

end
2 changes: 2 additions & 0 deletions app/views/api/v0/sessions/create.json.jbuilder
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ json.user do
end
end

json.breeding_sites_codes @user.selected_membership.organization.organizations_breeding_sites

json.neighborhoods Neighborhood.order("name ASC") do |n|
json.(n, :id, :name)
end
Expand Down
18 changes: 9 additions & 9 deletions app/workers/spreadsheet_parsing_worker.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require "sidekiq"

class SpreadsheetParsingWorker
include Sidekiq::Worker
include Sidekiq::Worker

sidekiq_options :queue => :csv_parsing, :retry => true, :backtrace => true
sidekiq_options :queue => :csv_parsing, :retry => true, :backtrace => true

def perform(csv_id)
# Make sure that we're parsing relative to the correct timezone.
Expand Down Expand Up @@ -36,15 +36,14 @@ def perform(csv_id)
# The start index is essentially the number of rows that are occupied by
# location metadata (including address, permission to record, etc)
header = Spreadsheet.extract_header_from_spreadsheet(spreadsheet)

#--------------------------------------------------------------------------
# At this point, we know that there is at least one row. Let's see if there
# are any incorrect breeding site codes.
@csv.check_for_breeding_site_errors(rows)

# Iterate over the rows, checking if any dates are invalid.
# puts "@csv.csv_errors1 #{@csv.csv_errors.to_yaml}"
@csv.check_for_date_errors(rows)

# puts "@csv.csv_errors2 #{@csv.csv_errors.to_yaml}"
# If there are any errors, we can't proceed so let's offload right now and let
# the user re-upload when they've fixed the errors.
return if @csv.csv_errors.present?
Expand Down Expand Up @@ -90,13 +89,13 @@ def perform(csv_id)
# the report.
uuid = Spreadsheet.generate_uuid_from_row_index_and_address(row, row_index, address)
description = Spreadsheet.generate_description_from_row_content(row_content)
breeding_site = Spreadsheet.extract_breeding_site_from_row(row_content)

# breeding_site = Spreadsheet.extract_breeding_site_from_row(row_content)
breeding_site = @csv.extract_breeding_site_from_row(row_content)
organization_breeding_site = @csv.extract_organization_breeding_site_from_row(row_content)
# We say that the report has a field identifier if the breeding site CSV column
# also has an integer associated with it.
field_id = nil
field_id = raw_breeding_code if raw_breeding_code =~ /\d/

field_id = raw_breeding_code if (raw_breeding_code && !organization_breeding_site.present?) =~ /\d/
# Add to reports only if the code doesn't equal "negative" code.
eliminated_at = Time.zone.parse( row_content[:eliminated_at] ) if row_content[:eliminated_at].present?

Expand Down Expand Up @@ -189,6 +188,7 @@ def perform(csv_id)
ins.inspected_at = current_visited_at
ins.description = description
ins.breeding_site_id = breeding_site.id if breeding_site.present?
ins.organization_breeding_site_id = organization_breeding_site.id if organization_breeding_site.present?
ins.protected = row_content[:protected]
ins.chemically_treated = row_content[:chemical]
ins.larvae = row_content[:larvae]
Expand Down
2 changes: 1 addition & 1 deletion config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
}
}

#----------------------------------------------------------------------------

# See: https://github.com/cyu/rack-cors
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateOrganizationsBreedingSitesJoinTables < ActiveRecord::Migration
def change
create_table :organizations_breeding_sites do |t|
t.integer :organization_id
t.integer :breeding_site_id
t.text :description, :default => ""
t.string :code
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddOrganizationBreedingSiteToInspections < ActiveRecord::Migration
def change
add_reference :inspections, :organization_breeding_site, index: false
end
end
12 changes: 10 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20180312171712) do
ActiveRecord::Schema.define(version: 20180403181708) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -204,7 +204,7 @@
t.integer "visit_id"
t.integer "report_id"
t.integer "identification_type"
t.integer "position", default: 0
t.integer "position", default: 0
t.integer "csv_id"
t.string "source"
t.datetime "last_synced_at"
Expand Down Expand Up @@ -233,6 +233,7 @@
t.string "csv_uuid"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "organization_breeding_site_id"
end

create_table "likes", force: :cascade do |t|
Expand Down Expand Up @@ -340,6 +341,13 @@
t.jsonb "breeding_sites_codes"
end

create_table "organizations_breeding_sites", force: :cascade do |t|
t.integer "organization_id"
t.integer "breeding_site_id"
t.text "description", default: ""
t.string "code"
end

create_table "posts", force: :cascade do |t|
t.integer "user_id"
t.string "title", limit: 255
Expand Down
5 changes: 4 additions & 1 deletion spec/factories.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# -*- encoding : utf-8 -*-
require 'rack/test'

FactoryGirl.define do
FactoryGirl.define do factory :organizations_breeding_sites_join_table do

end

factory :organization do
name "SSI"
end
Expand Down
5 changes: 5 additions & 0 deletions spec/models/organizations_breeding_sites_join_table_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe OrganizationsBreedingSitesJoinTable, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end