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