Mettre (visualisation) à la fin des classes
[auf_savoirs_en_partage_django.git] / auf_savoirs_en_partage / sitotheque / models.py
1 # -*- encoding: utf-8 -*-
2 import textwrap
3
4 from django.conf import settings
5 from django.core.urlresolvers import reverse
6 from django.db import models
7 from django.db.models import Q
8 from djangosphinx.models import SphinxSearch
9
10 from datamaster_modeles.models import *
11 from savoirs.models import Discipline, SEPManager, SEPSphinxQuerySet, SEPQuerySet, Search
12
13 TYPE_SITE_CHOICES = (
14 ('RV', 'Revue en ligne'),
15 ('BB', 'Bibliothèque en ligne'),
16 ('FD', 'Fonds documentaire'),
17 ('AR', 'Archive ouverte'),
18 ('CO', 'Cours en ligne'),
19 ('RE', 'Repertoire de ressource'),
20 ('SA', 'Site associatif'),
21 ('SC', 'Site culturel'),
22 ('SI', 'Site d\'information'),
23 ('AU', 'Autre type de site'),
24 )
25
26 class SiteQuerySet(SEPQuerySet):
27
28 def filter_pays(self, pays):
29 return self.filter(pays=pays)
30
31 def filter_date_maj(self, min=None, max=None):
32 return self._filter_date('date_maj', min=min, max=max)
33
34 class SiteSphinxQuerySet(SEPSphinxQuerySet):
35
36 def __init__(self, model=None):
37 SEPSphinxQuerySet.__init__(self, model=model, index='savoirsenpartage_sites', weights=dict(titre=3))
38
39 def filter_date_maj(self, min=None, max=None):
40 return self._filter_date('date_maj', min=min, max=max)
41
42 def filter_pays(self, pays):
43 return self.filter(pays_ids=pays.id)
44
45 class SiteManager(SEPManager):
46
47 def get_query_set(self):
48 return SiteQuerySet(self.model).filter(actif=True)
49
50 def get_sphinx_query_set(self):
51 return SiteSphinxQuerySet(self.model)
52
53 def filter_pays(self, pays):
54 return self.get_query_set().filter_pays(pays)
55
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
59 class Site(models.Model):
60 """Fiche d'info d'un site web"""
61 url = models.URLField(verify_exists=False) # dc:identifier (dc:source?)
62 titre = models.CharField(max_length=255, verbose_name='titre') # dc.title
63 description = models.TextField(null=True, blank=True)
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
66
67 #auf_partenaire = models.BooleanField() # dc.contributor
68
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,
71 verbose_name = 'Type de site') # dc.type
72 discipline = models.ManyToManyField(Discipline, blank=True)
73 thematique = models.ManyToManyField(Thematique, blank=True)
74
75 mots_cles = models.TextField(verbose_name='Mots-clés', null=True, blank=True) # dc:subject # indexation libre
76
77 # source # dc:source (dc:relation?)
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')
80
81 # meta
82 actif = models.BooleanField(default=True)
83 date_maj = models.DateField(auto_now=True)
84 recherche_google = models.BooleanField('Inclure dans la recherche Google', default=False)
85
86 # Manager
87 objects = SiteManager()
88 all_objects = models.Manager()
89
90 def __unicode__(self):
91 return "%s" % (self.titre)
92
93 def get_absolute_url(self):
94 return reverse('site', kwargs={'id': self.id})
95
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
102 def assigner_regions(self, regions):
103 self.regions.add(*regions)
104
105 def assigner_disciplines(self, disciplines):
106 self.discipline.add(*disciplines)
107
108 class SiteVoir(Site):
109
110 class Meta:
111 proxy = True
112 verbose_name = 'sites (visualisation)'
113 verbose_name_plural = 'sites (visualisation)'
114
115 class 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
122 def run(self, min_date=None, max_date=None):
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')
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)
138 return results.all()
139
140 def url(self):
141 qs = self.query_string()
142 return reverse('sites') + ('?' + qs if qs else '')
143
144 def rss_url(self):
145 qs = self.query_string()
146 return reverse('rss_sites') + ('?' + qs if qs else '')
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