removes unecessary dependence on existence of username for User migration01, + fix...
[mediagoblin.git] / mediagoblin / db / models.py
CommitLineData
8e1e744d 1# GNU MediaGoblin -- federated, autonomous media hosting
e5572c60
ML
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
db1a438f 17import datetime, uuid
4ad5af85 18
d232e0f6 19from mongokit import Document, Set
4329be14 20
0546833c 21from mediagoblin import util
4ad5af85 22from mediagoblin.auth import lib as auth_lib
6e7ce8d1 23from mediagoblin import mg_globals
757f37a5
CAW
24from mediagoblin.db import migrations
25from mediagoblin.db.util import ObjectId
d232e0f6 26
7bf3f5db
CAW
27###################
28# Custom validators
29###################
30
31########
32# Models
33########
34
35
d232e0f6 36class User(Document):
73a6e206
CAW
37 __collection__ = 'users'
38
d232e0f6
CAW
39 structure = {
40 'username': unicode,
24181820 41 'email': unicode,
d232e0f6
CAW
42 'created': datetime.datetime,
43 'plugin_data': dict, # plugins can dump stuff here.
44 'pw_hash': unicode,
24181820 45 'email_verified': bool,
4d75522b 46 'status': unicode,
18cf34d4
CAW
47 'verification_key': unicode,
48 'is_admin': bool,
630b57a3 49 'url' : unicode,
279d925e 50 'bio' : unicode
d232e0f6
CAW
51 }
52
db5912e3 53 required_fields = ['username', 'created', 'pw_hash', 'email']
fc9bb821
CAW
54
55 default_values = {
24181820 56 'created': datetime.datetime.utcnow,
4d75522b 57 'email_verified': False,
db1a438f 58 'status': u'needs_email_verification',
18cf34d4
CAW
59 'verification_key': lambda: unicode(uuid.uuid4()),
60 'is_admin': False}
fc9bb821 61
4ad5af85
CAW
62 def check_login(self, password):
63 """
64 See if a user can login with this password
65 """
66 return auth_lib.bcrypt_check_password(
67 password, self['pw_hash'])
68
d232e0f6 69
4d75522b
CAW
70class MediaEntry(Document):
71 __collection__ = 'media_entries'
72
73 structure = {
757f37a5 74 'uploader': ObjectId,
4d75522b 75 'title': unicode,
1013bdaf 76 'slug': unicode,
4d75522b 77 'created': datetime.datetime,
44e2da2f
JW
78 'description': unicode, # May contain markdown/up
79 'description_html': unicode, # May contain plaintext, or HTML
4d75522b
CAW
80 'media_type': unicode,
81 'media_data': dict, # extra data relevant to this media_type
82 'plugin_data': dict, # plugins can dump stuff here.
74ae6b11
CAW
83 'tags': [unicode],
84 'state': unicode,
85
fa7f9c61
CAW
86 # For now let's assume there can only be one main file queued
87 # at a time
88 'queued_media_file': [unicode],
89
90 # A dictionary of logical names to filepaths
91 'media_files': dict,
92
74ae6b11
CAW
93 # The following should be lists of lists, in appropriate file
94 # record form
74ae6b11 95 'attachment_files': list,
74ae6b11
CAW
96
97 # This one should just be a single file record
98 'thumbnail_file': [unicode]}
4d75522b
CAW
99
100 required_fields = [
b1ae76ae 101 'uploader', 'created', 'media_type', 'slug']
4d75522b
CAW
102
103 default_values = {
74ae6b11
CAW
104 'created': datetime.datetime.utcnow,
105 'state': u'unprocessed'}
4d75522b 106
757f37a5
CAW
107 migration_handler = migrations.MediaEntryMigration
108
b1ae76ae
CAW
109 indexes = [
110 # Referene uniqueness of slugs by uploader
111 {'fields': ['uploader', 'slug'],
112 'unique': True}]
37af09a2 113
4d75522b
CAW
114 def main_mediafile(self):
115 pass
116
0546833c
AW
117 def generate_slug(self):
118 self['slug'] = util.slugify(self['title'])
119
6e7ce8d1 120 duplicate = mg_globals.database.media_entries.find_one(
f0545dde 121 {'slug': self['slug']})
0546833c
AW
122
123 if duplicate:
124 self['slug'] = "%s-%s" % (self['_id'], self['slug'])
4d75522b 125
6926b23d
CAW
126 def url_for_self(self, urlgen):
127 """
128 Generate an appropriate url for ourselves
129
130 Use a slug if we have one, else use our '_id'.
131 """
16509be1
CAW
132 uploader = self.uploader()
133
6926b23d
CAW
134 if self.get('slug'):
135 return urlgen(
136 'mediagoblin.user_pages.media_home',
16509be1 137 user=uploader['username'],
6926b23d
CAW
138 media=self['slug'])
139 else:
140 return urlgen(
141 'mediagoblin.user_pages.media_home',
16509be1 142 user=uploader['username'],
6926b23d
CAW
143 media=unicode(self['_id']))
144
16509be1
CAW
145 def uploader(self):
146 return self.db.User.find_one({'_id': self['uploader']})
147
6926b23d 148
d232e0f6
CAW
149REGISTER_MODELS = [MediaEntry, User]
150
4329be14 151
d232e0f6
CAW
152def register_models(connection):
153 """
154 Register all models in REGISTER_MODELS with this connection.
155 """
db61f7d1
CAW
156 connection.register(REGISTER_MODELS)
157