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 | ||
a81082fc | 30 | import uuid |
72bb46c7 | 31 | |
5f8b4ae8 SS |
32 | from werkzeug.utils import cached_property |
33 | ||
814334f6 | 34 | from mediagoblin import mg_globals |
f42e49c3 | 35 | from mediagoblin.auth import lib as auth_lib |
5f8b4ae8 | 36 | from mediagoblin.media_types import get_media_managers, FileTypeNotSupported |
17c23e15 | 37 | from mediagoblin.tools import common, licenses |
e61ab099 | 38 | from mediagoblin.tools.text import cleaned_markdown_conversion |
814334f6 | 39 | from mediagoblin.tools.url import slugify |
f42e49c3 E |
40 | |
41 | ||
42 | class UserMixin(object): | |
43 | def check_login(self, password): | |
44 | """ | |
45 | See if a user can login with this password | |
46 | """ | |
47 | return auth_lib.bcrypt_check_password( | |
48 | password, self.pw_hash) | |
49 | ||
e61ab099 E |
50 | @property |
51 | def bio_html(self): | |
52 | return cleaned_markdown_conversion(self.bio) | |
53 | ||
f42e49c3 E |
54 | |
55 | class MediaEntryMixin(object): | |
814334f6 | 56 | def generate_slug(self): |
88de830f | 57 | """ |
98587109 CAW |
58 | Generate a unique slug for this MediaEntry. |
59 | ||
88de830f CAW |
60 | This one does not *force* slugs, but usually it will probably result |
61 | in a niceish one. | |
62 | ||
98587109 CAW |
63 | The end *result* of the algorithm will result in these resolutions for |
64 | these situations: | |
88de830f CAW |
65 | - If we have a slug, make sure it's clean and sanitized, and if it's |
66 | unique, we'll use that. | |
67 | - If we have a title, slugify it, and if it's unique, we'll use that. | |
68 | - If we can't get any sort of thing that looks like it'll be a useful | |
69 | slug out of a title or an existing slug, bail, and don't set the | |
70 | slug at all. Don't try to create something just because. Make | |
71 | sure we have a reasonable basis for a slug first. | |
72 | - If we have a reasonable basis for a slug (either based on existing | |
73 | slug or slugified title) but it's not unique, first try appending | |
74 | the entry's id, if that exists | |
75 | - If that doesn't result in something unique, tack on some randomly | |
76 | generated bits until it's unique. That'll be a little bit of junk, | |
77 | but at least it has the basis of a nice slug. | |
78 | """ | |
814334f6 E |
79 | # import this here due to a cyclic import issue |
80 | # (db.models -> db.mixin -> db.util -> db.models) | |
81 | from mediagoblin.db.util import check_media_slug_used | |
82 | ||
66d9f1b2 SS |
83 | #Is already a slug assigned? Check if it is valid |
84 | if self.slug: | |
85 | self.slug = slugify(self.slug) | |
88de830f CAW |
86 | |
87 | # otherwise, try to use the title. | |
66d9f1b2 | 88 | elif self.title: |
88de830f | 89 | # assign slug based on title |
66d9f1b2 | 90 | self.slug = slugify(self.title) |
88de830f | 91 | |
b0118957 CAW |
92 | # We don't want any empty string slugs |
93 | if self.slug == u"": | |
94 | self.slug = None | |
95 | ||
88de830f CAW |
96 | # Do we have anything at this point? |
97 | # If not, we're not going to get a slug | |
98 | # so just return... we're not going to force one. | |
99 | if not self.slug: | |
100 | return # giving up! | |
101 | ||
102 | # Otherwise, let's see if this is unique. | |
103 | if check_media_slug_used(self.uploader, self.slug, self.id): | |
104 | # It looks like it's being used... lame. | |
105 | ||
106 | # Can we just append the object's id to the end? | |
107 | if self.id: | |
98587109 | 108 | slug_with_id = u"%s-%s" % (self.slug, self.id) |
88de830f CAW |
109 | if not check_media_slug_used(self.uploader, |
110 | slug_with_id, self.id): | |
111 | self.slug = slug_with_id | |
112 | return # success! | |
113 | ||
114 | # okay, still no success; | |
115 | # let's whack junk on there till it's unique. | |
a81082fc CAW |
116 | self.slug += '-' + uuid.uuid4().hex[:4] |
117 | # keep going if necessary! | |
88de830f | 118 | while check_media_slug_used(self.uploader, self.slug, self.id): |
a81082fc | 119 | self.slug += uuid.uuid4().hex[:4] |
814334f6 | 120 | |
1e72e075 E |
121 | @property |
122 | def description_html(self): | |
123 | """ | |
124 | Rendered version of the description, run through | |
125 | Markdown and cleaned with our cleaning tool. | |
126 | """ | |
127 | return cleaned_markdown_conversion(self.description) | |
128 | ||
ddbf6af1 | 129 | def get_display_media(self, fetch_order=None): |
f42e49c3 E |
130 | """ |
131 | Find the best media for display. | |
132 | ||
133 | Args: | |
ddbf6af1 CAW |
134 | - fetch_order: the order we should try fetching images in. |
135 | If this isn't supplied, we try checking | |
136 | self.media_data.fetching_order if it exists. | |
f42e49c3 E |
137 | |
138 | Returns: | |
ddbf6af1 CAW |
139 | (media_size, media_path) |
140 | or, if not found, None. | |
f42e49c3 | 141 | """ |
ddbf6af1 | 142 | fetch_order = self.media_manager.get("media_fetch_order") |
f42e49c3 | 143 | |
ddbf6af1 CAW |
144 | # No fetching order found? well, give up! |
145 | if not fetch_order: | |
146 | return None | |
147 | ||
148 | media_sizes = self.media_files.keys() | |
149 | ||
150 | for media_size in fetch_order: | |
f42e49c3 | 151 | if media_size in media_sizes: |
ddbf6af1 | 152 | return media_size, self.media_files[media_size] |
f42e49c3 E |
153 | |
154 | def main_mediafile(self): | |
155 | pass | |
156 | ||
3e907d55 E |
157 | @property |
158 | def slug_or_id(self): | |
7de20e52 CAW |
159 | if self.slug: |
160 | return self.slug | |
161 | else: | |
162 | return u'id:%s' % self.id | |
5f8b4ae8 | 163 | |
cb7ae1e4 | 164 | def url_for_self(self, urlgen, **extra_args): |
f42e49c3 E |
165 | """ |
166 | Generate an appropriate url for ourselves | |
167 | ||
5c2b8486 | 168 | Use a slug if we have one, else use our 'id'. |
f42e49c3 E |
169 | """ |
170 | uploader = self.get_uploader | |
171 | ||
3e907d55 E |
172 | return urlgen( |
173 | 'mediagoblin.user_pages.media_home', | |
174 | user=uploader.username, | |
175 | media=self.slug_or_id, | |
176 | **extra_args) | |
f42e49c3 | 177 | |
2e4ad359 SS |
178 | @property |
179 | def thumb_url(self): | |
180 | """Return the thumbnail URL (for usage in templates) | |
181 | Will return either the real thumbnail or a default fallback icon.""" | |
182 | # TODO: implement generic fallback in case MEDIA_MANAGER does | |
183 | # not specify one? | |
184 | if u'thumb' in self.media_files: | |
185 | thumb_url = mg_globals.app.public_store.file_url( | |
186 | self.media_files[u'thumb']) | |
187 | else: | |
df1c4976 | 188 | # No thumbnail in media available. Get the media's |
2e4ad359 | 189 | # MEDIA_MANAGER for the fallback icon and return static URL |
5f8b4ae8 SS |
190 | # Raises FileTypeNotSupported in case no such manager is enabled |
191 | manager = self.media_manager | |
df1c4976 | 192 | thumb_url = mg_globals.app.staticdirector(manager[u'default_thumb']) |
2e4ad359 SS |
193 | return thumb_url |
194 | ||
5f8b4ae8 SS |
195 | @cached_property |
196 | def media_manager(self): | |
197 | """Returns the MEDIA_MANAGER of the media's media_type | |
198 | ||
199 | Raises FileTypeNotSupported in case no such manager is enabled | |
200 | """ | |
201 | # TODO, we should be able to make this a simple lookup rather | |
202 | # than iterating through all media managers. | |
203 | for media_type, manager in get_media_managers(): | |
204 | if media_type == self.media_type: | |
205 | return manager | |
206 | # Not found? Then raise an error | |
207 | raise FileTypeNotSupported( | |
208 | "MediaManager not in enabled types. Check media_types in config?") | |
209 | ||
f42e49c3 E |
210 | def get_fail_exception(self): |
211 | """ | |
212 | Get the exception that's appropriate for this error | |
213 | """ | |
51eb0267 JW |
214 | if self.fail_error: |
215 | return common.import_component(self.fail_error) | |
17c23e15 AW |
216 | |
217 | def get_license_data(self): | |
218 | """Return license dict for requested license""" | |
138a18fd | 219 | return licenses.get_license_by_url(self.license or "") |
feba5c52 | 220 | |
5bad26bc | 221 | def exif_display_iter(self): |
7b82f56b E |
222 | from mediagoblin.tools.exif import USEFUL_TAGS |
223 | ||
5bad26bc E |
224 | if not self.media_data: |
225 | return | |
226 | exif_all = self.media_data.get("exif_all") | |
227 | ||
228 | for key in USEFUL_TAGS: | |
229 | if key in exif_all: | |
230 | yield key, exif_all[key] | |
231 | ||
feba5c52 E |
232 | |
233 | class MediaCommentMixin(object): | |
234 | @property | |
235 | def content_html(self): | |
236 | """ | |
237 | the actual html-rendered version of the comment displayed. | |
238 | Run through Markdown and the HTML cleaner. | |
239 | """ | |
240 | return cleaned_markdown_conversion(self.content) | |
be5be115 AW |
241 | |
242 | ||
243 | class CollectionMixin(object): | |
244 | def generate_slug(self): | |
245 | # import this here due to a cyclic import issue | |
246 | # (db.models -> db.mixin -> db.util -> db.models) | |
247 | from mediagoblin.db.util import check_collection_slug_used | |
248 | ||
249 | self.slug = slugify(self.title) | |
250 | ||
251 | duplicate = check_collection_slug_used(mg_globals.database, | |
252 | self.creator, self.slug, self.id) | |
253 | ||
254 | if duplicate: | |
255 | if self.id is not None: | |
256 | self.slug = u"%s-%s" % (self.id, self.slug) | |
257 | else: | |
258 | self.slug = None | |
259 | ||
260 | @property | |
261 | def description_html(self): | |
262 | """ | |
263 | Rendered version of the description, run through | |
264 | Markdown and cleaned with our cleaning tool. | |
265 | """ | |
266 | return cleaned_markdown_conversion(self.description) | |
267 | ||
268 | @property | |
269 | def slug_or_id(self): | |
5c2b8486 | 270 | return (self.slug or self.id) |
be5be115 AW |
271 | |
272 | def url_for_self(self, urlgen, **extra_args): | |
273 | """ | |
274 | Generate an appropriate url for ourselves | |
275 | ||
5c2b8486 | 276 | Use a slug if we have one, else use our 'id'. |
be5be115 AW |
277 | """ |
278 | creator = self.get_creator | |
279 | ||
280 | return urlgen( | |
256f816f | 281 | 'mediagoblin.user_pages.user_collection', |
be5be115 AW |
282 | user=creator.username, |
283 | collection=self.slug_or_id, | |
284 | **extra_args) | |
285 | ||
6d1e55b2 | 286 | |
be5be115 AW |
287 | class CollectionItemMixin(object): |
288 | @property | |
289 | def note_html(self): | |
290 | """ | |
291 | the actual html-rendered version of the note displayed. | |
292 | Run through Markdown and the HTML cleaner. | |
293 | """ | |
294 | return cleaned_markdown_conversion(self.note) |