Diverse Code Cleanups

*Code wurde PEP-8 gerecht formatiert
* Kleine Fehler die der PyCharm Inspector beanstandet wurden korrigiert
This commit is contained in:
Christian Berg
2014-11-26 16:04:52 +01:00
committed by Christian Berg
parent f34281089d
commit 86a0db050d
76 changed files with 619 additions and 528 deletions

View File

@@ -1,5 +1,7 @@
# -*- coding: utf-8 -*-
from os import path
from django.conf import settings
from django.utils.timezone import now
from django.core.urlresolvers import reverse
from django.core.cache import cache
@@ -9,19 +11,21 @@ from django.utils.safestring import mark_safe
from django.utils.translation import get_language, ugettext as _
from imagekit.models import ImageSpecField
from imagekit.processors import SmartResize
from utils import STATUS_CHOICES, STATUS_WAITING, STATUS_PUBLISHED, \
STATUS_REJECTED, cleaner, OverwriteStorage # @UnusedImport
from django.core.exceptions import ValidationError
from utils import STATUS_CHOICES, STATUS_WAITING, STATUS_PUBLISHED, \
cleaner
CONTENT_CHOICES = (
(0, u'Django View'),
(1, u'HTML'),
(2, u'PDF')
(0, u'Django View'),
(1, u'HTML'),
(2, u'PDF')
)
def get_upload_path(instance, filename):
'''
"""
Generates the desired file path and filename for an uploaded Image.
With this function Django can save the uploaded images to subfolders that
also have a meaning for humans.
@@ -30,7 +34,7 @@ def get_upload_path(instance, filename):
@type instance: a instace of an models.Model sub-class.
@param filename: The filename of the uploaded image.
@type filename: String
'''
"""
extension = filename[filename.rfind('.') + 1:]
if isinstance(instance, Category):
return "categories/%s.%s" % (instance.slug, extension)
@@ -59,11 +63,11 @@ class Article(ImageModel):
content_en = models.TextField('Content', blank=True)
category = models.ForeignKey('Category', verbose_name=_('Category'))
image = models.ImageField(_('Image'), upload_to='news/',
blank=True, null=True)
blank=True, null=True)
slug = models.SlugField(_('Slug'), unique_for_month='date_created')
author = models.ForeignKey('auth.User', verbose_name=_('Author'))
author = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=_('Author'))
status = models.SmallIntegerField(_('Status'), choices=STATUS_CHOICES,
default=STATUS_PUBLISHED)
default=STATUS_PUBLISHED)
date_created = models.DateField(_('Created'), blank=True)
date_modified = models.DateTimeField(_('Modified'), auto_now=True)
objects = ArticleManager()
@@ -117,13 +121,13 @@ class Article(ImageModel):
class Page(models.Model):
'''
"""
Eine Seite auf der Homepage. Sie kann eine "statische" HTML Seite,
die URL einer dynamische Django View, oder ein PDF Dokument sein.
Jede Seite kann neben Deutsch auch auf Englisch angeboten werden.
Ist keine englische Übersetzung vorhanden, wird die deutsche Version
angeboten.
'''
"""
menu_name_de = models.CharField(
'Menü Name',
max_length=255,
@@ -136,26 +140,26 @@ class Page(models.Model):
help_text=_('The short name for the menu-entry of this page')
)
title_de = models.CharField('Titel', max_length=255,
help_text=_('This title appears in the HTML header'))
help_text=_('This title appears in the HTML header'))
title_en = models.CharField('Title', max_length=255, blank=True,
help_text=_('This title appears in the HTML header'))
help_text=_('This title appears in the HTML header'))
slug = models.SlugField(_('slug'))
path = models.CharField(_('Path'), max_length=100, db_index=True,
editable=False, unique=True)
editable=False, unique=True)
parent = models.ForeignKey('self', blank=True, null=True,
related_name='subpages', on_delete=models.SET_NULL)
related_name='subpages', on_delete=models.SET_NULL)
position = models.PositiveSmallIntegerField(_('Position'),
blank=True, null=True)
blank=True, null=True)
status = models.SmallIntegerField(_('status'), choices=STATUS_CHOICES,
default=STATUS_WAITING)
default=STATUS_WAITING)
content_type = models.IntegerField(choices=CONTENT_CHOICES)
content_de = models.TextField('Inhalt', blank=True)
content_en = models.TextField('Content', blank=True)
enable_comments = models.BooleanField(_('enable comments'), default=True)
template = models.CharField(_('Template'), max_length=100,
default="content/page.html")
default="content/page.html")
pdf_de = models.FileField(upload_to='pdf/de/', blank=True, null=True)
pdf_en = models.FileField(upload_to='pdf/en/', blank=True, null=True)
@@ -164,8 +168,8 @@ class Page(models.Model):
@property
def content(self):
return mark_safe(getattr(self, "content_%s" % get_language()) or \
self.content_de)
cont = getattr(self, "content_%s" % get_language()) or self.content_de
return mark_safe(cont)
@property
def css_class(self):
@@ -173,8 +177,8 @@ class Page(models.Model):
@property
def menu_name(self):
return getattr(self, "menu_name_%s" % get_language()) or \
self.menu_name_de
return getattr(self,
"menu_name_%s" % get_language()) or self.menu_name_de
@property
def pdf_file(self):
@@ -185,12 +189,12 @@ class Page(models.Model):
return getattr(self, "title_%s" % get_language()) or self.title_de
def clean(self):
if self.parent == None:
if self.parent is None:
self.path = self.slug
else:
self.path = path.join(self.parent.path, self.slug)
if self.content_type == None:
if self.content_type is None:
if self.pdf_de:
self.content_type = 2
if self.content_de:
@@ -205,14 +209,14 @@ class Page(models.Model):
_(u'Please upload a PDF-File to this PDF-Page.'))
def get_absolute_url(self):
path = '/' + self.path
aboslute_url = '/' + self.path
if self.content_type == 1:
path += '.html'
aboslute_url += '.html'
elif self.content_type == 2:
path += '.pdf'
aboslute_url += '.pdf'
else:
path += '/'
return path
aboslute_url += '/'
return aboslute_url
class Meta(object):
ordering = ['parent__id', 'position']
@@ -227,7 +231,7 @@ class Category(ImageModel):
description_de = models.TextField(_('Description'))
description_en = models.TextField(_('Description'), blank=True)
image = models.ImageField(_('Image'), upload_to='news/categories/',
blank=True, null=True)
blank=True, null=True)
slug = models.SlugField(_('Slug'), unique=True, db_index=True)
class Meta(object):
@@ -242,7 +246,7 @@ class Category(ImageModel):
@property
def description(self):
return getattr(self, "description_%s" % get_language(),
self.description_de)
self.description_de)
def get_absolute_url(self):
return reverse('article-archive', kwargs={'category': self.slug})
@@ -258,5 +262,6 @@ def force_cache_update(sender, instance, **kwargs):
cache.delete('all_pages')
cache.delete('top_level_pages')
models.signals.post_delete.connect(force_cache_update, sender=Page)
models.signals.post_save.connect(force_cache_update, sender=Page)