Pinax源代码分析10 - invitations, notices, messages, announcement
邀请
url
(r'^invitations/', include('friends_app.urls')),
逻辑与模板
url(r'^$', 'friends_app.views.friends', name='invitations'),
friends view 负责处理同意好友邀请,发送邮件的注册邀请,显示受到的和发送的邀请(好友邀请、注册邀请)。该view使用了 friends app 的 JoinRequestForm,实际上发送邮件的过程是由 JoinRequestForm 借助 JoinInvitationManager 完成的。JoinInvitationManager 会调用mailer或者是Djano自带的send_mail发送邮件。
invitations 模板扩展的是 message app 的 base,{% extends "messages/base.html" %}。
url(r'^contacts/$', 'friends_app.views.contacts', name='invitations_contacts'),
contacts view 借助friends.importer.import_vcards 负责 vCard的上传,借助 friends.importer.import_yahoo和friends.importer.import_google 来导入google和yahoo的联系人。
contacts 模板会负责显示当前用户所有的联系人。
url(r'^accept/(/w+)/$', 'friends_app.views.accept_join', name='friends_accept_join'),
accept_join view 负责为收到邀请的用户完成注册。
notices
通知,使用的是第三方app Django notification。
url
(r'^notices/', include('notification.urls')),
逻辑与模板
url(r'^$', notices, name="notification_notices"),
notices view 负责显示所有的通知,更改通知设置。没太仔细地看代码,感觉挺难懂的。
url(r'^(/d+)/$', single, name="notification_notice"),
显示单条通知。
url(r'^feed/$', feed_for_user, name="notification_feed_for_user"),
用户通知的RSS Feed,用的也是atomformat,atomformat.py 文件直接包含在notification app 目录里了。
url(r'^mark_all_seen/$', mark_all_seen, name="notification_mark_all_seen"),
将所有的通知标签为已读。感觉这个功能没什么用,Pinax在用户浏览notices页面的时候会自动将notice标记为已读。
此外,还有一个delete和archive两个view,但是没有提供访问的urls。
支持notification app
如何让一个app支持 notification app,在inax代码分析2 中已经分析过了。包括两个步骤,首先在app 目录下包含一个management.py,创建 app 的通知标签、比起哦题、描述。之后,在app的模板目录中要有同志的模板。以blog为例,blog创建了一个标签为 blog_friend_post 的通知,那么应该有下面这样一个目录:
blog/templates/notification/blog_friend_post/
里面包含full.txt和notice.html,分别用于发送邮件和在网站上显示。
发送notice
在blog app 的 views.new 中有如下代码:
notification.send((x['friend'] for x in Friendship.objects.friends_for_user(blog.author)), "blog_friend_post", {"post": blog})
messages
第三方app,Django_messages。
url
(r'^messages/', include('messages.urls')),
逻辑与模板
urlpatterns = patterns('',
#收件箱
url(r'^$', redirect_to, {'url': 'inbox/'}),
url(r'^inbox/$', inbox, name='messages_inbox'),
#发件箱
url(r'^outbox/$', outbox, name='messages_outbox'),
#发消息
url(r'^compose/$', compose, name='messages_compose'),
url(r'^compose/(?P<recipient>[/+/w]+)/$', compose, name='messages_compose_to'),
#回复消息
url(r'^reply/(?P<message_id>[/d]+)/$', reply, name='messages_reply'),
#查看消息
url(r'^view/(?P<message_id>[/d]+)/$', view, name='messages_detail'),
#删除消息
url(r'^delete/(?P<message_id>[/d]+)/$', delete, name='messages_delete'),
#取消删除
url(r'^undelete/(?P<message_id>[/d]+)/$', undelete, name='messages_undelete'),
#回收站
url(r'^trash/$', trash, name='messages_trash'),
)
感觉这个app比较好读,一来是确实相对简单,还有就是每个view都有docstring。
通知
messages app 在 models.py 最后有如下几行代码:
try:
notification = get_app('notification')
except ImproperlyConfigured:
from messages.utils import new_message_email
signals.post_save.connect(new_message_email, sender=Message)
在发现notification app 已经安装的情况下,使用notification发送邮件通知,否则使用自己的邮件发送机制。
announcements
公告,第三方app Django_announcements。
url
(r'^announcements/', include('announcements.urls')),
逻辑与模板
urlpatterns = patterns("",
#显示一个公告
url(r"^(?P<object_id>/d+)/$", list_detail.object_detail,
announcement_detail_info, name="announcement_detail"),
#隐藏一个公告
url(r"^(?P<object_id>/d+)/hide/$", announcement_hide,
name="announcement_hide"),
#所有公告
url(r"^$", announcement_list, name="announcement_home"),
)
这个app也比较简单,功能就是admin在后台添加公告。所有用户(访客)都会看到公告,并且可以隐藏公告。
该app使用了generic view,例如list_detail.object_detail,Django会自动根据Model的名字寻找模板,详细的规则定义在http://docs.djangoproject.com/en/dev/ref/generic-views/#django-views-generic-list-detail-object-list。