Files
cao_sunyata/cao_blogr/models/blog_record.py

55 lines
1.9 KiB
Python

import datetime #<- will be used to set default dates on models
from cao_blogr.models.meta import Base #<- we need to import our sqlalchemy metadata from which model classes will inherit
from sqlalchemy import (
Column,
Integer,
Unicode, #<- will provide Unicode field
UnicodeText, #<- will provide Unicode text field
DateTime, #<- time abstraction field
Index,
ForeignKey,
)
from sqlalchemy.orm import relationship
from webhelpers2.text import urlify #<- will generate slugs
from webhelpers2.date import distance_of_time_in_words #<- human friendly dates
import unidecode
class BlogRecord(Base):
__tablename__ = 'entries'
id = Column(Integer, primary_key=True)
title = Column(Unicode(255), unique=True, nullable=False)
body = Column(UnicodeText, default='')
created = Column(DateTime, default=datetime.datetime.now)
edited = Column(DateTime, default=datetime.datetime.now)
topic_id = Column(ForeignKey('topics.topic'), nullable=False)
topic = relationship('Topics', backref='topic_pages')
tag = Column(Unicode(25))
author = Column(Unicode(50), default='')
status = Column(Unicode(50), default='brouillon')
@property
def slug(self):
# remove ascents
title = unidecode.unidecode(self.title)
return urlify(title)
@property
def created_in_words(self):
return distance_of_time_in_words(self.created, datetime.datetime.now())
class Topics(Base):
__tablename__ = 'topics'
topic = Column(Unicode(25), primary_key=True)
topic_name = Column(Unicode(25), nullable=False)
topic_quote = Column(Unicode(255), default='')
class Tags(Base):
__tablename__ = 'tags'
id = Column(Integer, primary_key=True)
topic = Column(Unicode(25))
tag = Column(Unicode(25))
tag_name = Column(Unicode(25), nullable=False)
__table_args__ = (Index('topic_index', "topic", "tag"), )