mirror of
https://github.com/discourse/discourse.git
synced 2025-03-22 19:55:31 +08:00
FEATURE: rake task to bulk grant badge to members of a group
This commit is contained in:
parent
f8eddd40ad
commit
6ba914033c
29
lib/tasks/groups.rake
Normal file
29
lib/tasks/groups.rake
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
# bulk grant badge to members of a specific group
|
||||||
|
task "groups:grant_badge", [:group_id, :badge_id] => [:environment] do |_, args|
|
||||||
|
group_id = args[:group_id]
|
||||||
|
badge_id = args[:badge_id]
|
||||||
|
|
||||||
|
if !group_id || !badge_id
|
||||||
|
puts "ERROR: Expecting groups:grant_badge[group_id,badge_id]"
|
||||||
|
exit 1
|
||||||
|
end
|
||||||
|
|
||||||
|
group = Group.find(group_id)
|
||||||
|
badge = Badge.find(badge_id)
|
||||||
|
|
||||||
|
if group.present? && badge.present?
|
||||||
|
puts "Granting badge '#{badge.name}' to all users in group '#{group.name}'..."
|
||||||
|
|
||||||
|
count = 0
|
||||||
|
group.users.each do |user|
|
||||||
|
begin
|
||||||
|
BadgeGranter.grant(badge, user)
|
||||||
|
rescue => e
|
||||||
|
puts "", "Failed to grant badge to #{user.username}", e, e.backtrace.join("\n")
|
||||||
|
end
|
||||||
|
putc "." if (count += 1) % 5 == 0
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
puts "", "Done! Badge granted to #{count} members.", ""
|
||||||
|
end
|
Loading…
x
Reference in New Issue
Block a user