aboutsummaryrefslogtreecommitdiffstats
path: root/archive/models.py
blob: 467caa41de0d6be6d3a689af8334e0255762284d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# -*- coding: utf-8 -*-

from django.db import models
from django.db.models.signals import pre_delete
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _

from tools.storage 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) %s' % (
                self.id, 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)