Commit | Line | Data |
---|---|---|
f42e49c3 E |
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.auth import lib as auth_lib | |
31 | from mediagoblin.tools import common | |
32 | ||
33 | ||
34 | class UserMixin(object): | |
35 | def check_login(self, password): | |
36 | """ | |
37 | See if a user can login with this password | |
38 | """ | |
39 | return auth_lib.bcrypt_check_password( | |
40 | password, self.pw_hash) | |
41 | ||
42 | ||
43 | class MediaEntryMixin(object): | |
44 | def get_display_media(self, media_map, | |
45 | fetch_order=common.DISPLAY_IMAGE_FETCHING_ORDER): | |
46 | """ | |
47 | Find the best media for display. | |
48 | ||
49 | Args: | |
50 | - media_map: a dict like | |
51 | {u'image_size': [u'dir1', u'dir2', u'image.jpg']} | |
52 | - fetch_order: the order we should try fetching images in | |
53 | ||
54 | Returns: | |
55 | (media_size, media_path) | |
56 | """ | |
57 | media_sizes = media_map.keys() | |
58 | ||
59 | for media_size in common.DISPLAY_IMAGE_FETCHING_ORDER: | |
60 | if media_size in media_sizes: | |
61 | return media_map[media_size] | |
62 | ||
63 | def main_mediafile(self): | |
64 | pass | |
65 | ||
66 | def url_for_self(self, urlgen): | |
67 | """ | |
68 | Generate an appropriate url for ourselves | |
69 | ||
70 | Use a slug if we have one, else use our '_id'. | |
71 | """ | |
72 | uploader = self.get_uploader | |
73 | ||
74 | if self.get('slug'): | |
75 | return urlgen( | |
76 | 'mediagoblin.user_pages.media_home', | |
77 | user=uploader.username, | |
78 | media=self.slug) | |
79 | else: | |
80 | return urlgen( | |
81 | 'mediagoblin.user_pages.media_home', | |
82 | user=uploader.username, | |
83 | media=unicode(self._id)) | |
84 | ||
85 | def get_fail_exception(self): | |
86 | """ | |
87 | Get the exception that's appropriate for this error | |
88 | """ | |
89 | if self['fail_error']: | |
90 | return common.import_component(self['fail_error']) |