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
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
37 class UserMixin(object):
38 def check_login(self
, password
):
40 See if a user can login with this password
42 return auth_lib
.bcrypt_check_password(
43 password
, self
.pw_hash
)
47 return cleaned_markdown_conversion(self
.bio
)
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
56 self
.slug
= slugify(self
.title
)
58 duplicate
= check_media_slug_used(mg_globals
.database
,
59 self
.uploader
, self
.slug
, self
.id)
62 if self
.id is not None:
63 self
.slug
= "%s-%s" % (self
.id, self
.slug
)
68 def description_html(self
):
70 Rendered version of the description, run through
71 Markdown and cleaned with our cleaning tool.
73 return cleaned_markdown_conversion(self
.description
)
75 def get_display_media(self
, media_map
,
76 fetch_order
=common
.DISPLAY_IMAGE_FETCHING_ORDER
):
78 Find the best media for display.
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
86 (media_size, media_path)
88 media_sizes
= media_map
.keys()
90 for media_size
in common
.DISPLAY_IMAGE_FETCHING_ORDER
:
91 if media_size
in media_sizes
:
92 return media_map
[media_size
]
94 def main_mediafile(self
):
99 return (self
.slug
or self
._id
)
101 def url_for_self(self
, urlgen
, **extra_args
):
103 Generate an appropriate url for ourselves
105 Use a slug if we have one, else use our '_id'.
107 uploader
= self
.get_uploader
110 'mediagoblin.user_pages.media_home',
111 user
=uploader
.username
,
112 media
=self
.slug_or_id
,
115 def get_fail_exception(self
):
117 Get the exception that's appropriate for this error
119 if self
['fail_error']:
120 return common
.import_component(self
['fail_error'])
122 def get_license_data(self
):
123 """Return license dict for requested license"""
124 return licenses
.SUPPORTED_LICENSES
[self
.license
or ""]
126 def exif_display_iter(self
):
127 from mediagoblin
.tools
.exif
import USEFUL_TAGS
129 if not self
.media_data
:
131 exif_all
= self
.media_data
.get("exif_all")
133 for key
in USEFUL_TAGS
:
135 yield key
, exif_all
[key
]
138 class MediaCommentMixin(object):
140 def content_html(self
):
142 the actual html-rendered version of the comment displayed.
143 Run through Markdown and the HTML cleaner.
145 return cleaned_markdown_conversion(self
.content
)