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 | ||
72bb46c7 CAW |
30 | from uuid import uuid4 |
31 | ||
5f8b4ae8 SS |
32 | from werkzeug.utils import cached_property |
33 | ||
814334f6 | 34 | from mediagoblin import mg_globals |
f42e49c3 | 35 | from mediagoblin.auth import lib as auth_lib |
5f8b4ae8 | 36 | from mediagoblin.media_types import get_media_managers, FileTypeNotSupported |
17c23e15 | 37 | from mediagoblin.tools import common, licenses |
e61ab099 | 38 | from mediagoblin.tools.text import cleaned_markdown_conversion |
814334f6 | 39 | from mediagoblin.tools.url import slugify |
f42e49c3 E |
40 | |
41 | ||
42 | class UserMixin(object): | |
43 | def check_login(self, password): | |
44 | """ | |
45 | See if a user can login with this password | |
46 | """ | |
47 | return auth_lib.bcrypt_check_password( | |
48 | password, self.pw_hash) | |
49 | ||
e61ab099 E |
50 | @property |
51 | def bio_html(self): | |
52 | return cleaned_markdown_conversion(self.bio) | |
53 | ||
f42e49c3 E |
54 | |
55 | class MediaEntryMixin(object): | |
814334f6 E |
56 | def generate_slug(self): |
57 | # import this here due to a cyclic import issue | |
58 | # (db.models -> db.mixin -> db.util -> db.models) | |
59 | from mediagoblin.db.util import check_media_slug_used | |
60 | ||
66d9f1b2 SS |
61 | #Is already a slug assigned? Check if it is valid |
62 | if self.slug: | |
63 | self.slug = slugify(self.slug) | |
64 | elif self.title: | |
65 | #assign slug based on title | |
66 | self.slug = slugify(self.title) | |
67 | elif self.id: | |
68 | # Does the object already have an ID? (after adding to the session) | |
69 | self.slug = unicode(self.id) | |
70 | else: | |
71 | # Everything else failed, just use random garbage | |
72 | self.slug = unicode(uuid4())[1:4] | |
814334f6 | 73 | |
66d9f1b2 SS |
74 | while check_media_slug_used(self.uploader, self.slug, self.id): |
75 | # add garbage till it's unique | |
76 | self.slug = self.slug + unicode(uuid4())[1:4] | |
814334f6 | 77 | |
1e72e075 E |
78 | @property |
79 | def description_html(self): | |
80 | """ | |
81 | Rendered version of the description, run through | |
82 | Markdown and cleaned with our cleaning tool. | |
83 | """ | |
84 | return cleaned_markdown_conversion(self.description) | |
85 | ||
f42e49c3 E |
86 | def get_display_media(self, media_map, |
87 | fetch_order=common.DISPLAY_IMAGE_FETCHING_ORDER): | |
88 | """ | |
89 | Find the best media for display. | |
90 | ||
91 | Args: | |
92 | - media_map: a dict like | |
93 | {u'image_size': [u'dir1', u'dir2', u'image.jpg']} | |
94 | - fetch_order: the order we should try fetching images in | |
95 | ||
96 | Returns: | |
97 | (media_size, media_path) | |
98 | """ | |
99 | media_sizes = media_map.keys() | |
100 | ||
101 | for media_size in common.DISPLAY_IMAGE_FETCHING_ORDER: | |
102 | if media_size in media_sizes: | |
103 | return media_map[media_size] | |
104 | ||
105 | def main_mediafile(self): | |
106 | pass | |
107 | ||
3e907d55 E |
108 | @property |
109 | def slug_or_id(self): | |
5c2b8486 | 110 | return (self.slug or self.id) |
3e907d55 | 111 | |
5f8b4ae8 | 112 | |
cb7ae1e4 | 113 | def url_for_self(self, urlgen, **extra_args): |
f42e49c3 E |
114 | """ |
115 | Generate an appropriate url for ourselves | |
116 | ||
5c2b8486 | 117 | Use a slug if we have one, else use our 'id'. |
f42e49c3 E |
118 | """ |
119 | uploader = self.get_uploader | |
120 | ||
3e907d55 E |
121 | return urlgen( |
122 | 'mediagoblin.user_pages.media_home', | |
123 | user=uploader.username, | |
124 | media=self.slug_or_id, | |
125 | **extra_args) | |
f42e49c3 | 126 | |
2e4ad359 SS |
127 | @property |
128 | def thumb_url(self): | |
129 | """Return the thumbnail URL (for usage in templates) | |
130 | Will return either the real thumbnail or a default fallback icon.""" | |
131 | # TODO: implement generic fallback in case MEDIA_MANAGER does | |
132 | # not specify one? | |
133 | if u'thumb' in self.media_files: | |
134 | thumb_url = mg_globals.app.public_store.file_url( | |
135 | self.media_files[u'thumb']) | |
136 | else: | |
df1c4976 | 137 | # No thumbnail in media available. Get the media's |
2e4ad359 | 138 | # MEDIA_MANAGER for the fallback icon and return static URL |
5f8b4ae8 SS |
139 | # Raises FileTypeNotSupported in case no such manager is enabled |
140 | manager = self.media_manager | |
df1c4976 | 141 | thumb_url = mg_globals.app.staticdirector(manager[u'default_thumb']) |
2e4ad359 SS |
142 | return thumb_url |
143 | ||
5f8b4ae8 SS |
144 | @cached_property |
145 | def media_manager(self): | |
146 | """Returns the MEDIA_MANAGER of the media's media_type | |
147 | ||
148 | Raises FileTypeNotSupported in case no such manager is enabled | |
149 | """ | |
150 | # TODO, we should be able to make this a simple lookup rather | |
151 | # than iterating through all media managers. | |
152 | for media_type, manager in get_media_managers(): | |
153 | if media_type == self.media_type: | |
154 | return manager | |
155 | # Not found? Then raise an error | |
156 | raise FileTypeNotSupported( | |
157 | "MediaManager not in enabled types. Check media_types in config?") | |
158 | ||
f42e49c3 E |
159 | def get_fail_exception(self): |
160 | """ | |
161 | Get the exception that's appropriate for this error | |
162 | """ | |
51eb0267 JW |
163 | if self.fail_error: |
164 | return common.import_component(self.fail_error) | |
17c23e15 AW |
165 | |
166 | def get_license_data(self): | |
167 | """Return license dict for requested license""" | |
138a18fd | 168 | return licenses.get_license_by_url(self.license or "") |
feba5c52 | 169 | |
5bad26bc | 170 | def exif_display_iter(self): |
7b82f56b E |
171 | from mediagoblin.tools.exif import USEFUL_TAGS |
172 | ||
5bad26bc E |
173 | if not self.media_data: |
174 | return | |
175 | exif_all = self.media_data.get("exif_all") | |
176 | ||
177 | for key in USEFUL_TAGS: | |
178 | if key in exif_all: | |
179 | yield key, exif_all[key] | |
180 | ||
feba5c52 E |
181 | |
182 | class MediaCommentMixin(object): | |
183 | @property | |
184 | def content_html(self): | |
185 | """ | |
186 | the actual html-rendered version of the comment displayed. | |
187 | Run through Markdown and the HTML cleaner. | |
188 | """ | |
189 | return cleaned_markdown_conversion(self.content) | |
be5be115 AW |
190 | |
191 | ||
192 | class CollectionMixin(object): | |
193 | def generate_slug(self): | |
194 | # import this here due to a cyclic import issue | |
195 | # (db.models -> db.mixin -> db.util -> db.models) | |
196 | from mediagoblin.db.util import check_collection_slug_used | |
197 | ||
198 | self.slug = slugify(self.title) | |
199 | ||
200 | duplicate = check_collection_slug_used(mg_globals.database, | |
201 | self.creator, self.slug, self.id) | |
202 | ||
203 | if duplicate: | |
204 | if self.id is not None: | |
205 | self.slug = u"%s-%s" % (self.id, self.slug) | |
206 | else: | |
207 | self.slug = None | |
208 | ||
209 | @property | |
210 | def description_html(self): | |
211 | """ | |
212 | Rendered version of the description, run through | |
213 | Markdown and cleaned with our cleaning tool. | |
214 | """ | |
215 | return cleaned_markdown_conversion(self.description) | |
216 | ||
217 | @property | |
218 | def slug_or_id(self): | |
5c2b8486 | 219 | return (self.slug or self.id) |
be5be115 AW |
220 | |
221 | def url_for_self(self, urlgen, **extra_args): | |
222 | """ | |
223 | Generate an appropriate url for ourselves | |
224 | ||
5c2b8486 | 225 | Use a slug if we have one, else use our 'id'. |
be5be115 AW |
226 | """ |
227 | creator = self.get_creator | |
228 | ||
229 | return urlgen( | |
256f816f | 230 | 'mediagoblin.user_pages.user_collection', |
be5be115 AW |
231 | user=creator.username, |
232 | collection=self.slug_or_id, | |
233 | **extra_args) | |
234 | ||
6d1e55b2 | 235 | |
be5be115 AW |
236 | class CollectionItemMixin(object): |
237 | @property | |
238 | def note_html(self): | |
239 | """ | |
240 | the actual html-rendered version of the note displayed. | |
241 | Run through Markdown and the HTML cleaner. | |
242 | """ | |
243 | return cleaned_markdown_conversion(self.note) |