Merge branch 'nilovna' into dev
[auf_savoirs_en_partage_django.git] / auf_savoirs_en_partage / rappels / actions.py
1 # -*- coding: utf-8 -*-
2
3 import datetime
4
5 from django import template
6 from django.contrib.admin import helpers
7 from django.shortcuts import render_to_response
8 from django.utils.translation import ugettext as _
9 from django.forms.widgets import Select
10
11 from models import Rappel, RappelUser, RappelModele
12
13
14 def 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
24 modele_id = request.POST.get('modele')
25 rappelmodele = RappelModele.objects.get(pk=modele_id)
26
27 rappel = Rappel()
28 rappel.user_creation = request.user
29 rappel.date_cible = lastyear
30 rappel.date_limite = today + datetime.timedelta(days=30)
31 rappel.sujet = rappelmodele.sujet
32 rappel.contenu = rappelmodele.contenu
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
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)
49
50 return None
51
52 select = Select(choices=RappelModele.objects.values_list('id', 'nom'))
53
54 context = {
55 "title": _("Are you sure?"),
56 "queryset": queryset,
57 "templateselect": select.render("modele", ''),
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
66 rappel.short_description = 'Envoyer rappel: Vérification fiche chercheur'