Fix admin
[auf_savoirs_en_partage_django.git] / auf_savoirs_en_partage / sitotheque / models.py
1 # -*- encoding: utf-8 -*-
2 from django.db import models
3 from django.db.models import Q
4 from datamaster_modeles.models import *
5 from savoirs.models import Discipline, RandomQuerySetMixin
6
7 TYPE_SITE_CHOICES = (
8 ('RV', 'Revue en ligne'),
9 ('BB', 'Bibliothèque en ligne'),
10 ('FD', 'Fonds documentaire'),
11 ('AR', 'Archive ouverte'),
12 ('CO', 'Cours en ligne'),
13 ('RE', 'Repertoire de ressource'),
14 ('SA', 'Site associatif'),
15 ('SC', 'Site culturel'),
16 ('SI', 'Site d\'information'),
17 ('AU', 'Autre type de site'),
18 )
19
20 class SiteManager(models.Manager):
21
22 def get_query_set(self):
23 return SiteQuerySet(self.model).filter(actif=True)
24
25 def search(self, text):
26 return self.get_query_set().search(text)
27
28 def filter_region(self, region):
29 return self.get_query_set().filter_region(region)
30
31 def filter_discipline(self, discipline):
32 return self.get_query_set().filter_discipline(discipline)
33
34 class SiteQuerySet(models.query.QuerySet, RandomQuerySetMixin):
35
36 def search(self, text):
37 qs = self
38 q = None
39 for word in text.split():
40 part = (Q(titre__icontains=word) |
41 Q(description__icontains=word) |
42 Q(editeur__icontains=word) |
43 Q(auteur__icontains=word) |
44 Q(mots_cles__icontains=word) |
45 Q(discipline__nom__icontains=word) |
46 Q(pays__nom__icontains=word))
47 if q is None:
48 q = part
49 else:
50 q = q & part
51 if q is not None:
52 qs = qs.filter(q).distinct()
53 return qs
54
55 def filter_discipline(self, discipline):
56 """Ne conserve que les sites dans la discipline donnée.
57
58 Si ``disicipline`` est None, ce filtre n'a aucun effet."""
59 if discipline is None:
60 return self
61 if not isinstance(discipline, Discipline):
62 discipline = Discipline.objects.get(pk=discipline)
63 return self.filter(Q(discipline=discipline) |
64 Q(titre__icontains=discipline.nom) |
65 Q(description__icontains=discipline.nom) |
66 Q(mots_cles__icontains=discipline.nom))
67
68 def filter_region(self, region):
69 """Ne conserve que les sites dans la région donnée.
70
71 Si ``region`` est None, ce filtre n'a aucun effet."""
72 if region is None:
73 return self
74 if not isinstance(region, Region):
75 region = Region.objects.get(pk=region)
76 return self.filter(Q(pays__region=region) |
77 Q(titre__icontains=region.nom) |
78 Q(description__icontains=region.nom) |
79 Q(mots_cles__icontains=region.nom)).distinct()
80
81 class Site(models.Model):
82 """Fiche d'info d'un site web"""
83 url = models.URLField(verify_exists=False) # dc:identifier (dc:source?)
84 titre = models.CharField(max_length=255, verbose_name='titre') # dc.title
85 description = models.TextField(null=True, blank=True)
86 editeur = models.CharField(max_length=255, null=True, blank=True, verbose_name='éditeur') # dc.publisher : organisation resp
87 auteur = models.CharField(max_length=255, null=True, blank=True, verbose_name='auteur') # dc.creator : nom, prénom
88
89 #auf_partenaire = models.BooleanField() # dc.contributor
90
91 date_publication = models.DateField(null=True, blank=True) # dc.date : date de publication
92 type = models.CharField(max_length=2, null=True, blank=True, choices=TYPE_SITE_CHOICES,
93 verbose_name = 'Type de site') # dc.type
94 discipline = models.ManyToManyField(Discipline, blank=True)
95 thematique = models.ManyToManyField(Thematique, blank=True)
96
97 mots_cles = models.TextField(verbose_name='Mots-clés', null=True, blank=True) # dc:subject # indexation libre
98
99 # source # dc:source (dc:relation?)
100 pays = models.ForeignKey(Pays, null = True, blank=True, db_column='pays', to_field='code', verbose_name = 'pays')
101 regions = models.ManyToManyField(Region, blank=True, related_name="sites", verbose_name='régions')
102
103 # meta
104 actif = models.BooleanField()
105 date_maj = models.DateField(auto_now=True)
106
107 # Manager
108 objects = SiteManager()
109 all_objects = models.Manager()
110
111 def __unicode__(self):
112 return "%s" % (self.titre)
113
114 def type_display(self):
115 for t in TYPE_SITE_CHOICES:
116 if self.type == t[0]:
117 return t[1]
118 return "-"
119
120 def assigner_regions(self, regions):
121 self.regions.add(*regions)
122
123 def assigner_disciplines(self, disciplines):
124 self.discipline.add(*disciplines)