Change 'federation' name to 'api' which is more suitable
[larjonas-mediagoblin.git] / mediagoblin / db / mixin.py
blob4602c709a40b5192ebf6130836d99af6425f5f9a
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
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.
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.
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/>.
17 """
18 This module contains some Mixin classes for the db objects.
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.
26 These functions now live here and get "mixed in" into the
27 real objects.
28 """
30 import uuid
31 import re
32 from datetime import datetime
34 from pytz import UTC
35 from werkzeug.utils import cached_property
37 from mediagoblin.media_types import FileTypeNotSupported
38 from mediagoblin.tools import common, licenses
39 from mediagoblin.tools.pluginapi import hook_handle
40 from mediagoblin.tools.text import cleaned_markdown_conversion
41 from mediagoblin.tools.url import slugify
42 from mediagoblin.tools.translate import pass_to_ugettext as _
45 class UserMixin(object):
46 object_type = "person"
48 @property
49 def bio_html(self):
50 return cleaned_markdown_conversion(self.bio)
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)
58 class GenerateSlugMixin(object):
59 """
60 Mixin to add a generate_slug method to objects.
62 Depends on:
63 - self.slug
64 - self.title
65 - self.check_slug_used(new_slug)
66 """
67 def generate_slug(self):
68 """
69 Generate a unique slug for this object.
71 This one does not *force* slugs, but usually it will probably result
72 in a niceish one.
74 The end *result* of the algorithm will result in these resolutions for
75 these situations:
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 """
91 #Is already a slug assigned? Check if it is valid
92 if self.slug:
93 slug = slugify(self.slug)
95 # otherwise, try to use the title.
96 elif self.title:
97 # assign slug based on title
98 slug = slugify(self.title)
100 else:
101 # We don't have any information to set a slug
102 return
104 # We don't want any empty string slugs
105 if slug == u"":
106 return
108 # Otherwise, let's see if this is unique.
109 if self.check_slug_used(slug):
110 # It looks like it's being used... lame.
112 # Can we just append the object's id to the end?
113 if self.id:
114 slug_with_id = u"%s-%s" % (slug, self.id)
115 if not self.check_slug_used(slug_with_id):
116 self.slug = slug_with_id
117 return # success!
119 # okay, still no success;
120 # let's whack junk on there till it's unique.
121 slug += '-' + uuid.uuid4().hex[:4]
122 # keep going if necessary!
123 while self.check_slug_used(slug):
124 slug += uuid.uuid4().hex[:4]
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
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
137 return check_media_slug_used(self.uploader, slug, self.id)
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]
144 @property
145 def description_html(self):
147 Rendered version of the description, run through
148 Markdown and cleaned with our cleaning tool.
150 return cleaned_markdown_conversion(self.description)
152 def get_display_media(self):
153 """Find the best media for display.
155 We try checking self.media_manager.fetching_order if it exists to
156 pull down the order.
158 Returns:
159 (media_size, media_path)
160 or, if not found, None.
163 fetch_order = self.media_manager.media_fetch_order
165 # No fetching order found? well, give up!
166 if not fetch_order:
167 return None
169 media_sizes = self.media_files.keys()
171 for media_size in fetch_order:
172 if media_size in media_sizes:
173 return media_size, self.media_files[media_size]
175 def main_mediafile(self):
176 pass
178 @property
179 def slug_or_id(self):
180 if self.slug:
181 return self.slug
182 else:
183 return u'id:%s' % self.id
185 def url_for_self(self, urlgen, **extra_args):
187 Generate an appropriate url for ourselves
189 Use a slug if we have one, else use our 'id'.
191 uploader = self.get_uploader
193 return urlgen(
194 'mediagoblin.user_pages.media_home',
195 user=uploader.username,
196 media=self.slug_or_id,
197 **extra_args)
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 = self._app.public_store.file_url(
207 self.media_files[u'thumb'])
208 else:
209 # No thumbnail in media available. Get the media's
210 # MEDIA_MANAGER for the fallback icon and return static URL
211 # Raises FileTypeNotSupported in case no such manager is enabled
212 manager = self.media_manager
213 thumb_url = self._app.staticdirector(manager[u'default_thumb'])
214 return thumb_url
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
223 return self._app.public_store.file_url(
224 self.media_files[u"original"]
227 @cached_property
228 def media_manager(self):
229 """Returns the MEDIA_MANAGER of the media's media_type
231 Raises FileTypeNotSupported in case no such manager is enabled
233 manager = hook_handle(('media_manager', self.media_type))
234 if manager:
235 return manager(self)
237 # Not found? Then raise an error
238 raise FileTypeNotSupported(
239 "MediaManager not in enabled types. Check media_type plugins are"
240 " enabled in config?")
242 def get_fail_exception(self):
244 Get the exception that's appropriate for this error
246 if self.fail_error:
247 return common.import_component(self.fail_error)
249 def get_license_data(self):
250 """Return license dict for requested license"""
251 return licenses.get_license_by_url(self.license or "")
253 def exif_display_iter(self):
254 if not self.media_data:
255 return
256 exif_all = self.media_data.get("exif_all")
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]
262 def exif_display_data_short(self):
263 """Display a very short practical version of exif info"""
264 if not self.media_data:
265 return
267 exif_all = self.media_data.get("exif_all")
269 exif_short = {}
271 if 'Image DateTimeOriginal' in exif_all:
272 # format date taken
273 takendate = datetime.strptime(
274 exif_all['Image DateTimeOriginal']['printable'],
275 '%Y:%m:%d %H:%M:%S').date()
276 taken = takendate.strftime('%B %d %Y')
278 exif_short.update({'Date Taken': taken})
280 aperture = None
281 if 'EXIF FNumber' in exif_all:
282 fnum = str(exif_all['EXIF FNumber']['printable']).split('/')
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])
290 if aperture:
291 exif_short.update({'Aperture': aperture})
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)]
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
307 return exif_short
310 class MediaCommentMixin(object):
311 object_type = "comment"
313 @property
314 def content_html(self):
316 the actual html-rendered version of the comment displayed.
317 Run through Markdown and the HTML cleaner.
319 return cleaned_markdown_conversion(self.content)
321 def __unicode__(self):
322 return u'<{klass} #{id} {author} "{comment}">'.format(
323 klass=self.__class__.__name__,
324 id=self.id,
325 author=self.get_author,
326 comment=self.content)
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)
336 class CollectionMixin(GenerateSlugMixin):
337 object_type = "collection"
339 def check_slug_used(self, slug):
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
344 return check_collection_slug_used(self.creator, slug, self.id)
346 @property
347 def description_html(self):
349 Rendered version of the description, run through
350 Markdown and cleaned with our cleaning tool.
352 return cleaned_markdown_conversion(self.description)
354 @property
355 def slug_or_id(self):
356 return (self.slug or self.id)
358 def url_for_self(self, urlgen, **extra_args):
360 Generate an appropriate url for ourselves
362 Use a slug if we have one, else use our 'id'.
364 creator = self.get_creator
366 return urlgen(
367 'mediagoblin.user_pages.user_collection',
368 user=creator.username,
369 collection=self.slug_or_id,
370 **extra_args)
373 class CollectionItemMixin(object):
374 @property
375 def note_html(self):
377 the actual html-rendered version of the note displayed.
378 Run through Markdown and the HTML cleaner.
380 return cleaned_markdown_conversion(self.note)
382 class ActivityMixin(object):
383 object_type = "activity"
385 VALID_VERBS = ["add", "author", "create", "delete", "dislike", "favorite",
386 "follow", "like", "post", "share", "unfavorite", "unfollow",
387 "unlike", "unshare", "update", "tag"]
389 def get_url(self, request):
390 return request.urlgen(
391 "mediagoblin.user_pages.activity_view",
392 username=self.get_actor.username,
393 id=self.id,
394 qualified=True
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}"),
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}"),
416 "targetted": _("{username} posted {object} to {target}"),
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}")},
427 object_map = {
428 "image": _("an image"),
429 "comment": _("a comment"),
430 "collection": _("a collection"),
431 "video": _("a video"),
432 "audio": _("audio"),
433 "person": _("a person"),
436 obj = self.get_object
437 target = self.get_target
438 actor = self.get_actor
439 content = verb_to_content.get(self.verb, None)
441 if content is None or obj is None:
442 return
444 # Decide what to fill the object with
445 if hasattr(obj, "title") and obj.title.strip(" "):
446 object_value = obj.title
447 elif obj.object_type in object_map:
448 object_value = object_map[obj.object_type]
449 else:
450 object_value = _("an object")
452 # Do we want to add a target (indirect object) to content?
453 if target is not None and "targetted" in content:
454 if hasattr(target, "title") and target.title.strip(" "):
455 target_value = target.title
456 elif target.object_type in object_map:
457 target_value = object_map[target.object_type]
458 else:
459 target_value = _("an object")
461 self.content = content["targetted"].format(
462 username=actor.username,
463 object=object_value,
464 target=target_value
466 else:
467 self.content = content["simple"].format(
468 username=actor.username,
469 object=object_value
472 return self.content
474 def serialize(self, request):
475 href = request.urlgen(
476 "mediagoblin.api.object",
477 object_type=self.object_type,
478 id=self.id,
479 qualified=True
481 published = UTC.localize(self.published)
482 updated = UTC.localize(self.updated)
483 obj = {
484 "id": href,
485 "actor": self.get_actor.serialize(request),
486 "verb": self.verb,
487 "published": published.isoformat(),
488 "updated": updated.isoformat(),
489 "content": self.content,
490 "url": self.get_url(request),
491 "object": self.get_object.serialize(request),
492 "objectType": self.object_type,
493 "links": {
494 "self": {
495 "href": href,
500 if self.generator:
501 obj["generator"] = self.get_generator.serialize(request)
503 if self.title:
504 obj["title"] = self.title
506 target = self.get_target
507 if target is not None:
508 obj["target"] = target.serialize(request)
510 return obj
512 def unseralize(self, data):
514 Takes data given and set it on this activity.
516 Several pieces of data are not written on because of security
517 reasons. For example changing the author or id of an activity.
519 if "verb" in data:
520 self.verb = data["verb"]
522 if "title" in data:
523 self.title = data["title"]
525 if "content" in data:
526 self.content = data["content"]