Commit | Line | Data |
---|---|---|
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 | """ | |
18 | This module contains some Mixin classes for the db objects. | |
19 | ||
20 | A bunch of functions on the db objects are really more like | |
21 | "utility functions": They could live outside the classes | |
22 | and be called "by hand" passing the appropiate reference. | |
23 | They usually only use the public API of the object and | |
24 | rarely use database related stuff. | |
25 | ||
26 | These functions now live here and get "mixed in" into the | |
27 | real objects. | |
28 | """ | |
29 | ||
5f8b4ae8 SS |
30 | from werkzeug.utils import cached_property |
31 | ||
814334f6 | 32 | from mediagoblin import mg_globals |
f42e49c3 | 33 | from mediagoblin.auth import lib as auth_lib |
5f8b4ae8 | 34 | from mediagoblin.media_types import get_media_managers, FileTypeNotSupported |
17c23e15 | 35 | from mediagoblin.tools import common, licenses |
e61ab099 | 36 | from mediagoblin.tools.text import cleaned_markdown_conversion |
814334f6 | 37 | from mediagoblin.tools.url import slugify |
f42e49c3 E |
38 | |
39 | ||
40 | class UserMixin(object): | |
41 | def check_login(self, password): | |
42 | """ | |
43 | See if a user can login with this password | |
44 | """ | |
45 | return auth_lib.bcrypt_check_password( | |
46 | password, self.pw_hash) | |
47 | ||
e61ab099 E |
48 | @property |
49 | def bio_html(self): | |
50 | return cleaned_markdown_conversion(self.bio) | |
51 | ||
f42e49c3 E |
52 | |
53 | class MediaEntryMixin(object): | |
814334f6 E |
54 | def generate_slug(self): |
55 | # import this here due to a cyclic import issue | |
56 | # (db.models -> db.mixin -> db.util -> db.models) | |
57 | from mediagoblin.db.util import check_media_slug_used | |
58 | ||
66d9f1b2 SS |
59 | #Is already a slug assigned? Check if it is valid |
60 | if self.slug: | |
61 | self.slug = slugify(self.slug) | |
62 | elif self.title: | |
63 | #assign slug based on title | |
64 | self.slug = slugify(self.title) | |
65 | elif self.id: | |
66 | # Does the object already have an ID? (after adding to the session) | |
67 | self.slug = unicode(self.id) | |
68 | else: | |
69 | # Everything else failed, just use random garbage | |
70 | self.slug = unicode(uuid4())[1:4] | |
814334f6 | 71 | |
66d9f1b2 SS |
72 | while check_media_slug_used(self.uploader, self.slug, self.id): |
73 | # add garbage till it's unique | |
74 | self.slug = self.slug + unicode(uuid4())[1:4] | |
814334f6 | 75 | |
1e72e075 E |
76 | @property |
77 | def description_html(self): | |
78 | """ | |
79 | Rendered version of the description, run through | |
80 | Markdown and cleaned with our cleaning tool. | |
81 | """ | |
82 | return cleaned_markdown_conversion(self.description) | |
83 | ||
f42e49c3 E |
84 | def get_display_media(self, media_map, |
85 | fetch_order=common.DISPLAY_IMAGE_FETCHING_ORDER): | |
86 | """ | |
87 | Find the best media for display. | |
88 | ||
89 | Args: | |
90 | - media_map: a dict like | |
91 | {u'image_size': [u'dir1', u'dir2', u'image.jpg']} | |
92 | - fetch_order: the order we should try fetching images in | |
93 | ||
94 | Returns: | |
95 | (media_size, media_path) | |
96 | """ | |
97 | media_sizes = media_map.keys() | |
98 | ||
99 | for media_size in common.DISPLAY_IMAGE_FETCHING_ORDER: | |
100 | if media_size in media_sizes: | |
101 | return media_map[media_size] | |
102 | ||
103 | def main_mediafile(self): | |
104 | pass | |
105 | ||
3e907d55 E |
106 | @property |
107 | def slug_or_id(self): | |
5c2b8486 | 108 | return (self.slug or self.id) |
3e907d55 | 109 | |
5f8b4ae8 | 110 | |
cb7ae1e4 | 111 | def url_for_self(self, urlgen, **extra_args): |
f42e49c3 E |
112 | """ |
113 | Generate an appropriate url for ourselves | |
114 | ||
5c2b8486 | 115 | Use a slug if we have one, else use our 'id'. |
f42e49c3 E |
116 | """ |
117 | uploader = self.get_uploader | |
118 | ||
3e907d55 E |
119 | return urlgen( |
120 | 'mediagoblin.user_pages.media_home', | |
121 | user=uploader.username, | |
122 | media=self.slug_or_id, | |
123 | **extra_args) | |
f42e49c3 | 124 | |
2e4ad359 SS |
125 | @property |
126 | def thumb_url(self): | |
127 | """Return the thumbnail URL (for usage in templates) | |
128 | Will return either the real thumbnail or a default fallback icon.""" | |
129 | # TODO: implement generic fallback in case MEDIA_MANAGER does | |
130 | # not specify one? | |
131 | if u'thumb' in self.media_files: | |
132 | thumb_url = mg_globals.app.public_store.file_url( | |
133 | self.media_files[u'thumb']) | |
134 | else: | |
df1c4976 | 135 | # No thumbnail in media available. Get the media's |
2e4ad359 | 136 | # MEDIA_MANAGER for the fallback icon and return static URL |
5f8b4ae8 SS |
137 | # Raises FileTypeNotSupported in case no such manager is enabled |
138 | manager = self.media_manager | |
df1c4976 | 139 | thumb_url = mg_globals.app.staticdirector(manager[u'default_thumb']) |
2e4ad359 SS |
140 | return thumb_url |
141 | ||
5f8b4ae8 SS |
142 | @cached_property |
143 | def media_manager(self): | |
144 | """Returns the MEDIA_MANAGER of the media's media_type | |
145 | ||
146 | Raises FileTypeNotSupported in case no such manager is enabled | |
147 | """ | |
148 | # TODO, we should be able to make this a simple lookup rather | |
149 | # than iterating through all media managers. | |
150 | for media_type, manager in get_media_managers(): | |
151 | if media_type == self.media_type: | |
152 | return manager | |
153 | # Not found? Then raise an error | |
154 | raise FileTypeNotSupported( | |
155 | "MediaManager not in enabled types. Check media_types in config?") | |
156 | ||
f42e49c3 E |
157 | def get_fail_exception(self): |
158 | """ | |
159 | Get the exception that's appropriate for this error | |
160 | """ | |
51eb0267 JW |
161 | if self.fail_error: |
162 | return common.import_component(self.fail_error) | |
17c23e15 AW |
163 | |
164 | def get_license_data(self): | |
165 | """Return license dict for requested license""" | |
138a18fd | 166 | return licenses.get_license_by_url(self.license or "") |
feba5c52 | 167 | |
5bad26bc | 168 | def exif_display_iter(self): |
7b82f56b E |
169 | from mediagoblin.tools.exif import USEFUL_TAGS |
170 | ||
5bad26bc E |
171 | if not self.media_data: |
172 | return | |
173 | exif_all = self.media_data.get("exif_all") | |
174 | ||
175 | for key in USEFUL_TAGS: | |
176 | if key in exif_all: | |
177 | yield key, exif_all[key] | |
178 | ||
feba5c52 E |
179 | |
180 | class MediaCommentMixin(object): | |
181 | @property | |
182 | def content_html(self): | |
183 | """ | |
184 | the actual html-rendered version of the comment displayed. | |
185 | Run through Markdown and the HTML cleaner. | |
186 | """ | |
187 | return cleaned_markdown_conversion(self.content) | |
be5be115 AW |
188 | |
189 | ||
190 | class CollectionMixin(object): | |
191 | def generate_slug(self): | |
192 | # import this here due to a cyclic import issue | |
193 | # (db.models -> db.mixin -> db.util -> db.models) | |
194 | from mediagoblin.db.util import check_collection_slug_used | |
195 | ||
196 | self.slug = slugify(self.title) | |
197 | ||
198 | duplicate = check_collection_slug_used(mg_globals.database, | |
199 | self.creator, self.slug, self.id) | |
200 | ||
201 | if duplicate: | |
202 | if self.id is not None: | |
203 | self.slug = u"%s-%s" % (self.id, self.slug) | |
204 | else: | |
205 | self.slug = None | |
206 | ||
207 | @property | |
208 | def description_html(self): | |
209 | """ | |
210 | Rendered version of the description, run through | |
211 | Markdown and cleaned with our cleaning tool. | |
212 | """ | |
213 | return cleaned_markdown_conversion(self.description) | |
214 | ||
215 | @property | |
216 | def slug_or_id(self): | |
5c2b8486 | 217 | return (self.slug or self.id) |
be5be115 AW |
218 | |
219 | def url_for_self(self, urlgen, **extra_args): | |
220 | """ | |
221 | Generate an appropriate url for ourselves | |
222 | ||
5c2b8486 | 223 | Use a slug if we have one, else use our 'id'. |
be5be115 AW |
224 | """ |
225 | creator = self.get_creator | |
226 | ||
227 | return urlgen( | |
256f816f | 228 | 'mediagoblin.user_pages.user_collection', |
be5be115 AW |
229 | user=creator.username, |
230 | collection=self.slug_or_id, | |
231 | **extra_args) | |
232 | ||
6d1e55b2 | 233 | |
be5be115 AW |
234 | class CollectionItemMixin(object): |
235 | @property | |
236 | def note_html(self): | |
237 | """ | |
238 | the actual html-rendered version of the note displayed. | |
239 | Run through Markdown and the HTML cleaner. | |
240 | """ | |
241 | return cleaned_markdown_conversion(self.note) |