没有比人更高的山

彩色标签云的生成方法0

发现欧必杰(http://www.ooobj.com/blog)的标签远远比我多,用li列表显示已经不可容忍,所以我决定花点时间把标签的显示更新一下,最新的效果如http://zhlwish.appspot.com左侧的标签云。

思路很简单,就是为每个标签随机生成一种颜色(当然要考虑到如果生成的背景色,就需要再生成一次),然后根据引用这个标签的日志数按比例生成12-15号之间的字体。具体的代码如下:

这里偷了个懒,我这里的标签云是白色背景,就直接硬编码了,最好是作为列表参数传入。

这里遇到了一个问题就是python的除法问题,python有两种除法,即所谓的true除法和floor除法,有兴趣的朋友google一下啦。我就不重复占用网络资源了。

import random

def rand_color():
    hex_char = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f')
    color = "#"
    for i in range(6):
        color += random.choice(hex_char)
   
    if color == "#ffffff":
        return rand_color()
    else:
        return color

 

tags = Tag.all()                                     #返回所有的标签
tag_weighing = [len(tag.diaries) for tag in tags]
max_w = max(tag_weighing)
min_w = min(tag_weighing)
weighing = float(3/((max_w - min_w) + 1))            #计算权重
tag_strs = [];
for tag in tags:
    font_size = str(weighing * len(tag.diaries) + 12) + "px";
    color = rand_color()
    tag_strs.append("<a href='/blog/tag/" + str(tag.key()) + "/' style='color:" + color + ";font-size:" + font_size + ";'>" + tag.name + "</a>&nbsp;&nbsp;")

 

VN:F [1.7.5_995]
Rating: 0.0/10 (0 votes cast)
VN:F [1.7.5_995]
Rating: 0 (from 0 votes)

Google App Engine中开发中复用Django的syndication生成RSS0

因为django.contrib.syndication.feeds用到了from django.contrib.sites.models import Site, RequestSite

而其中的Site和RequestSite又用到了Django的db模块,因此在GAE中是不能用的,看了看源代码,发现其实这两个类是可以不用的,于是我就想改动改动Django的代码来实现。

我的网站里使用zipimport方式导入的Django,为了防止以后升级Django的时候将我的改动覆盖了,首先,将syndication从zip包里面移到外面来,而且要改动“名称空间”,我直接将django.contrib删掉了,剩下了syndication.feeds,至于后面的改动因为代码变动比较多,有需要可以参考我的网站的源代码

注:本人的之前用google app engine做过一个博客,由于ghs.google.com的IP时常被封,不得不使用WordPress,原网站的地址是http://zhlwish.appspot.com,已经停止更新。

VN:F [1.7.5_995]
Rating: 0.0/10 (0 votes cast)
VN:F [1.7.5_995]
Rating: 0 (from 0 votes)

Google App Engine中的多对多关联0

Google App Engine(GAE)中使用db.ReferenceProperty来处理实体之间的一对多关联,如Diary(日志)和Category(分类)是多对一的关系,可以简单配置如下:

1
2
3
4
5
6
7
8
class Category(db.Model):
    name = db.StringProperty( required = True)
<code>
class Diary(db.Model):
    title = db.StringProperty( required = True )
    content = db.TextProperty()
    category = db.ReferenceProperty(Category)
</code>

这样可以使用diary.category即可获取Diary(日志)所对应的Category(分类),使用category.diary_set即可获取Category(分类)所对应的Diary(日志)

但是再处理Tag(标签)和Diary(日志)时出现了问题,因为GAE并没有多对多关联,经过查询资料,可以有一种很简单的办法是在关系的其中一方创建一个的列表,如下:

1
2
3
4
5
6
7
8
9
10
11
12
class Tag(db.Model):
    name = db.StringProperty( required = True )
    diaries = db.ListProperty(db.Key)
 
class Diary(db.Model):
    title = db.StringProperty( required = True )
    content = db.TextProperty()
    category = db.ReferenceProperty(Category)
 
    @property
    def tags(self):
        return Tag.gql("WHERE diaries = :1", self.key())

在一篇日志(Diary)贴上标签(Tag)用如下办法即可:

1
tag.diaries.append(diary.key())

获取一篇日志(Diary)的标签(Tag)用如下语句即可:
1
diary.tags()

这样就比较简单的实现了多对多的关联,不能不说Google的工程师真是牛逼,这种办法都可以想出来。

VN:F [1.7.5_995]
Rating: 10.0/10 (1 vote cast)
VN:F [1.7.5_995]
Rating: 0 (from 0 votes)