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:
Christopher Allan Webber 2013-05-11 17:02:26 -05:00
parent 34d304441d
commit 33ddf4b1ef
3 changed files with 24 additions and 18 deletions

View File

@ -14,6 +14,8 @@
# 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/>.
import datetime
from mediagoblin.media_types import MediaManagerBase
from mediagoblin.media_types.image.processing import process_image, \
sniff_handler
@ -28,5 +30,26 @@ class ImageMediaManager(MediaManagerBase):
accepted_extensions = ["jpg", "jpeg", "png", "gif", "tiff"]
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

View File

@ -44,23 +44,6 @@ class ImageData(Base):
gps_altitude = 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
MODELS = [ImageData]

View File

@ -155,7 +155,7 @@
</span></p>
{% 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 %}
<h3>{% trans %}Created{% endtrans %}</h3>