<commit_before>require 'sinatra/base'

module Mailout 
  class App < Sinatra::Base
    include Authlogic::ControllerAdapters::SinatraAdapter::Adapter::Implementation

    set :views, File.dirname(__FILE__) + '/views'

    API_KEY = 'e03757750894d3afb19d93edf0bf9421-us1'
    
    def initialize_mailchimp
      @account = Account.find(params[:id])
      halt 404 unless @account
      @mailchimp = Hominid::Base.new({:api_key => API_KEY })
    end

    get '/accounts/:id/mailouts' do
      initialize_mailchimp
      @campaigns = @mailchimp.campaigns
      erb :mailouts
    end
    
    post '/accounts/:id/mailouts' do
      initialize_mailchimp
      @mailchimp.create_campaign('regular', { :list_id => 'c18292dd69', :from_email => params[:mailout][:from_email], :from_name => params[:mailout][:name], :subject => params[:mailout][:subject], :to_email => params[:mailout][:to_email] }, { :html => "<h1>Test email</h1>" , :test =>"Test email" })
      redirect "/accounts/#{@account.id}/mailouts"
    end
    <commit_msg>Protect mailouts behind signup wall.<commit_after>require 'sinatra/base'

module Mailout 
  class App < Sinatra::Base
    include Authlogic::ControllerAdapters::SinatraAdapter::Adapter::Implementation

    set :views, File.dirname(__FILE__) + '/views'

    API_KEY = 'e03757750894d3afb19d93edf0bf9421-us1'
    
    def load_session 
        @account = Account.find(params[:id])
        halt 404 unless @account
        
        @current_user_session = UserSession.find
        @current_user = @current_user_session.nil? ? nil : @current_user_session.user 
        unless @current_user && (@current_user.has_role?("staff", @account) || @current_user.has_role?("admin"))
          redirect '/user_session/new'
        end
    end
    
    def initialize_mailchimp
      load_session
      @mailchimp = Hominid::Base.new({:api_key => API_KEY })
    end

    get '/accounts/:id/mailouts' do
      initialize_mailchimp
      @campaigns = @mailchimp.campaigns
      erb :mailouts
    end
    
    post '/accounts/:id/mailouts' do
      initialize_mailchimp
      @mailchimp.create_campaign('regular', { :list_id => 'c18292dd69', :from_email => params[:mailout][:from_email], :from_name => params[:mailout][:name], :subject => params[:mailout][:subject], :to_email => params[:mailout][:to_email] }, { :html => "<h1>Test email</h1>" , :test =>"Test email" })
      redirect "/accounts/#{@account.id}/mailouts"
    end
    
{'fraction_non_alphanumeric': 0.140278917145201, 'fraction_numerical': 0.02789171452009844, 'mean_word_length': 3.593220338983051, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 23, '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': '3903629', 'n_tokens_mistral': 851, 'n_tokens_neox': 798, 'n_words': 142}