Drop pre-rendered html: User.bio_html
[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
30from mediagoblin.auth import lib as auth_lib
17c23e15 31from mediagoblin.tools import common, licenses
e61ab099 32from mediagoblin.tools.text import cleaned_markdown_conversion
f42e49c3
E
33
34
35class UserMixin(object):
36 def check_login(self, password):
37 """
38 See if a user can login with this password
39 """
40 return auth_lib.bcrypt_check_password(
41 password, self.pw_hash)
42
e61ab099
E
43 @property
44 def bio_html(self):
45 return cleaned_markdown_conversion(self.bio)
46
f42e49c3
E
47
48class MediaEntryMixin(object):
49 def get_display_media(self, media_map,
50 fetch_order=common.DISPLAY_IMAGE_FETCHING_ORDER):
51 """
52 Find the best media for display.
53
54 Args:
55 - media_map: a dict like
56 {u'image_size': [u'dir1', u'dir2', u'image.jpg']}
57 - fetch_order: the order we should try fetching images in
58
59 Returns:
60 (media_size, media_path)
61 """
62 media_sizes = media_map.keys()
63
64 for media_size in common.DISPLAY_IMAGE_FETCHING_ORDER:
65 if media_size in media_sizes:
66 return media_map[media_size]
67
68 def main_mediafile(self):
69 pass
70
3e907d55
E
71 @property
72 def slug_or_id(self):
73 return (self.slug or self._id)
74
cb7ae1e4 75 def url_for_self(self, urlgen, **extra_args):
f42e49c3
E
76 """
77 Generate an appropriate url for ourselves
78
79 Use a slug if we have one, else use our '_id'.
80 """
81 uploader = self.get_uploader
82
3e907d55
E
83 return urlgen(
84 'mediagoblin.user_pages.media_home',
85 user=uploader.username,
86 media=self.slug_or_id,
87 **extra_args)
f42e49c3
E
88
89 def get_fail_exception(self):
90 """
91 Get the exception that's appropriate for this error
92 """
93 if self['fail_error']:
94 return common.import_component(self['fail_error'])
17c23e15
AW
95
96 def get_license_data(self):
97 """Return license dict for requested license"""
2788e6a1 98 return licenses.SUPPORTED_LICENSES[self.license or ""]