-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expose more data over the API #143
base: master
Are you sure you want to change the base?
Changes from all commits
952b9ba
8b06df3
7452637
bcebec2
a24a188
2a8316f
1610e6d
fb3c2dc
3074c8d
edfeb9e
f2b15b5
d63c62e
9f7b0c4
fee0f93
e0b660a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -117,7 +117,7 @@ def status | |||||
return "Running" if is_running? | ||||||
return finalized? ? "Finalized" : "Preliminary" | ||||||
end | ||||||
|
||||||
def status_text(user_id) | ||||||
return "The contest has ended." if ended? | ||||||
|
||||||
|
@@ -166,4 +166,38 @@ def max_extra_time | |||||
(duration*3600).to_i | ||||||
end | ||||||
|
||||||
def to_xml(opts={}) | ||||||
opts[:exclude] ||= [:startcode] | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
super(opts) do |xml| | ||||||
if opts[:user] then | ||||||
policy = Pundit.policy(opts[:user], self) | ||||||
|
||||||
has_contestants = policy.contestants? | ||||||
has_scoreboard = policy.scoreboard? | ||||||
|
||||||
if policy.contestants? then | ||||||
XmlUtil.serialize_id_list xml, 'contestants', contestants | ||||||
end | ||||||
|
||||||
if policy.scoreboard? then | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Problem IDs should be hidden to users unless the contest has ended or they are a current/past contestant (we may want to reuse problems). We should probably also restrict access to |
||||||
# TODO: Include scoreboard info | ||||||
|
||||||
XmlUtil.serialize_list xml, 'problems', problem_set.problems do |problem| | ||||||
association = problem_associations.find {|i| i.problem_id == problem.id} | ||||||
xml.id( | ||||||
problem.id, | ||||||
'type' => ActiveSupport::XmlMini::TYPE_NAMES[association.weighting.class.name], | ||||||
'weighting' => association.weighting, | ||||||
) | ||||||
end | ||||||
end | ||||||
|
||||||
if policy.manage? then | ||||||
XmlUtil.serialize_id_list xml, 'groups', groups | ||||||
XmlUtil.serialize_id_list xml, 'registrants', registrants | ||||||
end | ||||||
end | ||||||
end | ||||||
end | ||||||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,7 +72,7 @@ def get_highest_scoring_submission(user, from = DateTime.new(1), to = DateTime.n | |
def get_score(user, from = DateTime.new(1), to = DateTime.now) | ||
subs = self.submissions.find(:all, :limit => 1, :order => "score DESC", :conditions => ["created_at between ? and ? and user_id = ?", from, to, user]) | ||
scores = subs.map {|s| s.score} | ||
return scores.max | ||
return scores.max | ||
end | ||
|
||
def submission_history(user, from = DateTime.new(1), to = DateTime.now) | ||
|
@@ -108,4 +108,24 @@ def weighted_score | |
return nil if self.points.nil? | ||
self.points * self.weighting / self.maximum_points | ||
end | ||
|
||
def to_xml(opts={}) | ||
# hide e.g. test submission stats | ||
opts[:only] ||= [:id, :name, :statement, :input, :output, :memory_limit, :time_limit, :owner_id, :created_at, :updated_at] | ||
|
||
super(opts) do |xml| | ||
XmlUtil.serialize_id_list xml, 'contests', contests | ||
XmlUtil.serialize_id_list xml, 'groups', groups | ||
XmlUtil.serialize_id_list xml, 'problem-sets', problem_sets | ||
Comment on lines
+117
to
+119
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we restrict these to admins? This will list upcoming/current contests and groups/problem sets that the user doesn't have access to. |
||
|
||
XmlUtil.serialize_list xml, 'sample-cases', sample_cases do |sample| | ||
xml.tag! 'sample-case' do | ||
XmlUtil.tag xml, 'input', sample.input | ||
XmlUtil.tag xml, 'output', sample.output | ||
end | ||
end | ||
|
||
# TODO: Possibly nice to include submission ids here if user is an admin? | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,4 +36,14 @@ def for_group_user? u_id | |
def total_weighting | ||
problem_associations.sum(:weighting) | ||
end | ||
|
||
def to_xml(opts={}) | ||
opts[:only] ||= [:id, :name, :owner_id, :created_at, :updated_at] | ||
|
||
super(opts) do |xml| | ||
XmlUtil.serialize_id_list xml, 'contests', contests | ||
XmlUtil.serialize_id_list xml, 'groups', groups | ||
Comment on lines
+44
to
+45
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as with |
||
XmlUtil.serialize_id_list xml, 'problems', problems | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
class Request < ActiveRecord::Base | ||
include ActiveModel::ForbiddenAttributesProtection | ||
|
||
belongs_to :requester, :class_name => :User # entity that initiated request | ||
belongs_to :subject, :polymorphic => true # subject controlled by requester | ||
# verb # action applying subject to target | ||
|
@@ -42,4 +42,18 @@ def cancel! | |
self.status = STATUS[:cancelled] | ||
self.save | ||
end | ||
|
||
def to_xml(opts={}) | ||
if self.expired_at == Float::INFINITY then | ||
# `expired_at` has the value Float::INFINITY when the request hasn't expired, | ||
# and the XML formatter explodes when it encounters that value. Adding :expired_at | ||
# to opts[:exclude] is the obvious solution, but for reasons unknown to me it does | ||
# not appear to work as expected. Changing `expired_at` to some other value (which isn't | ||
# a datetime) will result in an empty tag being emitted with a `nil="true"` attribute | ||
# which seems like the best solution after just omitting the tag entirely.` | ||
Comment on lines
+48
to
+53
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
self.expired_at = 0 | ||
end | ||
|
||
super(opts) | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module XmlUtil | ||
def self.serialize_list builder, name, docs | ||
array_type = ActiveSupport::XmlMini::TYPE_NAMES['Array'] | ||
builder.tag!(name, 'count' => docs.count, 'type' => array_type) do | ||
docs.each do |doc| yield doc end | ||
end | ||
end | ||
|
||
def self.serialize_id_list builder, name, docs | ||
array_type = ActiveSupport::XmlMini::TYPE_NAMES['Array'] | ||
|
||
XmlUtil.serialize_list builder, name, docs do |doc| | ||
XmlUtil.tag builder, 'id', doc.id | ||
end | ||
end | ||
|
||
# Wrapper around ActiveSupport's to_tag method as the original is pretty long | ||
def self.tag builder, name, value | ||
ActiveSupport::XmlMini.to_tag(name, value, {:builder => builder}) | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't seem to be used?