Markdown-enable user bio (Feature 410)
[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 mg_globals
24 from mediagoblin.db import migrations
25 from mediagoblin.db.util import ASCENDING, DESCENDING, 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 'url' : unicode,
50 'bio' : unicode, # May contain markdown
51 'bio_html': unicode, # May contain plaintext, or HTML
52 }
53
54 required_fields = ['username', 'created', 'pw_hash', 'email']
55
56 default_values = {
57 'created': datetime.datetime.utcnow,
58 'email_verified': False,
59 'status': u'needs_email_verification',
60 'verification_key': lambda: unicode(uuid.uuid4()),
61 'is_admin': False}
62
63 migration_handler = migrations.UserMigration
64
65 def check_login(self, password):
66 """
67 See if a user can login with this password
68 """
69 return auth_lib.bcrypt_check_password(
70 password, self['pw_hash'])
71
72
73 class MediaEntry(Document):
74 __collection__ = 'media_entries'
75
76 structure = {
77 'uploader': ObjectId,
78 'title': unicode,
79 'slug': unicode,
80 'created': datetime.datetime,
81 'description': unicode, # May contain markdown/up
82 'description_html': unicode, # May contain plaintext, or HTML
83 'media_type': unicode,
84 'media_data': dict, # extra data relevant to this media_type
85 'plugin_data': dict, # plugins can dump stuff here.
86 'tags': [unicode],
87 'state': unicode,
88
89 # For now let's assume there can only be one main file queued
90 # at a time
91 'queued_media_file': [unicode],
92
93 # A dictionary of logical names to filepaths
94 'media_files': dict,
95
96 # The following should be lists of lists, in appropriate file
97 # record form
98 'attachment_files': list,
99
100 # This one should just be a single file record
101 'thumbnail_file': [unicode]}
102
103 required_fields = [
104 'uploader', 'created', 'media_type', 'slug']
105
106 default_values = {
107 'created': datetime.datetime.utcnow,
108 'state': u'unprocessed'}
109
110 migration_handler = migrations.MediaEntryMigration
111
112 def get_comments(self):
113 return self.db.MediaComment.find({
114 'media_entry': self['_id']}).sort('created', DESCENDING)
115
116 def main_mediafile(self):
117 pass
118
119 def generate_slug(self):
120 self['slug'] = util.slugify(self['title'])
121
122 duplicate = mg_globals.database.media_entries.find_one(
123 {'slug': self['slug']})
124
125 if duplicate:
126 self['slug'] = "%s-%s" % (self['_id'], self['slug'])
127
128 def url_for_self(self, urlgen):
129 """
130 Generate an appropriate url for ourselves
131
132 Use a slug if we have one, else use our '_id'.
133 """
134 uploader = self.uploader()
135
136 if self.get('slug'):
137 return urlgen(
138 'mediagoblin.user_pages.media_home',
139 user=uploader['username'],
140 media=self['slug'])
141 else:
142 return urlgen(
143 'mediagoblin.user_pages.media_home',
144 user=uploader['username'],
145 media=unicode(self['_id']))
146
147 def url_to_prev(self, urlgen):
148 """
149 Provide a url to the previous entry from this user, if there is one
150 """
151 cursor = self.db.MediaEntry.find({'_id' : {"$gt": self['_id']},
152 'uploader': self['uploader'],
153 'state': 'processed'}).sort(
154 '_id', ASCENDING).limit(1)
155 if cursor.count():
156 return urlgen('mediagoblin.user_pages.media_home',
157 user=self.uploader()['username'],
158 media=unicode(cursor[0]['slug']))
159
160 def url_to_next(self, urlgen):
161 """
162 Provide a url to the next entry from this user, if there is one
163 """
164 cursor = self.db.MediaEntry.find({'_id' : {"$lt": self['_id']},
165 'uploader': self['uploader'],
166 'state': 'processed'}).sort(
167 '_id', DESCENDING).limit(1)
168
169 if cursor.count():
170 return urlgen('mediagoblin.user_pages.media_home',
171 user=self.uploader()['username'],
172 media=unicode(cursor[0]['slug']))
173
174 def uploader(self):
175 return self.db.User.find_one({'_id': self['uploader']})
176
177
178 class MediaComment(Document):
179 __collection__ = 'media_comments'
180
181 structure = {
182 'media_entry': ObjectId,
183 'author': ObjectId,
184 'created': datetime.datetime,
185 'content': unicode,
186 'content_html': unicode}
187
188 required_fields = [
189 'media_entry', 'author', 'created', 'content']
190
191 default_values = {
192 'created': datetime.datetime.utcnow}
193
194 def media_entry(self):
195 return self.db.MediaEntry.find_one({'_id': self['media_entry']})
196
197 def author(self):
198 return self.db.User.find_one({'_id': self['author']})
199
200 REGISTER_MODELS = [
201 MediaEntry,
202 User,
203 MediaComment]
204
205
206 def register_models(connection):
207 """
208 Register all models in REGISTER_MODELS with this connection.
209 """
210 connection.register(REGISTER_MODELS)
211