new file: news/__init__.py
[univ_ap.git] / news / views.py
1 #-*- coding: utf-8 -*-
2 # Create your views here.
3 from datetime import datetime
4 from django.shortcuts import render
5 from news.models import News,CategorieDossier
6 from basemodel.models import Pays
7 from django.http import Http404
8 from django.shortcuts import redirect
9 from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
10 from news.forms import SearchActualiteForm
11 from veille_academique.views import last_news,last_agenda, systeme_academique,tags
12
13 from math import *
14 import datetime
15 from datetime import timedelta
16
17 from rest_framework import generics
18 from rest_framework import filters
19 from news.newsserializer import NewsSerializer,CateDosSerializer
20 from filters import NewsFilter
21
22 class api_news_list(generics.ListCreateAPIView):
23 serializer_class = NewsSerializer
24 queryset = News.objects.filter(statut="publié")
25 filter_fields = ('categorie','titre','id','categorie_dossier','date_fin')
26 filter_backends = (filters.OrderingFilter, filters.DjangoFilterBackend,)
27 filter_class = NewsFilter
28
29 class api_cate_dossier_list(generics.ListCreateAPIView):
30 serializer_class = CateDosSerializer
31 queryset = CategorieDossier.objects.all()
32 # filter_fields = ('titre')
33 # filter_backends = (filters.OrderingFilter, filters.DjangoFilterBackend,)
34
35
36
37 def asie_pacifique():
38 return Pays.objects.filter(asie_pacifique = 1)
39
40 def last_news():
41 return News.objects.filter(alaune = 1).order_by('-date_modification')
42
43 def calendar(request):
44 calendars = News.objects.filter(categorie=5,statut='Publié').order_by('-date_creation')
45 for calendar in calendars:
46 calendar.titre = calendar.titre.replace('"','')
47 calendar.extrait_contenu = calendar.extrait_contenu.replace('"','')
48 calendar.extrait_contenu = calendar.extrait_contenu.replace(chr(13),'')
49 calendar.extrait_contenu = calendar.extrait_contenu.replace(chr(10),'')
50 calendar.total_days = (calendar.date_fin - calendar.date_debut).days +1
51 calendar.include = []
52 if calendar.total_days > 1:
53 for day in range(calendar.total_days):
54 # current_date = (calendar.date_debut + (24*3600*day)).date()
55 current_date = calendar.date_debut + timedelta(days=day)
56 calendar.include.append(current_date)
57 return render(request,'news/calendars.html', locals())
58
59 def view_news(request,slug):
60 # contenu_accueil = Contenu.objects.filter(typeContenu__designation="text",menu__titre="Accueil")
61 #presentation = Contenu.objects.get(id=1)
62 pays_asie_pacifique = asie_pacifique()
63 lastnews = last_news()
64 systemes = systeme_academique()
65 list_tags = tags()
66 try:
67 thenew = News.objects.get(slug = slug)
68 except News.DoesNotExist:
69 return redirect('news.views.news')
70 return render(request,'news/view_news.html', locals())
71
72
73
74 def news(request):
75 list_news = News.objects.all().order_by('-date_modification')
76
77 pays_asie_pacifique = Pays.objects.filter(asie_pacifique = 1)
78 if request.method == 'POST': # S'il s'agit d'une requête POST
79 form = SearchActualiteForm(request.POST) # Nous reprenons les données
80 if form.is_valid():
81 pays = form.cleaned_data['pays']
82 titre = form.cleaned_data['titre']
83 date_debut = form.cleaned_data['date_debut']
84 date_fin = form.cleaned_data['date_fin']
85 categorie = form.cleaned_data['categorie']
86 #contenu = form.cleaned_data['contenu']
87
88 # Nous vérifions que les données envoyées sont valides
89 #list_formation = Formation.objects.filter(pays = pays)
90 lesnews = News.objects.all().order_by('-date_modification')
91 pays = request.POST.get('pays', None)
92 titre = request.POST.get('titre', None)
93 categorie = request.POST.get('categorie', None)
94 date_debut = request.POST.get('date_debut', None)
95 date_fin = request.POST.get('date_fin', None)
96 #contenu = request.POST.get('contenu', None)
97 if pays:
98 lesnews = lesnews.filter(pays = pays)
99 if titre:
100 lesnews = lesnews.filter(contenu__icontains = titre) | lesnews.filter(titre__icontains= titre)
101 if categorie:
102 lesnews = lesnews.filter(categorie = categorie)
103
104 if date_debut:
105 debut = datetime.strptime(date_debut, '%d/%m/%Y')
106 lesnews = lesnews.filter(date_modification__gte = debut)
107 if date_fin:
108 fin = datetime.strptime(date_fin, '%d/%m/%Y')
109 lesnews = lesnews.filter(date_modification__lte = fin)
110
111 list_news = lesnews
112 else: # Si ce n'est pas du POST, c'est probablement une requête GET
113 form = SearchActualiteForm() # Nous créons un formulaire video
114 lastnews = last_news()
115 nbre_news_par_pages = 22
116 page_news = True
117
118 paginator = Paginator(list_news, nbre_news_par_pages) # Show 25 contacts per page
119 nombre_pages = paginator.num_pages
120 debut_page_fin = nombre_pages - 4
121 lien_page = paginator.page_range
122 lien_page_reverse = paginator.page_range[nombre_pages-3:nombre_pages+3]
123 n = nbre_news_par_pages / 2
124 page = request.GET.get('page')
125
126 try:
127 page = int(page)
128 lien_page_encours = paginator.page_range[page-3:page+3]
129 except :
130 news = paginator.page(1)
131 pass
132
133
134 try:
135 news = paginator.page(page)
136 nbre_news = news.object_list.count()
137 except PageNotAnInteger:
138 # If page is not an integer, deliver first page.
139 news = paginator.page(1)
140 nbre_news = news.object_list.count()
141
142 except EmptyPage:
143 # If page is out of range (e.g. 9999), deliver last page of results.
144 news = paginator.page(paginator.num_pages)
145 nbre_news = news.object_list.count()
146 if nbre_news < nbre_news_par_pages :
147 if((nbre_news % 2 )== 0):
148 n = int(nbre_news/2)
149 else:
150 n = int(nbre_news/2) +1
151
152
153 lastnews1 = news[0:n]
154 lastnews2 = news[n:n*2]
155 return render(request,'news/news.html', locals())