[#3466] Désactive 3 sources plus valide
[auf_savoirs_en_partage_django.git] / auf_savoirs_en_partage / sitotheque / models.py
CommitLineData
01a9197e 1# -*- encoding: utf-8 -*-
4b89a7df
EMS
2import textwrap
3
4from django.conf import settings
fdcf5874 5from django.core.urlresolvers import reverse
01a9197e 6from django.db import models
878de341 7from django.db.models import Q
39a63232 8from djangosphinx.models import SphinxSearch
fdcf5874 9
693c606b 10from auf.django.references.models import *
fdcf5874 11from savoirs.models import Discipline, SEPManager, SEPSphinxQuerySet, SEPQuerySet, Search
01a9197e 12
13TYPE_SITE_CHOICES = (
14 ('RV', 'Revue en ligne'),
15 ('BB', 'Bibliothèque en ligne'),
00755d9b 16 ('FD', 'Fonds documentaire'),
01a9197e 17 ('AR', 'Archive ouverte'),
18 ('CO', 'Cours en ligne'),
00755d9b
AJ
19 ('RE', 'Repertoire de ressource'),
20 ('SA', 'Site associatif'),
21 ('SC', 'Site culturel'),
22 ('SI', 'Site d\'information'),
01a9197e 23 ('AU', 'Autre type de site'),
fdcf5874 24)
01a9197e 25
39a63232 26class SiteQuerySet(SEPQuerySet):
878de341 27
39a63232
EMS
28 def filter_pays(self, pays):
29 return self.filter(pays=pays)
878de341 30
230671ff
EMS
31 def filter_date_maj(self, min=None, max=None):
32 return self._filter_date('date_maj', min=min, max=max)
33
39a63232 34class SiteSphinxQuerySet(SEPSphinxQuerySet):
878de341 35
39a63232
EMS
36 def __init__(self, model=None):
37 SEPSphinxQuerySet.__init__(self, model=model, index='savoirsenpartage_sites', weights=dict(titre=3))
c1b134f8 38
230671ff
EMS
39 def filter_date_maj(self, min=None, max=None):
40 return self._filter_date('date_maj', min=min, max=max)
41
39a63232 42 def filter_pays(self, pays):
27effd89
PP
43 return self.filter(pays_id=pays.id)
44
45 def filter_discipline(self, discipline):
46 return self.filter(discipline_ids=discipline.id)
47
48 def filter_region(self, region):
49 return self.filter(region_ids=region.id)
c1b134f8 50
39a63232 51class SiteManager(SEPManager):
878de341 52
39a63232
EMS
53 def get_query_set(self):
54 return SiteQuerySet(self.model).filter(actif=True)
878de341 55
39a63232
EMS
56 def get_sphinx_query_set(self):
57 return SiteSphinxQuerySet(self.model)
bae03b7b 58
39a63232
EMS
59 def filter_pays(self, pays):
60 return self.get_query_set().filter_pays(pays)
bae03b7b 61
230671ff
EMS
62 def filter_date_maj(self, min=None, max=None):
63 return self.get_query_set().filter_date_maj(self, min=min, max=max)
64
01a9197e 65class Site(models.Model):
66 """Fiche d'info d'un site web"""
f810842d 67 url = models.URLField(verify_exists=False) # dc:identifier (dc:source?)
a5f76eb4 68 titre = models.CharField(max_length=255, verbose_name='titre') # dc.title
e7d36875 69 description = models.TextField(null=True, blank=True)
a5f76eb4
EMS
70 editeur = models.CharField(max_length=255, null=True, blank=True, verbose_name='éditeur') # dc.publisher : organisation resp
71 auteur = models.CharField(max_length=255, null=True, blank=True, verbose_name='auteur') # dc.creator : nom, prénom
03e081cb
AJ
72
73 #auf_partenaire = models.BooleanField() # dc.contributor
74
e7d36875
AJ
75 date_publication = models.DateField(null=True, blank=True) # dc.date : date de publication
76 type = models.CharField(max_length=2, null=True, blank=True, choices=TYPE_SITE_CHOICES,
01a9197e 77 verbose_name = 'Type de site') # dc.type
03e081cb
AJ
78 discipline = models.ManyToManyField(Discipline, blank=True)
79 thematique = models.ManyToManyField(Thematique, blank=True)
80
e7d36875 81 mots_cles = models.TextField(verbose_name='Mots-clés', null=True, blank=True) # dc:subject # indexation libre
01a9197e 82
83 # source # dc:source (dc:relation?)
a5f76eb4
EMS
84 pays = models.ForeignKey(Pays, null = True, blank=True, db_column='pays', to_field='code', verbose_name = 'pays')
85 regions = models.ManyToManyField(Region, blank=True, related_name="sites", verbose_name='régions')
01a9197e 86
87 # meta
41d30718 88 actif = models.BooleanField(default=True)
03e081cb 89 date_maj = models.DateField(auto_now=True)
fc2908e4 90 recherche_google = models.BooleanField('Inclure dans la recherche Google', default=False)
3ec7988b 91 site_auf = models.BooleanField('soutenu par l\'AUF', default=False)
878de341
EMS
92
93 # Manager
94 objects = SiteManager()
c59dba82 95 all_objects = models.Manager()
6e001443 96
97 def __unicode__(self):
c08cba47 98 return "%s" % (self.titre)
54ab837c 99
230671ff
EMS
100 def get_absolute_url(self):
101 return reverse('site', kwargs={'id': self.id})
102
54ab837c
AJ
103 def type_display(self):
104 for t in TYPE_SITE_CHOICES:
105 if self.type == t[0]:
106 return t[1]
107 return "-"
108
264a3210
EMS
109 def assigner_regions(self, regions):
110 self.regions.add(*regions)
111
112 def assigner_disciplines(self, disciplines):
113 self.discipline.add(*disciplines)
fdcf5874 114
81fe476e
PP
115class SiteVoir(Site):
116
117 class Meta:
118 proxy = True
429222f1
PP
119 verbose_name = 'sites (visualisation)'
120 verbose_name_plural = 'sites (visualisation)'
81fe476e 121
fdcf5874
EMS
122class SiteSearch(Search):
123 pays = models.ForeignKey(Pays, blank=True, null=True)
c30bc3e7 124 site_auf = models.BooleanField('soutenu par l\'AUF', default=False)
fdcf5874
EMS
125
126 class Meta:
127 verbose_name = "recherche de sites"
128 verbose_name_plural = "recherches de sites"
129
4b89a7df 130 def run(self, min_date=None, max_date=None):
fdcf5874 131 results = Site.objects
c30bc3e7
PP
132 if self.site_auf:
133 results = results.filter(site_auf=True)
fdcf5874
EMS
134 if self.q:
135 results = results.search(self.q)
136 if self.discipline:
137 results = results.filter_discipline(self.discipline)
138 if self.region:
139 results = results.filter_region(self.region)
140 if self.pays:
141 results = results.filter_pays(pays=self.pays)
142 if not self.q:
143 results = results.order_by('-date_maj')
4b89a7df
EMS
144 if min_date:
145 results = results.filter_date_maj(min=min_date)
146 if max_date:
147 results = results.filter_date_maj(max=max_date)
fdcf5874
EMS
148 return results.all()
149
150 def url(self):
151 qs = self.query_string()
152 return reverse('sites') + ('?' + qs if qs else '')
153
da5bf7e9
EMS
154 def rss_url(self):
155 qs = self.query_string()
156 return reverse('rss_sites') + ('?' + qs if qs else '')
4b89a7df
EMS
157
158 def get_email_alert_content(self, results):
159 content = ''
160 for site in results:
161 content += u'- [%s](%s%s)\n\n' % (site.titre,
162 settings.SITE_ROOT_URL,
163 site.get_absolute_url())
164 if site.description:
165 content += '\n'
166 content += ''.join([' %s\n' % line for line in textwrap.wrap(site.description)])
167 content += '\n'
168 return content