Commit | Line | Data |
---|---|---|
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 | 17 | import datetime, uuid |
4ad5af85 | 18 | |
d232e0f6 | 19 | from mongokit import Document, Set |
4329be14 | 20 | |
0546833c | 21 | from mediagoblin import util |
4ad5af85 | 22 | from mediagoblin.auth import lib as auth_lib |
6e7ce8d1 | 23 | from mediagoblin import mg_globals |
757f37a5 | 24 | from mediagoblin.db import migrations |
9c0fe63f | 25 | from mediagoblin.db.util import ASCENDING, DESCENDING, ObjectId |
d232e0f6 | 26 | |
7bf3f5db CAW |
27 | ################### |
28 | # Custom validators | |
29 | ################### | |
30 | ||
31 | ######## | |
32 | # Models | |
33 | ######## | |
34 | ||
35 | ||
d232e0f6 | 36 | class 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} | |
0472653e | 61 | |
62 | migration_handler = migrations.UserMigration | |
fc9bb821 | 63 | |
4ad5af85 CAW |
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 | ||
d232e0f6 | 71 | |
4d75522b CAW |
72 | class MediaEntry(Document): |
73 | __collection__ = 'media_entries' | |
74 | ||
75 | structure = { | |
757f37a5 | 76 | 'uploader': ObjectId, |
4d75522b | 77 | 'title': unicode, |
1013bdaf | 78 | 'slug': unicode, |
4d75522b | 79 | 'created': datetime.datetime, |
44e2da2f JW |
80 | 'description': unicode, # May contain markdown/up |
81 | 'description_html': unicode, # May contain plaintext, or HTML | |
4d75522b CAW |
82 | 'media_type': unicode, |
83 | 'media_data': dict, # extra data relevant to this media_type | |
84 | 'plugin_data': dict, # plugins can dump stuff here. | |
74ae6b11 CAW |
85 | 'tags': [unicode], |
86 | 'state': unicode, | |
87 | ||
fa7f9c61 CAW |
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 | ||
74ae6b11 CAW |
95 | # The following should be lists of lists, in appropriate file |
96 | # record form | |
74ae6b11 | 97 | 'attachment_files': list, |
74ae6b11 CAW |
98 | |
99 | # This one should just be a single file record | |
100 | 'thumbnail_file': [unicode]} | |
4d75522b CAW |
101 | |
102 | required_fields = [ | |
b1ae76ae | 103 | 'uploader', 'created', 'media_type', 'slug'] |
4d75522b CAW |
104 | |
105 | default_values = { | |
74ae6b11 CAW |
106 | 'created': datetime.datetime.utcnow, |
107 | 'state': u'unprocessed'} | |
4d75522b | 108 | |
757f37a5 CAW |
109 | migration_handler = migrations.MediaEntryMigration |
110 | ||
6f59a3a3 JW |
111 | def get_comments(self): |
112 | return self.db.MediaComment.find({ | |
113 | 'media_entry': self['_id']}).sort('created', DESCENDING) | |
114 | ||
4d75522b CAW |
115 | def main_mediafile(self): |
116 | pass | |
6f59a3a3 | 117 | |
0546833c AW |
118 | def generate_slug(self): |
119 | self['slug'] = util.slugify(self['title']) | |
120 | ||
6e7ce8d1 | 121 | duplicate = mg_globals.database.media_entries.find_one( |
f0545dde | 122 | {'slug': self['slug']}) |
0546833c AW |
123 | |
124 | if duplicate: | |
125 | self['slug'] = "%s-%s" % (self['_id'], self['slug']) | |
4d75522b | 126 | |
6926b23d CAW |
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 | """ | |
16509be1 CAW |
133 | uploader = self.uploader() |
134 | ||
6926b23d CAW |
135 | if self.get('slug'): |
136 | return urlgen( | |
137 | 'mediagoblin.user_pages.media_home', | |
16509be1 | 138 | user=uploader['username'], |
6926b23d CAW |
139 | media=self['slug']) |
140 | else: | |
141 | return urlgen( | |
142 | 'mediagoblin.user_pages.media_home', | |
16509be1 | 143 | user=uploader['username'], |
6926b23d | 144 | media=unicode(self['_id'])) |
9c0fe63f CFD |
145 | |
146 | def url_to_prev(self, urlgen): | |
147 | """ | |
148 | Provide a url to the previous entry from this user, if there is one | |
149 | """ | |
e6fd112d | 150 | cursor = self.db.MediaEntry.find({'_id' : {"$gt": self['_id']}, |
ce2ac488 CFD |
151 | 'uploader': self['uploader'], |
152 | 'state': 'processed'}).sort( | |
77b95801 | 153 | '_id', ASCENDING).limit(1) |
9c0fe63f CFD |
154 | if cursor.count(): |
155 | return urlgen('mediagoblin.user_pages.media_home', | |
156 | user=self.uploader()['username'], | |
b1db2c2e | 157 | media=unicode(cursor[0]['slug'])) |
9c0fe63f CFD |
158 | |
159 | def url_to_next(self, urlgen): | |
160 | """ | |
161 | Provide a url to the next entry from this user, if there is one | |
162 | """ | |
e6fd112d | 163 | cursor = self.db.MediaEntry.find({'_id' : {"$lt": self['_id']}, |
ce2ac488 CFD |
164 | 'uploader': self['uploader'], |
165 | 'state': 'processed'}).sort( | |
77b95801 | 166 | '_id', DESCENDING).limit(1) |
9c0fe63f CFD |
167 | |
168 | if cursor.count(): | |
169 | return urlgen('mediagoblin.user_pages.media_home', | |
170 | user=self.uploader()['username'], | |
b1db2c2e | 171 | media=unicode(cursor[0]['slug'])) |
6926b23d | 172 | |
16509be1 CAW |
173 | def uploader(self): |
174 | return self.db.User.find_one({'_id': self['uploader']}) | |
175 | ||
b27ec167 | 176 | |
c11f21ab JW |
177 | class MediaComment(Document): |
178 | __collection__ = 'media_comments' | |
6926b23d | 179 | |
c11f21ab JW |
180 | structure = { |
181 | 'media_entry': ObjectId, | |
182 | 'author': ObjectId, | |
183 | 'created': datetime.datetime, | |
184 | 'content': unicode, | |
185 | 'content_html': unicode} | |
186 | ||
187 | required_fields = [ | |
7bd8197f | 188 | 'media_entry', 'author', 'created', 'content'] |
c11f21ab JW |
189 | |
190 | default_values = { | |
191 | 'created': datetime.datetime.utcnow} | |
192 | ||
193 | def media_entry(self): | |
7bd8197f | 194 | return self.db.MediaEntry.find_one({'_id': self['media_entry']}) |
c11f21ab JW |
195 | |
196 | def author(self): | |
197 | return self.db.User.find_one({'_id': self['author']}) | |
6926b23d | 198 | |
c11f21ab JW |
199 | REGISTER_MODELS = [ |
200 | MediaEntry, | |
201 | User, | |
202 | MediaComment] | |
d232e0f6 | 203 | |
4329be14 | 204 | |
d232e0f6 CAW |
205 | def register_models(connection): |
206 | """ | |
207 | Register all models in REGISTER_MODELS with this connection. | |
208 | """ | |
db61f7d1 CAW |
209 | connection.register(REGISTER_MODELS) |
210 |