<commit_before>class Site < ActiveRecord::Base
  has_many :observations, inverse_of: :site
  has_many :users, inverse_of: :site
  has_many :site_admins, inverse_of: :site
  has_many :posts, as: :parent, dependent: :destroy
  has_many :journal_posts, class_name: "Post", as: :parent, dependent: :destroy
  has_many :announcements, inverse_of: :site, dependent: :destroy

  scope :live, -> { where(draft: false) }
  scope :drafts, -> { where(draft: true) }

  # shortened version of the name
  preference :site_name_short, :string

  # Email addresses
  preference :email_admin, :string
  preference :email_noreply, :string
  preference :email_help, :string
  preference :email_info, :string

  # preference :flickr_key, :string
  # preference :flickr_shared_secret, :string
  # preference :facebook_app_id, :string

  preference :contact_first_name, :string
  preference :contact_last_name, :string
  preference :contact_organization, :string
  preference :contact_position, :string
  preference :contact_address, :string
  preference :contact_city, :string
  preference :contact_admin_area, :string
  preference :contact_postal_code, :string
  preference :contact_country, :string
  preference :contact_phone, :string
  preference :contact_email, :string
  preference :contact_url, :string

  preference :locale, :string, :default => "en"

  # default bounds for most maps, including /observations/new and the home page. Defaults to the whole world
  preference :geo_swlat, :string
  preference :geo_swlng, :string
  preference :geo_nelat, :string
  preference :geo_nelng, :string

  # default place ID for place filters. Presently only used on /places, but use may be expanded
  belongs_to :place, :inverse_of => :sites

  # header logo, should be at least 118x22
  if Rails.env.production?
    has_attached_file :logo,
      :storage => :s3,
      :s3_credentials => "#{Rails.root}/config/s3.yml",
      :s3_protocol => "https",
      :s3_host_alias => CONFIG.s3_bucket,
      :bucket => CONFIG.s3_bucket,
      :path => "sites/:id-logo.:extension",
      :url => ":s3_alias_url",
      :default_url => "/assets/logo-small.gif"
    invalidate_cloudfront_caches :logo, "sites/:id-logo.*"
  else
    has_attached_file :logo,
      :path => ":rails_root/public/attachments/sites/:id-logo.:extension",
      :url => "#{ CONFIG.attachments_host }/attachments/sites/:id-logo.:extension",
      :default_url => FakeView.image_url("logo-small.gif")
  end
  validates_attachment_content_type :logo, :content_type => [/jpe?g/i, /png/i, /gif/i, /octet-stream/], 
    :message => "must be JPG, PNG, or GIF"

  # large square branding image that appears on pages like /login. Should be 300 px wide and about that tall
  if Rails.env.production?
    has_attached_file :logo_square,
      :storage => :s3,
      :s3_credentials => "#{Rails.root}/config/s3.yml",
      :s3_protocol => "https",
      :s3_host_alias => CONFIG.s3_bucket,
      :bucket => CONFIG.s3_bucket,
      :path => "sites/:id-logo_square.:extension",
      :url => ":s3_alias_url",
      :default_url => ->(i){ FakeView.image_url("bird.png") }
    invalidate_cloudfront_caches :logo_square, "sites/:id-logo_square.*"
  else
    has_attached_file :logo_square,
      :path => ":rails_root/public/attachments/sites/:id-logo_square.:extension",
      :url => "#{ CONFIG.attachments_host }/attachments/sites/:id-logo_square.:extension",
      :default_url => FakeView.image_url("bird.png")
  end
  validates_attachment_content_type :logo_square, :content_type => [/jpe?g/i, /png/i, /gif/i, /octet-stream/], 
    :message => "must be JPG, PNG, or GIF"

  # large square branding image that appears on pages like /login. Should be 300 px wide and about that tall
  if Rails.env.production?
    has_attached_file :logo_email_banner,
      :storage => :s3,
      :s3_credentials => "#{Rails.root}/config/s3.yml",
      :s3_protocol => "https",
      :s3_host_alias => CONFIG.s3_bucket,
      :bucket => CONFIG.s3_bucket,
      :path => "sites/:id-logo_email_banner.:extension",
      :url => ":s3_alias_url",
      :default_url => ->(i){ FakeView.image_url("inat_email_banner.png") }
    invalidate_cloudfront_caches :logo_email_banner, "sites/:id-logo_email_banner.*"
  else
    has_attached_file :logo_email_banner,
      :path => ":rails_root/public/attachments/sites/:id-logo_email_banner.:extension",
      :url => "#{ CONFIG.attachments_host }/attachments/sites/:id-logo_email_banner.:extension",
      :default_url => FakeView.image_url("inat_email_banner.png")
  end
  validates_attachment_content_type :logo_email_banner, :content_type => [/jpe?g/i, /png/i, /gif/i, /octet-stream/], :message => "must be JPG, PNG, or GIF"
      
  # CSS file to override default styles
  if Rails.env.production?
    has_attached_file :stylesheet,
      :storage => :s3,
      :s3_credentials => "#{Rails.root}/config/s3.yml",
      :s3_protocol => "https",
      :s3_host_alias => CONFIG.s3_bucket,
      :bucket => CONFIG.s3_bucket,
      :path => "sites/:id-stylesheet.css",
      :url => ":s3_alias_url"
    invalidate_cloudfront_caches :stylesheet, "sites/:id-stylesheet.css"
  else
    has_attached_file :stylesheet,
      :path => ":rails_root/public/attachments/sites/:id-stylesheet.css",
      :url => "#{ CONFIG.attachments_host }/attachments/sites/:id-stylesheet.css"
  end

  validates_attachment_content_type :stylesheet, :content_type => "text/css", :message => "must be CSS"

  # URL where visitors can learn more about the site
  preference :about_url, :string

  # URL where visitors can get help using the site
  preference :help_url, :string, :default => FakeView.wiki_page_url("help")<commit_msg>Bugfix: sites were not allowing CSS uploads.<commit_after>class Site < ActiveRecord::Base
  has_many :observations, inverse_of: :site
  has_many :users, inverse_of: :site
  has_many :site_admins, inverse_of: :site
  has_many :posts, as: :parent, dependent: :destroy
  has_many :journal_posts, class_name: "Post", as: :parent, dependent: :destroy
  has_many :announcements, inverse_of: :site, dependent: :destroy

  scope :live, -> { where(draft: false) }
  scope :drafts, -> { where(draft: true) }

  # shortened version of the name
  preference :site_name_short, :string

  # Email addresses
  preference :email_admin, :string
  preference :email_noreply, :string
  preference :email_help, :string
  preference :email_info, :string

  # preference :flickr_key, :string
  # preference :flickr_shared_secret, :string
  # preference :facebook_app_id, :string

  preference :contact_first_name, :string
  preference :contact_last_name, :string
  preference :contact_organization, :string
  preference :contact_position, :string
  preference :contact_address, :string
  preference :contact_city, :string
  preference :contact_admin_area, :string
  preference :contact_postal_code, :string
  preference :contact_country, :string
  preference :contact_phone, :string
  preference :contact_email, :string
  preference :contact_url, :string

  preference :locale, :string, :default => "en"

  # default bounds for most maps, including /observations/new and the home page. Defaults to the whole world
  preference :geo_swlat, :string
  preference :geo_swlng, :string
  preference :geo_nelat, :string
  preference :geo_nelng, :string

  # default place ID for place filters. Presently only used on /places, but use may be expanded
  belongs_to :place, :inverse_of => :sites

  # header logo, should be at least 118x22
  if Rails.env.production?
    has_attached_file :logo,
      :storage => :s3,
      :s3_credentials => "#{Rails.root}/config/s3.yml",
      :s3_protocol => "https",
      :s3_host_alias => CONFIG.s3_bucket,
      :bucket => CONFIG.s3_bucket,
      :path => "sites/:id-logo.:extension",
      :url => ":s3_alias_url",
      :default_url => "/assets/logo-small.gif"
    invalidate_cloudfront_caches :logo, "sites/:id-logo.*"
  else
    has_attached_file :logo,
      :path => ":rails_root/public/attachments/sites/:id-logo.:extension",
      :url => "#{ CONFIG.attachments_host }/attachments/sites/:id-logo.:extension",
      :default_url => FakeView.image_url("logo-small.gif")
  end
  validates_attachment_content_type :logo, :content_type => [/jpe?g/i, /png/i, /gif/i, /octet-stream/], 
    :message => "must be JPG, PNG, or GIF"

  # large square branding image that appears on pages like /login. Should be 300 px wide and about that tall
  if Rails.env.production?
    has_attached_file :logo_square,
      :storage => :s3,
      :s3_credentials => "#{Rails.root}/config/s3.yml",
      :s3_protocol => "https",
      :s3_host_alias => CONFIG.s3_bucket,
      :bucket => CONFIG.s3_bucket,
      :path => "sites/:id-logo_square.:extension",
      :url => ":s3_alias_url",
      :default_url => ->(i){ FakeView.image_url("bird.png") }
    invalidate_cloudfront_caches :logo_square, "sites/:id-logo_square.*"
  else
    has_attached_file :logo_square,
      :path => ":rails_root/public/attachments/sites/:id-logo_square.:extension",
      :url => "#{ CONFIG.attachments_host }/attachments/sites/:id-logo_square.:extension",
      :default_url => FakeView.image_url("bird.png")
  end
  validates_attachment_content_type :logo_square, :content_type => [/jpe?g/i, /png/i, /gif/i, /octet-stream/], 
    :message => "must be JPG, PNG, or GIF"

  # large square branding image that appears on pages like /login. Should be 300 px wide and about that tall
  if Rails.env.production?
    has_attached_file :logo_email_banner,
      :storage => :s3,
      :s3_credentials => "#{Rails.root}/config/s3.yml",
      :s3_protocol => "https",
      :s3_host_alias => CONFIG.s3_bucket,
      :bucket => CONFIG.s3_bucket,
      :path => "sites/:id-logo_email_banner.:extension",
      :url => ":s3_alias_url",
      :default_url => ->(i){ FakeView.image_url("inat_email_banner.png") }
    invalidate_cloudfront_caches :logo_email_banner, "sites/:id-logo_email_banner.*"
  else
    has_attached_file :logo_email_banner,
      :path => ":rails_root/public/attachments/sites/:id-logo_email_banner.:extension",
      :url => "#{ CONFIG.attachments_host }/attachments/sites/:id-logo_email_banner.:extension",
      :default_url => FakeView.image_url("inat_email_banner.png")
  end
  validates_attachment_content_type :logo_email_banner, :content_type => [/jpe?g/i, /png/i, /gif/i, /octet-stream/], :message => "must be JPG, PNG, or GIF"
      
  # CSS file to override default styles
  if Rails.env.production?
    has_attached_file :stylesheet,
      :storage => :s3,
      :s3_credentials => "#{Rails.root}/config/s3.yml",
      :s3_protocol => "https",
      :s3_host_alias => CONFIG.s3_bucket,
      :bucket => CONFIG.s3_bucket,
      :path => "sites/:id-stylesheet.css",
      :url => ":s3_alias_url"
    invalidate_cloudfront_caches :stylesheet, "sites/:id-stylesheet.css"
  else
    has_attached_file :stylesheet,
      :path => ":rails_root/public/attachments/sites/:id-stylesheet.css",
      :url => "#{ CONFIG.attachments_host }/attachments/sites/:id-stylesheet.css"
  end

  validates_attachment_content_type :stylesheet, content_type: [
    "text/css", 
    # Not great, but probably ok here where only site admins can add the file.
    # Underlying problem is that we force all validations to depend on the file
    # commant (see paperclip initializer) and it reads CSS as plain/text
    "text/plain"
  ], message: "must be CSS"

  # URL where visitors can learn more about the site
  preference :about_url, :string

  # URL where visitors can get help using the site
  preference :help_url, :string, :default => FakeView.wiki_page_url("help")
{'fraction_non_alphanumeric': 0.12982758620689655, 'fraction_numerical': 0.007413793103448276, 'mean_word_length': 4.019904803115534, 'pattern_counts': {'":': 16, '<': 5, '<?xml version=': 0, '>': 115, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5586502', 'n_tokens_mistral': 3965, 'n_tokens_neox': 3910, 'n_words': 891}