Mettre (visualisation) à la fin des classes
[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
EMS
9
10from datamaster_modeles.models import *
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
EMS
42 def filter_pays(self, pays):
43 return self.filter(pays_ids=pays.id)
c1b134f8 44
39a63232 45class SiteManager(SEPManager):
878de341 46
39a63232
EMS
47 def get_query_set(self):
48 return SiteQuerySet(self.model).filter(actif=True)
878de341 49
39a63232
EMS
50 def get_sphinx_query_set(self):
51 return SiteSphinxQuerySet(self.model)
bae03b7b 52
39a63232
EMS
53 def filter_pays(self, pays):
54 return self.get_query_set().filter_pays(pays)
bae03b7b 55
230671ff
EMS
56 def filter_date_maj(self, min=None, max=None):
57 return self.get_query_set().filter_date_maj(self, min=min, max=max)
58
01a9197e 59class Site(models.Model):
60 """Fiche d'info d'un site web"""
f810842d 61 url = models.URLField(verify_exists=False) # dc:identifier (dc:source?)
a5f76eb4 62 titre = models.CharField(max_length=255, verbose_name='titre') # dc.title
e7d36875 63 description = models.TextField(null=True, blank=True)
a5f76eb4
EMS
64 editeur = models.CharField(max_length=255, null=True, blank=True, verbose_name='éditeur') # dc.publisher : organisation resp
65 auteur = models.CharField(max_length=255, null=True, blank=True, verbose_name='auteur') # dc.creator : nom, prénom
03e081cb
AJ
66
67 #auf_partenaire = models.BooleanField() # dc.contributor
68
e7d36875
AJ
69 date_publication = models.DateField(null=True, blank=True) # dc.date : date de publication
70 type = models.CharField(max_length=2, null=True, blank=True, choices=TYPE_SITE_CHOICES,
01a9197e 71 verbose_name = 'Type de site') # dc.type
03e081cb
AJ
72 discipline = models.ManyToManyField(Discipline, blank=True)
73 thematique = models.ManyToManyField(Thematique, blank=True)
74
e7d36875 75 mots_cles = models.TextField(verbose_name='Mots-clés', null=True, blank=True) # dc:subject # indexation libre
01a9197e 76
77 # source # dc:source (dc:relation?)
a5f76eb4
EMS
78 pays = models.ForeignKey(Pays, null = True, blank=True, db_column='pays', to_field='code', verbose_name = 'pays')
79 regions = models.ManyToManyField(Region, blank=True, related_name="sites", verbose_name='régions')
01a9197e 80
81 # meta
41d30718 82 actif = models.BooleanField(default=True)
03e081cb 83 date_maj = models.DateField(auto_now=True)
fc2908e4 84 recherche_google = models.BooleanField('Inclure dans la recherche Google', default=False)
878de341
EMS
85
86 # Manager
87 objects = SiteManager()
c59dba82 88 all_objects = models.Manager()
6e001443 89
90 def __unicode__(self):
c08cba47 91 return "%s" % (self.titre)
54ab837c 92
230671ff
EMS
93 def get_absolute_url(self):
94 return reverse('site', kwargs={'id': self.id})
95
54ab837c
AJ
96 def type_display(self):
97 for t in TYPE_SITE_CHOICES:
98 if self.type == t[0]:
99 return t[1]
100 return "-"
101
264a3210
EMS
102 def assigner_regions(self, regions):
103 self.regions.add(*regions)
104
105 def assigner_disciplines(self, disciplines):
106 self.discipline.add(*disciplines)
fdcf5874 107
81fe476e
PP
108class SiteVoir(Site):
109
110 class Meta:
111 proxy = True
429222f1
PP
112 verbose_name = 'sites (visualisation)'
113 verbose_name_plural = 'sites (visualisation)'
81fe476e 114
fdcf5874
EMS
115class SiteSearch(Search):
116 pays = models.ForeignKey(Pays, blank=True, null=True)
117
118 class Meta:
119 verbose_name = "recherche de sites"
120 verbose_name_plural = "recherches de sites"
121
4b89a7df 122 def run(self, min_date=None, max_date=None):
fdcf5874
EMS
123 results = Site.objects
124 if self.q:
125 results = results.search(self.q)
126 if self.discipline:
127 results = results.filter_discipline(self.discipline)
128 if self.region:
129 results = results.filter_region(self.region)
130 if self.pays:
131 results = results.filter_pays(pays=self.pays)
132 if not self.q:
133 results = results.order_by('-date_maj')
4b89a7df
EMS
134 if min_date:
135 results = results.filter_date_maj(min=min_date)
136 if max_date:
137 results = results.filter_date_maj(max=max_date)
fdcf5874
EMS
138 return results.all()
139
140 def url(self):
141 qs = self.query_string()
142 return reverse('sites') + ('?' + qs if qs else '')
143
da5bf7e9
EMS
144 def rss_url(self):
145 qs = self.query_string()
146 return reverse('rss_sites') + ('?' + qs if qs else '')
4b89a7df
EMS
147
148 def get_email_alert_content(self, results):
149 content = ''
150 for site in results:
151 content += u'- [%s](%s%s)\n\n' % (site.titre,
152 settings.SITE_ROOT_URL,
153 site.get_absolute_url())
154 if site.description:
155 content += '\n'
156 content += ''.join([' %s\n' % line for line in textwrap.wrap(site.description)])
157 content += '\n'
158 return content