- Timestamp:
- Feb 11, 2019, 6:55:55 PM (5 years ago)
- Branches:
- current, feature/docs, master
- Children:
- 02aee97
- Parents:
- 93bbb89
- Location:
- ow
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
ow/models/workout.py
r93bbb89 rd6f8304 423 423 return self.fit_file is not None 424 424 425 @property 426 def map_screenshot(self): 425 def map_screenshot(self, request): 427 426 """ 428 427 Return the static path to the screenshot image of the map for … … 432 431 return None 433 432 433 screenshot_name = os.path.join( 434 str(self.owner.uid), str(self.workout_id) + '.png') 434 435 current_path = os.path.abspath(os.path.dirname(__file__)) 435 436 screenshot_path = os.path.join( 436 current_path, '../static/maps', 437 str(self.owner.uid), str(self.workout_id)) + '.png' 437 current_path, '../static/maps', screenshot_name) 438 438 439 439 if not os.path.exists(screenshot_path): 440 440 # screenshot does not exist, generate it 441 save_map_screenshot(self) 442 443 # the value returned is relative to the static files served 444 # by the app, so we can use request.static_url() with it 445 static_path = os.path.join('static/maps', str(self.owner.uid), 446 str(self.workout_id)) 447 return 'ow:' + static_path + '.png' 441 save_map_screenshot(self, request) 442 443 static_path = os.path.join('static/maps', screenshot_name) 444 return request.static_url('ow:' + static_path) -
ow/templates/dashboard.pt
r93bbb89 rd6f8304 80 80 <div class="workout-map" tal:condition="workout.has_gpx"> 81 81 <a href="" tal:attributes="href request.resource_url(workout)"> 82 <img src="" tal:attributes="src request.static_url(workout.map_screenshot);82 <img src="" tal:attributes="src workout.map_screenshot(request); 83 83 alt workout.title; title workout.title"> 84 84 </a> -
ow/templates/profile.pt
r93bbb89 rd6f8304 144 144 <div class="workout-map" tal:condition="workout.has_gpx"> 145 145 <a href="" tal:attributes="href request.resource_url(workout)"> 146 <img src="" tal:attributes="src request.static_url(workout.map_screenshot);146 <img src="" tal:attributes="src workout.map_screenshot(request); 147 147 alt workout.title; title workout.title"> 148 148 </a> -
ow/tests/models/test_workout.py
r93bbb89 rd6f8304 7 7 import pytest 8 8 from pyramid.security import Allow, Everyone, Deny, ALL_PERMISSIONS 9 from pyramid.testing import DummyRequest 9 10 10 11 from ow.models.workout import Workout … … 576 577 def test_map_screenshot_no_gpx(self, sms, os, root): 577 578 workout = root['john']['1'] 578 assert workout.map_screenshot is None 579 request = DummyRequest() 580 assert workout.map_screenshot(request) is None 579 581 assert not os.path.abspath.called 580 582 assert not os.path.dirname.called … … 602 604 603 605 uid = str(root['john'].uid) 604 assert workout.map_screenshot == 'ow:/static/maps/' + uid + '/1.png' 606 request = DummyRequest() 607 # dummyrequest can't resolve static assets without adding a lot 608 # of boilerplate, no need for that here 609 request.static_url = Mock() 610 request.static_url.return_value = 'ow:/static/maps/' + uid + '/1.png' 611 res = workout.map_screenshot(request) 612 assert res == 'ow:/static/maps/' + uid + '/1.png' 605 613 assert os.path.abspath.called 606 614 assert os.path.dirname.called 607 assert os.path.join.call_count == 2615 assert os.path.join.call_count == 3 608 616 assert os.path.exists.called 609 sms.assert_called_once_with(workout )617 sms.assert_called_once_with(workout, request) 610 618 611 619 @patch('ow.models.workout.os') … … 619 627 os.path.join.side_effect = join 620 628 # This forces the "save screenshot" code NOT to be run 621 os.path.e xists.return_value = True629 os.path.eisxts.return_value = True 622 630 623 631 workout = root['john']['1'] … … 626 634 627 635 uid = str(root['john'].uid) 628 assert workout.map_screenshot == 'ow:/static/maps/' + uid + '/1.png' 636 request = DummyRequest() 637 # dummyrequest can't resolve static assets without adding a lot 638 # of boilerplate, no need for that here 639 request.static_url = Mock() 640 request.static_url.return_value = 'ow:/static/maps/' + uid + '/1.png' 641 res = workout.map_screenshot(request) 642 assert res == 'ow:/static/maps/' + uid + '/1.png' 629 643 assert os.path.abspath.called 630 644 assert os.path.dirname.called 631 assert os.path.join.call_count == 2645 assert os.path.join.call_count == 3 632 646 assert os.path.exists.called 633 647 assert not sms.called -
ow/tests/test_utilities.py
r93bbb89 rd6f8304 1 1 import os 2 2 from datetime import timedelta, datetime 3 from unittest.mock import patch 3 from unittest.mock import patch, Mock 4 4 from pyexpat import ExpatError 5 5 from xml.dom.minidom import Element 6 6 7 7 import pytest 8 from pyramid.testing import DummyRequest 8 9 9 10 from ow.models.root import OpenWorkouts … … 83 84 assert kmph_to_mps(30) == 30 * 0.277778 84 85 86 @patch('ow.utilities.shutil') 85 87 @patch('ow.utilities.os') 86 @patch('ow.utilities.subprocess') 87 def test_save_map_screenshot_no_gpx(self, subprocess, os, root, john): 88 saved = save_map_screenshot(john['1']) 88 @patch('ow.utilities.Browser') 89 def test_save_map_screenshot_no_gpx( 90 self, Browser, os, shutil, root, john): 91 request = DummyRequest() 92 saved = save_map_screenshot(john['1'], request) 89 93 assert not saved 94 assert not Browser.called 90 95 assert not os.path.abspath.called 91 96 assert not os.path.dirname.called … … 93 98 assert not os.path.exists.called 94 99 assert not os.makedirs.called 95 assert not s ubprocess.run.called100 assert not shutil.move.called 96 101 # even having a fit tracking file, nothing is done 97 102 john['1'].tracking_file = 'faked fit file' 98 103 john['1'].tracking_filetype = 'fit' 99 saved = save_map_screenshot(john['1'] )104 saved = save_map_screenshot(john['1'], request) 100 105 assert not saved 106 assert not Browser.called 101 107 assert not os.path.abspath.called 102 108 assert not os.path.dirname.called … … 104 110 assert not os.path.exists.called 105 111 assert not os.makedirs.called 106 assert not subprocess.run.called 107 112 assert not shutil.move.called 113 114 @patch('ow.utilities.shutil') 108 115 @patch('ow.utilities.os') 109 @patch('ow.utilities.subprocess') 110 def test_save_map_screenshot_with_gpx(self, subprocess, os, root, john): 116 @patch('ow.utilities.Browser') 117 def test_save_map_screenshot_with_gpx( 118 self, Browser, os, shutil, root, john): 119 request = DummyRequest() 120 browser = Mock() 121 Browser.return_value = browser 111 122 os.path.abspath.return_value = 'current_dir' 112 123 os.path.join.side_effect = join … … 116 127 os.path.exists.return_value = False 117 128 129 map_url = request.resource_url(john['1'], 'map') 130 118 131 john['1'].tracking_file = 'faked gpx content' 119 132 john['1'].tracking_filetype = 'gpx' 120 saved = save_map_screenshot(john['1'] )133 saved = save_map_screenshot(john['1'], request) 121 134 assert saved 135 Browser.assert_called_once_with('chrome', headless=True) 136 browser.driver.set_window_size.assert_called_once_with(1300, 436) 137 browser.visit.assert_called_once_with(map_url) 138 browser.screenshot.assert_called_once 122 139 os.path.abspath.assert_called_once 123 140 assert os.path.dirname.called 124 assert os.path.join.call_count == 3141 assert os.path.join.call_count == 2 125 142 assert os.path.exists.called 126 143 assert os.makedirs.called 127 subprocess.run.assert_called_once 128 144 os.shutil.move.assert_called_once 145 146 @patch('ow.utilities.shutil') 129 147 @patch('ow.utilities.os') 130 @patch('ow.utilities. subprocess')148 @patch('ow.utilities.Browser') 131 149 def test_save_map_screenshot_with_gpx_makedirs( 132 self, subprocess, os, root, john): 150 self, Browser, os, shutil, root, john): 151 request = DummyRequest() 152 browser = Mock() 153 Browser.return_value = browser 133 154 os.path.abspath.return_value = 'current_dir' 134 155 os.path.join.side_effect = join … … 136 157 os.path.exists.return_value = True 137 158 159 map_url = request.resource_url(john['1'], 'map') 160 138 161 john['1'].tracking_file = 'faked gpx content' 139 162 john['1'].tracking_filetype = 'gpx' 140 saved = save_map_screenshot(john['1'] )163 saved = save_map_screenshot(john['1'], request) 141 164 assert saved 165 Browser.assert_called_once_with('chrome', headless=True) 166 browser.driver.set_window_size.assert_called_once_with(1300, 436) 167 browser.visit.assert_called_once_with(map_url) 168 browser.screenshot.assert_called_once 142 169 os.path.abspath.assert_called_once 143 170 assert os.path.dirname.called 144 assert os.path.join.call_count == 3171 assert os.path.join.call_count == 2 145 172 assert os.path.exists.called 146 173 assert not os.makedirs.called 147 subprocess.run.assert_called_once174 os.shutil.move.assert_called_once 148 175 149 176 def test_timedelta_to_hms(self): -
ow/utilities.py
r93bbb89 rd6f8304 3 3 import logging 4 4 import calendar 5 import subprocess 5 import shutil 6 import time 6 7 from datetime import datetime, timedelta 7 8 from decimal import Decimal … … 11 12 from xml.dom import minidom 12 13 from ZODB.blob import Blob 14 from splinter import Browser 15 13 16 14 17 log = logging.getLogger(__name__) … … 192 195 193 196 194 def save_map_screenshot(workout): 197 def save_map_screenshot(workout, request): 198 195 199 if workout.has_gpx: 200 201 map_url = request.resource_url(workout, 'map') 202 203 browser = Browser('chrome', headless=True) 204 browser.driver.set_window_size(1300, 436) 205 206 browser.visit(map_url) 207 # we need to wait a moment before taking the screenshot, to ensure 208 # all tiles are loaded in the map. 209 time.sleep(5) 210 211 # splinter saves the screenshot with a random name (even if we do 212 # provide a name) so we get the path to that file and later on we 213 # move it to the proper place 214 splinter_screenshot_path = browser.screenshot() 215 196 216 current_path = os.path.abspath(os.path.dirname(__file__)) 197 tool_path = os.path.join(current_path, '../bin/screenshot_map')198 199 217 screenshots_path = os.path.join( 200 218 current_path, 'static/maps', str(workout.owner.uid)) … … 206 224 screenshot_path += '.png' 207 225 208 subprocess.run( 209 [tool_path, str(workout.owner.uid), str(workout.workout_id), 210 screenshot_path], stdout=subprocess.PIPE, stderr=subprocess.PIPE) 226 shutil.move(splinter_screenshot_path, screenshot_path) 211 227 212 228 return True
Note: See TracChangeset
for help on using the changeset viewer.