Moving the get_original_date method over to the MediaManager.
This fixes a bug so that it doesn't explode if the media_data doesn't exist. This commit sponsored by Ole-Morten Duesund. Thank you!
This commit is contained in:
parent
34d304441d
commit
33ddf4b1ef
@ -14,6 +14,8 @@
|
|||||||
# You should have received a copy of the GNU Affero General Public License
|
# You should have received a copy of the GNU Affero General Public License
|
||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
import datetime
|
||||||
|
|
||||||
from mediagoblin.media_types import MediaManagerBase
|
from mediagoblin.media_types import MediaManagerBase
|
||||||
from mediagoblin.media_types.image.processing import process_image, \
|
from mediagoblin.media_types.image.processing import process_image, \
|
||||||
sniff_handler
|
sniff_handler
|
||||||
@ -28,5 +30,26 @@ class ImageMediaManager(MediaManagerBase):
|
|||||||
accepted_extensions = ["jpg", "jpeg", "png", "gif", "tiff"]
|
accepted_extensions = ["jpg", "jpeg", "png", "gif", "tiff"]
|
||||||
media_fetch_order = [u'medium', u'original', u'thumb']
|
media_fetch_order = [u'medium', u'original', u'thumb']
|
||||||
|
|
||||||
|
def get_original_date(self):
|
||||||
|
"""
|
||||||
|
Get the original date and time from the EXIF information. Returns
|
||||||
|
either a datetime object or None (if anything goes wrong)
|
||||||
|
"""
|
||||||
|
if not self.entry.media_data or not self.entry.media_data.exif_all:
|
||||||
|
return None
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Try wrapped around all since exif_all might be none,
|
||||||
|
# EXIF DateTimeOriginal or printable might not exist
|
||||||
|
# or strptime might not be able to parse date correctly
|
||||||
|
exif_date = self.entry.media_data.exif_all[
|
||||||
|
'EXIF DateTimeOriginal']['printable']
|
||||||
|
original_date = datetime.datetime.strptime(
|
||||||
|
exif_date,
|
||||||
|
'%Y:%m:%d %H:%M:%S')
|
||||||
|
return original_date
|
||||||
|
except (KeyError, ValueError):
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
MEDIA_MANAGER = ImageMediaManager
|
MEDIA_MANAGER = ImageMediaManager
|
||||||
|
@ -44,23 +44,6 @@ class ImageData(Base):
|
|||||||
gps_altitude = Column(Float)
|
gps_altitude = Column(Float)
|
||||||
gps_direction = Column(Float)
|
gps_direction = Column(Float)
|
||||||
|
|
||||||
def get_original_date(self):
|
|
||||||
"""
|
|
||||||
Get the original date and time from the EXIF information. Returns
|
|
||||||
either a datetime object or None (if anything goes wrong)
|
|
||||||
"""
|
|
||||||
|
|
||||||
import datetime
|
|
||||||
try:
|
|
||||||
# Try wrapped around all since exif_all might be none,
|
|
||||||
# EXIF DateTimeOriginal or printable might not exist, or
|
|
||||||
# strptime might not be able to parse date correctly
|
|
||||||
exif_date = self.exif_all['EXIF DateTimeOriginal']['printable']
|
|
||||||
original_date = datetime.datetime.strptime(exif_date,
|
|
||||||
'%Y:%m:%d %H:%M:%S')
|
|
||||||
return original_date
|
|
||||||
except:
|
|
||||||
return None
|
|
||||||
|
|
||||||
DATA_MODEL = ImageData
|
DATA_MODEL = ImageData
|
||||||
MODELS = [ImageData]
|
MODELS = [ImageData]
|
||||||
|
@ -155,7 +155,7 @@
|
|||||||
</span></p>
|
</span></p>
|
||||||
|
|
||||||
{% if app_config['original_date_visible'] %}
|
{% if app_config['original_date_visible'] %}
|
||||||
{% set original_date = media.media_data.get_original_date() %}
|
{% set original_date = media.media_manager.get_original_date() %}
|
||||||
|
|
||||||
{% if original_date %}
|
{% if original_date %}
|
||||||
<h3>{% trans %}Created{% endtrans %}</h3>
|
<h3>{% trans %}Created{% endtrans %}</h3>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user