Skip to content

Commit

Permalink
[webui] remove the first webui model: StatusMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
coolo committed Oct 22, 2013
1 parent 8267041 commit 690ab15
Show file tree
Hide file tree
Showing 16 changed files with 178 additions and 208 deletions.
39 changes: 18 additions & 21 deletions src/api/app/controllers/status_controller.rb
Expand Up @@ -29,29 +29,26 @@ def update_messages

new_messages = ActiveXML::Node.new(request.raw_post)

begin
if new_messages.has_element? 'message'
# message(s) are wrapped in outer xml tag 'status_messages'
new_messages.each_message do |msg|
message = StatusMessage.new
message.message = msg.to_s
message.severity = msg.value :severity
message.user = @http_user
message.save
end
else
raise RuntimeError.new 'no message' if new_messages.element_name != 'message'
# just one message, NOT wrapped in outer xml tag 'status_messages'
message = StatusMessage.new
message.message = new_messages.to_s
message.severity = new_messages.value :severity
message.user = @http_user
message.save
if new_messages.has_element? 'message'
# message(s) are wrapped in outer xml tag 'status_messages'
new_messages.each_message do |msg|
save_new_message(msg)
end
render_ok
rescue RuntimeError
raise CreatingMessagesError.new "message(s) cannot be created"
else
# TODO: make use of a validator
raise CreatingMessagesError.new "no message #{new_messages.dump_xml}" if new_messages.element_name != 'message'
# just one message, NOT wrapped in outer xml tag 'status_messages'
save_new_message(new_messages)
end
render_ok
end

def save_new_message(msg)
message = StatusMessage.new
message.message = msg.to_s
message.severity = msg.value :severity
message.user = User.current
message.save!
end

def delete_message
Expand Down
4 changes: 2 additions & 2 deletions src/api/app/models/status_message.rb
@@ -1,8 +1,8 @@
class StatusMessage < ActiveRecord::Base

belongs_to :user

scope :alive, -> { where(:deleted_at => nil) }
validates :user, :severity, :message, presence: true
scope :alive, -> { where(:deleted_at => nil).order("created_at DESC") }

def delete
self.deleted_at = Time.now
Expand Down
5 changes: 5 additions & 0 deletions src/api/app/models/user.rb
Expand Up @@ -346,6 +346,11 @@ def get_by_login(login)
def find_by_email(email)
return where(:email => email).first
end

def realname_for_login(login)
User.find_by_login(login).realname
end

end

# After validation, the password should be encrypted
Expand Down
7 changes: 5 additions & 2 deletions src/api/test/functional/status_controller_test.rb
Expand Up @@ -22,8 +22,11 @@ def test_new_message
put "/status/messages", '<whereareyou/>'
assert_response 400

login_king
put "/status/messages", '<message>I have nothing to say</message>'
put "/status/messages", '<messages><message>nada</message></messages>'
assert_response 400
assert_xml_tag attributes: { code: 'invalid_record' }

put "/status/messages", '<message severity="1">I have nothing to say</message>'
assert_response :success

# delete it again
Expand Down
2 changes: 1 addition & 1 deletion src/api/test/functional/webui/messages_test.rb
Expand Up @@ -19,7 +19,7 @@ class Webui::MessagesTest < Webui::IntegrationTest
find_button("Ok").click

find(:id, 'messages').must_have_text message
find(:css, '.delete-message').click
first(:css, '.delete-message').click
find_button("Ok").click

# check that it's gone
Expand Down
6 changes: 3 additions & 3 deletions src/api/test/unit/status_message_test.rb
Expand Up @@ -4,13 +4,13 @@ class StatusMessageTest < ActiveSupport::TestCase
fixtures :users

def test_something
sm = StatusMessage.new :message => 'nothing is here'
sm = StatusMessage.new message: 'nothing is here', severity: 2
sm.user = User.find_by_login 'tom'
sm.save
sm.save!
end

def test_delete
tbd = StatusMessage.create :message => 'to be deleted', :user => User.find_by_login('tom')
tbd = StatusMessage.create! message: 'to be deleted', user: User.find_by_login('tom'), severity: 1
#tbd.user = User.find_by_login 'tom'
tbd.delete

Expand Down
56 changes: 24 additions & 32 deletions src/api/webui/app/controllers/webui/main_controller.rb
@@ -1,18 +1,19 @@
module Webui
class MainController < WebuiController
class Webui::MainController < Webui::WebuiController

include Webui::WebuiHelper

before_filter :require_admin, only: [:delete_message]
# permissions.status_message_create
before_filter :require_admin, only: [:delete_message, :add_news]


def index
@news = find_cached(Webui::Statusmessage, :conditions => 'deleted_at IS NULL', :order => 'create_at DESC', :limit => 4, :expires_in => 15.minutes)
@news = StatusMessage.alive.limit(4)
unless @spider_bot
@latest_updates = find_cached(LatestUpdated, :limit => 6, :expires_in => 5.minutes, :shared => true)
@latest_updates = find_cached(Webui::LatestUpdated, :limit => 6, :expires_in => 5.minutes, :shared => true)
end
rescue ActiveXML::Transport::UnauthorizedError
@anonymous_forbidden = true
logger.error "Could not load all frontpage data, probably due to forbidden anonymous access in the api."
logger.error 'Could not load all frontpage data, probably due to forbidden anonymous access in the api.'
end

# This action does the heavy lifting for the index method and is only invoked by an AJAX request
Expand All @@ -26,16 +27,16 @@ def systemstatus
end
end
@waiting_packages = 0
@workerstatus.elements("waiting") {|waiting| @waiting_packages += waiting["jobs"].to_i}
@global_counters = find_cached(GlobalCounters, :expires_in => 15.minutes, :shared => true)
@workerstatus.elements('waiting') {|waiting| @waiting_packages += waiting['jobs'].to_i}
@global_counters = find_cached(Webui::GlobalCounters, :expires_in => 15.minutes, :shared => true)
@busy = nil
require_available_architectures unless @spider_bot
if @available_architectures
@available_architectures.each.map {|arch| map_to_workers(arch.name) }.uniq.each do |arch|
archret = frontend.gethistory("building_" + arch, 168).map {|time,value| [time,value]}
archret = frontend.gethistory('building_' + arch, 168).map {|time,value| [time,value]}
if archret.length > 0
if @busy
@busy = MonitorController.addarrays(@busy, archret)
@busy = Webui::MonitorController.addarrays(@busy, archret)
else
@busy = archret
end
Expand All @@ -49,36 +50,36 @@ def systemstatus
end

def news
@news = find_cached(Statusmessage, :conditions => 'deleted_at IS NULL', :order => 'create_at DESC', :limit => 5, :expires_in => 15.minutes)
@news = StatusMessage.alive.limit(5)
raise ActionController::RoutingError.new('expected application/rss') unless request.format == Mime::RSS
render layout: false
end

def latest_updates
raise ActionController::RoutingError.new('expected application/rss') unless request.format == Mime::RSS
@latest_updates = find_cached(LatestUpdated, :limit => 6, :expires_in => 5.minutes, :shared => true)
@latest_updates = find_cached(Webui::LatestUpdated, :limit => 6, :expires_in => 5.minutes, :shared => true)
render layout: false
end

def sitemap
render :layout => false, :content_type => "application/xml"
render :layout => false, :content_type => 'application/xml'
end

def require_projects
@projects = Array.new
find_cached(Collection, :id, :what => "project").each_project do |p|
find_cached(Webui::Collection, :id, :what => 'project').each_project do |p|
@projects << p.value(:name)
end
end

def sitemap_projects
require_projects
render :layout => false, :content_type => "application/xml"
render :layout => false, :content_type => 'application/xml'
end

def sitemap_projects_subpage(action, changefreq, priority)
require_projects
render :template => "webui/main/sitemap_projects_subpage", :layout => false, :locals => { :action => action, :changefreq => changefreq, :priority => priority }, :content_type => "application/xml"
render :template => 'webui/main/sitemap_projects_subpage', :layout => false, :locals => { :action => action, :changefreq => changefreq, :priority => priority }, :content_type => 'application/xml'
end

def sitemap_projects_packages
Expand All @@ -100,10 +101,10 @@ def sitemap_packages
elsif category == 'main'
predicate = "not(starts-with(@project,'home:')) and not(starts-with(@project,'DISCONTINUED:')) and not(starts-with(@project,'openSUSE:'))"
end
find_cached(Collection, :id, :what => 'package', :predicate => predicate).each_package do |p|
find_cached(Webui::Collection, :id, :what => 'package', :predicate => predicate).each_package do |p|
@packages << [p.value(:project), p.value(:name)]
end
render :template => 'webui/main/sitemap_packages', :layout => false, :locals => {:action => params[:listaction]}, :content_type => "application/xml"
render :template => 'webui/main/sitemap_packages', :layout => false, :locals => {:action => params[:listaction]}, :content_type => 'application/xml'
end

def add_news_dialog
Expand All @@ -112,17 +113,11 @@ def add_news_dialog

def add_news
if params[:message].nil? or params[:severity].empty?
flash[:error] = "Please provide a message and severity"
flash[:error] = 'Please provide a message and severity'
redirect_to(:action => 'index') and return
end

begin
message = Statusmessage.new(:message => params[:message], :severity => params[:severity])
message.save
Statusmessage.free_cache(:conditions => 'deleted_at IS NULL', :order => 'create_at DESC', :limit => 5)
rescue ActiveXML::Transport::ForbiddenError
flash[:error] = 'Only admin users may post status messages'
end
#TODO - make use of permissions.status_message_create
StatusMessage.create!(message: params[:message], severity: params[:severity], user: @user.api_user)
redirect_to(:action => 'index')
end

Expand All @@ -132,18 +127,15 @@ def delete_message_dialog

def delete_message
required_parameters :message_id
message = Statusmessage.find(:id => params[:message_id])
message.delete
Statusmessage.free_cache(:conditions => 'deleted_at IS NULL', :order => 'create_at DESC', :limit => 5)
StatusMessage.find(params[:message_id]).delete
redirect_to(:action => 'index')
end

def require_available_architectures
super # Call ApplicationController implementation, but catch an additional exception
rescue ActiveXML::Transport::UnauthorizedError
@anonymous_forbidden = true
logger.error "Could not load all frontpage data, probably due to forbidden anonymous access in the api."
logger.error 'Could not load all frontpage data, probably due to forbidden anonymous access in the api.'
end

end
end
12 changes: 1 addition & 11 deletions src/api/webui/app/helpers/webui/webui_helper.rb
Expand Up @@ -66,17 +66,7 @@ def user_icon(login, size=20, css_class=nil, alt=login)
end

def fuzzy_time_string(time)
diff = Time.now - Time.parse(time)
return "now" if diff < 60
return (diff/60).to_i.to_s + " min ago" if diff < 3600
diff = Integer(diff/3600) # now hours
return diff.to_s + (diff == 1 ? " hour ago" : " hours ago") if diff < 24
diff = Integer(diff/24) # now days
return diff.to_s + (diff == 1 ? " day ago" : " days ago") if diff < 14
diff_w = Integer(diff/7) # now weeks
return diff_w.to_s + (diff_w == 1 ? " week ago" : " weeks ago") if diff < 63
diff_m = Integer(diff/30.5) # roughly months
return diff_m.to_s + " months ago"
time_ago_in_words(Time.parse(time))
end

def status_for( repo, arch, package )
Expand Down
14 changes: 9 additions & 5 deletions src/api/webui/app/models/webui/person.rb
Expand Up @@ -40,14 +40,18 @@ def self.find_cached(login, opts = {})
super
end

def self.email_for_login(person)
p = Person.find_hashed(person)
return p["email"] || ''
# temporary aid
def self.from_user(user)
Person.new(user.render_axml)
end

def self.realname_for_login(person)
def api_user
User.find_by_login(login)
end

def self.email_for_login(person)
p = Person.find_hashed(person)
return p["realname"] || ''
return p["email"] || ''
end

def initialize(data)
Expand Down
2 changes: 0 additions & 2 deletions src/api/webui/app/models/webui/project_status.rb

This file was deleted.

15 changes: 0 additions & 15 deletions src/api/webui/app/models/webui/statusmessage.rb

This file was deleted.

@@ -1,7 +1,7 @@
<% short ||= false %>
<% no_icon ||= false %>
<% no_link ||= false %>
<% realname = Person.realname_for_login(user) %>
<% realname = User.realname_for_login(user) %>
<% unless no_icon %>
<%= user_icon(user) %>
Expand Down
@@ -1,7 +1,7 @@
<% short ||= false %>
<% no_icon ||= false %>
<% no_link ||= false %>
<% realname = Webui::Person.realname_for_login(user) %>
<% realname = User.realname_for_login(user) %>
<% unless no_icon %>
<%= user_icon(user) %>
Expand Down
Expand Up @@ -7,7 +7,7 @@
<%= label_tag(:message, 'Message:') %><br/>
<%= text_area_tag(:message, '', :size => '40x3') %><br/>
<%= label_tag(:severity, "Severity:") %><br/>
<%= select_tag(:severity, options_for_select([['Information', 0], ['Green', 1], ['Yello', 2], ['Red', 3]])) %>
<%= select_tag(:severity, options_for_select([['Information', 0], ['Green', 1], ['Yellow', 2], ['Red', 3]])) %>
</p>
<div class="buttons">
<%= submit_tag('Ok') %>
Expand Down

0 comments on commit 690ab15

Please sign in to comment.