diff options
author | Weitian LI <liweitianux@gmail.com> | 2014-04-28 13:34:41 +0800 |
---|---|---|
committer | Weitian LI <liweitianux@gmail.com> | 2014-04-28 13:34:41 +0800 |
commit | 650b6c5f48bccd2f79e2489ecd9df9157bac421a (patch) | |
tree | ab7fb7065a721dc1af2029c8dbf8021c1cbc2b27 /archive/models.py | |
parent | 5af86ee6cbb4d9561c8f0a32d064f1ccd4e4b1cf (diff) | |
download | django-skaschool-650b6c5f48bccd2f79e2489ecd9df9157bac421a.tar.bz2 |
* added app 'archive' to provides downloads
o models: Archive, ArchiveCategory
o views: ArchiveView based on TemplateView
o template: archive.html
o sidebar: sidebar.js, sidebar.css
Diffstat (limited to 'archive/models.py')
-rw-r--r-- | archive/models.py | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/archive/models.py b/archive/models.py new file mode 100644 index 0000000..791750a --- /dev/null +++ b/archive/models.py @@ -0,0 +1,79 @@ +# -*- coding: utf-8 -*- + +from django.db import models +from django.contrib.auth.models import User +from django.contrib.contenttypes.models import ContentType +from django.contrib.contenttypes import generic +from django.utils.translation import ugettext_lazy as _ + +from django.db.models.signals import pre_delete + +from account.extra import OverwriteStorage, file_cleanup + + +class Archive(models.Model): + """ + model 'Archive' to record uploaded files and provide downloads + """ + title = models.CharField(_("Title"), max_length=100) + pubtime = models.DateTimeField(_("Publish time"), auto_now_add=True) + category = models.ForeignKey('ArchiveCategory', verbose_name=_("Category")) + publisher = models.ForeignKey(User, verbose_name=_("Publisher")) + description = models.TextField(_("Description")) + file = models.FileField(verbose_name=_("File"), + upload_to=lambda instance, filename: u'archive/cat{0}/{1}'.format(instance.category.id, filename), + storage=OverwriteStorage()) + + class Meta: + verbose_name = _('archive') + verbose_name_plural = _('archives') + ordering = ['category', '-pubtime', 'id'] + + def __unicode__(self): + return u'Archive %s: %s' % (self.category.name, self.title) + + def show_pubtime(self): + # used in 'list_notice.html' template + return self.pubtime.strftime('%Y-%m-%d') + + def save(self, *args, **kwargs): + """ + overwrite the original save method to delete old file + """ + if not self.pk: + # -> create + return super(type(self), self).save(*args, **kwargs) + # already exists -> edit + old_obj = type(self).objects.get(pk=self.pk) + result = super(type(self), self).save(*args, **kwargs) + # if the path is the same, the file is overwritten already + if old_obj.file.name: + # old_obj has file + if not self.file.name: + # new object has no file + old_obj.file.delete(save=False) + elif old_obj.file.path != self.file.path: + # new object has file, and differ from old_obj + old_obj.file.delete(save=False) + # + return result + + +class ArchiveCategory(models.Model): + """ + model 'NoticeCategory' to provide category selections for 'Notice' + """ + name = models.CharField(_("Category name"), max_length=100) + created_at = models.DateTimeField(_("Created time"), auto_now_add=True) + + class Meta: + verbose_name = _('archive category') + verbose_name_plural = _('archive categories') + + def __unicode__(self): + return u'ArchiveCategory: %s' % self.name + + +### connect to signal and sender +pre_delete.connect(file_cleanup, sender=Archive) + |