1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011,2012 MediaGoblin contributors. See AUTHORS.
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.
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.
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/>.
20 from sqlalchemy
.ext
.declarative
import declarative_base
21 from sqlalchemy
import (
22 Column
, Integer
, Unicode
, UnicodeText
, DateTime
, Boolean
, ForeignKey
,
24 from sqlalchemy
.orm
import relationship
25 from sqlalchemy
.orm
.collections
import attribute_mapped_collection
26 from sqlalchemy
.ext
.associationproxy
import association_proxy
28 from mediagoblin
.db
.sql
.extratypes
import PathTupleWithSlashes
29 from mediagoblin
.db
.sql
.base
import GMGTableBase
30 from mediagoblin
.db
.mixin
import UserMixin
, MediaEntryMixin
33 Base
= declarative_base(cls
=GMGTableBase
)
36 class SimpleFieldAlias(object):
37 """An alias for any field"""
38 def __init__(self
, fieldname
):
39 self
.fieldname
= fieldname
41 def __get__(self
, instance
, cls
):
42 return getattr(instance
, self
.fieldname
)
44 def __set__(self
, instance
, val
):
45 setattr(instance
, self
.fieldname
, val
)
48 class User(Base
, UserMixin
):
49 __tablename__
= "users"
51 id = Column(Integer
, primary_key
=True)
52 username
= Column(Unicode
, nullable
=False, unique
=True)
53 email
= Column(Unicode
, nullable
=False)
54 created
= Column(DateTime
, nullable
=False, default
=datetime
.datetime
.now
)
55 pw_hash
= Column(Unicode
, nullable
=False)
56 email_verified
= Column(Boolean
)
57 status
= Column(Unicode
, default
=u
"needs_email_verification", nullable
=False)
58 verification_key
= Column(Unicode
)
59 is_admin
= Column(Boolean
, default
=False, nullable
=False)
61 bio
= Column(UnicodeText
) # ??
62 bio_html
= Column(UnicodeText
) # ??
63 fp_verification_key
= Column(Unicode
)
64 fp_token_expire
= Column(DateTime
)
67 # plugin data would be in a separate model
69 _id
= SimpleFieldAlias("id")
72 class MediaEntry(Base
, MediaEntryMixin
):
73 __tablename__
= "media_entries"
75 id = Column(Integer
, primary_key
=True)
76 uploader
= Column(Integer
, ForeignKey('users.id'), nullable
=False)
77 title
= Column(Unicode
, nullable
=False)
78 slug
= Column(Unicode
, nullable
=False)
79 created
= Column(DateTime
, nullable
=False, default
=datetime
.datetime
.now
)
80 description
= Column(UnicodeText
) # ??
81 description_html
= Column(UnicodeText
) # ??
82 media_type
= Column(Unicode
, nullable
=False)
83 state
= Column(Unicode
, nullable
=False) # or use sqlalchemy.types.Enum?
84 license
= Column(Unicode
)
86 fail_error
= Column(Unicode
)
87 fail_metadata
= Column(UnicodeText
)
89 queued_media_file
= Column(PathTupleWithSlashes
)
91 queued_task_id
= Column(Unicode
)
94 UniqueConstraint('uploader', 'slug'),
97 get_uploader
= relationship(User
)
99 media_files_helper
= relationship("MediaFile",
100 collection_class
=attribute_mapped_collection("name"),
101 cascade
="all, delete-orphan"
103 media_files
= association_proxy('media_files_helper', 'file_path',
104 creator
=lambda k
, v
: MediaFile(name
=k
, file_path
=v
)
112 def get_comments(self
, ascending
=False):
113 order_col
= MediaComment
.created
115 order_col
= desc(order_col
)
116 return MediaComment
.query
.filter_by(
117 media_entry
=self
.id).order_by(order_col
)
120 class MediaFile(Base
):
121 __tablename__
= "mediafiles"
123 media_entry
= Column(
124 Integer
, ForeignKey(MediaEntry
.id),
125 nullable
=False, primary_key
=True)
126 name
= Column(Unicode
, primary_key
=True)
127 file_path
= Column(PathTupleWithSlashes
)
130 return "<MediaFile %s: %r>" % (self
.name
, self
.file_path
)
134 __tablename__
= "tags"
136 id = Column(Integer
, primary_key
=True)
137 slug
= Column(Unicode
, nullable
=False, unique
=True)
140 class MediaTag(Base
):
141 __tablename__
= "media_tags"
143 id = Column(Integer
, primary_key
=True)
144 tag
= Column(Integer
, ForeignKey('tags.id'), nullable
=False)
145 name
= Column(Unicode
)
146 media_entry
= Column(
147 Integer
, ForeignKey('media_entries.id'),
149 # created = Column(DateTime, nullable=False, default=datetime.datetime.now)
152 UniqueConstraint('tag', 'media_entry'),
156 class MediaComment(Base
):
157 __tablename__
= "media_comments"
159 id = Column(Integer
, primary_key
=True)
160 media_entry
= Column(
161 Integer
, ForeignKey('media_entries.id'), nullable
=False)
162 author
= Column(Integer
, ForeignKey('users.id'), nullable
=False)
163 created
= Column(DateTime
, nullable
=False, default
=datetime
.datetime
.now
)
164 content
= Column(UnicodeText
, nullable
=False)
165 content_html
= Column(UnicodeText
)
167 get_author
= relationship(User
)
170 def show_table_init():
171 from sqlalchemy
import create_engine
172 engine
= create_engine('sqlite:///:memory:', echo
=True)
174 Base
.metadata
.create_all(engine
)
177 if __name__
== '__main__':