liquid_feedback_frontend
view model/event.lua @ 482:3bc96debbd05
Add more missing group by clauses for search selectors
| author | bsw | 
|---|---|
| date | Thu Mar 15 13:23:42 2012 +0100 (2012-03-15) | 
| parents | 699b9fa7bc36 | 
| children | d22125d054aa | 
 line source
     1 Event = mondelefant.new_class()
     2 Event.table = 'event'
     4 Event:add_reference{
     5   mode          = 'm1',
     6   to            = "Issue",
     7   this_key      = 'issue_id',
     8   that_key      = 'id',
     9   ref           = 'issue',
    10 }
    12 Event:add_reference{
    13   mode          = 'm1',
    14   to            = "Suggestion",
    15   this_key      = 'suggestion_id',
    16   that_key      = 'id',
    17   ref           = 'suggestion',
    18 }
    20 Event:add_reference{
    21   mode          = 'm1',
    22   to            = "Member",
    23   this_key      = 'member_id',
    24   that_key      = 'id',
    25   ref           = 'member',
    26 }
    28 function Event.object_get:event_name()
    29   return ({
    30     issue_state_changed = _"Issue reached next phase",
    31     initiative_created_in_new_issue = _"New issue",
    32     initiative_created_in_existing_issue = _"New initiative",
    33     initiative_revoked = _"Initiative revoked",
    34     new_draft_created = _"New initiative draft",
    35     suggestion_created = _"New suggestion"
    36   })[self.event]
    37 end
    39 function Event.object_get:state_name()
    40   return ({
    41     admission = _"New",
    42     discussion = _"Discussion",
    43     verification = _"Frozen",
    44     voting = _"Voting",
    45     canceled_revoked_before_accepted = _"Cancelled (before accepted due to revocation)",
    46     canceled_issue_not_accepted = _"Cancelled (issue not accepted)",
    47     canceled_after_revocation_during_discussion = _"Cancelled (during discussion due to revocation)",
    48     canceled_after_revocation_during_verification = _"Cancelled (during verification due to revocation)",
    49     calculation = _"Calculation",
    50     canceled_no_initiative_admitted = _"Cancelled (no initiative admitted)",
    51     finished_without_winner = _"Finished (without winner)",
    52     finished_with_winner = _"Finished (with winner)"
    53   })[self.state]
    54 end
    56 function Event.object:send_notification() 
    58   local members_to_notify = Member:new_selector()
    59     :join("event_seen_by_member", nil, { "event_seen_by_member.seen_by_member_id = member.id AND event_seen_by_member.notify_level <= member.notify_level AND event_seen_by_member.id = ?", self.id } )
    60     :add_where("member.activated NOTNULL AND member.notify_email NOTNULL")
    61     :exec()
    63   print (_("Event #{id} -> #{num} members", { id = self.id, num = #members_to_notify }))
    66   local url
    68   for i, member in ipairs(members_to_notify) do
    69     local subject
    70     local body = ""
    72     locale.do_with(
    73       { lang = member.lang or config.default_lang or 'en' },
    74       function()
    75         subject = config.mail_subject_prefix .. " " .. self.event_name
    76         body = body .. _("[event mail]      Unit: #{name}", { name = self.issue.area.unit.name }) .. "\n"
    77         body = body .. _("[event mail]      Area: #{name}", { name = self.issue.area.name }) .. "\n"
    78         body = body .. _("[event mail]     Issue: ##{id}", { id = self.issue_id }) .. "\n\n"
    79         body = body .. _("[event mail]    Policy: #{policy}", { policy = self.issue.policy.name }) .. "\n\n"
    80         body = body .. _("[event mail]     Event: #{event}", { event = self.event_name }) .. "\n\n"
    81         body = body .. _("[event mail]     Phase: #{phase}", { phase = self.state_name }) .. "\n\n"
    83         if self.initiative_id then
    84           url = request.get_absolute_baseurl() .. "initiative/show/" .. self.initiative_id .. ".html"
    85         elseif self.suggestion_id then
    86           url = request.get_absolute_baseurl() .. "suggestion/show/" .. self.suggestion_id .. ".html"
    87         else
    88           url = request.get_absolute_baseurl() .. "issue/show/" .. self.issue_id .. ".html"
    89         end
    91         body = body .. _("[event mail]       URL: #{url}", { url = url }) .. "\n\n"
    93         if self.initiative_id then
    94           local initiative = Initiative:by_id(self.initiative_id)
    95           body = body .. _("i#{id}: #{name}", { id = initiative.id, name = initiative.name }) .. "\n\n"
    96         else
    97           local initiative_count = Initiative:new_selector()
    98             :add_where{ "initiative.issue_id = ?", self.issue_id }
    99             :count()
   100           local initiatives = Initiative:new_selector()
   101             :add_where{ "initiative.issue_id = ?", self.issue_id }
   102             :add_order_by("initiative.supporter_count DESC")
   103             :limit(3)
   104             :exec()
   105           for i, initiative in ipairs(initiatives) do
   106             body = body .. _("i#{id}: #{name}", { id = initiative.id, name = initiative.name }) .. "\n"
   107           end
   108           if initiative_count - 3 > 0 then
   109             body = body .. _("and #{count} more initiatives", { count = initiative_count }) .. "\n"
   110           end
   111           body = body .. "\n"
   112         end
   114         if self.suggestion_id then
   115           local suggestion = Suggestion:by_id(self.suggestion_id)
   116           body = body .. _("#{name}\n\n", { name = suggestion.name })
   117         end
   119         local success = net.send_mail{
   120           envelope_from = config.mail_envelope_from,
   121           from          = config.mail_from,
   122           reply_to      = config.mail_reply_to,
   123           to            = member.notify_email,
   124           subject       = subject,
   125           content_type  = "text/plain; charset=UTF-8",
   126           content       = body
   127         }
   129       end
   130     )
   131   end
   133 end
   135 function Event:send_next_notification()
   137   local notification_sent = NotificationSent:new_selector()
   138     :optional_object_mode()
   139     :for_update()
   140     :exec()
   142   local last_event_id = 0
   143   if notification_sent then
   144     last_event_id = notification_sent.event_id
   145   end
   147   local event = Event:new_selector()
   148     :add_where{ "event.id > ?", last_event_id }
   149     :add_order_by("event.id")
   150     :limit(1)
   151     :optional_object_mode()
   152     :exec()
   154   if event then
   155     if last_event_id == 0 then
   156       db:query{ "INSERT INTO notification_sent (event_id) VALUES (?)", event.id }
   157     else
   158       db:query{ "UPDATE notification_sent SET event_id = ?", event.id }
   159     end
   161     event:send_notification()
   163     return true
   165   end
   167 end
   169 function Event:send_notifications_loop()
   171   while true do
   172     local did_work = Event:send_next_notification()
   173     if not did_work then
   174       print "Sleeping 1 second"
   175       os.execute("sleep 1")
   176     end
   177   end
   179 end
