[#4773] Lien vers chercheurs et ressources similaire
[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 auf.django.references.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_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)
50
51 class SiteManager(SEPManager):
52
53 def get_query_set(self):
54 return SiteQuerySet(self.model).filter(actif=True)
55
56 def get_sphinx_query_set(self):
57 return SiteSphinxQuerySet(self.model)
58
59 def filter_pays(self, pays):
60 return self.get_query_set().filter_pays(pays)
61
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
65 class Site(models.Model):
66 """Fiche d'info d'un site web"""
67 url = models.URLField(verify_exists=False) # dc:identifier (dc:source?)
68 titre = models.CharField(max_length=255, verbose_name='titre') # dc.title
69 description = models.TextField(null=True, blank=True)
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
72
73 #auf_partenaire = models.BooleanField() # dc.contributor
74
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,
77 verbose_name = 'Type de site') # dc.type
78 discipline = models.ManyToManyField(Discipline, blank=True)
79 thematique = models.ManyToManyField(Thematique, blank=True)
80
81 mots_cles = models.TextField(verbose_name='Mots-clés', null=True, blank=True) # dc:subject # indexation libre
82
83 # source # dc:source (dc:relation?)
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')
86
87 # meta
88 actif = models.BooleanField(default=True)
89 date_maj = models.DateField(auto_now=True)
90 recherche_google = models.BooleanField('Inclure dans la recherche Google', default=False)
91 site_auf = models.BooleanField('soutenu par l\'AUF', default=False)
92
93 # Manager
94 objects = SiteManager()
95 all_objects = models.Manager()
96
97 def __unicode__(self):
98 return "%s" % (self.titre)
99
100 def get_absolute_url(self):
101 return reverse('site', kwargs={'id': self.id})
102
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
109 def assigner_regions(self, regions):
110 self.regions.add(*regions)
111
112 def assigner_disciplines(self, disciplines):
113 self.discipline.add(*disciplines)
114
115 class SiteVoir(Site):
116
117 class Meta:
118 proxy = True
119 verbose_name = 'sites (visualisation)'
120 verbose_name_plural = 'sites (visualisation)'
121
122 class SiteSearch(Search):
123 pays = models.ForeignKey(Pays, blank=True, null=True)
124 site_auf = models.BooleanField('soutenu par l\'AUF', default=False)
125
126 class Meta:
127 verbose_name = "recherche de sites"
128 verbose_name_plural = "recherches de sites"
129
130 def run(self, min_date=None, max_date=None):
131 results = Site.objects
132 if self.site_auf:
133 results = results.filter(site_auf=True)
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')
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)
148 return results.all()
149
150 def url(self):
151 qs = self.query_string()
152 return reverse('sites') + ('?' + qs if qs else '')
153
154 def rss_url(self):
155 qs = self.query_string()
156 return reverse('rss_sites') + ('?' + qs if qs else '')
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