Added the ability to regenerate a verification key.
[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']
107
108 default_values = {
109 'created': datetime.datetime.utcnow,
110 'state': u'unprocessed'}
111
112 migration_handler = migrations.MediaEntryMigration
113
114 # Actually we should referene uniqueness by uploader, but we
115 # should fix http://bugs.foocorp.net/issues/340 first.
116 # indexes = [
117 # {'fields': ['uploader', 'slug'],
118 # 'unique': True}]
119
120 def main_mediafile(self):
121 pass
122
123 def generate_slug(self):
124 self['slug'] = util.slugify(self['title'])
125
126 duplicate = mediagoblin_globals.database.media_entries.find_one(
127 {'slug': self['slug']})
128
129 if duplicate:
130 self['slug'] = "%s-%s" % (self['_id'], self['slug'])
131
132 def url_for_self(self, urlgen):
133 """
134 Generate an appropriate url for ourselves
135
136 Use a slug if we have one, else use our '_id'.
137 """
138 uploader = self.uploader()
139
140 if self.get('slug'):
141 return urlgen(
142 'mediagoblin.user_pages.media_home',
143 user=uploader['username'],
144 media=self['slug'])
145 else:
146 return urlgen(
147 'mediagoblin.user_pages.media_home',
148 user=uploader['username'],
149 media=unicode(self['_id']))
150
151 def uploader(self):
152 return self.db.User.find_one({'_id': self['uploader']})
153
154
155 REGISTER_MODELS = [MediaEntry, User]
156
157
158 def register_models(connection):
159 """
160 Register all models in REGISTER_MODELS with this connection.
161 """
162 connection.register(REGISTER_MODELS)
163