Commit | Line | Data |
---|---|---|
f42e49c3 | 1 | # GNU MediaGoblin -- federated, autonomous media hosting |
7f4ebeed | 2 | # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS. |
f42e49c3 E |
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 | ||
814334f6 | 30 | from mediagoblin import mg_globals |
f42e49c3 | 31 | from mediagoblin.auth import lib as auth_lib |
17c23e15 | 32 | from mediagoblin.tools import common, licenses |
e61ab099 | 33 | from mediagoblin.tools.text import cleaned_markdown_conversion |
814334f6 | 34 | from mediagoblin.tools.url import slugify |
f42e49c3 E |
35 | |
36 | ||
37 | class UserMixin(object): | |
38 | def check_login(self, password): | |
39 | """ | |
40 | See if a user can login with this password | |
41 | """ | |
42 | return auth_lib.bcrypt_check_password( | |
43 | password, self.pw_hash) | |
44 | ||
e61ab099 E |
45 | @property |
46 | def bio_html(self): | |
47 | return cleaned_markdown_conversion(self.bio) | |
48 | ||
f42e49c3 E |
49 | |
50 | class MediaEntryMixin(object): | |
814334f6 E |
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 | |
55 | ||
56 | self.slug = slugify(self.title) | |
57 | ||
58 | duplicate = check_media_slug_used(mg_globals.database, | |
59 | self.uploader, self.slug, self.id) | |
60 | ||
61 | if duplicate: | |
62 | if self.id is not None: | |
5bd0adeb | 63 | self.slug = u"%s-%s" % (self.id, self.slug) |
814334f6 E |
64 | else: |
65 | self.slug = None | |
66 | ||
1e72e075 E |
67 | @property |
68 | def description_html(self): | |
69 | """ | |
70 | Rendered version of the description, run through | |
71 | Markdown and cleaned with our cleaning tool. | |
72 | """ | |
73 | return cleaned_markdown_conversion(self.description) | |
74 | ||
f42e49c3 E |
75 | def get_display_media(self, media_map, |
76 | fetch_order=common.DISPLAY_IMAGE_FETCHING_ORDER): | |
77 | """ | |
78 | Find the best media for display. | |
79 | ||
80 | Args: | |
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 | |
84 | ||
85 | Returns: | |
86 | (media_size, media_path) | |
87 | """ | |
88 | media_sizes = media_map.keys() | |
89 | ||
90 | for media_size in common.DISPLAY_IMAGE_FETCHING_ORDER: | |
91 | if media_size in media_sizes: | |
92 | return media_map[media_size] | |
93 | ||
94 | def main_mediafile(self): | |
95 | pass | |
96 | ||
3e907d55 E |
97 | @property |
98 | def slug_or_id(self): | |
99 | return (self.slug or self._id) | |
100 | ||
cb7ae1e4 | 101 | def url_for_self(self, urlgen, **extra_args): |
f42e49c3 E |
102 | """ |
103 | Generate an appropriate url for ourselves | |
104 | ||
105 | Use a slug if we have one, else use our '_id'. | |
106 | """ | |
107 | uploader = self.get_uploader | |
108 | ||
3e907d55 E |
109 | return urlgen( |
110 | 'mediagoblin.user_pages.media_home', | |
111 | user=uploader.username, | |
112 | media=self.slug_or_id, | |
113 | **extra_args) | |
f42e49c3 | 114 | |
2e4ad359 SS |
115 | @property |
116 | def thumb_url(self): | |
117 | """Return the thumbnail URL (for usage in templates) | |
118 | Will return either the real thumbnail or a default fallback icon.""" | |
119 | # TODO: implement generic fallback in case MEDIA_MANAGER does | |
120 | # not specify one? | |
121 | if u'thumb' in self.media_files: | |
122 | thumb_url = mg_globals.app.public_store.file_url( | |
123 | self.media_files[u'thumb']) | |
124 | else: | |
125 | # no thumbnail in media available. Get the media's | |
126 | # MEDIA_MANAGER for the fallback icon and return static URL | |
27886480 | 127 | manager = __import__(self.media_type) |
2e4ad359 SS |
128 | thumb_url = manager.MEDIA_MANAGER[u'default_thumb'] |
129 | thumb_url = mg_globals.app.staticdirector(thumb_url) # use static | |
130 | return thumb_url | |
131 | ||
f42e49c3 E |
132 | def get_fail_exception(self): |
133 | """ | |
134 | Get the exception that's appropriate for this error | |
135 | """ | |
51eb0267 JW |
136 | if self.fail_error: |
137 | return common.import_component(self.fail_error) | |
17c23e15 AW |
138 | |
139 | def get_license_data(self): | |
140 | """Return license dict for requested license""" | |
2788e6a1 | 141 | return licenses.SUPPORTED_LICENSES[self.license or ""] |
feba5c52 | 142 | |
5bad26bc | 143 | def exif_display_iter(self): |
7b82f56b E |
144 | from mediagoblin.tools.exif import USEFUL_TAGS |
145 | ||
5bad26bc E |
146 | if not self.media_data: |
147 | return | |
148 | exif_all = self.media_data.get("exif_all") | |
149 | ||
150 | for key in USEFUL_TAGS: | |
151 | if key in exif_all: | |
152 | yield key, exif_all[key] | |
153 | ||
feba5c52 E |
154 | |
155 | class MediaCommentMixin(object): | |
156 | @property | |
157 | def content_html(self): | |
158 | """ | |
159 | the actual html-rendered version of the comment displayed. | |
160 | Run through Markdown and the HTML cleaner. | |
161 | """ | |
162 | return cleaned_markdown_conversion(self.content) | |
be5be115 AW |
163 | |
164 | ||
165 | class CollectionMixin(object): | |
166 | def generate_slug(self): | |
167 | # import this here due to a cyclic import issue | |
168 | # (db.models -> db.mixin -> db.util -> db.models) | |
169 | from mediagoblin.db.util import check_collection_slug_used | |
170 | ||
171 | self.slug = slugify(self.title) | |
172 | ||
173 | duplicate = check_collection_slug_used(mg_globals.database, | |
174 | self.creator, self.slug, self.id) | |
175 | ||
176 | if duplicate: | |
177 | if self.id is not None: | |
178 | self.slug = u"%s-%s" % (self.id, self.slug) | |
179 | else: | |
180 | self.slug = None | |
181 | ||
182 | @property | |
183 | def description_html(self): | |
184 | """ | |
185 | Rendered version of the description, run through | |
186 | Markdown and cleaned with our cleaning tool. | |
187 | """ | |
188 | return cleaned_markdown_conversion(self.description) | |
189 | ||
190 | @property | |
191 | def slug_or_id(self): | |
192 | return (self.slug or self._id) | |
193 | ||
194 | def url_for_self(self, urlgen, **extra_args): | |
195 | """ | |
196 | Generate an appropriate url for ourselves | |
197 | ||
198 | Use a slug if we have one, else use our '_id'. | |
199 | """ | |
200 | creator = self.get_creator | |
201 | ||
202 | return urlgen( | |
256f816f | 203 | 'mediagoblin.user_pages.user_collection', |
be5be115 AW |
204 | user=creator.username, |
205 | collection=self.slug_or_id, | |
206 | **extra_args) | |
207 | ||
6d1e55b2 | 208 | |
be5be115 AW |
209 | class CollectionItemMixin(object): |
210 | @property | |
211 | def note_html(self): | |
212 | """ | |
213 | the actual html-rendered version of the note displayed. | |
214 | Run through Markdown and the HTML cleaner. | |
215 | """ | |
216 | return cleaned_markdown_conversion(self.note) |