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 |
0546833c | 23 | from mediagoblin import globals as mediagoblin_globals |
757f37a5 CAW |
24 | from mediagoblin.db import migrations |
25 | from mediagoblin.db.util import 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, | |
d232e0f6 CAW |
49 | } |
50 | ||
24181820 | 51 | required_fields = ['username', 'created', 'pw_hash', 'email'] |
fc9bb821 CAW |
52 | |
53 | default_values = { | |
24181820 | 54 | 'created': datetime.datetime.utcnow, |
4d75522b | 55 | 'email_verified': False, |
db1a438f | 56 | 'status': u'needs_email_verification', |
18cf34d4 CAW |
57 | 'verification_key': lambda: unicode(uuid.uuid4()), |
58 | 'is_admin': False} | |
fc9bb821 | 59 | |
4ad5af85 CAW |
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 | ||
d232e0f6 | 67 | |
4d75522b CAW |
68 | class MediaEntry(Document): |
69 | __collection__ = 'media_entries' | |
70 | ||
71 | structure = { | |
757f37a5 | 72 | 'uploader': ObjectId, |
4d75522b | 73 | 'title': unicode, |
1013bdaf | 74 | 'slug': unicode, |
4d75522b CAW |
75 | 'created': datetime.datetime, |
76 | 'description': unicode, | |
77 | 'media_type': unicode, | |
78 | 'media_data': dict, # extra data relevant to this media_type | |
79 | 'plugin_data': dict, # plugins can dump stuff here. | |
74ae6b11 CAW |
80 | 'tags': [unicode], |
81 | 'state': unicode, | |
82 | ||
fa7f9c61 CAW |
83 | # For now let's assume there can only be one main file queued |
84 | # at a time | |
85 | 'queued_media_file': [unicode], | |
86 | ||
87 | # A dictionary of logical names to filepaths | |
88 | 'media_files': dict, | |
89 | ||
74ae6b11 CAW |
90 | # The following should be lists of lists, in appropriate file |
91 | # record form | |
74ae6b11 | 92 | 'attachment_files': list, |
74ae6b11 CAW |
93 | |
94 | # This one should just be a single file record | |
95 | 'thumbnail_file': [unicode]} | |
4d75522b CAW |
96 | |
97 | required_fields = [ | |
bb49e56f | 98 | 'uploader', 'created', 'media_type'] |
4d75522b CAW |
99 | |
100 | default_values = { | |
74ae6b11 CAW |
101 | 'created': datetime.datetime.utcnow, |
102 | 'state': u'unprocessed'} | |
4d75522b | 103 | |
757f37a5 CAW |
104 | migration_handler = migrations.MediaEntryMigration |
105 | ||
931f318c CAW |
106 | # Actually we should referene uniqueness by uploader, but we |
107 | # should fix http://bugs.foocorp.net/issues/340 first. | |
108 | # indexes = [ | |
109 | # {'fields': ['uploader', 'slug'], | |
110 | # 'unique': True}] | |
37af09a2 | 111 | |
4d75522b CAW |
112 | def main_mediafile(self): |
113 | pass | |
114 | ||
0546833c AW |
115 | def generate_slug(self): |
116 | self['slug'] = util.slugify(self['title']) | |
117 | ||
f0545dde CAW |
118 | duplicate = mediagoblin_globals.database.media_entries.find_one( |
119 | {'slug': self['slug']}) | |
0546833c AW |
120 | |
121 | if duplicate: | |
122 | self['slug'] = "%s-%s" % (self['_id'], self['slug']) | |
4d75522b | 123 | |
6926b23d CAW |
124 | def url_for_self(self, urlgen): |
125 | """ | |
126 | Generate an appropriate url for ourselves | |
127 | ||
128 | Use a slug if we have one, else use our '_id'. | |
129 | """ | |
130 | if self.get('slug'): | |
131 | return urlgen( | |
132 | 'mediagoblin.user_pages.media_home', | |
133 | user=self['uploader']['username'], | |
134 | media=self['slug']) | |
135 | else: | |
136 | return urlgen( | |
137 | 'mediagoblin.user_pages.media_home', | |
138 | user=self['uploader']['username'], | |
139 | media=unicode(self['_id'])) | |
140 | ||
141 | ||
d232e0f6 CAW |
142 | REGISTER_MODELS = [MediaEntry, User] |
143 | ||
4329be14 | 144 | |
d232e0f6 CAW |
145 | def register_models(connection): |
146 | """ | |
147 | Register all models in REGISTER_MODELS with this connection. | |
148 | """ | |
db61f7d1 CAW |
149 | connection.register(REGISTER_MODELS) |
150 |