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 | |
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
-rw-r--r-- | archive/README.txt | 4 | ||||
-rw-r--r-- | archive/__init__.py | 0 | ||||
-rw-r--r-- | archive/admin.py | 11 | ||||
-rw-r--r-- | archive/models.py | 79 | ||||
-rw-r--r-- | archive/templates/archive/archive.html | 67 | ||||
-rw-r--r-- | archive/tests.py | 3 | ||||
-rw-r--r-- | archive/urls.py | 16 | ||||
-rw-r--r-- | archive/views.py | 28 |
8 files changed, 208 insertions, 0 deletions
diff --git a/archive/README.txt b/archive/README.txt new file mode 100644 index 0000000..8c1330c --- /dev/null +++ b/archive/README.txt @@ -0,0 +1,4 @@ +App archive + +This app deals with uploaded documents and provides downloads. + diff --git a/archive/__init__.py b/archive/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/archive/__init__.py diff --git a/archive/admin.py b/archive/admin.py new file mode 100644 index 0000000..9b1e665 --- /dev/null +++ b/archive/admin.py @@ -0,0 +1,11 @@ +# -*- coding: utf-8 -*- + +from django.contrib import admin + +from archive.models import Archive, ArchiveCategory + + +## register to admin +admin.site.register(Archive) +admin.site.register(ArchiveCategory) + 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) + diff --git a/archive/templates/archive/archive.html b/archive/templates/archive/archive.html new file mode 100644 index 0000000..96a93c4 --- /dev/null +++ b/archive/templates/archive/archive.html @@ -0,0 +1,67 @@ +{% extends 'base.html' %} +{% load staticfiles %} +{% load url from future %} +{% load i18n %} +{% load bootstrap3 %} + +{# template to show notice list #} + +{% block title %} +资料下载 | 2014 SKA Summer School +{% endblock %} + +{% block css_extra %} + <link href="{% static 'css/sidebar.css' %}" rel="stylesheet"> +{% endblock %} + +{% block content %} + <div class="container"> + <h2>资料下载</h2> + <hr> + </div> + + <div class="container"> + {% if archive_empty %} + <div class="alert alert-info"> + 暂无资料提供下载…… + </div> + {% else %} + <div class="row"> + <div class="col-md-10" id="content-col"> + {# archive contents #} + {% for category in archive_categories %} + <h3 id="cat{{ category.id }}">{{ category.name }}</h3> + <table class="table table-stripped table-bordered table-hover"> + {% with archives=category.archive_set.all %} + {% for archive in archives %} + <tr> + <th class="col-md-3"><a href="{{ archive.file.url }}">{{ archive.title }}</a></th> + <td class="col-md-7">{{ archive.description }}</td> + </tr> + {% empty %} + <tr class="warning"> + <td>暂无该类资料可供下载</td> + </tr> + {% endfor %} + {% endwith %} + </table> + {% endfor %} + </div> + <div class="col-md-2" id="sidebar-col"> + {# archive sidebar #} + <ul class="nav nav-stacked" id="sidebar"> + {% for category in archive_categories %} + <li><a href="#cat{{ category.id }}">{{ category.name }}</a></li> + {% endfor %} + </ul> + </div> + </div> + {% endif %} + </div> +{% endblock %} + +{% block js_extra %} + <script src="{% static 'js/sidebar.js' %}" type="text/javascript"></script> +{% endblock %} + +{# vim: set ts=8 sw=2 tw=0 fenc=utf-8 ft=htmldjango.html: #} diff --git a/archive/tests.py b/archive/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/archive/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/archive/urls.py b/archive/urls.py new file mode 100644 index 0000000..8657fed --- /dev/null +++ b/archive/urls.py @@ -0,0 +1,16 @@ +# -*- coding: utf-8 -*- +# +# urls.py for app 'page' +# + +from django.conf.urls import patterns, include, url + +from archive.views import ArchiveView + + +urlpatterns = patterns('', + # notice list view + url(r'^archive/all$', ArchiveView.as_view(), + name='archive_all'), +) + diff --git a/archive/views.py b/archive/views.py new file mode 100644 index 0000000..eec7d36 --- /dev/null +++ b/archive/views.py @@ -0,0 +1,28 @@ +# -*- coding: utf-8 -*- + +from django.shortcuts import render +from django.views.generic.base import TemplateView + +from archive.models import Archive, ArchiveCategory + + +class ArchiveView(TemplateView): + """ + class-based view to show archives + """ + template_name = 'archive/archive.html' + + def get_context_data(self, **kwargs): + """ + add 'user' context + """ + context = super(type(self), self).get_context_data(**kwargs) + archive_categories = ArchiveCategory.objects.all() + if Archive.objects.all(): + archive_empty = False + else: + archive_empty = True + context['archive_categories'] = archive_categories + context['archive_empty'] = archive_empty + return context + |