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