Pinax源代码分析10 - invitations, notices, messages, announcement

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_yahoofriends.importer.import_google 来导入googleyahoo的联系人。
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,用的也是atomformatatomformat.py 文件直接包含在notification app 目录里了。

url(r'^mark_all_seen/$', mark_all_seen, name="notification_mark_all_seen"),

将所有的通知标签为已读。感觉这个功能没什么用,Pinax在用户浏览notices页面的时候会自动将notice标记为已读。

此外,还有一个deletearchive两个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.txtnotice.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

第三方appDjango_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_detailDjango会自动根据Model的名字寻找模板,详细的规则定义在http://docs.djangoproject.com/en/dev/ref/generic-views/#django-views-generic-list-detail-object-list



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值