Fils RSS pour toutes les briques
[auf_savoirs_en_partage_django.git] / auf_savoirs_en_partage / sitotheque / models.py
CommitLineData
01a9197e 1# -*- encoding: utf-8 -*-
fdcf5874 2from django.core.urlresolvers import reverse
01a9197e 3from django.db import models
878de341 4from django.db.models import Q
39a63232 5from djangosphinx.models import SphinxSearch
fdcf5874
EMS
6
7from datamaster_modeles.models import *
8from savoirs.models import Discipline, SEPManager, SEPSphinxQuerySet, SEPQuerySet, Search
01a9197e 9
10TYPE_SITE_CHOICES = (
11 ('RV', 'Revue en ligne'),
12 ('BB', 'Bibliothèque en ligne'),
00755d9b 13 ('FD', 'Fonds documentaire'),
01a9197e 14 ('AR', 'Archive ouverte'),
15 ('CO', 'Cours en ligne'),
00755d9b
AJ
16 ('RE', 'Repertoire de ressource'),
17 ('SA', 'Site associatif'),
18 ('SC', 'Site culturel'),
19 ('SI', 'Site d\'information'),
01a9197e 20 ('AU', 'Autre type de site'),
fdcf5874 21)
01a9197e 22
39a63232 23class SiteQuerySet(SEPQuerySet):
878de341 24
39a63232
EMS
25 def filter_pays(self, pays):
26 return self.filter(pays=pays)
878de341 27
230671ff
EMS
28 def filter_date_maj(self, min=None, max=None):
29 return self._filter_date('date_maj', min=min, max=max)
30
39a63232 31class SiteSphinxQuerySet(SEPSphinxQuerySet):
878de341 32
39a63232
EMS
33 def __init__(self, model=None):
34 SEPSphinxQuerySet.__init__(self, model=model, index='savoirsenpartage_sites', weights=dict(titre=3))
c1b134f8 35
230671ff
EMS
36 def filter_date_maj(self, min=None, max=None):
37 return self._filter_date('date_maj', min=min, max=max)
38
39a63232
EMS
39 def filter_pays(self, pays):
40 return self.filter(pays_ids=pays.id)
c1b134f8 41
39a63232 42class SiteManager(SEPManager):
878de341 43
39a63232
EMS
44 def get_query_set(self):
45 return SiteQuerySet(self.model).filter(actif=True)
878de341 46
39a63232
EMS
47 def get_sphinx_query_set(self):
48 return SiteSphinxQuerySet(self.model)
bae03b7b 49
39a63232
EMS
50 def filter_pays(self, pays):
51 return self.get_query_set().filter_pays(pays)
bae03b7b 52
230671ff
EMS
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
01a9197e 56class Site(models.Model):
57 """Fiche d'info d'un site web"""
f810842d 58 url = models.URLField(verify_exists=False) # dc:identifier (dc:source?)
a5f76eb4 59 titre = models.CharField(max_length=255, verbose_name='titre') # dc.title
e7d36875 60 description = models.TextField(null=True, blank=True)
a5f76eb4
EMS
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
03e081cb
AJ
63
64 #auf_partenaire = models.BooleanField() # dc.contributor
65
e7d36875
AJ
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,
01a9197e 68 verbose_name = 'Type de site') # dc.type
03e081cb
AJ
69 discipline = models.ManyToManyField(Discipline, blank=True)
70 thematique = models.ManyToManyField(Thematique, blank=True)
71
e7d36875 72 mots_cles = models.TextField(verbose_name='Mots-clés', null=True, blank=True) # dc:subject # indexation libre
01a9197e 73
74 # source # dc:source (dc:relation?)
a5f76eb4
EMS
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')
01a9197e 77
78 # meta
41d30718 79 actif = models.BooleanField(default=True)
03e081cb 80 date_maj = models.DateField(auto_now=True)
fc2908e4 81 recherche_google = models.BooleanField('Inclure dans la recherche Google', default=False)
878de341
EMS
82
83 # Manager
84 objects = SiteManager()
c59dba82 85 all_objects = models.Manager()
6e001443 86
87 def __unicode__(self):
c08cba47 88 return "%s" % (self.titre)
54ab837c 89
230671ff
EMS
90 def get_absolute_url(self):
91 return reverse('site', kwargs={'id': self.id})
92
54ab837c
AJ
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
264a3210
EMS
99 def assigner_regions(self, regions):
100 self.regions.add(*regions)
101
102 def assigner_disciplines(self, disciplines):
103 self.discipline.add(*disciplines)
fdcf5874
EMS
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