Merge remote branch 'remotes/aleks/aleks'
[mediagoblin.git] / mediagoblin / db / 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 from mediagoblin.db import migrations
25 from mediagoblin.db.util import ObjectId
26
27 ###################
28 # Custom validators
29 ###################
30
31 ########
32 # Models
33 ########
34
35
36 class User(Document):
37 __collection__ = 'users'
38
39 structure = {
40 'username': unicode,
41 'email': unicode,
42 'created': datetime.datetime,
43 'plugin_data': dict, # plugins can dump stuff here.
44 'pw_hash': unicode,
45 'email_verified': bool,
46 'status': unicode,
47 'verification_key': unicode,
48 'is_admin': bool,
49 }
50
51 required_fields = ['username', 'created', 'pw_hash', 'email']
52
53 default_values = {
54 'created': datetime.datetime.utcnow,
55 'email_verified': False,
56 'status': u'needs_email_verification',
57 'verification_key': lambda: unicode(uuid.uuid4()),
58 'is_admin': False}
59
60 def check_login(self, password):
61 """
62 See if a user can login with this password
63 """
64 return auth_lib.bcrypt_check_password(
65 password, self['pw_hash'])
66
67 def generate_new_verification_key(self):
68 """
69 Create a new verification key, overwriting the old one.
70 """
71
72 self['verification_key'] = unicode(uuid.uuid4())
73 self.save(validate=False)
74
75
76 class MediaEntry(Document):
77 __collection__ = 'media_entries'
78
79 structure = {
80 'uploader': ObjectId,
81 'title': unicode,
82 'slug': unicode,
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.
88 'tags': [unicode],
89 'state': unicode,
90
91 # For now let's assume there can only be one main file queued
92 # at a time
93 'queued_media_file': [unicode],
94
95 # A dictionary of logical names to filepaths
96 'media_files': dict,
97
98 # The following should be lists of lists, in appropriate file
99 # record form
100 'attachment_files': list,
101
102 # This one should just be a single file record
103 'thumbnail_file': [unicode]}
104
105 required_fields = [
106 'uploader', 'created', 'media_type', 'slug']
107
108 default_values = {
109 'created': datetime.datetime.utcnow,
110 'state': u'unprocessed'}
111
112 migration_handler = migrations.MediaEntryMigration
113
114 indexes = [
115 # Referene uniqueness of slugs by uploader
116 {'fields': ['uploader', 'slug'],
117 'unique': True}]
118
119 def main_mediafile(self):
120 pass
121
122 def generate_slug(self):
123 self['slug'] = util.slugify(self['title'])
124
125 duplicate = mediagoblin_globals.database.media_entries.find_one(
126 {'slug': self['slug']})
127
128 if duplicate:
129 self['slug'] = "%s-%s" % (self['_id'], self['slug'])
130
131 def url_for_self(self, urlgen):
132 """
133 Generate an appropriate url for ourselves
134
135 Use a slug if we have one, else use our '_id'.
136 """
137 uploader = self.uploader()
138
139 if self.get('slug'):
140 return urlgen(
141 'mediagoblin.user_pages.media_home',
142 user=uploader['username'],
143 media=self['slug'])
144 else:
145 return urlgen(
146 'mediagoblin.user_pages.media_home',
147 user=uploader['username'],
148 media=unicode(self['_id']))
149
150 def uploader(self):
151 return self.db.User.find_one({'_id': self['uploader']})
152
153
154 REGISTER_MODELS = [MediaEntry, User]
155
156
157 def register_models(connection):
158 """
159 Register all models in REGISTER_MODELS with this connection.
160 """
161 connection.register(REGISTER_MODELS)
162