symposion_app/symposion/cms/models.py

80 lines
2.2 KiB
Python
Raw Normal View History

2012-10-18 06:43:00 +00:00
import datetime
import os
2012-07-10 22:18:48 +00:00
import re
2012-07-10 22:18:48 +00:00
from django.conf import settings
2012-10-18 06:43:00 +00:00
from django.core.urlresolvers import reverse
2012-07-10 22:18:48 +00:00
from django.core.exceptions import ValidationError
from django.db import models
from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _
from markitup.fields import MarkupField
from taggit.managers import TaggableManager
import reversion
2012-07-10 22:18:48 +00:00
from .managers import PublishedPageManager
@python_2_unicode_compatible
2012-07-10 22:18:48 +00:00
class Page(models.Model):
2014-07-30 18:19:26 +00:00
STATUS_CHOICES = (
(1, _("Draft")),
(2, _("Public")),
)
2014-07-30 18:19:26 +00:00
title = models.CharField(max_length=100)
2012-07-10 22:18:48 +00:00
path = models.CharField(max_length=100, unique=True)
body = MarkupField()
status = models.IntegerField(choices=STATUS_CHOICES, default=2)
2012-10-18 06:43:00 +00:00
publish_date = models.DateTimeField(default=datetime.datetime.now)
created = models.DateTimeField(editable=False, default=datetime.datetime.now)
updated = models.DateTimeField(editable=False, default=datetime.datetime.now)
2012-07-10 22:18:48 +00:00
tags = TaggableManager(blank=True)
2014-07-30 18:19:26 +00:00
2012-07-10 22:18:48 +00:00
published = PublishedPageManager()
2014-07-30 18:19:26 +00:00
def __str__(self):
return self.title
2014-07-30 18:19:26 +00:00
class Meta:
verbose_name = _("page")
verbose_name_plural = _("pages")
2012-07-10 22:18:48 +00:00
@models.permalink
def get_absolute_url(self):
return ("cms_page", [self.path])
2014-07-30 18:19:26 +00:00
2012-12-20 06:48:19 +00:00
@property
def is_community(self):
return self.path.lower().startswith("community/")
2014-07-30 18:19:26 +00:00
2012-07-10 22:18:48 +00:00
def save(self, *args, **kwargs):
2012-10-18 06:43:00 +00:00
self.updated = datetime.datetime.now()
super(Page, self).save(*args, **kwargs)
2014-07-30 18:19:26 +00:00
2012-07-10 22:18:48 +00:00
def clean_fields(self, exclude=None):
super(Page, self).clean_fields(exclude)
if not re.match(settings.SYMPOSION_PAGE_REGEX, self.path):
2014-07-30 18:19:26 +00:00
raise ValidationError(
{"path": [_("Path can only contain letters, numbers and hyphens and end with /")]})
reversion.register(Page)
2012-10-18 06:43:00 +00:00
def generate_filename(instance, filename):
return filename
class File(models.Model):
2014-07-30 18:19:26 +00:00
2012-10-18 06:43:00 +00:00
file = models.FileField(upload_to=generate_filename)
created = models.DateTimeField(default=datetime.datetime.now)
2014-07-30 18:19:26 +00:00
2012-10-18 06:43:00 +00:00
def download_url(self):
return reverse("file_download", args=[self.pk, os.path.basename(self.file.name).lower()])