Class: Admin::BadgesController
- Inherits:
-
AdminController
- Object
- AdminController
- Admin::BadgesController
- Defined in:
- app/controllers/admin/badges_controller.rb
Constant Summary collapse
- MAX_CSV_LINES =
50_000
- BATCH_SIZE =
200
Instance Method Summary collapse
- #award ⇒ Object
- #badge_types ⇒ Object
- #create ⇒ Object
- #destroy ⇒ Object
- #index ⇒ Object
- #mass_award ⇒ Object
- #new ⇒ Object
- #preview ⇒ Object
- #save_badge_groupings ⇒ Object
- #show ⇒ Object
- #update ⇒ Object
Instance Method Details
#award ⇒ Object
44 45 |
# File 'app/controllers/admin/badges_controller.rb', line 44 def award end |
#badge_types ⇒ Object
121 122 123 124 |
# File 'app/controllers/admin/badges_controller.rb', line 121 def badge_types badge_types = BadgeType.all.to_a render_serialized(badge_types, BadgeTypeSerializer, root: "badge_types") end |
#create ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 |
# File 'app/controllers/admin/badges_controller.rb', line 144 def create badge = Badge.new errors = update_badge_from_params(badge, new: true) if errors.present? render_json_error errors else StaffActionLogger.new(current_user).log_badge_creation(badge) render_serialized(badge, AdminBadgeSerializer, root: "badge") end end |
#destroy ⇒ Object
168 169 170 171 172 173 174 175 176 |
# File 'app/controllers/admin/badges_controller.rb', line 168 def destroy Badge.transaction do badge = find_badge StaffActionLogger.new(current_user).log_badge_deletion(badge) badge.clear_user_titles! badge.destroy! end render body: nil end |
#index ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'app/controllers/admin/badges_controller.rb', line 9 def index data = { badge_types: BadgeType.all.order(:id).to_a, badge_groupings: BadgeGrouping.all.order(:position).to_a, badges: Badge .includes(:badge_grouping) .includes(:badge_type, :image_upload) .references(:badge_grouping) .order("enabled DESC", "badge_groupings.position, badge_type_id, badges.name") .to_a, protected_system_fields: Badge.protected_system_fields, triggers: Badge.trigger_hash, } render_serialized(OpenStruct.new(data), AdminBadgesSerializer) end |
#mass_award ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'app/controllers/admin/badges_controller.rb', line 47 def mass_award csv_file = params.permit(:file).fetch(:file, nil) badge = Badge.find_by(id: params[:badge_id]) raise Discourse::InvalidParameters if csv_file.try(:tempfile).nil? || badge.nil? if !badge.enabled? render_json_error( I18n.t("badges.mass_award.errors.badge_disabled", badge_name: badge.display_name), status: 422, ) return end replace_badge_owners = params[:replace_badge_owners] == "true" ensure_users_have_badge_once = params[:grant_existing_holders] != "true" if !ensure_users_have_badge_once && !badge.multiple_grant? render_json_error( I18n.t( "badges.mass_award.errors.cant_grant_multiple_times", badge_name: badge.display_name, ), status: 422, ) return end line_number = 1 usernames = [] emails = [] File.open(csv_file) do |csv| csv.each_line do |line| line = CSV.parse_line(line).first&.strip line_number += 1 if line.present? if line.include?("@") emails << line else usernames << line end end if emails.size + usernames.size > MAX_CSV_LINES return( render_json_error I18n.t( "badges.mass_award.errors.too_many_csv_entries", count: MAX_CSV_LINES, ), status: 400 ) end end end BadgeGranter.revoke_all(badge) if replace_badge_owners results = BadgeGranter.enqueue_mass_grant_for_users( badge, emails: emails, usernames: usernames, ensure_users_have_badge_once: ensure_users_have_badge_once, ) render json: { unmatched_entries: results[:unmatched_entries].first(100), matched_users_count: results[:matched_users_count], unmatched_entries_count: results[:unmatched_entries_count], }, status: :ok rescue CSV::MalformedCSVError render_json_error I18n.t("badges.mass_award.errors.invalid_csv", line_number: line_number), status: 400 end |
#new ⇒ Object
38 39 |
# File 'app/controllers/admin/badges_controller.rb', line 38 def new end |
#preview ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 |
# File 'app/controllers/admin/badges_controller.rb', line 26 def preview return render json: "preview not allowed", status: 403 unless SiteSetting.enable_badge_sql render json: BadgeGranter.preview( params[:sql], target_posts: params[:target_posts] == "true", explain: params[:explain] == "true", trigger: params[:trigger].to_i, ) end |
#save_badge_groupings ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'app/controllers/admin/badges_controller.rb', line 126 def save_badge_groupings badge_groupings = BadgeGrouping.all.order(:position).to_a ids = params[:ids].map(&:to_i) params[:names].each_with_index do |name, index| id = ids[index].to_i group = badge_groupings.find { |b| b.id == id } || BadgeGrouping.new group.name = name group.position = index group.save end badge_groupings.each { |g| g.destroy unless g.system? || ids.include?(g.id) } badge_groupings = BadgeGrouping.all.order(:position).to_a render_serialized(badge_groupings, BadgeGroupingSerializer, root: "badge_groupings") end |
#show ⇒ Object
41 42 |
# File 'app/controllers/admin/badges_controller.rb', line 41 def show end |
#update ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 |
# File 'app/controllers/admin/badges_controller.rb', line 156 def update badge = find_badge errors = update_badge_from_params(badge) if errors.present? render_json_error errors else StaffActionLogger.new(current_user).log_badge_change(badge) render_serialized(badge, AdminBadgeSerializer, root: "badge") end end |