{{ skargor.blog }}

Chocolate ThunderIMG_3411IMG_3386IMG_3379IMG_3389IMG_3316IMG_3354

Plan Next Again

1. NT on Google App Engine
2. Project with someone
3. Django + mongodb/mysql
4. so on

Python, Django, MongoDB

最近在看 Python + Django , 因为想取代现在 NTRpg 的结构, 并且容易以后继续开发. 目前大多数的框架都支持 SQL 数据库, 但是在和 zeg 讨论之后, 他说他正在给 NGA 寻找 NoSQL 的数据库来方便索引搜索等.

首先我们定位到了日本 mixi 在使用的 Tokyo 系列, 不过 zeg 测试下来后, 似乎速度不错, 但是索引不行. 于是又找到了 MongoDB.

随后谈到用什么语言来架构, 因为 NGA 用的是 PHP, 并且 zeg 比较熟悉, 所以他建议使用 PHP 来开发. 可惜的是, 我是一个热血冲动,喜欢新鲜刺激玩意儿的人, 最近在比较了 PHP, Django, RoR 之后, 觉得 Django 就是我想要的那个东西(我也不知道是什么东西, 就是看到之后热血澎湃)! 于是乎, 目前还没决定用什么来开发.

在做这个决定的过程中, 我找到了 mongoengine , 可以在 Django 中连接 MongoDB 并且有一套自己的 API . 作者用 mongoengine 开发了一个 Blog – Mumblr , 并且提供了下载, 个人觉得挺不错.

Ruby on Rails Simple Notes

to increment a string like ’14T0001′ or even simple string ’2′, fast way is ‘succ’!

irb(main):001:0> u = '14T0001'
=> "14T0001"
irb(main):002:0> u.succ!
=> "14T0002"
irb(main):003:0> puts u
14T0002
=> nil
irb(main):04:0> puts u
14T0002
=> nil

My first Redmine Plugin – Clients

For each project, they can have some clients who are not members. And reporter can select one of them as client (issue requester/issue contact/others) in each issue.

Made this for my company internal usage. We have lots of customers who belongs to different suburbs/projects. Supporter will get lots of issues everyday, they need know as soon as issue is solved, who they should contact for.

You can download from Github

Vim snipMate set two snippets for one file type

找了一个 django 的 snippet, 发现每次打开 .py 文件只加载 python.snippets, 而不会加载 django.snippets, 需要在 ~/.vimrc 内设置下

filetype indent plugin on

" auto load django/python snip
autocmd FileType python set ft=python.django
autocmd FileType html set ft=html.django_template

CSS Reset Notes

備份下

html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, font, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td {
	margin: 0;
	padding: 0;
	border: 0;
	outline: 0;
	font-size: 100%;
	vertical-align: baseline;
	background: transparent;
}
body {
	line-height: 1;
}
ol, ul {
	list-style: none;
}
blockquote, q {
	quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
	content: '';
	content: none;
}

/* remember to define focus styles! */
:focus {
	outline: 0;
}

/* remember to highlight inserts somehow! */
ins {
	text-decoration: none;
}
del {
	text-decoration: line-through;
}

/* tables still need 'cellspacing="0"' in the markup */
table {
	border-collapse: collapse;
	border-spacing: 0;
}

via http://meyerweb.com/eric/tools/css/reset/index.html

Ruby on Rails – update records without timestamping

Ruby 裡每次更新一條 record 都會自動更新 updated_at 時間戳, 但是有時候比如只是更新點擊的時候, 并不想更新 updated_at, 就需要用到

User.record_timestamps=false

這個方法了.

但是問題是,如果用了這個方法的同時, 有人更新了 User 表內的其他一條 record, 那麼那條 record 也不會更新 updated_at. 需要單獨創建一個 instance 來使用.
把以下代碼粘貼進 environment.rb

module ActiveRecord
  class Base

    def update_record_without_timestamping
      class << self
        def record_timestamps; false; end
      end

      save!

      class << self
        def record_timestamps; super ; end
      end
    end

  end
end

另外一個比較好的方法是使用 remove_method 方法

module ActiveRecord
  class Base

    def update_record_without_timestamping
      class << self
        def record_timestamps; false; end
      end

      save!

      class << self
        remove_method :record_timestamps
      end
    end

  end
end

這樣, 使用 update_record_without_timestamping 更新 record 的時候就不會更新 updated_at 了.
via nerraj

PHP Simple Digital Captcha

Captcha.php文件:

<?php
session_start();
Header("Content-type: image/PNG");
$im = imagecreate(44,18); //captcha size
$back = ImageColorAllocate($im, 245,245,245);
imagefill($im,0,0,$back); //background color

srand((double)microtime()*1000000);
//genereate 4 digital numbers
for($i=0;$i<4;$i++){
	$font = ImageColorAllocate($im, rand(100,255),rand(0,100),rand(100,255));
	$authnum=rand(1,9);
	$vcodes.=$authnum;
	imagestring($im, 5, 2+$i*10, 1, $authnum, $font);
}

for($i=0;$i<100;$i++) { //make it hard to read
	$randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));
	imagesetpixel($im, rand()%70, rand()%30, $randcolor);
}

ImagePNG($im);
ImageDestroy($im);
$_SESSION['vcode'] = $vcodes;
?>

調用

<img src="captcha.php" align="absmiddle"/><input type="text" name="f_Code" maxlength="4" style="width:40px;margin-left:5px;">

驗證

$_SESSION['vcode'] == $_POST['f_Code']

Mac 下 NTFS 的讀取

NTFS-3G

安裝完成,重啟之後變能支持 NTFS 的寫操作了

SQL Query on Special Order

SELECT suburb_id, suburb_name
FROM suburbs
ORDER BY
(case suburb_name
when 'Sydney' then 0
when 'Melbourne' then 1
else 2
end), suburb_name

This will put the Sydney as the first result, Melbourne as the second result, and all others suburbs ordered by suburb_name, from A-Z

← Before