| author | ymh <ymh.work@gmail.com> |
| Wed, 11 Apr 2018 12:19:47 +0200 | |
| branch | documentation |
| changeset 693 | 09e00f38d177 |
| parent 686 | 385e3a12ee27 |
| permissions | -rw-r--r-- |
|
458
604b887e70c3
add state history to renkan, correct get into post, generally prepare ground for mail management
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1 |
# -*- coding: utf-8 -*- |
|
604b887e70c3
add state history to renkan, correct get into post, generally prepare ground for mail management
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2 |
''' |
|
604b887e70c3
add state history to renkan, correct get into post, generally prepare ground for mail management
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
3 |
Created on Feb 22, 2015 |
|
604b887e70c3
add state history to renkan, correct get into post, generally prepare ground for mail management
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
4 |
|
|
604b887e70c3
add state history to renkan, correct get into post, generally prepare ground for mail management
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
5 |
@author: ymh |
|
604b887e70c3
add state history to renkan, correct get into post, generally prepare ground for mail management
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
6 |
''' |
| 462 | 7 |
import logging |
| 492 | 8 |
import os |
| 462 | 9 |
|
| 492 | 10 |
from django.conf import settings |
11 |
from django.core.urlresolvers import reverse |
|
|
458
604b887e70c3
add state history to renkan, correct get into post, generally prepare ground for mail management
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
12 |
from django.db import transaction |
| 462 | 13 |
|
|
458
604b887e70c3
add state history to renkan, correct get into post, generally prepare ground for mail management
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
14 |
from hdalab.models.renkan import HdalabRenkanStateTransition |
|
604b887e70c3
add state history to renkan, correct get into post, generally prepare ground for mail management
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
15 |
|
| 462 | 16 |
|
| 492 | 17 |
from subprocess import check_call |
18 |
||
19 |
||
20 |
||
21 |
||
| 462 | 22 |
logger = logging.getLogger(__name__) |
23 |
||
|
458
604b887e70c3
add state history to renkan, correct get into post, generally prepare ground for mail management
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
24 |
@transaction.atomic |
|
604b887e70c3
add state history to renkan, correct get into post, generally prepare ground for mail management
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
25 |
def change_renkan_state(hda_renkan, state, message=None, author=None): |
| 693 | 26 |
""" |
27 |
Change l'état d'un renkan et crée un objet :class:`hdalab.models.HdalabRenkanStateTransition` dans une transaction unique. |
|
28 |
||
29 |
:param:hda_renkan: L'objet :class:`hdalab.models.HdaRenkan`. |
|
30 |
:param:state: L'état cible. |
|
31 |
:param:message: Le message de changement d'état. |
|
32 |
:param:author: L'utilisateur (:class:`hdabo.models.User`). |
|
33 |
""" |
|
|
686
385e3a12ee27
Containerization and various corrections to make it work
ymh <ymh.work@gmail.com>
parents:
618
diff
changeset
|
34 |
|
|
458
604b887e70c3
add state history to renkan, correct get into post, generally prepare ground for mail management
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
35 |
if state != hda_renkan.state: |
|
686
385e3a12ee27
Containerization and various corrections to make it work
ymh <ymh.work@gmail.com>
parents:
618
diff
changeset
|
36 |
|
|
458
604b887e70c3
add state history to renkan, correct get into post, generally prepare ground for mail management
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
37 |
HdalabRenkanStateTransition.objects.create(renkan=hda_renkan, from_state=hda_renkan.state, to_state=state, message=message, author=author) |
|
604b887e70c3
add state history to renkan, correct get into post, generally prepare ground for mail management
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
38 |
hda_renkan.state = state |
| 462 | 39 |
hda_renkan.save() |
|
686
385e3a12ee27
Containerization and various corrections to make it work
ymh <ymh.work@gmail.com>
parents:
618
diff
changeset
|
40 |
|
| 492 | 41 |
|
42 |
def renkan_capture_preview(hdalab_renkan): |
|
| 693 | 43 |
""" |
44 |
Lance la création d'une miniature pour un renkan. |
|
45 |
Ceci est fait en ouvrant l'url `renkan_full` (:ref:`affichage-d-un-renkan-en-plein-ecran`) avec PhantomJS, et en déclenchant une capture "d'écran". |
|
46 |
||
47 |
:param:hdalab_renkan: Une instance de :class:`hdalab.models.renkan.HdalabRenkan`. |
|
48 |
||
49 |
""" |
|
| 492 | 50 |
#get last state date or last modification date |
51 |
#states are ordered by ts |
|
52 |
folder_date = hdalab_renkan.renkan.modification_date |
|
53 |
states = hdalab_renkan.states |
|
54 |
state = None |
|
55 |
if states.count() > 0: |
|
56 |
state = states.all()[0] |
|
57 |
if state: |
|
58 |
folder_date = state.ts |
|
59 |
||
60 |
rel_export_path_dir = "thumbnails/renkan/%04d/%02d/%02d" % (folder_date.year, folder_date.month, folder_date.day) |
|
61 |
export_path_dir = os.path.join(settings.MEDIA_ROOT,rel_export_path_dir) |
|
62 |
export_filename = "%s.png" % hdalab_renkan.renkan.rk_id |
|
|
686
385e3a12ee27
Containerization and various corrections to make it work
ymh <ymh.work@gmail.com>
parents:
618
diff
changeset
|
63 |
|
| 492 | 64 |
export_path = os.path.join(export_path_dir, export_filename) |
65 |
rel_export_path = os.path.join(rel_export_path_dir, export_filename) |
|
|
686
385e3a12ee27
Containerization and various corrections to make it work
ymh <ymh.work@gmail.com>
parents:
618
diff
changeset
|
66 |
|
| 492 | 67 |
if not os.path.exists(export_path_dir): |
| 618 | 68 |
try: |
69 |
os.makedirs(export_path_dir) |
|
70 |
except OSError, e: |
|
71 |
if e.errno != 17: |
|
|
686
385e3a12ee27
Containerization and various corrections to make it work
ymh <ymh.work@gmail.com>
parents:
618
diff
changeset
|
72 |
raise |
| 618 | 73 |
# time.sleep might help here |
74 |
pass |
|
|
686
385e3a12ee27
Containerization and various corrections to make it work
ymh <ymh.work@gmail.com>
parents:
618
diff
changeset
|
75 |
|
| 492 | 76 |
preview_dim = getattr(settings, 'RENKAN_PREVIEW_DIM', (500,500)) |
77 |
preview_wait = getattr(settings, 'RENKAN_PREVIEW_WAIT', 5000) |
|
78 |
||
79 |
preview_args = [ |
|
80 |
getattr(settings,'RENKAN_PREVIEW_PHANTOMJS_PATH','phantomjs'), |
|
81 |
os.path.join(os.path.dirname(os.path.abspath(__file__)),'scripts/capture-phantomjs.js'), |
|
|
686
385e3a12ee27
Containerization and various corrections to make it work
ymh <ymh.work@gmail.com>
parents:
618
diff
changeset
|
82 |
"%s%s%s?rk_id=%s" % (getattr(settings, 'FRONT_WEB_URL', settings.WEB_URL), settings.BASE_URL, reverse('renkan_full'),hdalab_renkan.renkan.rk_id), |
| 492 | 83 |
export_path, |
84 |
"--width=%d" % preview_dim[0], |
|
85 |
"--height=%d" % preview_dim[1], |
|
86 |
"--wait=%d" % preview_wait |
|
87 |
] |
|
88 |
check_call(preview_args) |
|
|
686
385e3a12ee27
Containerization and various corrections to make it work
ymh <ymh.work@gmail.com>
parents:
618
diff
changeset
|
89 |
|
| 492 | 90 |
hdalab_renkan.renkan.image = rel_export_path |
91 |
hdalab_renkan.renkan.save() |
|
92 |