new file: projetCuf/__init__.py
[univ_ap.git] / cartographie / admin.py
1 # -*- coding:utf-8 -*-
2 from django.contrib import admin
3 from django import forms
4 from suit_ckeditor.widgets import CKEditorWidget
5 from cartographie.models import Cartonews, CartoFormation
6 from basemodel.models import User_right
7 import smtplib
8 from email.mime.text import MIMEText
9
10 class CartonewsForm(forms.ModelForm):
11 contenu = forms.CharField(widget=CKEditorWidget)
12 lien_video = forms.URLField(required=False)
13 class Meta:
14 widgets = {'name': CKEditorWidget(editor_options={'startupFocus': True})}
15 model = Cartonews
16
17
18 def send_email(auteur, pk):
19 msg = MIMEText("Bonjour,\n%s vous a demande de valider une nouvelle actualite numero %s.\nCordialement,\n" % (auteur,pk) )
20
21 # me == the sender's email address
22 # you == the recipient's email address
23 msg['Subject'] = 'Site cartographie: Actualité numéro %s a valider' % pk
24 msg['From'] = 'no-reply@auf.org'
25 #msg['To'] = 'truong.tung.lam@vn.auf.org'
26
27 recipients = ['nguyen.thuy.huyen@auf.org','marion.alcaraz@auf.org','truong.tung.lam@auf.org']
28 # Send the message via our own SMTP server, but don't include the
29 # envelope header.
30 s = smtplib.SMTP('localhost')
31 s.sendmail('truong.tung.lam@auf.org',recipients, msg.as_string())
32 s.quit()
33
34 class CartonewsAdmin(admin.ModelAdmin):
35 def queryset(self, request):
36 Cartonews = super(CartonewsAdmin, self).queryset(request)
37 if request.user.is_superuser:
38 return Cartonews
39
40 try:
41 user_right = User_right.objects.get(user=request.user,application="Carto")
42 if (user_right.user_right == "Admin"):
43 return Cartonews
44 else:
45 return Cartonews.filter(user=request.user)
46 except:
47 return Cartonews.filter(user=request.user)
48
49 return Cartonews.filter(user=request.user)
50
51 def save_model(self, request, obj, form, change):
52
53
54
55 if (change):
56 if request.user.is_superuser:
57 obj.save()
58 else:
59 try:
60 user_right = User_right.objects.get(user=request.user,application="Carto")
61 if (user_right.user_right == "Admin"):
62 obj.save()
63 else:
64 if (obj.statut == u"Publié"):
65 obj.statut = "A valider"
66
67 obj.save()
68 except:
69 if (obj.statut == u"Publié"):
70 obj.statut = "A valider"
71 obj.save()
72
73 else:
74 obj.user = request.user
75
76 if request.user.is_superuser:
77 obj.save()
78 else:
79 try:
80 user_right = User_right.objects.get(user=request.user,application="Carto")
81 if (user_right.user_right == "Admin"):
82 obj.save()
83 else:
84 if (obj.statut == u"Publié"):
85 obj.statut = "A valider"
86
87 obj.save()
88 except:
89 if (obj.statut == u"Publié"):
90 obj.statut = "A valider"
91 obj.save()
92
93 if (obj.statut == "A valider"):
94 send_email(request.user, obj.pk)
95
96 list_display = ('titre','extrait_contenu','statut','focus','admin_img1','admin_img2','admin_img3','date_modification')
97 list_filter = ('user','statut')
98 form = CartonewsForm
99 fieldsets = [('Ajouter une actualité', {'classes': ('full-width'), 'fields': ('focus','statut','titre','slug','images','extrait_contenu','contenu','pdf1','pdf2','img1','img2','img3')})]
100 prepopulated_fields = {'slug': ['titre'] , }
101
102
103 admin.site.register(Cartonews,CartonewsAdmin)
104 admin.site.register(CartoFormation)
105