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 |
907bba31 | 31 | import re |
2d7b6bde | 32 | from datetime import datetime |
72bb46c7 | 33 | |
5f8b4ae8 SS |
34 | from werkzeug.utils import cached_property |
35 | ||
814334f6 | 36 | from mediagoblin import mg_globals |
58a94757 | 37 | from mediagoblin.media_types import FileTypeNotSupported |
17c23e15 | 38 | from mediagoblin.tools import common, licenses |
58a94757 | 39 | from mediagoblin.tools.pluginapi import hook_handle |
e61ab099 | 40 | from mediagoblin.tools.text import cleaned_markdown_conversion |
814334f6 | 41 | from mediagoblin.tools.url import slugify |
ce46470c | 42 | from mediagoblin.tools.translate import pass_to_ugettext as _ |
f42e49c3 E |
43 | |
44 | ||
45 | class UserMixin(object): | |
0421fc5e JT |
46 | object_type = "person" |
47 | ||
e61ab099 E |
48 | @property |
49 | def bio_html(self): | |
50 | return cleaned_markdown_conversion(self.bio) | |
51 | ||
de6a313c BP |
52 | def url_for_self(self, urlgen, **kwargs): |
53 | """Generate a URL for this User's home page.""" | |
54 | return urlgen('mediagoblin.user_pages.user_home', | |
55 | user=self.username, **kwargs) | |
56 | ||
57 | ||
29c65044 | 58 | class GenerateSlugMixin(object): |
44123853 E |
59 | """ |
60 | Mixin to add a generate_slug method to objects. | |
61 | ||
62 | Depends on: | |
63 | - self.slug | |
64 | - self.title | |
65 | - self.check_slug_used(new_slug) | |
66 | """ | |
814334f6 | 67 | def generate_slug(self): |
88de830f | 68 | """ |
44123853 | 69 | Generate a unique slug for this object. |
98587109 | 70 | |
88de830f CAW |
71 | This one does not *force* slugs, but usually it will probably result |
72 | in a niceish one. | |
73 | ||
98587109 CAW |
74 | The end *result* of the algorithm will result in these resolutions for |
75 | these situations: | |
88de830f CAW |
76 | - If we have a slug, make sure it's clean and sanitized, and if it's |
77 | unique, we'll use that. | |
78 | - If we have a title, slugify it, and if it's unique, we'll use that. | |
79 | - If we can't get any sort of thing that looks like it'll be a useful | |
80 | slug out of a title or an existing slug, bail, and don't set the | |
81 | slug at all. Don't try to create something just because. Make | |
82 | sure we have a reasonable basis for a slug first. | |
83 | - If we have a reasonable basis for a slug (either based on existing | |
84 | slug or slugified title) but it's not unique, first try appending | |
85 | the entry's id, if that exists | |
86 | - If that doesn't result in something unique, tack on some randomly | |
87 | generated bits until it's unique. That'll be a little bit of junk, | |
88 | but at least it has the basis of a nice slug. | |
89 | """ | |
c785f3a0 | 90 | |
66d9f1b2 SS |
91 | #Is already a slug assigned? Check if it is valid |
92 | if self.slug: | |
c785f3a0 | 93 | slug = slugify(self.slug) |
b1126f71 | 94 | |
88de830f | 95 | # otherwise, try to use the title. |
66d9f1b2 | 96 | elif self.title: |
88de830f | 97 | # assign slug based on title |
c785f3a0 | 98 | slug = slugify(self.title) |
b1126f71 | 99 | |
c785f3a0 JT |
100 | else: |
101 | # We don't have any information to set a slug | |
102 | return | |
b0118957 | 103 | |
c785f3a0 JT |
104 | # We don't want any empty string slugs |
105 | if slug == u"": | |
106 | return | |
b1126f71 | 107 | |
88de830f | 108 | # Otherwise, let's see if this is unique. |
c785f3a0 | 109 | if self.check_slug_used(slug): |
88de830f | 110 | # It looks like it's being used... lame. |
b1126f71 | 111 | |
88de830f CAW |
112 | # Can we just append the object's id to the end? |
113 | if self.id: | |
c785f3a0 | 114 | slug_with_id = u"%s-%s" % (slug, self.id) |
29c65044 | 115 | if not self.check_slug_used(slug_with_id): |
88de830f CAW |
116 | self.slug = slug_with_id |
117 | return # success! | |
b1126f71 | 118 | |
88de830f CAW |
119 | # okay, still no success; |
120 | # let's whack junk on there till it's unique. | |
c785f3a0 | 121 | slug += '-' + uuid.uuid4().hex[:4] |
a81082fc | 122 | # keep going if necessary! |
c785f3a0 JT |
123 | while self.check_slug_used(slug): |
124 | slug += uuid.uuid4().hex[:4] | |
125 | ||
126 | # self.check_slug_used(slug) must be False now so we have a slug that | |
127 | # we can use now. | |
128 | self.slug = slug | |
814334f6 | 129 | |
29c65044 E |
130 | |
131 | class MediaEntryMixin(GenerateSlugMixin): | |
132 | def check_slug_used(self, slug): | |
133 | # import this here due to a cyclic import issue | |
134 | # (db.models -> db.mixin -> db.util -> db.models) | |
135 | from mediagoblin.db.util import check_media_slug_used | |
136 | ||
137 | return check_media_slug_used(self.uploader, slug, self.id) | |
138 | ||
0421fc5e JT |
139 | @property |
140 | def object_type(self): | |
141 | """ Converts media_type to pump-like type - don't use internally """ | |
142 | return self.media_type.split(".")[-1] | |
143 | ||
1e72e075 E |
144 | @property |
145 | def description_html(self): | |
146 | """ | |
147 | Rendered version of the description, run through | |
148 | Markdown and cleaned with our cleaning tool. | |
149 | """ | |
150 | return cleaned_markdown_conversion(self.description) | |
151 | ||
e77df64f CAW |
152 | def get_display_media(self): |
153 | """Find the best media for display. | |
f42e49c3 | 154 | |
53024776 | 155 | We try checking self.media_manager.fetching_order if it exists to |
e77df64f | 156 | pull down the order. |
f42e49c3 E |
157 | |
158 | Returns: | |
ddbf6af1 CAW |
159 | (media_size, media_path) |
160 | or, if not found, None. | |
e77df64f | 161 | |
f42e49c3 | 162 | """ |
e8676fa3 | 163 | fetch_order = self.media_manager.media_fetch_order |
f42e49c3 | 164 | |
ddbf6af1 CAW |
165 | # No fetching order found? well, give up! |
166 | if not fetch_order: | |
167 | return None | |
168 | ||
169 | media_sizes = self.media_files.keys() | |
170 | ||
171 | for media_size in fetch_order: | |
f42e49c3 | 172 | if media_size in media_sizes: |
ddbf6af1 | 173 | return media_size, self.media_files[media_size] |
f42e49c3 E |
174 | |
175 | def main_mediafile(self): | |
176 | pass | |
177 | ||
3e907d55 E |
178 | @property |
179 | def slug_or_id(self): | |
7de20e52 CAW |
180 | if self.slug: |
181 | return self.slug | |
182 | else: | |
183 | return u'id:%s' % self.id | |
5f8b4ae8 | 184 | |
cb7ae1e4 | 185 | def url_for_self(self, urlgen, **extra_args): |
f42e49c3 E |
186 | """ |
187 | Generate an appropriate url for ourselves | |
188 | ||
5c2b8486 | 189 | Use a slug if we have one, else use our 'id'. |
f42e49c3 E |
190 | """ |
191 | uploader = self.get_uploader | |
192 | ||
3e907d55 E |
193 | return urlgen( |
194 | 'mediagoblin.user_pages.media_home', | |
195 | user=uploader.username, | |
196 | media=self.slug_or_id, | |
197 | **extra_args) | |
f42e49c3 | 198 | |
2e4ad359 SS |
199 | @property |
200 | def thumb_url(self): | |
201 | """Return the thumbnail URL (for usage in templates) | |
202 | Will return either the real thumbnail or a default fallback icon.""" | |
203 | # TODO: implement generic fallback in case MEDIA_MANAGER does | |
204 | # not specify one? | |
205 | if u'thumb' in self.media_files: | |
206 | thumb_url = mg_globals.app.public_store.file_url( | |
207 | self.media_files[u'thumb']) | |
208 | else: | |
df1c4976 | 209 | # No thumbnail in media available. Get the media's |
2e4ad359 | 210 | # MEDIA_MANAGER for the fallback icon and return static URL |
5f8b4ae8 SS |
211 | # Raises FileTypeNotSupported in case no such manager is enabled |
212 | manager = self.media_manager | |
df1c4976 | 213 | thumb_url = mg_globals.app.staticdirector(manager[u'default_thumb']) |
2e4ad359 SS |
214 | return thumb_url |
215 | ||
5b014a08 JT |
216 | @property |
217 | def original_url(self): | |
218 | """ Returns the URL for the original image | |
219 | will return self.thumb_url if original url doesn't exist""" | |
220 | if u"original" not in self.media_files: | |
221 | return self.thumb_url | |
ce46470c | 222 | |
5b014a08 JT |
223 | return mg_globals.app.public_store.file_url( |
224 | self.media_files[u"original"] | |
225 | ) | |
226 | ||
5f8b4ae8 SS |
227 | @cached_property |
228 | def media_manager(self): | |
229 | """Returns the MEDIA_MANAGER of the media's media_type | |
230 | ||
231 | Raises FileTypeNotSupported in case no such manager is enabled | |
232 | """ | |
6403bc92 | 233 | manager = hook_handle(('media_manager', self.media_type)) |
58a94757 | 234 | if manager: |
4259ad5b CAW |
235 | return manager(self) |
236 | ||
5f8b4ae8 SS |
237 | # Not found? Then raise an error |
238 | raise FileTypeNotSupported( | |
e6991972 RE |
239 | "MediaManager not in enabled types. Check media_type plugins are" |
240 | " enabled in config?") | |
5f8b4ae8 | 241 | |
f42e49c3 E |
242 | def get_fail_exception(self): |
243 | """ | |
244 | Get the exception that's appropriate for this error | |
245 | """ | |
51eb0267 JW |
246 | if self.fail_error: |
247 | return common.import_component(self.fail_error) | |
17c23e15 AW |
248 | |
249 | def get_license_data(self): | |
250 | """Return license dict for requested license""" | |
138a18fd | 251 | return licenses.get_license_by_url(self.license or "") |
feba5c52 | 252 | |
5bad26bc E |
253 | def exif_display_iter(self): |
254 | if not self.media_data: | |
255 | return | |
256 | exif_all = self.media_data.get("exif_all") | |
257 | ||
b3566e1d GS |
258 | for key in exif_all: |
259 | label = re.sub('(.)([A-Z][a-z]+)', r'\1 \2', key) | |
260 | yield label.replace('EXIF', '').replace('Image', ''), exif_all[key] | |
5bad26bc | 261 | |
420e1374 GS |
262 | def exif_display_data_short(self): |
263 | """Display a very short practical version of exif info""" | |
420e1374 GS |
264 | if not self.media_data: |
265 | return | |
907bba31 | 266 | |
420e1374 | 267 | exif_all = self.media_data.get("exif_all") |
907bba31 | 268 | |
14aa2eaa JW |
269 | exif_short = {} |
270 | ||
907bba31 JW |
271 | if 'Image DateTimeOriginal' in exif_all: |
272 | # format date taken | |
196cef38 | 273 | takendate = datetime.strptime( |
907bba31 JW |
274 | exif_all['Image DateTimeOriginal']['printable'], |
275 | '%Y:%m:%d %H:%M:%S').date() | |
276 | taken = takendate.strftime('%B %d %Y') | |
277 | ||
14aa2eaa JW |
278 | exif_short.update({'Date Taken': taken}) |
279 | ||
1b6a2b85 | 280 | aperture = None |
907bba31 JW |
281 | if 'EXIF FNumber' in exif_all: |
282 | fnum = str(exif_all['EXIF FNumber']['printable']).split('/') | |
283 | ||
1b6a2b85 JW |
284 | # calculate aperture |
285 | if len(fnum) == 2: | |
286 | aperture = "f/%.1f" % (float(fnum[0])/float(fnum[1])) | |
287 | elif fnum[0] != 'None': | |
288 | aperture = "f/%s" % (fnum[0]) | |
907bba31 | 289 | |
14aa2eaa JW |
290 | if aperture: |
291 | exif_short.update({'Aperture': aperture}) | |
292 | ||
293 | short_keys = [ | |
294 | ('Camera', 'Image Model', None), | |
295 | ('Exposure', 'EXIF ExposureTime', lambda x: '%s sec' % x), | |
296 | ('ISO Speed', 'EXIF ISOSpeedRatings', None), | |
297 | ('Focal Length', 'EXIF FocalLength', lambda x: '%s mm' % x)] | |
298 | ||
299 | for label, key, fmt_func in short_keys: | |
300 | try: | |
301 | val = fmt_func(exif_all[key]['printable']) if fmt_func \ | |
302 | else exif_all[key]['printable'] | |
303 | exif_short.update({label: val}) | |
304 | except KeyError: | |
305 | pass | |
306 | ||
307 | return exif_short | |
308 | ||
feba5c52 E |
309 | |
310 | class MediaCommentMixin(object): | |
0421fc5e JT |
311 | object_type = "comment" |
312 | ||
feba5c52 E |
313 | @property |
314 | def content_html(self): | |
315 | """ | |
316 | the actual html-rendered version of the comment displayed. | |
317 | Run through Markdown and the HTML cleaner. | |
318 | """ | |
319 | return cleaned_markdown_conversion(self.content) | |
be5be115 | 320 | |
dc19e98d | 321 | def __unicode__(self): |
09bed9a7 | 322 | return u'<{klass} #{id} {author} "{comment}">'.format( |
2d7b6bde JW |
323 | klass=self.__class__.__name__, |
324 | id=self.id, | |
325 | author=self.get_author, | |
326 | comment=self.content) | |
327 | ||
dc19e98d TB |
328 | def __repr__(self): |
329 | return '<{klass} #{id} {author} "{comment}">'.format( | |
330 | klass=self.__class__.__name__, | |
331 | id=self.id, | |
332 | author=self.get_author, | |
333 | comment=self.content) | |
334 | ||
be5be115 | 335 | |
455fd36f | 336 | class CollectionMixin(GenerateSlugMixin): |
0421fc5e JT |
337 | object_type = "collection" |
338 | ||
455fd36f | 339 | def check_slug_used(self, slug): |
be5be115 AW |
340 | # import this here due to a cyclic import issue |
341 | # (db.models -> db.mixin -> db.util -> db.models) | |
342 | from mediagoblin.db.util import check_collection_slug_used | |
343 | ||
455fd36f | 344 | return check_collection_slug_used(self.creator, slug, self.id) |
be5be115 AW |
345 | |
346 | @property | |
347 | def description_html(self): | |
348 | """ | |
349 | Rendered version of the description, run through | |
350 | Markdown and cleaned with our cleaning tool. | |
351 | """ | |
352 | return cleaned_markdown_conversion(self.description) | |
353 | ||
354 | @property | |
355 | def slug_or_id(self): | |
5c2b8486 | 356 | return (self.slug or self.id) |
be5be115 AW |
357 | |
358 | def url_for_self(self, urlgen, **extra_args): | |
359 | """ | |
360 | Generate an appropriate url for ourselves | |
361 | ||
5c2b8486 | 362 | Use a slug if we have one, else use our 'id'. |
be5be115 AW |
363 | """ |
364 | creator = self.get_creator | |
365 | ||
366 | return urlgen( | |
256f816f | 367 | 'mediagoblin.user_pages.user_collection', |
be5be115 AW |
368 | user=creator.username, |
369 | collection=self.slug_or_id, | |
370 | **extra_args) | |
371 | ||
6d1e55b2 | 372 | |
be5be115 AW |
373 | class CollectionItemMixin(object): |
374 | @property | |
375 | def note_html(self): | |
376 | """ | |
377 | the actual html-rendered version of the note displayed. | |
378 | Run through Markdown and the HTML cleaner. | |
379 | """ | |
380 | return cleaned_markdown_conversion(self.note) | |
ce46470c JT |
381 | |
382 | class ActivityMixin(object): | |
0421fc5e | 383 | object_type = "activity" |
ce46470c JT |
384 | |
385 | VALID_VERBS = ["add", "author", "create", "delete", "dislike", "favorite", | |
386 | "follow", "like", "post", "share", "unfavorite", "unfollow", | |
387 | "unlike", "unshare", "update", "tag"] | |
388 | ||
389 | def get_url(self, request): | |
390 | return request.urlgen( | |
391 | "mediagoblin.federation.activity_view", | |
392 | username=self.get_actor.username, | |
393 | id=self.id, | |
394 | qualified=True | |
395 | ) | |
396 | ||
397 | def generate_content(self): | |
398 | """ Produces a HTML content for object """ | |
399 | # some of these have simple and targetted. If self.target it set | |
400 | # it will pick the targetted. If they DON'T have a targetted version | |
401 | # the information in targetted won't be added to the content. | |
402 | verb_to_content = { | |
403 | "add": { | |
404 | "simple" : _("{username} added {object}"), | |
405 | "targetted": _("{username} added {object} to {target}"), | |
406 | }, | |
407 | "author": {"simple": _("{username} authored {object}")}, | |
408 | "create": {"simple": _("{username} created {object}")}, | |
409 | "delete": {"simple": _("{username} deleted {object}")}, | |
410 | "dislike": {"simple": _("{username} disliked {object}")}, | |
411 | "favorite": {"simple": _("{username} favorited {object}")}, | |
412 | "follow": {"simple": _("{username} followed {object}")}, | |
413 | "like": {"simple": _("{username} liked {object}")}, | |
414 | "post": { | |
415 | "simple": _("{username} posted {object}"), | |
2b191618 | 416 | "targetted": _("{username} posted {object} to {target}"), |
ce46470c JT |
417 | }, |
418 | "share": {"simple": _("{username} shared {object}")}, | |
419 | "unfavorite": {"simple": _("{username} unfavorited {object}")}, | |
420 | "unfollow": {"simple": _("{username} stopped following {object}")}, | |
421 | "unlike": {"simple": _("{username} unliked {object}")}, | |
422 | "unshare": {"simple": _("{username} unshared {object}")}, | |
423 | "update": {"simple": _("{username} updated {object}")}, | |
424 | "tag": {"simple": _("{username} tagged {object}")}, | |
425 | } | |
426 | ||
6d36f75f JT |
427 | obj = self.get_object |
428 | target = self.get_target | |
ce46470c JT |
429 | actor = self.get_actor |
430 | content = verb_to_content.get(self.verb, None) | |
431 | ||
432 | if content is None or obj is None: | |
433 | return | |
434 | ||
435 | if target is None or "targetted" not in content: | |
436 | self.content = content["simple"].format( | |
437 | username=actor.username, | |
6d36f75f | 438 | object=obj.object_type |
ce46470c JT |
439 | ) |
440 | else: | |
441 | self.content = content["targetted"].format( | |
442 | username=actor.username, | |
6d36f75f JT |
443 | object=obj.object_type, |
444 | target=target.object_type, | |
ce46470c JT |
445 | ) |
446 | ||
447 | return self.content | |
448 | ||
449 | def serialize(self, request): | |
450 | obj = { | |
451 | "id": self.id, | |
452 | "actor": self.get_actor.serialize(request), | |
453 | "verb": self.verb, | |
454 | "published": self.published.isoformat(), | |
455 | "updated": self.updated.isoformat(), | |
456 | "content": self.content, | |
457 | "url": self.get_url(request), | |
6d36f75f | 458 | "object": self.get_object.serialize(request), |
0421fc5e | 459 | "objectType": self.object_type, |
ce46470c JT |
460 | } |
461 | ||
462 | if self.generator: | |
6d36f75f | 463 | obj["generator"] = self.get_generator.serialize(request) |
ce46470c JT |
464 | |
465 | if self.title: | |
466 | obj["title"] = self.title | |
467 | ||
6d36f75f | 468 | target = self.get_target |
ce46470c | 469 | if target is not None: |
6d36f75f | 470 | obj["target"] = target.serialize(request) |
ce46470c JT |
471 | |
472 | return obj | |
473 | ||
474 | def unseralize(self, data): | |
475 | """ | |
476 | Takes data given and set it on this activity. | |
477 | ||
478 | Several pieces of data are not written on because of security | |
479 | reasons. For example changing the author or id of an activity. | |
480 | """ | |
481 | if "verb" in data: | |
482 | self.verb = data["verb"] | |
483 | ||
484 | if "title" in data: | |
485 | self.title = data["title"] | |
486 | ||
487 | if "content" in data: | |
488 | self.content = data["content"] |