Fils RSS pour toutes les briques
[auf_savoirs_en_partage_django.git] / auf_savoirs_en_partage / sitotheque / models.py
... / ...
CommitLineData
1# -*- encoding: utf-8 -*-
2from django.core.urlresolvers import reverse
3from django.db import models
4from django.db.models import Q
5from djangosphinx.models import SphinxSearch
6
7from datamaster_modeles.models import *
8from savoirs.models import Discipline, SEPManager, SEPSphinxQuerySet, SEPQuerySet, Search
9
10TYPE_SITE_CHOICES = (
11 ('RV', 'Revue en ligne'),
12 ('BB', 'Bibliothèque en ligne'),
13 ('FD', 'Fonds documentaire'),
14 ('AR', 'Archive ouverte'),
15 ('CO', 'Cours en ligne'),
16 ('RE', 'Repertoire de ressource'),
17 ('SA', 'Site associatif'),
18 ('SC', 'Site culturel'),
19 ('SI', 'Site d\'information'),
20 ('AU', 'Autre type de site'),
21)
22
23class SiteQuerySet(SEPQuerySet):
24
25 def filter_pays(self, pays):
26 return self.filter(pays=pays)
27
28 def filter_date_maj(self, min=None, max=None):
29 return self._filter_date('date_maj', min=min, max=max)
30
31class SiteSphinxQuerySet(SEPSphinxQuerySet):
32
33 def __init__(self, model=None):
34 SEPSphinxQuerySet.__init__(self, model=model, index='savoirsenpartage_sites', weights=dict(titre=3))
35
36 def filter_date_maj(self, min=None, max=None):
37 return self._filter_date('date_maj', min=min, max=max)
38
39 def filter_pays(self, pays):
40 return self.filter(pays_ids=pays.id)
41
42class SiteManager(SEPManager):
43
44 def get_query_set(self):
45 return SiteQuerySet(self.model).filter(actif=True)
46
47 def get_sphinx_query_set(self):
48 return SiteSphinxQuerySet(self.model)
49
50 def filter_pays(self, pays):
51 return self.get_query_set().filter_pays(pays)
52
53 def filter_date_maj(self, min=None, max=None):
54 return self.get_query_set().filter_date_maj(self, min=min, max=max)
55
56class Site(models.Model):
57 """Fiche d'info d'un site web"""
58 url = models.URLField(verify_exists=False) # dc:identifier (dc:source?)
59 titre = models.CharField(max_length=255, verbose_name='titre') # dc.title
60 description = models.TextField(null=True, blank=True)
61 editeur = models.CharField(max_length=255, null=True, blank=True, verbose_name='éditeur') # dc.publisher : organisation resp
62 auteur = models.CharField(max_length=255, null=True, blank=True, verbose_name='auteur') # dc.creator : nom, prénom
63
64 #auf_partenaire = models.BooleanField() # dc.contributor
65
66 date_publication = models.DateField(null=True, blank=True) # dc.date : date de publication
67 type = models.CharField(max_length=2, null=True, blank=True, choices=TYPE_SITE_CHOICES,
68 verbose_name = 'Type de site') # dc.type
69 discipline = models.ManyToManyField(Discipline, blank=True)
70 thematique = models.ManyToManyField(Thematique, blank=True)
71
72 mots_cles = models.TextField(verbose_name='Mots-clés', null=True, blank=True) # dc:subject # indexation libre
73
74 # source # dc:source (dc:relation?)
75 pays = models.ForeignKey(Pays, null = True, blank=True, db_column='pays', to_field='code', verbose_name = 'pays')
76 regions = models.ManyToManyField(Region, blank=True, related_name="sites", verbose_name='régions')
77
78 # meta
79 actif = models.BooleanField(default=True)
80 date_maj = models.DateField(auto_now=True)
81 recherche_google = models.BooleanField('Inclure dans la recherche Google', default=False)
82
83 # Manager
84 objects = SiteManager()
85 all_objects = models.Manager()
86
87 def __unicode__(self):
88 return "%s" % (self.titre)
89
90 def get_absolute_url(self):
91 return reverse('site', kwargs={'id': self.id})
92
93 def type_display(self):
94 for t in TYPE_SITE_CHOICES:
95 if self.type == t[0]:
96 return t[1]
97 return "-"
98
99 def assigner_regions(self, regions):
100 self.regions.add(*regions)
101
102 def assigner_disciplines(self, disciplines):
103 self.discipline.add(*disciplines)
104
105class SiteSearch(Search):
106 pays = models.ForeignKey(Pays, blank=True, null=True)
107
108 class Meta:
109 verbose_name = "recherche de sites"
110 verbose_name_plural = "recherches de sites"
111
112 def run(self):
113 results = Site.objects
114 if self.q:
115 results = results.search(self.q)
116 if self.discipline:
117 results = results.filter_discipline(self.discipline)
118 if self.region:
119 results = results.filter_region(self.region)
120 if self.pays:
121 results = results.filter_pays(pays=self.pays)
122 if not self.q:
123 results = results.order_by('-date_maj')
124 return results.all()
125
126 def url(self):
127 qs = self.query_string()
128 return reverse('sites') + ('?' + qs if qs else '')
129