Skip to content

Commit

Permalink
Fixed multiple issues: multi-user, updating, parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
AJ ONeal committed Aug 30, 2009
1 parent 19d1b44 commit deec0cb
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 83 deletions.
89 changes: 57 additions & 32 deletions app/controllers/user_sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,62 @@ def create
render :action => :new
return
end
# TODO in Ldsorg.rb
# If they haven't upgraded to an LDS Account this won't mean much
# TODO in Ldsorg.rb / here: Notify if they have a valid old account, but need to upgrade to LDSAccount
flash[:notice] = "Login successful!"
@user = User.find_or_create_by_login(params[:user_session][:login])
@user_session = UserSession.create(@user, true)
Contact.current_user = current_user

# Build relationships
# Stake & Wards
@stake = Stake.find_or_create_by_name(@ldsorg.stake_name)
@ldsorg.wards_in_stake.each do |ward|
w = Ward.find_or_create_by_name(ward)
w.stake = @stake
w.save
end
@stake.save

# Ward & Members
i = 0
ward = Ward.find_by_name(@ldsorg.ward_name)
while ward.partial? do
if ward.updated_at < 15.seconds.ago || i > 300
#log timed out
ward.drop_contacts
break
end
i += 2
sleep 2
ward = Ward.find_by_name(@ldsorg.ward_name)
end
@ward = Ward.find_by_name(@ldsorg.ward_name)
puts @ldsorg.ward_name
if @ward.stale? or params[:user_session][:force_download]
@ward.destroy
@ward = Ward.new({:name => @ldsorg.ward_name})
if @ward.stale? #or params[:user_session][:force_download]
# NOTE if I change this to @ward.destroy, I have to relink the new one to each user
@ward.drop_contacts
#@ward.destroy
#@ward = Ward.new({:name => @ldsorg.ward_name})
#@ward.stake = @stake
#@ward.save # update time is after directory uploads
end
if @ward.empty? # if the ward is empty of contacts
download_directory
end
@ward.stake = @stake
@ward.save

# User & Member
# TODO BUG anomaly: First Last Jr w/ Family E-Mail
contact = Contact.find(:first, :conditions => @ldsorg.user_profile)
user = User.find_or_create_by_login(params[:user_session][:login])
contact.user = user
contact.save

# Double Check
#assert user.contact
#assert user.contact.ward
#assert user.contact.ward.contacts
#assert user.contact.ward.stake
#assert user.contact.ward.stake.wards
flash[:notice] = "Login successful! + Ward Partial: " + @ward.partial?.to_s

# All Systems Go!
@user_session = UserSession.create(user, true)
Contact.current_user = current_user
redirect_to :controller => 'home', :action => 'index'
end

Expand Down Expand Up @@ -75,37 +107,25 @@ def download_directory
# update rather than delete?

@ldsorg.directory_init #takes about 10 seconds in links2
puts @ldsorg.directory_length
me = Contact.new(@ldsorg.user_profile)
#@ldsorg.directory_length
contacts = []
while record = @ldsorg.directory_next
abort contact.errors unless contact = Contact.new(record)
contact.save
if contact.address_line_2 && contact.address_line_1
# TODO scope to ward?
if !contact.address_line_1
abort 'error: line_2 without line_1 for ' + contact.first + ' ' + contact.last
end
contact.address_group = AddressGroup.find_or_create_by_name(contact.address_line_1)
contact.address_group.save
else
contact.address_group = AddressGroup.find_or_create_by_name('default')
end
name = contact.address_line_1 ? contact.address_line_1 : 'default'
contact.address_group = AddressGroup.find_or_create_by_name(name)
contact.ward = @ward
# TODO BUG anomaly: First Last Jr w/ Family E-Mail
if (contact.first == me.first) && (contact.last == me.last) && (contact.email == me.email)
contact.user = @user
end
contact.save
abort "A member from the directory could not be saved..." +
" I guess they don't make it to Celestial glory..." unless contact.save
contacts << contact
end
abort 'User has no contact' unless @user.contact
@ward.updated_at = Time.now and @ward.save

#spawn do # TODO Queue w/ 10 minute limit
# Link Ward Members to their photos
for contact in contacts
if not contact.photo.data
#TODO contact.photo.data = File.open('images/anonymous.png').read
next
end
contact.photo.data = @ldsorg.member_photo(contact.photo.data)
Expand All @@ -119,6 +139,7 @@ def download_directory
#end

#spawn do
# Link Ward Members to their Callings
@ldsorg.calling_types.each do |t|
type = CallingType.find_or_create_by_name(t)
@ldsorg.callings_for_type(t).each do |tuple|
Expand All @@ -128,8 +149,12 @@ def download_directory
type.save
#end
contact = Contact.find(:first, :conditions => tuple[:contact])
contact.callings << calling
contact.save
if not contact
logger.error tuple.inspect
else
contact.callings << calling
contact.save
end
end
end
#end
Expand Down
23 changes: 16 additions & 7 deletions app/models/ward.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,22 @@ class Ward < ActiveRecord::Base
#TODO
#has_many :wards, :through => :stake

def empty?
return contacts.nil? ? true : contacts.empty?
end
def stale?
if not self.completed_at
return true
end
if not self.updated_at
return true
end
return self.updated_at < 72.hours.ago
return !updated_at.nil? && updated_at < 72.hours.ago
end
def complete?
return !completed_at.nil? && completed_at > 30.days.ago
end
def partial?
return !empty? && !complete?
end
def drop_contacts
updated_at = nil
completed_at = nil
contacts.each {|c| c.destroy}
save
end
end
1 change: 1 addition & 0 deletions app/views/user_sessions/_show.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<p>
<% @user = current_user %>
<%= @user.contact.ward.stake.name %><br/>
<%= @user.contact.ward.stake.name %><br/>
<ul>
<li><strong><%= link_to @user.contact.ward.name, :controller => 'contacts', :action => 'index' %></strong></li>
<% for ward in @user.contact.ward.stake.wards
Expand Down
17 changes: 12 additions & 5 deletions lib/ldsorg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,23 @@ def member_photo(url)
end

def user_profile
return @user_profile unless not @user_profile
profile_page = @unit_page.links.find {|l| l.text =~ MY_ACCOUNT_SEARCH}.click
form = profile_page.forms.find {|f| f.name = 'profileForm'}
last_query = 'html/body/table[2]/tr[1]/td[2]/table/tr[2]/td[2]/table/tr[1]/td[1]/table/tr/td[1]/form/table/tr[1]/td[2]/span'

record = {}
record[:first] = form['prefName'].strip
record[:last] = profile_page.search(last_query).inner_text.strip
record[:email] = form['email'].strip
first_second = form['prefName'].strip.split('\s+')
second = first_second[1] ? first_second[1] : nil
third_last = profile_page.search(last_query).inner_text.strip.split('\s+')
@user_profile = {
:first => first_second[0],
:second => first_second[1] ? first_second[1] : nil,
:third => third_last[1] ? third_last[0] : nil,
:last => third_last[1] ? third_last[1] : third_last[0],
:email => form['email'].strip,
}

return record
return @user_profile
end

def calling_types
Expand Down
4 changes: 2 additions & 2 deletions lib/test_ldsorg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class TestLdsorg
require 'ldsorg'

def initialize
#@ldsorg = Ldsorg.new('user', 'secret')
@ldsorg = Ldsorg.new('user', 'secret')
abort 'no login' unless @ldsorg.ldslogin
end

Expand Down Expand Up @@ -51,4 +51,4 @@ def test_callings_and_types
test = TestLdsorg.new
test.test_user_profile
#test.test_directory_next
test.test_callings_and_types
#test.test_callings_and_types
37 changes: 0 additions & 37 deletions test.rb

This file was deleted.

0 comments on commit deec0cb

Please sign in to comment.