Fixing changes
[mediagoblin.git] / mediagoblin / models.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 Free Software Foundation, Inc
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 import datetime, uuid
18
19 from mongokit import Document, Set
20
21 from mediagoblin import util
22 from mediagoblin.auth import lib as auth_lib
23 from mediagoblin import globals as mediagoblin_globals
24
25 ###################
26 # Custom validators
27 ###################
28
29 ########
30 # Models
31 ########
32
33
34 class User(Document):
35 __collection__ = 'users'
36
37 structure = {
38 'username': unicode,
39 'email': unicode,
40 'created': datetime.datetime,
41 'plugin_data': dict, # plugins can dump stuff here.
42 'pw_hash': unicode,
43 'email_verified': bool,
44 'status': unicode,
45 'verification_key': unicode
46 }
47
48 required_fields = ['username', 'created', 'pw_hash', 'email']
49
50 default_values = {
51 'created': datetime.datetime.utcnow,
52 'email_verified': False,
53 'status': u'needs_email_verification',
54 'verification_key': lambda: unicode( uuid.uuid4() ) }
55
56 def check_login(self, password):
57 """
58 See if a user can login with this password
59 """
60 return auth_lib.bcrypt_check_password(
61 password, self['pw_hash'])
62
63
64 class MediaEntry(Document):
65 __collection__ = 'media_entries'
66
67 structure = {
68 'uploader': User,
69 'title': unicode,
70 'slug': unicode,
71 'created': datetime.datetime,
72 'description': unicode,
73 'media_type': unicode,
74 'media_data': dict, # extra data relevant to this media_type
75 'plugin_data': dict, # plugins can dump stuff here.
76 'tags': [unicode],
77 'state': unicode,
78
79 # For now let's assume there can only be one main file queued
80 # at a time
81 'queued_media_file': [unicode],
82
83 # A dictionary of logical names to filepaths
84 'media_files': dict,
85
86 # The following should be lists of lists, in appropriate file
87 # record form
88 'attachment_files': list,
89
90 # This one should just be a single file record
91 'thumbnail_file': [unicode]}
92
93 required_fields = [
94 'uploader', 'created', 'media_type']
95
96 default_values = {
97 'created': datetime.datetime.utcnow,
98 'state': u'unprocessed'}
99
100 def main_mediafile(self):
101 pass
102
103 def generate_slug(self):
104 self['slug'] = util.slugify(self['title'])
105
106 duplicate = mediagoblin_globals.database.media_entries.find_one({'slug': self['slug']})
107
108 if duplicate:
109 self['slug'] = "%s-%s" % (self['_id'], self['slug'])
110
111 REGISTER_MODELS = [MediaEntry, User]
112
113
114 def register_models(connection):
115 """
116 Register all models in REGISTER_MODELS with this connection.
117 """
118 connection.register(REGISTER_MODELS)
119