./bin/gmg shell documented in the hackinghowto
[mediagoblin.git] / mediagoblin / 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,
db1a438f 45 'verification_key': unicode
d232e0f6
CAW
46 }
47
24181820 48 required_fields = ['username', 'created', 'pw_hash', 'email']
fc9bb821
CAW
49
50 default_values = {
24181820 51 'created': datetime.datetime.utcnow,
4d75522b 52 'email_verified': False,
db1a438f 53 'status': u'needs_email_verification',
b16ebe0e 54 'verification_key': lambda: unicode( uuid.uuid4() ) }
fc9bb821 55
4ad5af85
CAW
56 def check_login(self, password):
57 """
58 See if a user can login with this password
59 """
60 return auth_lib.bcrypt_check_password(
61 password, self['pw_hash'])
62
d232e0f6 63
4d75522b
CAW
64class MediaEntry(Document):
65 __collection__ = 'media_entries'
66
67 structure = {
68 'uploader': User,
69 'title': unicode,
1013bdaf 70 'slug': unicode,
4d75522b
CAW
71 'created': datetime.datetime,
72 'description': unicode,
73 'media_type': unicode,
74 'media_data': dict, # extra data relevant to this media_type
75 'plugin_data': dict, # plugins can dump stuff here.
74ae6b11
CAW
76 'tags': [unicode],
77 'state': unicode,
78
fa7f9c61
CAW
79 # For now let's assume there can only be one main file queued
80 # at a time
81 'queued_media_file': [unicode],
82
83 # A dictionary of logical names to filepaths
84 'media_files': dict,
85
74ae6b11
CAW
86 # The following should be lists of lists, in appropriate file
87 # record form
74ae6b11 88 'attachment_files': list,
74ae6b11
CAW
89
90 # This one should just be a single file record
91 'thumbnail_file': [unicode]}
4d75522b
CAW
92
93 required_fields = [
bb49e56f 94 'uploader', 'created', 'media_type']
4d75522b
CAW
95
96 default_values = {
74ae6b11
CAW
97 'created': datetime.datetime.utcnow,
98 'state': u'unprocessed'}
4d75522b
CAW
99
100 def main_mediafile(self):
101 pass
102
0546833c
AW
103 def generate_slug(self):
104 self['slug'] = util.slugify(self['title'])
105
106 duplicate = mediagoblin_globals.database.media_entries.find_one({'slug': self['slug']})
107
108 if duplicate:
109 self['slug'] = "%s-%s" % (self['_id'], self['slug'])
4d75522b 110
d232e0f6
CAW
111REGISTER_MODELS = [MediaEntry, User]
112
4329be14 113
d232e0f6
CAW
114def register_models(connection):
115 """
116 Register all models in REGISTER_MODELS with this connection.
117 """
db61f7d1
CAW
118 connection.register(REGISTER_MODELS)
119