Remove the DictReadProxy thing.
[mediagoblin.git] / mediagoblin / db / mixin.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
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
30 from mediagoblin import mg_globals
31 from mediagoblin.auth import lib as auth_lib
32 from mediagoblin.tools import common, licenses
33 from mediagoblin.tools.text import cleaned_markdown_conversion
34 from mediagoblin.tools.url import slugify
35
36
37 class UserMixin(object):
38 def check_login(self, password):
39 """
40 See if a user can login with this password
41 """
42 return auth_lib.bcrypt_check_password(
43 password, self.pw_hash)
44
45 @property
46 def bio_html(self):
47 return cleaned_markdown_conversion(self.bio)
48
49
50 class MediaEntryMixin(object):
51 def generate_slug(self):
52 # import this here due to a cyclic import issue
53 # (db.models -> db.mixin -> db.util -> db.models)
54 from mediagoblin.db.util import check_media_slug_used
55
56 self.slug = slugify(self.title)
57
58 duplicate = check_media_slug_used(mg_globals.database,
59 self.uploader, self.slug, self.id)
60
61 if duplicate:
62 if self.id is not None:
63 self.slug = "%s-%s" % (self.id, self.slug)
64 else:
65 self.slug = None
66
67 @property
68 def description_html(self):
69 """
70 Rendered version of the description, run through
71 Markdown and cleaned with our cleaning tool.
72 """
73 return cleaned_markdown_conversion(self.description)
74
75 def get_display_media(self, media_map,
76 fetch_order=common.DISPLAY_IMAGE_FETCHING_ORDER):
77 """
78 Find the best media for display.
79
80 Args:
81 - media_map: a dict like
82 {u'image_size': [u'dir1', u'dir2', u'image.jpg']}
83 - fetch_order: the order we should try fetching images in
84
85 Returns:
86 (media_size, media_path)
87 """
88 media_sizes = media_map.keys()
89
90 for media_size in common.DISPLAY_IMAGE_FETCHING_ORDER:
91 if media_size in media_sizes:
92 return media_map[media_size]
93
94 def main_mediafile(self):
95 pass
96
97 @property
98 def slug_or_id(self):
99 return (self.slug or self._id)
100
101 def url_for_self(self, urlgen, **extra_args):
102 """
103 Generate an appropriate url for ourselves
104
105 Use a slug if we have one, else use our '_id'.
106 """
107 uploader = self.get_uploader
108
109 return urlgen(
110 'mediagoblin.user_pages.media_home',
111 user=uploader.username,
112 media=self.slug_or_id,
113 **extra_args)
114
115 def get_fail_exception(self):
116 """
117 Get the exception that's appropriate for this error
118 """
119 if self['fail_error']:
120 return common.import_component(self['fail_error'])
121
122 def get_license_data(self):
123 """Return license dict for requested license"""
124 return licenses.SUPPORTED_LICENSES[self.license or ""]
125
126 def exif_display_iter(self):
127 from mediagoblin.tools.exif import USEFUL_TAGS
128
129 if not self.media_data:
130 return
131 exif_all = self.media_data.get("exif_all")
132
133 for key in USEFUL_TAGS:
134 if key in exif_all:
135 yield key, exif_all[key]
136
137
138 class MediaCommentMixin(object):
139 @property
140 def content_html(self):
141 """
142 the actual html-rendered version of the comment displayed.
143 Run through Markdown and the HTML cleaner.
144 """
145 return cleaned_markdown_conversion(self.content)