1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 Free Software Foundation, Inc
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/>.
19 from mongokit
import Document
, Set
21 from mediagoblin
import util
22 from mediagoblin
.auth
import lib
as auth_lib
23 from mediagoblin
import mg_globals
24 from mediagoblin
.db
import migrations
25 from mediagoblin
.db
.util
import DESCENDING
, ObjectId
26 from mediagoblin
.util
import Pagination
38 __collection__
= 'users'
43 'created': datetime
.datetime
,
44 'plugin_data': dict, # plugins can dump stuff here.
46 'email_verified': bool,
48 'verification_key': unicode,
54 required_fields
= ['username', 'created', 'pw_hash', 'email']
57 'created': datetime
.datetime
.utcnow
,
58 'email_verified': False,
59 'status': u
'needs_email_verification',
60 'verification_key': lambda: unicode(uuid
.uuid4()),
63 migration_handler
= migrations
.UserMigration
65 def check_login(self
, password
):
67 See if a user can login with this password
69 return auth_lib
.bcrypt_check_password(
70 password
, self
['pw_hash'])
73 class MediaEntry(Document
):
74 __collection__
= 'media_entries'
80 'created': datetime
.datetime
,
81 'description': unicode, # May contain markdown/up
82 'description_html': unicode, # May contain plaintext, or HTML
83 'media_type': unicode,
84 'media_data': dict, # extra data relevant to this media_type
85 'plugin_data': dict, # plugins can dump stuff here.
89 # For now let's assume there can only be one main file queued
91 'queued_media_file': [unicode],
93 # A dictionary of logical names to filepaths
96 # The following should be lists of lists, in appropriate file
98 'attachment_files': list,
100 # This one should just be a single file record
101 'thumbnail_file': [unicode]}
104 'uploader', 'created', 'media_type', 'slug']
107 'created': datetime
.datetime
.utcnow
,
108 'state': u
'unprocessed'}
110 migration_handler
= migrations
.MediaEntryMigration
113 # Referene uniqueness of slugs by uploader
114 {'fields': ['uploader', 'slug'],
117 def main_mediafile(self
):
120 def get_comments(self
, page
):
121 cursor
= self
.db
.MediaComment
.find({
122 'media_entry': self
['_id']}).sort('created', DESCENDING
)
124 pagination
= Pagination(page
, cursor
)
125 comments
= pagination()
128 for comment
in comments
:
129 comment
['author'] = self
.db
.User
.find_one({
130 '_id': comment
['author']})
133 return (data
, pagination
)
135 def generate_slug(self
):
136 self
['slug'] = util
.slugify(self
['title'])
138 duplicate
= mg_globals
.database
.media_entries
.find_one(
139 {'slug': self
['slug']})
142 self
['slug'] = "%s-%s" % (self
['_id'], self
['slug'])
144 def url_for_self(self
, urlgen
):
146 Generate an appropriate url for ourselves
148 Use a slug if we have one, else use our '_id'.
150 uploader
= self
.uploader()
154 'mediagoblin.user_pages.media_home',
155 user
=uploader
['username'],
159 'mediagoblin.user_pages.media_home',
160 user
=uploader
['username'],
161 media
=unicode(self
['_id']))
164 return self
.db
.User
.find_one({'_id': self
['uploader']})
166 class MediaComment(Document
):
167 __collection__
= 'media_comments'
170 'media_entry': ObjectId
,
172 'created': datetime
.datetime
,
174 'content_html': unicode}
177 'media_entry', 'author', 'created', 'content']
180 'created': datetime
.datetime
.utcnow
}
182 def media_entry(self
):
183 return self
.db
.MediaEntry
.find_one({'_id': self
['media_entry']})
186 return self
.db
.User
.find_one({'_id': self
['author']})
194 def register_models(connection
):
196 Register all models in REGISTER_MODELS with this connection.
198 connection
.register(REGISTER_MODELS
)