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 uuid
import uuid4
32 from werkzeug
.utils
import cached_property
34 from mediagoblin
import mg_globals
35 from mediagoblin
.auth
import lib
as auth_lib
36 from mediagoblin
.media_types
import get_media_managers
, FileTypeNotSupported
37 from mediagoblin
.tools
import common
, licenses
38 from mediagoblin
.tools
.text
import cleaned_markdown_conversion
39 from mediagoblin
.tools
.url
import slugify
42 class UserMixin(object):
43 def check_login(self
, password
):
45 See if a user can login with this password
47 return auth_lib
.bcrypt_check_password(
48 password
, self
.pw_hash
)
52 return cleaned_markdown_conversion(self
.bio
)
55 class MediaEntryMixin(object):
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
61 #Is already a slug assigned? Check if it is valid
63 self
.slug
= slugify(self
.slug
)
65 #assign slug based on title
66 self
.slug
= slugify(self
.title
)
68 # Does the object already have an ID? (after adding to the session)
69 self
.slug
= unicode(self
.id)
71 # Everything else failed, just use random garbage
72 self
.slug
= unicode(uuid4())[1:4]
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]
79 def description_html(self
):
81 Rendered version of the description, run through
82 Markdown and cleaned with our cleaning tool.
84 return cleaned_markdown_conversion(self
.description
)
86 def get_display_media(self
, media_map
,
87 fetch_order
=common
.DISPLAY_IMAGE_FETCHING_ORDER
):
89 Find the best media for display.
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
97 (media_size, media_path)
99 media_sizes
= media_map
.keys()
101 for media_size
in common
.DISPLAY_IMAGE_FETCHING_ORDER
:
102 if media_size
in media_sizes
:
103 return media_map
[media_size
]
105 def main_mediafile(self
):
109 def slug_or_id(self
):
110 return (self
.slug
or self
.id)
113 def url_for_self(self
, urlgen
, **extra_args
):
115 Generate an appropriate url for ourselves
117 Use a slug if we have one, else use our 'id'.
119 uploader
= self
.get_uploader
122 'mediagoblin.user_pages.media_home',
123 user
=uploader
.username
,
124 media
=self
.slug_or_id
,
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
133 if u
'thumb' in self
.media_files
:
134 thumb_url
= mg_globals
.app
.public_store
.file_url(
135 self
.media_files
[u
'thumb'])
137 # No thumbnail in media available. Get the media's
138 # MEDIA_MANAGER for the fallback icon and return static URL
139 # Raises FileTypeNotSupported in case no such manager is enabled
140 manager
= self
.media_manager
141 thumb_url
= mg_globals
.app
.staticdirector(manager
[u
'default_thumb'])
145 def media_manager(self
):
146 """Returns the MEDIA_MANAGER of the media's media_type
148 Raises FileTypeNotSupported in case no such manager is enabled
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
:
155 # Not found? Then raise an error
156 raise FileTypeNotSupported(
157 "MediaManager not in enabled types. Check media_types in config?")
159 def get_fail_exception(self
):
161 Get the exception that's appropriate for this error
164 return common
.import_component(self
.fail_error
)
166 def get_license_data(self
):
167 """Return license dict for requested license"""
168 return licenses
.get_license_by_url(self
.license
or "")
170 def exif_display_iter(self
):
171 from mediagoblin
.tools
.exif
import USEFUL_TAGS
173 if not self
.media_data
:
175 exif_all
= self
.media_data
.get("exif_all")
177 for key
in USEFUL_TAGS
:
179 yield key
, exif_all
[key
]
182 class MediaCommentMixin(object):
184 def content_html(self
):
186 the actual html-rendered version of the comment displayed.
187 Run through Markdown and the HTML cleaner.
189 return cleaned_markdown_conversion(self
.content
)
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
198 self
.slug
= slugify(self
.title
)
200 duplicate
= check_collection_slug_used(mg_globals
.database
,
201 self
.creator
, self
.slug
, self
.id)
204 if self
.id is not None:
205 self
.slug
= u
"%s-%s" % (self
.id, self
.slug
)
210 def description_html(self
):
212 Rendered version of the description, run through
213 Markdown and cleaned with our cleaning tool.
215 return cleaned_markdown_conversion(self
.description
)
218 def slug_or_id(self
):
219 return (self
.slug
or self
.id)
221 def url_for_self(self
, urlgen
, **extra_args
):
223 Generate an appropriate url for ourselves
225 Use a slug if we have one, else use our 'id'.
227 creator
= self
.get_creator
230 'mediagoblin.user_pages.user_collection',
231 user
=creator
.username
,
232 collection
=self
.slug_or_id
,
236 class CollectionItemMixin(object):
240 the actual html-rendered version of the note displayed.
241 Run through Markdown and the HTML cleaner.
243 return cleaned_markdown_conversion(self
.note
)