new file: news/__init__.py
[univ_ap.git] / news / 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 news.models import News, Categorie, Tags, Tagsnews,CategorieDossier
6 from basemodel.models import Pays, User_right
7 import smtplib
8 from email.mime.text import MIMEText
9
10 class NewsForm(forms.ModelForm):
11 contenu = forms.CharField(widget=CKEditorWidget)
12 pays = forms.ModelChoiceField(queryset = Pays.objects.filter(asie_pacifique=1),required=False)
13
14 class Meta:
15 widgets = {'name': CKEditorWidget(editor_options={'startupFocus': True})}
16 model = News
17
18
19 def send_email(auteur, pk):
20 msg = MIMEText("Bonjour,\n%s vous a demande de valider une nouvelle actualite numero %s.\nCordialement,\n" % (auteur,pk) )
21
22 # me == the sender's email address
23 # you == the recipient's email address
24 msg['Subject'] = 'Site de veille: Actualité numéro %s à valider' % pk
25 msg['From'] = 'no-reply@auf.org'
26 #msg['To'] = 'truong.tung.lam@vn.auf.org'
27
28 recipients = ['nguyen.thuy.huyen@auf.org','marion.alcaraz@auf.org','truong.tung.lam@auf.org']
29 # Send the message via our own SMTP server, but don't include the
30 # envelope header.
31 s = smtplib.SMTP('localhost')
32 s.sendmail('truong.tung.lam@auf.org',recipients, msg.as_string())
33 s.quit()
34
35 class NewsAdmin(admin.ModelAdmin):
36 def queryset(self, request):
37 news = super(NewsAdmin, self).queryset(request)
38 if request.user.is_superuser:
39 return news
40
41 try:
42 user_right = User_right.objects.get(user=request.user,application="Veille")
43 if (user_right.user_right == "Admin"):
44 return news
45 else:
46 return news.filter(user=request.user)
47 except:
48 return news.filter(user=request.user)
49
50 return news.filter(user=request.user)
51
52 def save_model(self, request, obj, form, change):
53 if (change):
54 if request.user.is_superuser:
55 obj.save()
56 else:
57 try:
58 user_right = User_right.objects.get(user=request.user,application="Veille")
59 if (user_right.user_right == "Admin"):
60 obj.save()
61 else:
62 if (obj.statut == u"Publié"):
63 obj.statut = "A valider"
64 obj.save()
65 except:
66 if (obj.statut == u"Publié"):
67 obj.statut = "A valider"
68 obj.save()
69
70 else:
71 obj.user = request.user
72
73 if request.user.is_superuser:
74 obj.save()
75 else:
76 try:
77 user_right = User_right.objects.get(user=request.user,application="Veille")
78 if (user_right.user_right == "Admin"):
79 obj.save()
80 else:
81 if (obj.statut == u"Publié"):
82 obj.statut = "A valider"
83 obj.save()
84 except:
85 if (obj.statut == u"Publié"):
86 obj.statut = "A valider"
87 obj.save()
88
89 if (obj.statut == "A valider"):
90 send_email(request.user, obj.pk)
91
92 list_display = ('titre','categorie','pays','date_debut','date_fin','extrait_contenu','images','type_news','date_creation','date_modification','statut')
93 list_filter = ('user','categorie','statut')
94 search_fields = ('titre','extrait_contenu')
95 form = NewsForm
96 fieldsets = [('Ajouter une actualité', {'classes': ('full-width'), 'fields': ('categorie','statut','titre','slug','type_news','categorie_dossier','tags','source','images','date_debut','date_fin','extrait_contenu','contenu','lien_vers_site','titre_fichier1','fichier1','titre_fichier2','fichier2','titre_fichier3','fichier3','titre_fichier4','fichier4','titre_fichier5','fichier5','titre_fichier6','fichier6','titre_fichier7','fichier7','titre_fichier8','fichier8','titre_fichier9','fichier9','titre_fichier10','fichier10')})]
97 prepopulated_fields = {'slug': ['titre'], }
98 filter_horizontal = ('tags',)
99
100
101 class TagsAdmin(admin.ModelAdmin):
102 list_display = ('tag', 'frequence', )
103
104
105
106 admin.site.register(News, NewsAdmin)
107 admin.site.register(Categorie)
108 admin.site.register(Tags,TagsAdmin)
109 admin.site.register(Tagsnews)
110 admin.site.register(CategorieDossier)