1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-08-31 22:54:11 +00:00

Add notification at info-bar, and fix bug in visiting register/login page when user is already logged in.

This commit is contained in:
xiez
2012-06-04 21:27:32 +08:00
parent 28dbc2ad73
commit f9c72b57a4
17 changed files with 286 additions and 5 deletions

View File

18
notifications/models.py Normal file
View File

@@ -0,0 +1,18 @@
from django.db import models
from django.forms import ModelForm, Textarea
class Notification(models.Model):
message = models.CharField(max_length=512)
primary = models.BooleanField(default=False)
class NotificationForm(ModelForm):
"""
Form for adding notification.
"""
class Meta:
model = Notification
fields = ('message', 'primary')
widgets = {
'message': Textarea(),
}

View File

@@ -0,0 +1,18 @@
{% extends "admin_base.html" %}
{% block title %}添加通知{% endblock %}
{% block nav_notificationadmin_class %}class="cur"{% endblock %}
{% block main_panel %}
<div class="narrow-panel">
<h3>添加通知</h3>
<form action="" method="post">
<label for="id_message">通知:</label>
{{ form.message }}
<input type="submit" value="提交" />
</form>
</div>
{% endblock %}
{% block extra_script %}
<script type="text/javascript">
</script>
{% endblock %}

View File

@@ -0,0 +1,74 @@
{% extends "admin_base.html" %}
{% block nav_notificationadmin_class %}class="cur"{% endblock %}
{% block left_panel %}
<h3>操作</h3>
<ul class="with-bg">
<li><a href="{{ SITE_ROOT }}notification/add/">添加新通知</a></li>
</ul>
{% endblock %}
{% block right_panel %}
<h3>所有通知</h3>
{% if notes %}
<table>
<tr>
<th width="75%">内容</th>
<th width="25%">操作</th>
</tr>
{% for note in notes %}
<tr>
<td>{{ note.message }}
{% if note.primary == 1 %}
<span class="cur-note">(当前通知)</span>
{% endif %}
<td>
{% if note.primary != 1 %}
<button data="{{ SITE_ROOT }}notification/set-primary/{{ note.id }}/" class="note-change-btn">设为当前通知</button>
{% endif %}
<button data="{{ SITE_ROOT }}notification/delete/{{ note.id }}/" class="note-delete-btn">删除</button>
</td>
</tr>
{% endfor %}
</table>
<!--
<div id="paginator">
{% if current_page != 1 %}
<a href="{{ SITE_ROOT }}seafadmin/?page={{ prev_page }}&per_page={{ per_page }}">上一页</a>
{% endif %}
{% if page_next %}
<a href="{{ SITE_ROOT }}seafadmin/?page={{ next_page }}&per_page={{ per_page }}">下一页</a>
{% endif %}
<span>每页:</span>
{% if per_page == 25 %}
<span> 25 </span>
{% else %}
<a href="{{ SITE_ROOT }}seafadmin/?per_page=25" class="per-page">25</a>
{% endif %}
{% if per_page == 50 %}
<span> 50 </span>
{% else %}
<a href="{{ SITE_ROOT }}seafadmin/?per_page=50" class="per-page">50</a>
{% endif %}
{% if per_page == 100 %}
<span> 100 </span>
{% else %}
<a href="{{ SITE_ROOT }}seafadmin/?per_page=100" class="per-page">100</a>
{% endif %}
</div>
-->
{% else %}
<p>暂无</p>
{% endif %}
{% endblock %}
{% block extra_script %}
<script type="text/javascript">
addConfirmTo($('.note-delete-btn'));
$('.note-change-btn').click(function() {
window.location = $(this).attr('data');
})
</script>
{% endblock %}

16
notifications/tests.py Normal file
View File

@@ -0,0 +1,16 @@
"""
This file demonstrates writing tests using the unittest module. These will pass
when you run "manage.py test".
Replace this with more appropriate tests for your application.
"""
from django.test import TestCase
class SimpleTest(TestCase):
def test_basic_addition(self):
"""
Tests that 1 + 1 always equals 2.
"""
self.assertEqual(1 + 1, 2)

11
notifications/urls.py Normal file
View File

@@ -0,0 +1,11 @@
from django.conf.urls.defaults import patterns, url
urlpatterns = patterns('notifications.views',
# url(r'^$', 'notification_list', name='notification_list'),
url(r'^add/$', 'notification_add', name='notification_add'),
url(r'^delete/(?P<nid>[\d]+)/$', 'notification_delete', name='notification_delete'),
url(r'^set-primary/(?P<nid>[\d]+)/$', 'set_primary', name='set_primary'),
url(r'^close/(?P<note_id>[\d]+)/$', 'notification_close', name='notification_close'),
)

11
notifications/utils.py Normal file
View File

@@ -0,0 +1,11 @@
from django.core.cache import cache
from seahub.notifications.models import Notification
def refresh_cache():
"""
Function to be called when change primary notification.
"""
cache.set('CUR_TOPINFO', Notification.objects.all().filter(primary=1),
24*60*60)

68
notifications/views.py Normal file
View File

@@ -0,0 +1,68 @@
import datetime
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect, Http404
from django.shortcuts import render_to_response
from django.template import RequestContext
from auth.decorators import login_required
from seahub.notifications.models import Notification, NotificationForm
from seahub.notifications.utils import refresh_cache
@login_required
def notification_close(request, note_id):
note_id += ','
topinfo_close = request.COOKIES.get('topinfo', '')
topinfo_close += note_id
res = HttpResponseRedirect(request.META['HTTP_REFERER'])
res.set_cookie("topinfo", topinfo_close, max_age=14*24*60*60)
return res
@login_required
def notification_list(request):
if not request.user.is_staff:
raise Http404
notes = Notification.objects.all().order_by('-id')
return render_to_response('notifications/notification_list.html', {
'notes': notes,
}, context_instance=RequestContext(request))
@login_required
def notification_add(request):
if not request.user.is_staff:
raise Http404
if request.method == 'POST':
f = NotificationForm(request.POST)
f.save()
return HttpResponseRedirect(reverse('notification_list', args=[]))
else:
form = NotificationForm()
return render_to_response("notifications/add_notification_form.html", {
'form': form,
}, context_instance=RequestContext(request))
@login_required
def notification_delete(request, nid):
if not request.user.is_staff:
raise Http404
Notification.objects.filter(id=nid).delete()
refresh_cache()
return HttpResponseRedirect(reverse('notification_list', args=[]))
@login_required
def set_primary(request, nid):
if not request.user.is_staff:
raise Http404
# TODO: use transaction?
Notification.objects.filter(primary=1).update(primary=0)
Notification.objects.filter(id=nid).update(primary=1)
refresh_cache()
return HttpResponseRedirect(reverse('notification_list', args=[]))