Commit | Line | Data |
---|---|---|
fbad3a9f E |
1 | # GNU MediaGoblin -- federated, autonomous media hosting |
2 | # Copyright (C) 2011,2012 MediaGoblin contributors. See AUTHORS. | |
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 | ||
ccca0fbf CAW |
18 | import datetime |
19 | ||
ccca0fbf CAW |
20 | from sqlalchemy import ( |
21 | Column, Integer, Unicode, UnicodeText, DateTime, Boolean, ForeignKey, | |
22 | UniqueConstraint) | |
88e90f41 | 23 | from sqlalchemy.orm import relationship |
02db7e0a | 24 | from sqlalchemy.orm.collections import attribute_mapped_collection |
c47a03b9 | 25 | from sqlalchemy.sql.expression import desc |
02db7e0a | 26 | from sqlalchemy.ext.associationproxy import association_proxy |
ccca0fbf | 27 | |
02db7e0a | 28 | from mediagoblin.db.sql.extratypes import PathTupleWithSlashes |
de917303 | 29 | from mediagoblin.db.sql.base import Base, DictReadAttrProxy |
f42e49c3 | 30 | from mediagoblin.db.mixin import UserMixin, MediaEntryMixin |
ccca0fbf | 31 | |
7b194a79 | 32 | |
19ed039b E |
33 | class SimpleFieldAlias(object): |
34 | """An alias for any field""" | |
35 | def __init__(self, fieldname): | |
36 | self.fieldname = fieldname | |
37 | ||
38 | def __get__(self, instance, cls): | |
39 | return getattr(instance, self.fieldname) | |
40 | ||
41 | def __set__(self, instance, val): | |
42 | setattr(instance, self.fieldname, val) | |
43 | ||
44 | ||
f42e49c3 | 45 | class User(Base, UserMixin): |
ccca0fbf CAW |
46 | __tablename__ = "users" |
47 | ||
48 | id = Column(Integer, primary_key=True) | |
49 | username = Column(Unicode, nullable=False, unique=True) | |
50 | email = Column(Unicode, nullable=False) | |
51 | created = Column(DateTime, nullable=False, default=datetime.datetime.now) | |
52 | pw_hash = Column(Unicode, nullable=False) | |
53 | email_verified = Column(Boolean) | |
e365f980 | 54 | status = Column(Unicode, default=u"needs_email_verification", nullable=False) |
ccca0fbf CAW |
55 | verification_key = Column(Unicode) |
56 | is_admin = Column(Boolean, default=False, nullable=False) | |
57 | url = Column(Unicode) | |
fbad3a9f E |
58 | bio = Column(UnicodeText) # ?? |
59 | bio_html = Column(UnicodeText) # ?? | |
ccca0fbf | 60 | fp_verification_key = Column(Unicode) |
7c2c56a5 | 61 | fp_token_expire = Column(DateTime) |
ccca0fbf CAW |
62 | |
63 | ## TODO | |
64 | # plugin data would be in a separate model | |
65 | ||
19ed039b E |
66 | _id = SimpleFieldAlias("id") |
67 | ||
ccca0fbf | 68 | |
f42e49c3 | 69 | class MediaEntry(Base, MediaEntryMixin): |
ccca0fbf CAW |
70 | __tablename__ = "media_entries" |
71 | ||
72 | id = Column(Integer, primary_key=True) | |
73 | uploader = Column(Integer, ForeignKey('users.id'), nullable=False) | |
7c2c56a5 | 74 | title = Column(Unicode, nullable=False) |
3e907d55 | 75 | slug = Column(Unicode) |
ccca0fbf CAW |
76 | created = Column(DateTime, nullable=False, default=datetime.datetime.now) |
77 | description = Column(UnicodeText) # ?? | |
78 | description_html = Column(UnicodeText) # ?? | |
79 | media_type = Column(Unicode, nullable=False) | |
c6263400 | 80 | state = Column(Unicode, nullable=False) # or use sqlalchemy.types.Enum? |
2788e6a1 | 81 | license = Column(Unicode) |
fbad3a9f | 82 | |
ccca0fbf CAW |
83 | fail_error = Column(Unicode) |
84 | fail_metadata = Column(UnicodeText) | |
85 | ||
02db7e0a | 86 | queued_media_file = Column(PathTupleWithSlashes) |
ccca0fbf CAW |
87 | |
88 | queued_task_id = Column(Unicode) | |
89 | ||
90 | __table_args__ = ( | |
91 | UniqueConstraint('uploader', 'slug'), | |
92 | {}) | |
93 | ||
88e90f41 E |
94 | get_uploader = relationship(User) |
95 | ||
02db7e0a E |
96 | media_files_helper = relationship("MediaFile", |
97 | collection_class=attribute_mapped_collection("name"), | |
98 | cascade="all, delete-orphan" | |
99 | ) | |
100 | media_files = association_proxy('media_files_helper', 'file_path', | |
fbad3a9f | 101 | creator=lambda k, v: MediaFile(name=k, file_path=v) |
02db7e0a E |
102 | ) |
103 | ||
de917303 E |
104 | tags_helper = relationship("MediaTag", |
105 | cascade="all, delete-orphan" | |
106 | ) | |
107 | tags = association_proxy("tags_helper", "dict_view", | |
108 | creator=lambda v: MediaTag(name=v["name"], slug=v["slug"]) | |
109 | ) | |
110 | ||
ccca0fbf | 111 | ## TODO |
ccca0fbf CAW |
112 | # media_data |
113 | # attachment_files | |
114 | # fail_error | |
115 | ||
02ede858 E |
116 | def get_comments(self, ascending=False): |
117 | order_col = MediaComment.created | |
118 | if not ascending: | |
119 | order_col = desc(order_col) | |
120 | return MediaComment.query.filter_by( | |
121 | media_entry=self.id).order_by(order_col) | |
122 | ||
c47a03b9 E |
123 | def url_to_prev(self, urlgen): |
124 | """get the next 'newer' entry by this user""" | |
125 | media = MediaEntry.query.filter( | |
126 | (MediaEntry.uploader == self.uploader) | |
127 | & (MediaEntry.state == 'processed') | |
128 | & (MediaEntry.id > self.id)).order_by(MediaEntry.id).first() | |
129 | ||
130 | if media is not None: | |
131 | return media.url_for_self(urlgen) | |
132 | ||
133 | def url_to_next(self, urlgen): | |
134 | """get the next 'older' entry by this user""" | |
135 | media = MediaEntry.query.filter( | |
136 | (MediaEntry.uploader == self.uploader) | |
137 | & (MediaEntry.state == 'processed') | |
138 | & (MediaEntry.id < self.id)).order_by(desc(MediaEntry.id)).first() | |
139 | ||
140 | if media is not None: | |
141 | return media.url_for_self(urlgen) | |
142 | ||
ccca0fbf | 143 | |
02db7e0a E |
144 | class MediaFile(Base): |
145 | __tablename__ = "mediafiles" | |
146 | ||
147 | media_entry = Column( | |
148 | Integer, ForeignKey(MediaEntry.id), | |
149 | nullable=False, primary_key=True) | |
150 | name = Column(Unicode, primary_key=True) | |
151 | file_path = Column(PathTupleWithSlashes) | |
152 | ||
153 | def __repr__(self): | |
154 | return "<MediaFile %s: %r>" % (self.name, self.file_path) | |
155 | ||
156 | ||
ccca0fbf CAW |
157 | class Tag(Base): |
158 | __tablename__ = "tags" | |
159 | ||
160 | id = Column(Integer, primary_key=True) | |
161 | slug = Column(Unicode, nullable=False, unique=True) | |
162 | ||
de917303 E |
163 | def __repr__(self): |
164 | return "<Tag %r: %r>" % (self.id, self.slug) | |
165 | ||
166 | @classmethod | |
167 | def find_or_new(cls, slug): | |
168 | t = cls.query.filter_by(slug=slug).first() | |
169 | if t is not None: | |
170 | return t | |
171 | return cls(slug=slug) | |
172 | ||
ccca0fbf CAW |
173 | |
174 | class MediaTag(Base): | |
175 | __tablename__ = "media_tags" | |
176 | ||
177 | id = Column(Integer, primary_key=True) | |
ccca0fbf | 178 | media_entry = Column( |
de917303 | 179 | Integer, ForeignKey(MediaEntry.id), |
ccca0fbf | 180 | nullable=False) |
de917303 E |
181 | tag = Column(Integer, ForeignKey('tags.id'), nullable=False) |
182 | name = Column(Unicode) | |
ccca0fbf CAW |
183 | # created = Column(DateTime, nullable=False, default=datetime.datetime.now) |
184 | ||
185 | __table_args__ = ( | |
186 | UniqueConstraint('tag', 'media_entry'), | |
187 | {}) | |
188 | ||
de917303 E |
189 | tag_helper = relationship(Tag) |
190 | slug = association_proxy('tag_helper', 'slug', | |
191 | creator=Tag.find_or_new | |
192 | ) | |
193 | ||
194 | def __init__(self, name, slug): | |
195 | Base.__init__(self) | |
196 | self.name = name | |
197 | self.tag_helper = Tag.find_or_new(slug) | |
198 | ||
199 | @property | |
200 | def dict_view(self): | |
201 | """A dict like view on this object""" | |
202 | return DictReadAttrProxy(self) | |
203 | ||
ccca0fbf CAW |
204 | |
205 | class MediaComment(Base): | |
206 | __tablename__ = "media_comments" | |
fbad3a9f | 207 | |
ccca0fbf CAW |
208 | id = Column(Integer, primary_key=True) |
209 | media_entry = Column( | |
210 | Integer, ForeignKey('media_entries.id'), nullable=False) | |
211 | author = Column(Integer, ForeignKey('users.id'), nullable=False) | |
212 | created = Column(DateTime, nullable=False, default=datetime.datetime.now) | |
213 | content = Column(UnicodeText, nullable=False) | |
214 | content_html = Column(UnicodeText) | |
e365f980 | 215 | |
88e90f41 E |
216 | get_author = relationship(User) |
217 | ||
e365f980 | 218 | |
70b44584 CAW |
219 | MODELS = [ |
220 | User, MediaEntry, Tag, MediaTag, MediaComment] | |
221 | ||
222 | ||
223 | ###################################################### | |
224 | # Special, migrations-tracking table | |
225 | # | |
226 | # Not listed in MODELS because this is special and not | |
227 | # really migrated, but used for migrations (for now) | |
228 | ###################################################### | |
229 | ||
230 | class MigrationData(Base): | |
231 | __tablename__ = "migrations" | |
232 | ||
233 | id = Column(Integer, primary_key=True) | |
234 | name = Column(Unicode, nullable=False, unique=True) | |
235 | version = Column(Integer, nullable=False, default=0) | |
236 | ||
237 | ###################################################### | |
238 | ||
239 | ||
e365f980 E |
240 | def show_table_init(): |
241 | from sqlalchemy import create_engine | |
242 | engine = create_engine('sqlite:///:memory:', echo=True) | |
243 | ||
244 | Base.metadata.create_all(engine) | |
245 | ||
246 | ||
247 | if __name__ == '__main__': | |
248 | show_table_init() |