| author | ymh <ymh.work@gmail.com> |
| Fri, 24 Jan 2020 16:49:22 +0100 | |
| branch | documentation |
| changeset 701 | bf0820deea40 |
| parent 693 | 09e00f38d177 |
| permissions | -rw-r--r-- |
| 253 | 1 |
# -*- coding: utf-8 -*- |
2 |
||
|
607
17f3582ecdb1
correct display about page, especially on windows
ymh <ymh.work@gmail.com>
parents:
594
diff
changeset
|
3 |
import re |
|
17f3582ecdb1
correct display about page, especially on windows
ymh <ymh.work@gmail.com>
parents:
594
diff
changeset
|
4 |
|
|
299
8e00641076e7
remove renkan management to an other django app (see renkan hg repo)
cavaliet
parents:
271
diff
changeset
|
5 |
from django.shortcuts import render_to_response |
| 253 | 6 |
from django.template import RequestContext |
|
607
17f3582ecdb1
correct display about page, especially on windows
ymh <ymh.work@gmail.com>
parents:
594
diff
changeset
|
7 |
from django.views.generic.base import TemplateView |
|
675
d68e7b3a2e4f
Added support for video popup in renkan edit page and in about page
durandn
parents:
636
diff
changeset
|
8 |
from django.conf import settings |
|
607
17f3582ecdb1
correct display about page, especially on windows
ymh <ymh.work@gmail.com>
parents:
594
diff
changeset
|
9 |
import user_agents |
|
17f3582ecdb1
correct display about page, especially on windows
ymh <ymh.work@gmail.com>
parents:
594
diff
changeset
|
10 |
|
| 253 | 11 |
from hdabo.models import Datasheet, TaggedSheet |
|
636
cc2f7294f1d2
add a 404 when notice are not found when displaying their page
ymh <ymh.work@gmail.com>
parents:
607
diff
changeset
|
12 |
from django.http.response import Http404 |
|
607
17f3582ecdb1
correct display about page, especially on windows
ymh <ymh.work@gmail.com>
parents:
594
diff
changeset
|
13 |
|
| 253 | 14 |
|
15 |
def datasheet(request, hda_id=None): |
|
| 693 | 16 |
""" |
17 |
Methode Vue affichant le détail d'une fiche HDA. |
|
18 |
Cette vue affiche en fait un résumé de la notice mise en relation avec les notices liées, via les mots clefs. |
|
19 |
""" |
|
| 253 | 20 |
MAX_TAG = 15 |
21 |
MAX_RELATED = 50 |
|
|
676
111906d4c8b0
correct video embed and new renkan version
ymh <ymh.work@gmail.com>
parents:
675
diff
changeset
|
22 |
|
|
636
cc2f7294f1d2
add a 404 when notice are not found when displaying their page
ymh <ymh.work@gmail.com>
parents:
607
diff
changeset
|
23 |
datasheet = None |
|
cc2f7294f1d2
add a 404 when notice are not found when displaying their page
ymh <ymh.work@gmail.com>
parents:
607
diff
changeset
|
24 |
try: |
|
cc2f7294f1d2
add a 404 when notice are not found when displaying their page
ymh <ymh.work@gmail.com>
parents:
607
diff
changeset
|
25 |
datasheet = Datasheet.objects.select_related("organisation").get(hda_id=hda_id) |
|
cc2f7294f1d2
add a 404 when notice are not found when displaying their page
ymh <ymh.work@gmail.com>
parents:
607
diff
changeset
|
26 |
except Datasheet.DoesNotExist: |
|
cc2f7294f1d2
add a 404 when notice are not found when displaying their page
ymh <ymh.work@gmail.com>
parents:
607
diff
changeset
|
27 |
raise Http404("The datasheet %s is not found."%hda_id) |
|
676
111906d4c8b0
correct video embed and new renkan version
ymh <ymh.work@gmail.com>
parents:
675
diff
changeset
|
28 |
|
| 253 | 29 |
domain = re.findall(r"^https?://(www\.)?([^/]+)",datasheet.url) |
30 |
ordered_tags = TaggedSheet.objects.filter(datasheet=datasheet,order__lte=MAX_TAG).select_related("tag").order_by('order') |
|
31 |
tags = [t.tag.id for t in ordered_tags] |
|
32 |
tagorders = dict([(t.tag.id,t.order) for t in ordered_tags]) |
|
33 |
tsqs = TaggedSheet.objects.exclude(datasheet=datasheet).filter(order__lte=MAX_TAG,tag_id__in=tags) |
|
34 |
dsscores = {} |
|
| 260 | 35 |
addtoscore = 2*MAX_TAG |
|
676
111906d4c8b0
correct video embed and new renkan version
ymh <ymh.work@gmail.com>
parents:
675
diff
changeset
|
36 |
|
| 253 | 37 |
for ts in tsqs: |
38 |
a_order = tagorders[ts.tag_id] |
|
39 |
b_order = ts.order |
|
40 |
score = addtoscore - a_order - b_order |
|
41 |
dsscore = dsscores.get(ts.datasheet_id,0) |
|
42 |
dsscores[ts.datasheet_id] = dsscore + score |
|
43 |
relatedqs = Datasheet.objects.filter(id__in=dsscores.keys()).select_related("organisation") |
|
44 |
relatedds = [{ |
|
45 |
'id': ds.id, |
|
46 |
'hda_id': ds.hda_id, |
|
47 |
'title': ds.title, |
|
48 |
'description': ds.description, |
|
|
594
922fc1545933
correct alignments of search results + link to tag maps
ymh <ymh.work@gmail.com>
parents:
299
diff
changeset
|
49 |
'organisation_name': ds.organisation.name if ds.organisation else '', |
|
922fc1545933
correct alignments of search results + link to tag maps
ymh <ymh.work@gmail.com>
parents:
299
diff
changeset
|
50 |
'organisation_url': ds.organisation.website if ds.organisation else '', |
| 253 | 51 |
'score': dsscores.get(ds.id,0), |
52 |
} for ds in relatedqs] |
|
53 |
relatedds = sorted(relatedds, key=lambda ds: -ds['score'])[:MAX_RELATED] |
|
|
676
111906d4c8b0
correct video embed and new renkan version
ymh <ymh.work@gmail.com>
parents:
675
diff
changeset
|
54 |
|
| 253 | 55 |
for ds in relatedds: |
56 |
otqs = TaggedSheet.objects.filter(datasheet_id=ds['id'],order__lte=MAX_TAG).select_related("tag").order_by('order') |
|
57 |
ds['ordered_tags'] = [{ |
|
58 |
'id': t.tag.id, |
|
59 |
'label': t.tag.label, |
|
60 |
'order': t.order, |
|
61 |
'common' : (t.tag.id in tags) |
|
62 |
} for t in otqs] |
|
|
676
111906d4c8b0
correct video embed and new renkan version
ymh <ymh.work@gmail.com>
parents:
675
diff
changeset
|
63 |
|
| 253 | 64 |
return render_to_response( |
65 |
"notice.html", |
|
66 |
{ |
|
67 |
'datasheet':datasheet, |
|
68 |
'domain': domain[0][1] if domain else "", |
|
69 |
'ordered_tags': ordered_tags, |
|
70 |
'related': relatedds, |
|
71 |
}, |
|
72 |
context_instance=RequestContext(request) |
|
73 |
) |
|
|
607
17f3582ecdb1
correct display about page, especially on windows
ymh <ymh.work@gmail.com>
parents:
594
diff
changeset
|
74 |
|
|
17f3582ecdb1
correct display about page, especially on windows
ymh <ymh.work@gmail.com>
parents:
594
diff
changeset
|
75 |
|
|
17f3582ecdb1
correct display about page, especially on windows
ymh <ymh.work@gmail.com>
parents:
594
diff
changeset
|
76 |
class HdalabAboutPage(TemplateView): |
| 693 | 77 |
""" |
78 |
Classe de vue en charge de l'affichage de la page à propos. |
|
79 |
C'est une simple django.views.generic.TemplateView qui ajoute quelques informations dans le contexte de rendu du template. |
|
80 |
""" |
|
|
676
111906d4c8b0
correct video embed and new renkan version
ymh <ymh.work@gmail.com>
parents:
675
diff
changeset
|
81 |
|
|
607
17f3582ecdb1
correct display about page, especially on windows
ymh <ymh.work@gmail.com>
parents:
594
diff
changeset
|
82 |
template_name = "a_propos.html" |
|
676
111906d4c8b0
correct video embed and new renkan version
ymh <ymh.work@gmail.com>
parents:
675
diff
changeset
|
83 |
|
|
607
17f3582ecdb1
correct display about page, especially on windows
ymh <ymh.work@gmail.com>
parents:
594
diff
changeset
|
84 |
def get_context_data(self, **kwargs): |
|
17f3582ecdb1
correct display about page, especially on windows
ymh <ymh.work@gmail.com>
parents:
594
diff
changeset
|
85 |
context = super(HdalabAboutPage, self).get_context_data(**kwargs) |
|
17f3582ecdb1
correct display about page, especially on windows
ymh <ymh.work@gmail.com>
parents:
594
diff
changeset
|
86 |
ua_str = self.request.META.get('HTTP_USER_AGENT', '') |
|
17f3582ecdb1
correct display about page, especially on windows
ymh <ymh.work@gmail.com>
parents:
594
diff
changeset
|
87 |
ua = user_agents.parse(ua_str) |
|
676
111906d4c8b0
correct video embed and new renkan version
ymh <ymh.work@gmail.com>
parents:
675
diff
changeset
|
88 |
context['add_kanji_font'] = (ua.os.family in settings.OLDER_WINDOWS) |
|
111906d4c8b0
correct video embed and new renkan version
ymh <ymh.work@gmail.com>
parents:
675
diff
changeset
|
89 |
context['tutorial_video_urls'] = getattr(settings, 'RENKAN_TUTORIAL_VIDEO_URLS', []) |
|
111906d4c8b0
correct video embed and new renkan version
ymh <ymh.work@gmail.com>
parents:
675
diff
changeset
|
90 |
|
|
607
17f3582ecdb1
correct display about page, especially on windows
ymh <ymh.work@gmail.com>
parents:
594
diff
changeset
|
91 |
return context |