image_mediadata: Add exif_all column
[mediagoblin.git] / mediagoblin / db / mixin.py
CommitLineData
f42e49c3 1# GNU MediaGoblin -- federated, autonomous media hosting
7f4ebeed 2# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
f42e49c3
E
3#
4# This program is free software: you can redistribute it and/or modify
5# it under the terms of the GNU Affero General Public License as published by
6# the Free Software Foundation, either version 3 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU Affero General Public License for more details.
13#
14# You should have received a copy of the GNU Affero General Public License
15# along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17"""
18This module contains some Mixin classes for the db objects.
19
20A bunch of functions on the db objects are really more like
21"utility functions": They could live outside the classes
22and be called "by hand" passing the appropiate reference.
23They usually only use the public API of the object and
24rarely use database related stuff.
25
26These functions now live here and get "mixed in" into the
27real objects.
28"""
29
814334f6 30from mediagoblin import mg_globals
f42e49c3 31from mediagoblin.auth import lib as auth_lib
17c23e15 32from mediagoblin.tools import common, licenses
e61ab099 33from mediagoblin.tools.text import cleaned_markdown_conversion
814334f6 34from mediagoblin.tools.url import slugify
f42e49c3
E
35
36
37class UserMixin(object):
38 def check_login(self, password):
39 """
40 See if a user can login with this password
41 """
42 return auth_lib.bcrypt_check_password(
43 password, self.pw_hash)
44
e61ab099
E
45 @property
46 def bio_html(self):
47 return cleaned_markdown_conversion(self.bio)
48
f42e49c3
E
49
50class MediaEntryMixin(object):
814334f6
E
51 def generate_slug(self):
52 # import this here due to a cyclic import issue
53 # (db.models -> db.mixin -> db.util -> db.models)
54 from mediagoblin.db.util import check_media_slug_used
55
56 self.slug = slugify(self.title)
57
58 duplicate = check_media_slug_used(mg_globals.database,
59 self.uploader, self.slug, self.id)
60
61 if duplicate:
62 if self.id is not None:
63 self.slug = "%s-%s" % (self.id, self.slug)
64 else:
65 self.slug = None
66
1e72e075
E
67 @property
68 def description_html(self):
69 """
70 Rendered version of the description, run through
71 Markdown and cleaned with our cleaning tool.
72 """
73 return cleaned_markdown_conversion(self.description)
74
f42e49c3
E
75 def get_display_media(self, media_map,
76 fetch_order=common.DISPLAY_IMAGE_FETCHING_ORDER):
77 """
78 Find the best media for display.
79
80 Args:
81 - media_map: a dict like
82 {u'image_size': [u'dir1', u'dir2', u'image.jpg']}
83 - fetch_order: the order we should try fetching images in
84
85 Returns:
86 (media_size, media_path)
87 """
88 media_sizes = media_map.keys()
89
90 for media_size in common.DISPLAY_IMAGE_FETCHING_ORDER:
91 if media_size in media_sizes:
92 return media_map[media_size]
93
94 def main_mediafile(self):
95 pass
96
3e907d55
E
97 @property
98 def slug_or_id(self):
99 return (self.slug or self._id)
100
cb7ae1e4 101 def url_for_self(self, urlgen, **extra_args):
f42e49c3
E
102 """
103 Generate an appropriate url for ourselves
104
105 Use a slug if we have one, else use our '_id'.
106 """
107 uploader = self.get_uploader
108
3e907d55
E
109 return urlgen(
110 'mediagoblin.user_pages.media_home',
111 user=uploader.username,
112 media=self.slug_or_id,
113 **extra_args)
f42e49c3
E
114
115 def get_fail_exception(self):
116 """
117 Get the exception that's appropriate for this error
118 """
119 if self['fail_error']:
120 return common.import_component(self['fail_error'])
17c23e15
AW
121
122 def get_license_data(self):
123 """Return license dict for requested license"""
2788e6a1 124 return licenses.SUPPORTED_LICENSES[self.license or ""]
feba5c52
E
125
126
127class MediaCommentMixin(object):
128 @property
129 def content_html(self):
130 """
131 the actual html-rendered version of the comment displayed.
132 Run through Markdown and the HTML cleaner.
133 """
134 return cleaned_markdown_conversion(self.content)