SharePlat介绍
limodou
limodou@gmail.com
内容
- SharePlat目标
- SharePlat设计
- SharePlat开发
- SharePlat演示
[any material that should appear in print but not on the slide]
一、SharePlat目标
它是一个协作管理平台,主要目标有(功能和开发):
- 小组管理模式
- 文档、讨论、日程、任务管理
- 对基本的web开发功能进行总结和提炼
- 使用Ajax(jQuery)
[any material that should appear in print but not on the slide]
二、SharePlat设计
基于Django开发,将常见的功能独立为app,如用户管理,Tag, Comment等
所有功能都基于小组和用户进行。操作单位主要为:小组和用户。用户分为三级:系统管理员,小组管理员,组员
应对主要操作对象可进行控制,如公开等
[any material that should appear in print but not on the slide]
三、SharePlat开发
- Generic Relation的使用
- Custom Tag介绍
- Captcha介绍
- Decorator与Url
- Singal的处理
- Ajax的处理
[any material that should appear in print but not on the slide]
3.1 Generic Relation的使用
用来解决通过关系的引用,主要有两部分:GenericForeignKey和GenericRelation
学习Blog
Django的文档
[any material that should appear in print but not on the slide]
3.1.1 GenericForeignKey
象ForeignKey一样,用来指向其它的记录。
from django.contrib.contenttypes.models import ContentType
class Comment(models.Model):
content = models.TextField(default='')
content_type = models.ForeignKey(ContentType)
object_id = models.IntegerField()
content_object = models.GenericForeignKey()
需要定义三个东西: content_type, object_id, content_object。使用时不需要给出content_type和object_id,只要给出对应的content_object即可。
[any material that should appear in print but not on the slide]
3.1.2 GenericForeignKey
GenericForeignKey可以在一个model中定义多个。在定义多个时,需要指明GenericForeignKey的ct_field,fk_field值。缺省为content_type和object_id。
[any material that should appear in print but not on the slide]
3.1.3 GenericRelation
用来引用一个使用了GenericForeignKey的记录。
class Topic(models.Model):
tags = models.GenericRelation(TagObject)
通过tags可以直接找到对应的对象。
[any material that should appear in print but not on the slide]
3.2 Custom Tag
针对django模板处理的不足进行了扩展,主要有以下不足:
- 表达式计算
- 局部片段的重用
- 带参数的模板重用
- 变量生成
- 复杂判断
[any material that should appear in print but not on the slide]
3.2.1 Custom Tag
- {% expr expression [as varname] %} 计算表达式,可保存到新变量中
- {% catch as varname %}...{% endcatch %} 计算包含的模板内容,保存到新变量中
- {% call templatename with var1=value1, var2=value2, ... %} 使用指定的参数来调用子模板
- {% pyif expression %}...[{% else ...%}]...{% endif %} 复杂逻辑判断
- {% pycall modelname.funcname(paras...) [as varname] %} 调用python函数
[any material that should appear in print but not on the slide]
3.3 Captcha介绍
简化的pycaptcha的处理:
- key=word+date+md5(word+date+secret_key),md5防止篡改,date可以在后台判断失效
- 在后台使用cache来防止重复使用。第一次认证成功则保存进去,再认证如果cache中存在则失败。
- key要在模板中生成隐含字段,同时要生成一个图片链接
- 在后台需要有一个可以根据key生成图片的view方法
[any material that should appear in print but not on the slide]
3.4 Decorator与Url
在trunk代码中,urls.py中可以导入方法,因此可以把一些decorator的处理转移到urls.py中进行。(举例)
d_list = template('users/user_document.html')
d_edit = template('users/user_document_detail.html')
d_tag = template('users/user_document_tag.html')
para = {'model':User, 'url':'user'}
urlpatterns += patterns('',
(r'^(?P\d+)/document/$', d_list(add_person(apps.document.views.list)), para),
(r'^(?P\d+)/document/addfile/$', d_list(apps.document.views.addfile), para),
(r'^(?P\d+)/document/list/$', 'apps.document.views.ajax_list', para),
[any material that should appear in print but not on the slide]
3.4.1 SharePlat中定义的一些decorator
- json 将结果包裹为ajax格式
- template 将结果使用某个template进行渲染
- exceptionhandle 可处理自定义的HttpRedirectException
- ...
[any material that should appear in print but not on the slide]
3.4.2 一些好处
将控制的处理更加集中。而且提高了重用性。比如view只关心结果,不关心具体的输出形式。
[any material that should appear in print but not on the slide]
3.5 Singal的处理
0.95 版已经包含了PyDispatcher模块的使用
db/singals.py中定义了8个预定义的signal
observer模式的实现
它可以实现plugin的功能,实现松耦合
[any material that should appear in print but not on the slide]
3.5.1 Singal的发送
dispatcher.send(signal=signals.pre_save,
sender=self.__class__, instance=self)
signal 是任意的对象
sender 是发送信息的对象
[any material that should appear in print but not on the slide]
3.5.2 Singal的接收
def increment_tag(sender, instance, signal, *args, **kwargs):
if instance.id == None:
instance.tag.count += 1
instance.tag.save()
dispatcher.connect( increment_tag , signal=signals.pre_save,
sender=TagObject )
signal和sender是用来匹配的。
[any material that should appear in print but not on the slide]
3.6 Ajax的处理
包括两方面内容:后台返回数据的ajax化,前端ajax的交互(jQuery)
在SharePlat中实现了以下ajax功能:
- ajax的数据标准化
- 常用的ajax数据封装helper函数(后台)
- ajax_form.js
- jBasicExt.js
- paginate.js
[any material that should appear in print but not on the slide]
3.6.1 ajax的数据标准化
utils/ajax.py
{'response':'ok'|'fail', 'data': 'error': 'next': 'message'}
[any material that should appear in print but not on the slide]
3.6.2 常用的ajax数据封装helper函数(后台)
utils/ajax.py
ajax_ok_data(data='', next=None, message=None)
ajax_fail_data(error='', next=None, message=None)
ajax_ok(data='', next=None, message=None)
ajax_fail(error='', next=None, message=None)
json(data)
[any material that should appear in print but not on the slide]
3.6.3 jBasicExt.js
基本的扩展,如字符串,列表等。特别是提供了template的使用。
[any material that should appear in print but not on the slide]
3.6.4 ajax_form.js
将一个一般form处理转为ajax的提交方式,不能处理带文件的上传
[any material that should appear in print but not on the slide]
3.6.5 paginate.js
分页的ajax处理
[any material that should appear in print but not on the slide]
四、SharePlat演示
SharePlat还正在开发中,项目主页在啄木鸟。
[any material that should appear in print but not on the slide]