3 Created on Feb 22, 2015 |
3 Created on Feb 22, 2015 |
4 |
4 |
5 @author: ymh |
5 @author: ymh |
6 ''' |
6 ''' |
7 import logging |
7 import logging |
|
8 import os |
8 |
9 |
|
10 from django.conf import settings |
|
11 from django.core.urlresolvers import reverse |
9 from django.db import transaction |
12 from django.db import transaction |
10 |
13 |
11 from hdalab.models.renkan import HdalabRenkanStateTransition |
14 from hdalab.models.renkan import HdalabRenkanStateTransition |
|
15 |
|
16 |
|
17 from subprocess import check_call |
|
18 |
|
19 |
12 |
20 |
13 |
21 |
14 logger = logging.getLogger(__name__) |
22 logger = logging.getLogger(__name__) |
15 |
23 |
16 @transaction.atomic |
24 @transaction.atomic |
20 |
28 |
21 HdalabRenkanStateTransition.objects.create(renkan=hda_renkan, from_state=hda_renkan.state, to_state=state, message=message, author=author) |
29 HdalabRenkanStateTransition.objects.create(renkan=hda_renkan, from_state=hda_renkan.state, to_state=state, message=message, author=author) |
22 hda_renkan.state = state |
30 hda_renkan.state = state |
23 hda_renkan.save() |
31 hda_renkan.save() |
24 |
32 |
|
33 |
|
34 def renkan_capture_preview(hdalab_renkan): |
|
35 #get last state date or last modification date |
|
36 #states are ordered by ts |
|
37 folder_date = hdalab_renkan.renkan.modification_date |
|
38 states = hdalab_renkan.states |
|
39 state = None |
|
40 if states.count() > 0: |
|
41 state = states.all()[0] |
|
42 if state: |
|
43 folder_date = state.ts |
|
44 |
|
45 rel_export_path_dir = "thumbnails/renkan/%04d/%02d/%02d" % (folder_date.year, folder_date.month, folder_date.day) |
|
46 export_path_dir = os.path.join(settings.MEDIA_ROOT,rel_export_path_dir) |
|
47 export_filename = "%s.png" % hdalab_renkan.renkan.rk_id |
|
48 |
|
49 export_path = os.path.join(export_path_dir, export_filename) |
|
50 rel_export_path = os.path.join(rel_export_path_dir, export_filename) |
|
51 |
|
52 if not os.path.exists(export_path_dir): |
|
53 os.makedirs(export_path_dir) |
|
54 |
|
55 preview_dim = getattr(settings, 'RENKAN_PREVIEW_DIM', (500,500)) |
|
56 preview_wait = getattr(settings, 'RENKAN_PREVIEW_WAIT', 5000) |
|
57 |
|
58 preview_args = [ |
|
59 getattr(settings,'RENKAN_PREVIEW_PHANTOMJS_PATH','phantomjs'), |
|
60 os.path.join(os.path.dirname(os.path.abspath(__file__)),'scripts/capture-phantomjs.js'), |
|
61 "%s%shdalab%s?rk_id=%s" % (settings.WEB_URL, settings.BASE_URL, reverse('renkan_full'),hdalab_renkan.renkan.rk_id), |
|
62 export_path, |
|
63 "--width=%d" % preview_dim[0], |
|
64 "--height=%d" % preview_dim[1], |
|
65 "--wait=%d" % preview_wait |
|
66 ] |
|
67 check_call(preview_args) |
|
68 |
|
69 hdalab_renkan.renkan.image = rel_export_path |
|
70 hdalab_renkan.renkan.save() |
|
71 |