Drop pre-rendered html: MediaEntry.description_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):
1e72e075
E
49 @property
50 def description_html(self):
51 """
52 Rendered version of the description, run through
53 Markdown and cleaned with our cleaning tool.
54 """
55 return cleaned_markdown_conversion(self.description)
56
f42e49c3
E
57 def get_display_media(self, media_map,
58 fetch_order=common.DISPLAY_IMAGE_FETCHING_ORDER):
59 """
60 Find the best media for display.
61
62 Args:
63 - media_map: a dict like
64 {u'image_size': [u'dir1', u'dir2', u'image.jpg']}
65 - fetch_order: the order we should try fetching images in
66
67 Returns:
68 (media_size, media_path)
69 """
70 media_sizes = media_map.keys()
71
72 for media_size in common.DISPLAY_IMAGE_FETCHING_ORDER:
73 if media_size in media_sizes:
74 return media_map[media_size]
75
76 def main_mediafile(self):
77 pass
78
3e907d55
E
79 @property
80 def slug_or_id(self):
81 return (self.slug or self._id)
82
cb7ae1e4 83 def url_for_self(self, urlgen, **extra_args):
f42e49c3
E
84 """
85 Generate an appropriate url for ourselves
86
87 Use a slug if we have one, else use our '_id'.
88 """
89 uploader = self.get_uploader
90
3e907d55
E
91 return urlgen(
92 'mediagoblin.user_pages.media_home',
93 user=uploader.username,
94 media=self.slug_or_id,
95 **extra_args)
f42e49c3
E
96
97 def get_fail_exception(self):
98 """
99 Get the exception that's appropriate for this error
100 """
101 if self['fail_error']:
102 return common.import_component(self['fail_error'])
17c23e15
AW
103
104 def get_license_data(self):
105 """Return license dict for requested license"""
2788e6a1 106 return licenses.SUPPORTED_LICENSES[self.license or ""]