From a998ad94f454d3a145650671ba1a8731f1bfaea4 Mon Sep 17 00:00:00 2001 From: Weitian LI Date: Wed, 23 Apr 2014 01:45:46 +0800 Subject: * implemented 'ListNoticeView' based on ListView * added list_notice template, css, urls * updated models and admin of app notice * updated navbar.html --- notice/admin.py | 1 + notice/models.py | 11 +++++ notice/static/css/list_notice.css | 84 ++++++++++++++++++++++++++++++++ notice/templates/notice/list_notice.html | 50 +++++++++++++++++++ notice/urls.py | 16 ++++++ notice/views.py | 25 +++++++++- templates/navbar.html | 4 +- 7 files changed, 188 insertions(+), 3 deletions(-) create mode 100644 notice/static/css/list_notice.css create mode 100644 notice/templates/notice/list_notice.html create mode 100644 notice/urls.py diff --git a/notice/admin.py b/notice/admin.py index bed31e4..6dcd819 100644 --- a/notice/admin.py +++ b/notice/admin.py @@ -19,4 +19,5 @@ class NoticeAdmin(admin.ModelAdmin): ## register to admin admin.site.register(Notice, NoticeAdmin) admin.site.register(NoticeCategory) +admin.site.register(NoticeAttachment) diff --git a/notice/models.py b/notice/models.py index af2e648..f5201d5 100644 --- a/notice/models.py +++ b/notice/models.py @@ -28,6 +28,15 @@ class Notice(models.Model): def __unicode__(self): return u'Notice at %s' % self.pubtime.isoformat() + def show_pubtime(self): + # used in 'list_notice.html' template + return self.pubtime.strftime('%Y-%m-%d') + + def get_attachment_list(self): + # used in 'list_notice.html' template, + # because 'GenericRelatedObjectManager' is not iterable + return list(self.attachments.all()) + class NoticeCategory(models.Model): """ @@ -45,6 +54,8 @@ class NoticeCategory(models.Model): class NoticeAttachment(models.Model): + title = models.CharField(_("Title"), max_length=100) + description = models.TextField(_("Description")) attachment = models.FileField(upload_to='notice/attachments', verbose_name=_("Attachment")) content_type = models.ForeignKey(ContentType) diff --git a/notice/static/css/list_notice.css b/notice/static/css/list_notice.css new file mode 100644 index 0000000..79cb1d1 --- /dev/null +++ b/notice/static/css/list_notice.css @@ -0,0 +1,84 @@ +/* + * css for 'list_notice.html' + * + * based on 'docs.css' Examples section from bootstrap 3 + */ + +.notice-title { + position: relative; + padding: 0px 15px 0px 15px; + margin: 0 -15px 15px; + /* background-color: #fafafa; */ + background-color: #f4f8fa; + border-color: #e5e5e5 #eee #eee; + border-style: solid; + border-width: 1px 0; + -webkit-box-shadow: inset 0 3px 6px rgba(0,0,0,.05); + box-shadow: inset 0 3px 6px rgba(0,0,0,.05); +} + +/* Tweak display of the code snippets when following an example */ +.notice-title + .notice-content { + margin: -15px -15px 15px; + border-width: 0 0 1px; + border-radius: 0; +} + +/* Make the examples and snippets not full-width */ +@media (min-width: 768px) { + .notice-title { + margin-right: 0; + margin-left: 0; + background-color: #f4f8fa; + border-color: #ddd; + border-width: 1px; + border-radius: 4px 4px 0 0; + -webkit-box-shadow: none; + box-shadow: none; + } + .notice-title + .notice-content { + margin-top: -16px; + margin-right: 0; + margin-left: 0; + border-width: 1px; + /* + border-bottom-right-radius: 4px; + border-bottom-left-radius: 4px; + */ + } + .notice-content + .notice-attachment { + margin-top: -16px; + margin-right: 0; + margin-left: 0; + border-width: 1px; + border-radius: 0 0 4px 4px; + } +} + +/* Undo width of container */ +.notice-title .container { + width: auto; +} + +.notice-content { + padding: 9px 14px; + margin-bottom: 14px; + background-color: #f7f7f9; + border: 1px solid #e1e1e8; + /* border-radius: 4px; */ +} + +.notice-attachment { + padding: 9px 14px; + margin-bottom: 14px; + background-color: #ffffff; + border: 1px solid #e1e1e8; + border-radius: 4px; +} + +/* remove 'ul' margin-bottom */ +.notice-attachment ul { + margin-bottom: 0; +} + + diff --git a/notice/templates/notice/list_notice.html b/notice/templates/notice/list_notice.html new file mode 100644 index 0000000..77d194c --- /dev/null +++ b/notice/templates/notice/list_notice.html @@ -0,0 +1,50 @@ +{% 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 %} +
+

全部通知

+
+ {% for notice in object_list %} +
+

{{ notice.title }} {% if notice.is_important %}重要{% endif %}

+

{{ notice.show_pubtime }}, {{ notice.category.category_name }}

+
+
+ {{ notice.contents }} +
+ {% if notice.attachments %} +
+
    + {% for attachment in notice.get_attachment_list %} +
  • + 附件{{ forloop.counter }}: + {{ attachment.title }} +
  • + {% endfor %} +
+
+ {% endif %} +
+ {% empty %} +
+ 暂时没有通知,多谢您的关注…… +
+ {% endfor %} +
+{% endblock %} + +{# vim: set ts=8 sw=2 tw=0 fenc=utf-8 ft=htmldjango.html: #} diff --git a/notice/urls.py b/notice/urls.py new file mode 100644 index 0000000..9362213 --- /dev/null +++ b/notice/urls.py @@ -0,0 +1,16 @@ +# -*- coding: utf-8 -*- +# +# urls.py for app 'page' +# + +from django.conf.urls import patterns, include, url + +from notice.views import ListNoticeView + + +urlpatterns = patterns('', + # notice list view + url(r'^list/$', ListNoticeView.as_view(), + name='list_notice'), +) + diff --git a/notice/views.py b/notice/views.py index 91ea44a..eb6b0b1 100644 --- a/notice/views.py +++ b/notice/views.py @@ -1,3 +1,26 @@ +# -*- coding: utf-8 -*- + from django.shortcuts import render +from django.views.generic.list import ListView + +from notice.models import Notice + + +class ListNoticeView(ListView): + """ + class-based view to show list of notice + """ + queryset = Notice.objects.all() # default order by '-pubtime' + template_name = 'notice/list_notice.html' + # allow queryset/model returns no object, otherwise 404error raised + allow_empty = True + + def get_context_data(self, **kwargs): + """ + add 'user' context + """ + context = super(ListNoticeView, self).get_context_data(**kwargs) + user = self.request.user + context['user'] = user + return context -# Create your views here. diff --git a/templates/navbar.html b/templates/navbar.html index c4f84c2..98423f9 100644 --- a/templates/navbar.html +++ b/templates/navbar.html @@ -16,8 +16,8 @@