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 globals as mediagoblin_globals
24 from mediagoblin
.db
import migrations
25 from mediagoblin
.db
.util
import ObjectId
37 __collection__
= 'users'
42 'created': datetime
.datetime
,
43 'plugin_data': dict, # plugins can dump stuff here.
45 'email_verified': bool,
47 'verification_key': unicode,
51 required_fields
= ['username', 'created', 'pw_hash', 'email']
54 'created': datetime
.datetime
.utcnow
,
55 'email_verified': False,
56 'status': u
'needs_email_verification',
57 'verification_key': lambda: unicode(uuid
.uuid4()),
60 def check_login(self
, password
):
62 See if a user can login with this password
64 return auth_lib
.bcrypt_check_password(
65 password
, self
['pw_hash'])
67 def generate_new_verification_key(self
):
69 Create a new verification key, overwriting the old one.
72 self
['verification_key'] = unicode(uuid
.uuid4())
73 self
.save(validate
=False)
76 class MediaEntry(Document
):
77 __collection__
= 'media_entries'
83 'created': datetime
.datetime
,
84 'description': unicode,
85 'media_type': unicode,
86 'media_data': dict, # extra data relevant to this media_type
87 'plugin_data': dict, # plugins can dump stuff here.
91 # For now let's assume there can only be one main file queued
93 'queued_media_file': [unicode],
95 # A dictionary of logical names to filepaths
98 # The following should be lists of lists, in appropriate file
100 'attachment_files': list,
102 # This one should just be a single file record
103 'thumbnail_file': [unicode]}
106 'uploader', 'created', 'media_type', 'slug']
109 'created': datetime
.datetime
.utcnow
,
110 'state': u
'unprocessed'}
112 migration_handler
= migrations
.MediaEntryMigration
115 # Referene uniqueness of slugs by uploader
116 {'fields': ['uploader', 'slug'],
119 def main_mediafile(self
):
122 def generate_slug(self
):
123 self
['slug'] = util
.slugify(self
['title'])
125 duplicate
= mediagoblin_globals
.database
.media_entries
.find_one(
126 {'slug': self
['slug']})
129 self
['slug'] = "%s-%s" % (self
['_id'], self
['slug'])
131 def url_for_self(self
, urlgen
):
133 Generate an appropriate url for ourselves
135 Use a slug if we have one, else use our '_id'.
137 uploader
= self
.uploader()
141 'mediagoblin.user_pages.media_home',
142 user
=uploader
['username'],
146 'mediagoblin.user_pages.media_home',
147 user
=uploader
['username'],
148 media
=unicode(self
['_id']))
151 return self
.db
.User
.find_one({'_id': self
['uploader']})
154 REGISTER_MODELS
= [MediaEntry
, User
]
157 def register_models(connection
):
159 Register all models in REGISTER_MODELS with this connection.
161 connection
.register(REGISTER_MODELS
)