class TextMixin: title = db.Column(db.VARCHAR(length=150), nullable=False) content = db.Column(db.Text) slug = db.Column(db.VARCHAR(150), nullable=False) def content_with_summary(self): return make_summary(self.content)
class NewsletterRecipient(BaseModel): """Recipient of the newsletter""" name = db.Column(db.VARCHAR(length=150), nullable=False) email = db.Column(db.Text(), nullable=False) hash = db.Column(db.VARCHAR(length=150), nullable=False) confirmed = db.Column(db.Boolean, default=False, nullable=False) @classmethod def create(cls, name, email, confirmed=False): o = cls() o.name = name o.email = email o.hash = secrets.token_urlsafe(nbytes=16) o.confirmed = confirmed return o def get_scrambled_email(self): n = len(self.name) % 3 s = self.email.split('@') return s[0][:1 + n] + '***' + (s[0][n - 3:] if len(s[0]) >= 4 else '') + '@***.' + s[1].split('.')[-1]
class Category(OrderableMixin, BaseModel): """Category (of the pages)""" name = db.Column(db.VARCHAR(length=150), nullable=False) @classmethod def create(cls, name): o = cls() o.name = name # set order last_c = Category.ordered_items(desc=True).first() o.order = last_c.order + 1 if last_c else 0 return o def __str__(self): return 'Catégorie {} ({})'.format(self.id, self.name)
class Email(BaseModel): title = db.Column(db.VARCHAR(length=150), nullable=False) content = db.Column(db.Text(), nullable=False) sent = db.Column(db.Boolean(), default=False, nullable=False) recipient_id = db.Column(db.Integer, db.ForeignKey('newsletter_recipient.id')) recipient = db.relationship('NewsletterRecipient') @classmethod def create(cls, title, content, recipient_id): o = cls() o.title = title o.content = content o.recipient_id = recipient_id return o def attachments(self): return EmailImageAttachment.query.filter(EmailImageAttachment.email_id.is_(self.id)).all()
class UploadedFile(BaseModel): base_file_name = db.Column(db.VARCHAR(length=150), nullable=False) file_name = db.Column(db.VARCHAR(length=150), nullable=False) file_size = db.Column(db.Integer) possible_mime = db.Column(db.VARCHAR(length=150), nullable=False) description = db.Column(db.Text()) @staticmethod def get_mimetype(path): """Get the mimetype of a file, using either libmagic or the mimetypes module when libmagic is not available.""" if not os.path.exists(path): raise ValueError(path) try: import magic mime = magic.Magic(mime=True) return mime.from_file(path) except ImportError: import mimetypes mime = mimetypes.MimeTypes() return mime.guess_type(path)[0] @classmethod def create(cls, uploaded, filename, description=None): o = cls() o.base_file_name = uploaded.filename o.file_name = uploads_set.save(uploaded, name=filename) o.file_size = os.path.getsize(o.path()) o.possible_mime = UploadedFile.get_mimetype(o.path()) o.description = description return o def path(self): return uploads_set.path(self.file_name) def get_fa_icon(self): icons = { # documents 'application/pdf': 'fas fa-file-pdf', 'application/msword': 'fas fa-file-word', 'application/vnd.ms-word': 'fas fa-file-word', 'application/vnd.oasis.opendocument.text': 'fas fa-file-word', 'application/vnd.openxmlformats-officedocument.wordprocessingml': 'fas fa-file-word', 'application/vnd.ms-excel': 'fas fa-file-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml': 'fas fa-file-excel', 'application/vnd.oasis.opendocument.spreadsheet': 'fas fa-file-excel', 'application/vnd.ms-powerpoint': 'fas fa-file-powerpoint', 'application/vnd.openxmlformats-officedocument.presentationml': 'fas fa-file-powerpoint', 'application/vnd.oasis.opendocument.presentation': 'fas fa-file-powerpoint', 'text/plain': 'fas fa-file-alt', 'text/html': 'fas fa-file-code', # archive 'application/json': 'fas fa-file-code', 'application/gzip': 'fas fa-file-archive', 'application/zip': 'fas fa-file-archive', # images 'image/png': 'fas fa-file-image', 'image/jpeg': 'fas fa-file-image' } return icons[self.possible_mime] if self.possible_mime in icons else 'fas fa-file'