symposion_app/symposion/cms/models.py
Hiroshi Miura a95825ede8 python3 compatibility
- Things are suggested in python3 porting guide.
https://docs.djangoproject.com/en/1.8/topics/python3/

     1. adding ```from django.utils.encoding import
     python_2_unicode_compatible```

     2. ``` __str__``` instead of ```__unicode__```
     https://docs.djangoproject.com/en/1.8/topics/python3/#str-and-unicode-methods

     3. Adding ```from __future__ import unicode_literals``` at the top
     of your Python modules
     https://docs.djangoproject.com/en/1.8/topics/python3/#unicode-literals

     4. Removing the `u` prefix before unicode strings;
     https://docs.djangoproject.com/en/1.8/topics/python3/#unicode-literals

- also closed #66

Signed-off-by: Hiroshi Miura <miurahr@linux.com>
2015-08-03 23:32:25 +09:00

75 lines
2.1 KiB
Python

import datetime
import os
import re
from django.conf import settings
from django.core.urlresolvers import reverse
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
from .managers import PublishedPageManager
@python_2_unicode_compatible
class Page(models.Model):
STATUS_CHOICES = (
(1, _("Draft")),
(2, _("Public")),
)
title = models.CharField(max_length=100)
path = models.CharField(max_length=100, unique=True)
body = MarkupField()
status = models.IntegerField(choices=STATUS_CHOICES, default=2)
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)
tags = TaggableManager(blank=True)
published = PublishedPageManager()
def __str__(self):
return self.title
@models.permalink
def get_absolute_url(self):
return ("cms_page", [self.path])
@property
def is_community(self):
return self.path.lower().startswith("community/")
def save(self, *args, **kwargs):
self.updated = datetime.datetime.now()
super(Page, self).save(*args, **kwargs)
def clean_fields(self, exclude=None):
super(Page, self).clean_fields(exclude)
if not re.match(settings.SYMPOSION_PAGE_REGEX, self.path):
raise ValidationError(
{"path": [_("Path can only contain letters, numbers and hyphens and end with /")]})
reversion.register(Page)
def generate_filename(instance, filename):
return filename
class File(models.Model):
file = models.FileField(upload_to=generate_filename)
created = models.DateTimeField(default=datetime.datetime.now)
def download_url(self):
return reverse("file_download", args=[self.pk, os.path.basename(self.file.name).lower()])