Envoi de mail pour chaque création d'événement

This commit is contained in:
echarp 2014-05-25 23:59:03 +02:00
parent 0bbfe114be
commit b13af2ea6a
19 changed files with 208 additions and 21 deletions

View File

@ -94,11 +94,20 @@ main
hr
margin: 10px 20px
input, textarea, select
input, textarea, select, a.button
margin: 3px 0
border: 1px solid #868686
&:focus
background-color: #F0F8FF
&[type=submit], &.button
color: black
padding: 0.2em 0.6em
font-size: larger
line-height: 1.1em
font-weight: normal
vertical-align: baseline
background-color: buttonface
/*
input, textarea, select
color: black

View File

@ -1,4 +1,4 @@
.moderations, .notes
.events.edit, .events.update, .events.cancel, .moderations, .notes
table.list
width: 100%
margin-top: 15px

View File

@ -1,5 +1,7 @@
class EventsController < InheritedResources::Base
before_action :set_event, only: [:show, :edit, :update, :destroy]
before_action :set_event, only: [:show, :edit, :update, :cancel, :destroy]
before_action :check_secret, only: [:edit, :update, :cancel, :destroy]
before_filter :set_mailer_host
def index
@events = Event
@ -43,6 +45,9 @@ class EventsController < InheritedResources::Base
respond_to do |format|
if @event.save
# Send an event creation mail to its author
EventMailer.create(@event).deliver
format.html { redirect_to @event, notice: 'Event was successfully created.' }
format.json { render action: 'show', status: :created, location: @event }
else
@ -55,15 +60,16 @@ class EventsController < InheritedResources::Base
# PATCH/PUT /events/1
# PATCH/PUT /events/1.json
def update
# This is a special case, required to handle the region attribute with same foreign key name
@event.region = Region.find(params[:event][:region])
if params[:visu]
@event.attributes = params[:event]
@event.attributes = event_params
render action: :edit
return
end
respond_to do |format|
# This is a special case, required to handle the region attribute with same foreign key name
@event.region = Region.find(params[:event][:region])
if @event.update(event_params)
format.html { redirect_to @event, notice: 'Event was successfully updated.' }
format.json { head :no_content }
@ -81,7 +87,11 @@ class EventsController < InheritedResources::Base
# Use callbacks to share common setup or constraints between actions.
def set_event
@event = Event.find(params[:id])
if (params[:secret].present?)
@event = Event.unscoped.where(secret: params[:secret]).find(params[:id])
else
@event = Event.find(params[:id])
end
end
# Never trust parameters from the scary internet, only allow the white list through.
@ -89,4 +99,15 @@ class EventsController < InheritedResources::Base
params.require(:event)
.permit :title, :start_time, :end_time, :description, :city, :locality, :url, :contact, :submitter, :tags
end
def check_secret
if (params[:secret] != @event.secret)
redirect_to @event, notice: t(:forbidden, scope: [:events, :edit])
end
end
# Useful to manage absolute url in mails
def set_mailer_host
ActionMailer::Base.default_url_options[:host] = request.host_with_port
end
end

View File

@ -9,6 +9,9 @@ class ModerationsController < InheritedResources::Base
# PATCH/PUT /moderations/1
# PATCH/PUT /moderations/1.json
def update
# This is a special case, required to handle the region attribute with same foreign key name
@event.region = Region.find(params[:event][:region])
if params[:visu]
@event.attributes = event_params
render action: 'edit'

View File

@ -0,0 +1,13 @@
class EventMailer < ActionMailer::Base
default from: 'moderateurs@agendadulibre.org'
# Subject can be set in your I18n file at config/locales/en.yml
# with the following lookup:
#
# en.event_mailer.create.subject
#
def create(event)
@event = event
mail to: "to@example.org", subject: t('event_mailer.create.subject', subject: event.title)
end
end

View File

@ -30,6 +30,12 @@ class Event < ActiveRecord::Base
self.decision_time = Date.today
end
before_create do
self.secret = SecureRandom.urlsafe_base64(32)[0...32]
self.moderator_mail_id = SecureRandom.urlsafe_base64(32)[0...32]
self.submitter_mail_id = SecureRandom.urlsafe_base64(32)[0...32]
end
def same_day?
start_time.to_date == end_time.to_date
end

View File

@ -0,0 +1,21 @@
=t '.title'
\
=t '.body', subject: @event.title, start_time: l(@event.start_time, format: :at)
\
= edit_event_url @event, secret: @event.secret
\
=t '.delete_link'
\
= cancel_event_url @event, secret: @event.secret
\
=t '.signature'

View File

@ -13,6 +13,9 @@
#advises= raw markdown.render t '.advises'
- else
= hidden_field_tag :secret, params[:secret]
.field
.helper= raw markdown.render t '.title_helper'
= f.label :title

View File

@ -0,0 +1,21 @@
%h3
>>>
=t '.title'
.box
- if @event.moderated
%h3=t '.already_moderated'
= form_for @event, method: :delete do |f|
= hidden_field_tag :secret, params[:secret]
.confirm=t '.confirm'
%br/
= f.submit t('.ok'), name: :yes
= link_to t('.ko'), @event, class: :button
%h3
>>>
=t '.preview'
.box= render file: '/events/show'

View File

@ -1,5 +1,15 @@
%h2=t '.title'
%h3
>>>
=t '.preview'
= render 'form'
.box
%h3=t '.warning'
= link_to 'Show', @event
.box= render file: '/events/show'
%h3
= link_to t('.moderation'), moderations_path
>>>
=t '.edit'
.box= render '/events/form'

View File

@ -1,6 +1,4 @@
- if request.format == 'text/html' && controller.controller_name != 'moderations' && controller.controller_name != 'notes'
%p#notice= notice
#lug-list
%h1=t '.lug-list'
- if @event.region

View File

@ -1,5 +1,3 @@
%p#notice= notice
%p
%b Login:
= @user.login

View File

@ -0,0 +1,9 @@
# config/initializers/override_mail_recipient.rb
if Rails.env.development?
class OverrideMailRecipient
def self.delivering_email(mail)
mail.to = 'manu@localhost'
end
end
ActionMailer::Base.register_interceptor(OverrideMailRecipient)
end

View File

@ -116,9 +116,20 @@ fr:
title: Soumettre un évènement
edit:
title: Éditer un évènement
preview: Prévisualisation de l'évènement
warning: Attention, cet évènement est déjà modéré. Toute modification sera immédiatement visible sur le site.
forbidden: Vous n'êtes pas authorisé à modifier cet événement
edit: Édition de l'évènement
form:
save: Valider
visualise: Visualiser
cancel:
title: Annulation de l'événement
already_moderated: Attention, cet évènement est déjà modéré. Cette annulation le fera disparaître de l'Agenda du Libre.
confirm: Confirmez-vous l'annulation de cet évènement?
preview: Visualisation de l'évènement
ok: Oui
ko: Non
regions:
index:
title: Liste des flux RSS
@ -180,3 +191,17 @@ fr:
new:
title: Identification
sign_in: Identifier
event_mailer:
create:
title: Nouvel événement
event_mailer:
create:
subject: "[Agenda du Libre] Votre évènement: '%{subject}' est en attente de modération"
title: Bonjour,
body: "Votre évènement intitulé '%{subject}',
\nqui aura lieu le '%{start_time}' a bien été enregistré dans l'Agenda du Libre.
\n\nL'équipe de modération le prendra en charge très prochainement.
\n\nPendant la modération et après celle-ci si votre évènement est validé, vous pouvez éditer votre évènement à l'adresse:"
delete_link: "et vous pouvez l'annuler en utilisant l'adresse:"
signature: "Merci de votre participation!\n\n-- \nAgenda du Libre"

View File

@ -7,7 +7,9 @@ Rails.application.routes.draw do
get 'application/contact'
resources :users
resources :events
resources :events do
get :cancel, on: :member
end
resources :moderations do
resources :notes, only: [:new, :create]
end

View File

@ -64,12 +64,17 @@ class EventsControllerTest < ActionController::TestCase
end
test "should get edit" do
get :edit, id: @event
get :edit, id: @event, secret: 'MyString'
assert_response :success
end
test "should not get edit" do
get :edit, id: @event
assert_redirected_to event_path(assigns(:event))
end
test "should update event" do
patch :update, id: @event, event: {
patch :update, id: @event, secret: 'MyString', event: {
title: @event.title,
start_time: @event.start_time,
end_time: @event.end_time,
@ -89,9 +94,14 @@ class EventsControllerTest < ActionController::TestCase
assert_redirected_to event_path(assigns(:event))
end
test "should get cancel page" do
get :cancel, id: @event, secret: 'MyString'
assert_response :success
end
test "should destroy event" do
assert_difference('Event.count', -1) do
delete :destroy, id: @event
delete :destroy, id: @event, secret: 'MyString'
end
assert_redirected_to events_path

View File

@ -0,0 +1,16 @@
require 'test_helper'
class EventMailerTest < ActionMailer::TestCase
setup do
ActionMailer::Base.default_url_options[:host] = 'localhost:3000'
end
test "create" do
mail = EventMailer.create(Event.last)
assert_match(/\[Agenda du Libre\] Votre évènement: .* est en attente de modération/, mail.subject)
assert_equal ["to@example.org"], mail.to
assert_equal ["moderateurs@agendadulibre.org"], mail.from
assert_match(/Bonjour.*/, mail.body.encoded)
end
end

View File

@ -0,0 +1,10 @@
# Preview all emails at http://localhost:3000/rails/mailers/event_mailer
class EventMailerPreview < ActionMailer::Preview
# Preview this email at http://localhost:3000/rails/mailers/event_mailer/create
def create
ActionMailer::Base.default_url_options[:host] = 'localhost:3000'
EventMailer.create(Event.last)
end
end

View File

@ -1,7 +1,19 @@
require 'test_helper'
class EventTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
test "basic event" do
@event = Event.new(
title: 'hello world',
start_time: Time.new(),
end_time: Time.new() + 1,
description: 'et hop!',
region: Region.first()
)
assert @event.save()
assert_equal 32, @event.secret.size()
assert_equal 32, @event.moderator_mail_id.size()
assert_equal 32, @event.submitter_mail_id.size()
end
end