trac#687: Add unit tests for `redirect` and `redirect_obj`.
[larjonas-mediagoblin.git] / mediagoblin / media_types / blog / models.py
blob0e1ddf974350f3f72ce724655a63a167b76a5f85
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/>.
17 import datetime
19 from mediagoblin.db.base import Base
20 from mediagoblin.db.base import Session
21 from mediagoblin.db.models import Collection, User, MediaEntry
22 from mediagoblin.db.mixin import GenerateSlugMixin
24 from mediagoblin.media_types.blog.lib import check_blog_slug_used
26 from mediagoblin.tools.text import cleaned_markdown_conversion
28 from sqlalchemy import (
29 Column, Integer, ForeignKey, Unicode, UnicodeText, DateTime)
30 from sqlalchemy.orm import relationship, backref
33 class BlogMixin(GenerateSlugMixin):
34 def check_slug_used(self, slug):
35 return check_blog_slug_used(self.author, slug, self.id)
38 class Blog(Base, BlogMixin):
39 __tablename__ = "mediatype__blogs"
40 id = Column(Integer, primary_key=True)
41 title = Column(Unicode)
42 description = Column(UnicodeText)
43 author = Column(Integer, ForeignKey(User.id), nullable=False, index=True) #similar to uploader
44 created = Column(DateTime, nullable=False, default=datetime.datetime.now, index=True)
45 slug = Column(Unicode)
47 @property
48 def slug_or_id(self):
49 return (self.slug or u'blog_{0}'.format(self.id))
51 def get_all_blog_posts(self, state=None):
52 blog_posts = Session.query(MediaEntry).join(BlogPostData)\
53 .filter(BlogPostData.blog == self.id)
54 if state is not None:
55 blog_posts = blog_posts.filter(MediaEntry.state==state)
56 return blog_posts
58 def delete(self, **kwargs):
59 all_posts = self.get_all_blog_posts()
60 for post in all_posts:
61 post.delete(del_orphan_tags=False, commit=False)
62 from mediagoblin.db.util import clean_orphan_tags
63 clean_orphan_tags(commit=False)
64 super(Blog, self).delete(**kwargs)
69 BACKREF_NAME = "blogpost__media_data"
71 class BlogPostData(Base):
72 __tablename__ = "blogpost__mediadata"
74 # The primary key *and* reference to the main media_entry
75 media_entry = Column(Integer, ForeignKey('core__media_entries.id'), primary_key=True)
76 blog = Column(Integer, ForeignKey('mediatype__blogs.id'), nullable=False)
77 get_media_entry = relationship("MediaEntry",
78 backref=backref(BACKREF_NAME, uselist=False,
79 cascade="all, delete-orphan"))
82 DATA_MODEL = BlogPostData
83 MODELS = [BlogPostData, Blog]