Archive for the ‘Webpage’ Category

Go to the elog website and log into one of the logbooks (say DRS4).

Go to “Config”

Go to “Change config file”

Select “Create new logbook”

Give it a name and use template or template_password as the template.

Under “Config” you can add a new user. Don’t check any boxes by subscribe to logbooks. This sends these people email each time a post is made.

We have a rails application that allows users to sign up for an event. People not logged in are only allowed to see the names of people who have signed up to attend. People with logins are allowed to see all the contact info for the attendees. I decided it would also be nice to make a downloadable spreadsheet of this information for those people who will need to contact the attendees. Here is how I did it.

First, get the fasterCSV gem. Add this to the Gemfile:

gem 'fastercsv', :require => 'faster_csv'

Basically, all that will happen is that there will be a link on the attendees index page that only logged in users will see (and be allowed to access). This is done in two steps. First the csv file is generated and then it is downloaded.

So, where shall I put the file? Since my development and production workstations have different operating systems, I’m going to have different paths on both of them. I created an yaml file to hold this information.

config/download_csv.yml

development:
    csvfile: /Users/me/Desktop/output.csv
    
production:
    csvfile: /local/code/web/app/myapp/shared/system/output.csv

In my attendee model, I created a method to generate the csv file.

  def self.generate_csv
    csv_settings = YAML.load_file("#{Rails.root.to_s}/config/download_csv.yml")[Rails.env]
    csvfile = csv_settings['csvfile']
    @attendees = Attendee.order('lastname ASC')
    FasterCSV.open("#{csvfile}",'w') do |csv|
      csv << ['Firstname','Lastname','Institution','Address','Email','Number attending']
      @attendees.each do |attendee|
        csv << [attendee.firstname, attendee.lastname, attendee.institution, attendee.address, attendee.email, attendee.fest_count]
      end
    end
  end

In my attendee controller, I created a method called download_csv. In this method, I actually call the generate_csv method above. (Side note: I originally had it set up so that the generate_csv method was called with each new create method. But then decided that fewer people would be downloading the csv file than would be registering. And I changed things so that the csv is only generated when someone wants to download it.)

  def download_csv
    csv_settings = YAML.load_file("#{Rails.root.to_s}/config/download_csv.yml")[Rails.env]
    csvfile = csv_settings['csvfile']
    Attendee.generate_csv
    send_file "#{csvfile}", :type => 'text/csv'
  end

Since I'm using authlogic and declarative_authorization in this app, I also needed to edit my filter_resource_access line in the method to:

filter_resource_access :additional_collection => [[:download_csv, :edit], :edit]

(I.E. People who are allowed to edit attendees are also allowed to download the file.)

Lastly, on my view I have the link:

<% if current_user %>
	<%= link_to 'Download Spreadsheet of Attendees', :controller => :attendees, :action => :download_csv %>
<% end %>

This all worked perfectly on my laptop while I tested it. But when I uploaded it to the server, it kept giving me an empty file, even though the file on the server was not empty. Apparently, this is a common problem. All I needed to do was edit my environments/production.rb file to match this:

  # Specifies the header that your server uses for sending files
  #config.action_dispatch.x_sendfile_header = "X-Sendfile"

  # For nginx:
  config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect'

We have an openldap server running. I wrote a little rails app that required authentication and that I wanted to authenticate against users in the openldap server. Here’s how I did it.

Here is the relevant part of my Gemfile. I’m using Rails 3.0.3.

gem 'authlogic'
gem 'declarative_authorization'
gem 'net-ldap', :require => 'net/ldap'

My user migration:

class CreateUsers < ActiveRecord::Migration
  def self.up
    create_table :users do |t|
      t.string :username
      t.string :firstname
      t.string :lastname
      t.string :persistence_token
      t.datetime :last_request_at
      t.string :role

      t.timestamps
    end
  end

Add to my user model:

  acts_as_authentic do |c|
    c.validate_password_field = false
    c.logged_in_timeout = 2.hours
  end    

  def valid_password?(password)
    ldap_settings = YAML.load_file("#{Rails.root.to_s}/config/ldap.yml")[Rails.env]
    #logger.info ldap_settings.inspect
    
    ldap_settings[:host] = ldap_settings['host']
    ldap_settings[:port] = ldap_settings['port']
    ldap_settings[:encryption] = { :method => :simple_tls } if ldap_settings['ssl']
    ldap_settings[:auth] = 
      { :method => :simple, :username => "uid=#{self.username},#{ldap_settings['base']}", :password => password}

    #logger.info ldap_settings.inspect
    
    ldap = Net::LDAP.new(ldap_settings)
    ldap.bind
  end

Add to my user_session model:

  logout_on_timeout true

My new user_session:

<%= form_for @user_session, :as => :user_session, :url => { :action => 'create' } do |f| -%>
  

<%= f.label :username %>
<%= f.text_field :username %>

<%= f.label :password %>
<%= f.password_field :password %>

<%= f.submit 'Login' %>

<% end %> <%= link_to 'Back', :back %>

Needed the stuff after @user_session because of a problem with authlogic and rails3.

Finally, I created a yaml file with the info for my ldap server. It's in config/ldap.yml.

development:
    host: eagle.example.com
    port: 636
    base: ou=people,dc=group,dc=example,dc=com
    ssl: true

production:
    host: eagle.example.com
    port: 636
    base: ou=people,dc=group,dc=example,dc=com
    ssl: true

That pretty much did it. You MUST use port 636 and ssl:true for this to work. Starttls over port 389 will not work. (At least, it didn't for me.)

After updating some webpages, I needed to redirect people from the old page to the new one. Put this in the old page location.

<html> <head>
<META HTTP-EQUIV="Refresh"
content="0;URL=../tilecal/index.php">
</head>
</html>

Until I create a php file to add/change events to the webpage, I have to do this by hand. Here are some examples:

To add a Monday Seminar by Mark:

insert into events values
(NULL,2,'2007-04-09',3,'16:15:00','17:15:00','RI-480','Mark Oreglia','University of Chicago','The Roadmap to the ILC','',1);

To add an announcement about the floors in HEP being redone

insert into events values
(NULL,1,'2007-04-09','17:00:00','19:00:00','HEP','Dennis Hannum','4-5661 with questions','HEP Floors Cleaned-Please Remove All Personal Items from the Floor','',7);

To change the speaker info on a Lunch Talk

update events set speaker='Bruce Mellado',institution='UW-Madison',title='Missing ET Reconstruction in ATLAS' where date='2005-04-02' and starttime='12:15:00';

To delete an entry
Find the id of the entry to delete

select * from events where speaker='Dennis Hannum';
delete from events where id=70;

Could do this command in one line, but it’s a good idea to make sure you’re deleting the right data.