Merge branch 'nilovna' into dev
[auf_savoirs_en_partage_django.git] / auf_savoirs_en_partage / rappels / actions.py
CommitLineData
3015f97a
PP
1# -*- coding: utf-8 -*-
2
3import datetime
4
5from django import template
6from django.contrib.admin import helpers
7from django.shortcuts import render_to_response
8from django.utils.translation import ugettext as _
da30702c 9from django.forms.widgets import Select
3015f97a 10
da30702c 11from models import Rappel, RappelUser, RappelModele
3015f97a
PP
12
13
14def rappel(modeladmin, request, queryset):
15
16 opts = modeladmin.model._meta
17 app_label = opts.app_label
18
19 if request.POST.get('post'):
20
21 today = datetime.date.today()
22 lastyear = today - datetime.timedelta(days=365)
23
da30702c
PP
24 modele_id = request.POST.get('modele')
25 rappelmodele = RappelModele.objects.get(pk=modele_id)
26
3015f97a
PP
27 rappel = Rappel()
28 rappel.user_creation = request.user
29 rappel.date_cible = lastyear
30 rappel.date_limite = today + datetime.timedelta(days=30)
da30702c
PP
31 rappel.sujet = rappelmodele.sujet
32 rappel.contenu = rappelmodele.contenu
3015f97a
PP
33 rappel.save()
34
35 for chercheur in queryset:
36 rappeluser = RappelUser()
37 rappeluser.rappel = rappel
38 rappeluser.user = chercheur.user
39 rappeluser.save()
40
41 n = queryset.count()
42
a2e34fdd
PP
43 if n == 1:
44 message = u"1 rappel a été envoyé."
45 else:
46 message = u"%(count)d rappels ont été envoyés." % {"count": n}
47
48 modeladmin.message_user(request, message)
3015f97a
PP
49
50 return None
51
da30702c
PP
52 select = Select(choices=RappelModele.objects.values_list('id', 'nom'))
53
3015f97a
PP
54 context = {
55 "title": _("Are you sure?"),
56 "queryset": queryset,
da30702c 57 "templateselect": select.render("modele", ''),
3015f97a
PP
58 "app_label": app_label,
59 "opts": opts,
60 "action_checkbox_name": helpers.ACTION_CHECKBOX_NAME,
61 }
62
63 return render_to_response("admin/rappels/chercheurrappel/rappel_selected_confirmation.html",
64 context, context_instance=template.RequestContext(request))
65
66rappel.short_description = 'Envoyer rappel: Vérification fiche chercheur'