Commit | Line | Data |
---|---|---|
01a9197e | 1 | # -*- encoding: utf-8 -*- |
2 | from django.db import models | |
878de341 | 3 | from django.db.models import Q |
03e081cb AJ |
4 | from datamaster_modeles.models import * |
5 | from savoirs.models import Discipline | |
01a9197e | 6 | |
7 | TYPE_SITE_CHOICES = ( | |
8 | ('RV', 'Revue en ligne'), | |
9 | ('BB', 'Bibliothèque en ligne'), | |
00755d9b | 10 | ('FD', 'Fonds documentaire'), |
01a9197e | 11 | ('AR', 'Archive ouverte'), |
12 | ('CO', 'Cours en ligne'), | |
00755d9b AJ |
13 | ('RE', 'Repertoire de ressource'), |
14 | ('SA', 'Site associatif'), | |
15 | ('SC', 'Site culturel'), | |
16 | ('SI', 'Site d\'information'), | |
01a9197e | 17 | ('AU', 'Autre type de site'), |
18 | ) | |
19 | ||
878de341 EMS |
20 | class SiteManager(models.Manager): |
21 | ||
22 | def get_query_set(self): | |
23 | return SiteQuerySet(self.model) | |
24 | ||
25 | def search(self, text): | |
26 | return self.get_query_set().search(text) | |
27 | ||
28 | class SiteQuerySet(models.query.QuerySet): | |
29 | ||
30 | def search(self, text): | |
31 | qs = self | |
0b1ddc11 | 32 | q = None |
878de341 | 33 | for word in text.split(): |
0b1ddc11 EMS |
34 | part = (Q(titre__icontains=word) | |
35 | Q(description__icontains=word) | | |
36 | Q(editeur__icontains=word) | | |
37 | Q(auteur__icontains=word) | | |
38 | Q(mots_cles__icontains=word) | | |
39 | Q(discipline__nom__icontains=word) | | |
ad0600bc | 40 | Q(pays__nom__icontains=word)) |
0b1ddc11 EMS |
41 | if q is None: |
42 | q = part | |
43 | else: | |
44 | q = q & part | |
3c23982e EMS |
45 | if q is not None: |
46 | qs = qs.filter(q).distinct() | |
878de341 EMS |
47 | return qs |
48 | ||
01a9197e | 49 | class Site(models.Model): |
50 | """Fiche d'info d'un site web""" | |
f810842d | 51 | url = models.URLField(verify_exists=False) # dc:identifier (dc:source?) |
03e081cb | 52 | titre = models.CharField(max_length=255, verbose_name='Titre') # dc.title |
e7d36875 AJ |
53 | description = models.TextField(null=True, blank=True) |
54 | editeur = models.CharField(max_length=255, null=True, blank=True, verbose_name='Éditeur') # dc.publisher : organisation resp | |
55 | auteur = models.CharField(max_length=255, null=True, blank=True, verbose_name='Auteur') # dc.creator : nom, prénom | |
03e081cb AJ |
56 | |
57 | #auf_partenaire = models.BooleanField() # dc.contributor | |
58 | ||
e7d36875 AJ |
59 | date_publication = models.DateField(null=True, blank=True) # dc.date : date de publication |
60 | type = models.CharField(max_length=2, null=True, blank=True, choices=TYPE_SITE_CHOICES, | |
01a9197e | 61 | verbose_name = 'Type de site') # dc.type |
03e081cb AJ |
62 | discipline = models.ManyToManyField(Discipline, blank=True) |
63 | thematique = models.ManyToManyField(Thematique, blank=True) | |
64 | ||
e7d36875 | 65 | mots_cles = models.TextField(verbose_name='Mots-clés', null=True, blank=True) # dc:subject # indexation libre |
01a9197e | 66 | |
67 | # source # dc:source (dc:relation?) | |
e7d36875 | 68 | pays = models.ForeignKey(Pays, null = True, blank=True, db_column='pays', to_field='code', verbose_name = 'Pays') |
3a45eb64 | 69 | regions = models.ManyToManyField(Region, blank=True, related_name="sites", verbose_name='Régions') |
01a9197e | 70 | |
71 | # meta | |
72 | actif = models.BooleanField() | |
03e081cb | 73 | date_maj = models.DateField(auto_now=True) |
878de341 EMS |
74 | |
75 | # Manager | |
76 | objects = SiteManager() | |
6e001443 | 77 | |
78 | def __unicode__(self): | |
c08cba47 | 79 | return "%s" % (self.titre) |
54ab837c AJ |
80 | |
81 | def type_display(self): | |
82 | for t in TYPE_SITE_CHOICES: | |
83 | if self.type == t[0]: | |
84 | return t[1] | |
85 | return "-" | |
86 | ||
264a3210 EMS |
87 | def assigner_regions(self, regions): |
88 | self.regions.add(*regions) | |
89 | ||
90 | def assigner_disciplines(self, disciplines): | |
91 | self.discipline.add(*disciplines) |