Merge remote-tracking branch 'gsoc2016/Subtitle-1'
[mediagoblin.git] / mediagoblin / media_types / blog / models.py
CommitLineData
2b2df22d 1# GNU MediaGoblin -- federated, autonomous media hosting
8ab67da4 2# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
2b2df22d
A
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
17import datetime
18
19from mediagoblin.db.base import Base
a6ad5ddd 20from mediagoblin.db.base import Session
8ab67da4
A
21from mediagoblin.db.models import Collection, User, MediaEntry
22from mediagoblin.db.mixin import GenerateSlugMixin
23
24from mediagoblin.media_types.blog.lib import check_blog_slug_used
25
26from mediagoblin.tools.text import cleaned_markdown_conversion
2b2df22d
A
27
28from sqlalchemy import (
29 Column, Integer, ForeignKey, Unicode, UnicodeText, DateTime)
30from sqlalchemy.orm import relationship, backref
31
8ab67da4
A
32
33class BlogMixin(GenerateSlugMixin):
34 def check_slug_used(self, slug):
35 return check_blog_slug_used(self.author, slug, self.id)
36
a08f0bfc 37BLOG_BACKREF_NAME = "mediatype__blogs"
36990574 38
8ab67da4
A
39class Blog(Base, BlogMixin):
40 __tablename__ = "mediatype__blogs"
41 id = Column(Integer, primary_key=True)
42 title = Column(Unicode)
43 description = Column(UnicodeText)
44 author = Column(Integer, ForeignKey(User.id), nullable=False, index=True) #similar to uploader
45 created = Column(DateTime, nullable=False, default=datetime.datetime.now, index=True)
46 slug = Column(Unicode)
a08f0bfc 47 get_author = relationship("User", backref=backref(BLOG_BACKREF_NAME, cascade="all, delete-orphan"))
36990574
BB
48
49 @property
50 def slug_or_id(self):
51 return (self.slug or u'blog_{0}'.format(self.id))
caeab88f 52
fe741055 53 def get_all_blog_posts(self, state=None):
caeab88f
AM
54 blog_posts = Session.query(MediaEntry).join(BlogPostData)\
55 .filter(BlogPostData.blog == self.id)
56 if state is not None:
57 blog_posts = blog_posts.filter(MediaEntry.state==state)
58 return blog_posts
59
60 def delete(self, **kwargs):
61 all_posts = self.get_all_blog_posts()
62 for post in all_posts:
63 post.delete(del_orphan_tags=False, commit=False)
64 from mediagoblin.db.util import clean_orphan_tags
65 clean_orphan_tags(commit=False)
66 super(Blog, self).delete(**kwargs)
67
68
69
2b2df22d 70
a08f0bfc 71BLOG_POST_BACKREF_NAME = "blogpost__media_data"
2b2df22d 72
63533625 73class BlogPostData(Base):
2b2df22d
A
74 __tablename__ = "blogpost__mediadata"
75
76 # The primary key *and* reference to the main media_entry
8ab67da4 77 media_entry = Column(Integer, ForeignKey('core__media_entries.id'), primary_key=True)
63533625 78 blog = Column(Integer, ForeignKey('mediatype__blogs.id'), nullable=False)
2b2df22d 79 get_media_entry = relationship("MediaEntry",
a08f0bfc 80 backref=backref(BLOG_POST_BACKREF_NAME, uselist=False,
2b2df22d
A
81 cascade="all, delete-orphan"))
82
83
63533625
A
84DATA_MODEL = BlogPostData
85MODELS = [BlogPostData, Blog]