1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011,2012 MediaGoblin contributors. See AUTHORS.
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.
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.
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/>.
18 This module contains some Mixin classes for the db objects.
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.
26 These functions now live here and get "mixed in" into the
30 from mediagoblin
.auth
import lib
as auth_lib
31 from mediagoblin
.tools
import common
, licenses
34 class UserMixin(object):
35 def check_login(self
, password
):
37 See if a user can login with this password
39 return auth_lib
.bcrypt_check_password(
40 password
, self
.pw_hash
)
43 class MediaEntryMixin(object):
44 def get_display_media(self
, media_map
,
45 fetch_order
=common
.DISPLAY_IMAGE_FETCHING_ORDER
):
47 Find the best media for display.
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
55 (media_size, media_path)
57 media_sizes
= media_map
.keys()
59 for media_size
in common
.DISPLAY_IMAGE_FETCHING_ORDER
:
60 if media_size
in media_sizes
:
61 return media_map
[media_size
]
63 def main_mediafile(self
):
66 def url_for_self(self
, urlgen
, **extra_args
):
68 Generate an appropriate url for ourselves
70 Use a slug if we have one, else use our '_id'.
72 uploader
= self
.get_uploader
76 'mediagoblin.user_pages.media_home',
77 user
=uploader
.username
,
82 'mediagoblin.user_pages.media_home',
83 user
=uploader
.username
,
84 media
=unicode(self
._id
),
87 def get_fail_exception(self
):
89 Get the exception that's appropriate for this error
91 if self
['fail_error']:
92 return common
.import_component(self
['fail_error'])
94 def get_license_data(self
):
95 """Return license dict for requested license"""
96 return licenses
.SUPPORTED_LICENSES
[self
.license
]