Merge branch 'master' of http://git.gitorious.org/mediagoblin/mediagoblin
[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 globals as mediagoblin_globals
24 from mediagoblin.db import migrations
25 from mediagoblin.db.util import 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 }
50
51 required_fields = ['username', 'created', 'pw_hash', 'email']
52
53 default_values = {
54 'created': datetime.datetime.utcnow,
55 'email_verified': False,
56 'status': u'needs_email_verification',
57 'verification_key': lambda: unicode(uuid.uuid4()),
58 'is_admin': False}
59
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
67
68 class MediaEntry(Document):
69 __collection__ = 'media_entries'
70
71 structure = {
72 'uploader': ObjectId,
73 'title': unicode,
74 'slug': unicode,
75 'created': datetime.datetime,
76 'description': unicode, # May contain markdown/up
77 'description_html': unicode, # May contain plaintext, or HTML
78 'media_type': unicode,
79 'media_data': dict, # extra data relevant to this media_type
80 'plugin_data': dict, # plugins can dump stuff here.
81 'tags': [unicode],
82 'state': unicode,
83
84 # For now let's assume there can only be one main file queued
85 # at a time
86 'queued_media_file': [unicode],
87
88 # A dictionary of logical names to filepaths
89 'media_files': dict,
90
91 # The following should be lists of lists, in appropriate file
92 # record form
93 'attachment_files': list,
94
95 # This one should just be a single file record
96 'thumbnail_file': [unicode]}
97
98 required_fields = [
99 'uploader', 'created', 'media_type', 'slug']
100
101 default_values = {
102 'created': datetime.datetime.utcnow,
103 'state': u'unprocessed'}
104
105 migration_handler = migrations.MediaEntryMigration
106
107 indexes = [
108 # Referene uniqueness of slugs by uploader
109 {'fields': ['uploader', 'slug'],
110 'unique': True}]
111
112 def main_mediafile(self):
113 pass
114
115 def generate_slug(self):
116 self['slug'] = util.slugify(self['title'])
117
118 duplicate = mediagoblin_globals.database.media_entries.find_one(
119 {'slug': self['slug']})
120
121 if duplicate:
122 self['slug'] = "%s-%s" % (self['_id'], self['slug'])
123
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 uploader = self.uploader()
131
132 if self.get('slug'):
133 return urlgen(
134 'mediagoblin.user_pages.media_home',
135 user=uploader['username'],
136 media=self['slug'])
137 else:
138 return urlgen(
139 'mediagoblin.user_pages.media_home',
140 user=uploader['username'],
141 media=unicode(self['_id']))
142
143 def uploader(self):
144 return self.db.User.find_one({'_id': self['uploader']})
145
146
147 REGISTER_MODELS = [MediaEntry, User]
148
149
150 def register_models(connection):
151 """
152 Register all models in REGISTER_MODELS with this connection.
153 """
154 connection.register(REGISTER_MODELS)
155