From 650b6c5f48bccd2f79e2489ecd9df9157bac421a Mon Sep 17 00:00:00 2001 From: Weitian LI Date: Mon, 28 Apr 2014 13:34:41 +0800 Subject: * 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 --- archive/README.txt | 4 ++ archive/__init__.py | 0 archive/admin.py | 11 +++++ archive/models.py | 79 ++++++++++++++++++++++++++++++++++ archive/templates/archive/archive.html | 67 ++++++++++++++++++++++++++++ archive/tests.py | 3 ++ archive/urls.py | 16 +++++++ archive/views.py | 28 ++++++++++++ 8 files changed, 208 insertions(+) create mode 100644 archive/README.txt create mode 100644 archive/__init__.py create mode 100644 archive/admin.py create mode 100644 archive/models.py create mode 100644 archive/templates/archive/archive.html create mode 100644 archive/tests.py create mode 100644 archive/urls.py create mode 100644 archive/views.py (limited to 'archive') 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 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 %} + +{% endblock %} + +{% block content %} +
+

资料下载

+
+
+ +
+ {% if archive_empty %} +
+ 暂无资料提供下载…… +
+ {% else %} +
+
+ {# archive contents #} + {% for category in archive_categories %} +

{{ category.name }}

+ + {% with archives=category.archive_set.all %} + {% for archive in archives %} + + + + + {% empty %} + + + + {% endfor %} + {% endwith %} +
{{ archive.title }}{{ archive.description }}
暂无该类资料可供下载
+ {% endfor %} +
+ +
+ {% endif %} +
+{% endblock %} + +{% block js_extra %} + +{% 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 + -- cgit v1.2.2