b0fecad3f31a41009df13f50778e19e1d6059932
[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.auth import lib as auth_lib
31 from mediagoblin.tools import common, licenses
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, **extra_args):
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 **extra_args)
80 else:
81 return urlgen(
82 'mediagoblin.user_pages.media_home',
83 user=uploader.username,
84 media=unicode(self._id),
85 **extra_args)
86
87 def get_fail_exception(self):
88 """
89 Get the exception that's appropriate for this error
90 """
91 if self['fail_error']:
92 return common.import_component(self['fail_error'])
93
94 def get_license_data(self):
95 """Return license dict for requested license"""
96 return licenses.SUPPORTED_LICENSES[self.license]