2009年7月29日 星期三

Image縮圖處理

from PIL import Image
import os.path
import StringIO

def thumbnail(filename, size=(50, 50), output_filename=None):
image = Image.open(filename)
if image.mode not in ('L', 'RGB'):
image = image.convert('RGB')
image = image.resize(size, Image.ANTIALIAS)

# get the thumbnail data in memory.
if not output_filename:
output_filename = get_default_thumbnail_filename(filename)
image.save(output_filename, image.format)
return output_filename

def thumbnail_string(buf, size=(50, 50)):
f = StringIO.StringIO(buf)
image = Image.open(f)
if image.mode not in ('L', 'RGB'):
image = image.convert('RGB')
image = image.resize(size, Image.ANTIALIAS)
o = StringIO.StringIO()
image.save(o, "JPEG")
return o.getvalue()

def get_default_thumbnail_filename(filename):
path, ext = os.path.splitext(filename)
return path + '.thumb.jpg'

欄位用ImageField的儲存與顯示

1.model.py欄位的設定
image1 = models.ImageField('圖1.',upload_to ='tcimage/',blank=True,null=True)
image2 = models.ImageField('圖2.',upload_to ='tcimage/',blank=True,null=True)

2.setting.py的設定
MEDIA_ROOT = 'C:/python25/opentc/media/'

3.問題
admin中的網址顯示錯誤

解決方案在setting.py中設定MEDIA_URL
MEDIA_URL = '/'

4.在Template的顯示

The ImageField and FileField URLs are relative to MEDIA_URL
It's important to skip the leading slash, and add an slash to the end of
the path Attribute.
example:

image = models.Imagefield(path="images/")

In the Template use {{MEDIA_URL}}{{image.url}}


參考資料如下:

Using a FileField or an ImageField (see below) in a model takes a few steps:

  1. In your settings file, you'll need to define MEDIA_ROOT as the full path to a directory where you'd like Django to store uploaded files. (For performance, these files are not stored in the database.) Define MEDIA_URL as the base public URL of that directory. Make sure that this directory is writable by the Web server's user account.
  2. Add the FileField or ImageField to your model, making sure to define the upload_to option to tell Django to which subdirectory of MEDIA_ROOT it should upload files.
  3. All that will be stored in your database is a path to the file (relative to MEDIA_ROOT). You'll most likely want to use the convenience url function provided by Django. For example, if your ImageField is called mug_shot, you can get the absolute URL to your image in a template with {{ object.mug_shot.url }}.

For example, say your MEDIA_ROOT is set to '/home/media', and upload_to is set to 'photos/%Y/%m/%d'. The '%Y/%m/%d' part of upload_to is strftime formatting; '%Y' is the four-digit year, '%m' is the two-digit month and '%d' is the two-digit day. If you upload a file on Jan. 15, 2007, it will be saved in the directory /home/media/photos/2007/01/15.

If you want to retrieve the upload file's on-disk filename, or a URL that refers to that file, or the file's size, you can use the name, url and size attributes; see Managing files.

Note that whenever you deal with uploaded files, you should pay close attention to where you're uploading them and what type of files they are, to avoid security holes. Validate all uploaded files so that you're sure the files are what you think they are. For example, if you blindly let somebody upload files, without validation, to a directory that's within your Web server's document root, then somebody could upload a CGI or PHP script and execute that script by visiting its URL on your site. Don't allow that.

資料存檔時要如何記錄使用者帳號

在admin.py中
class questionAdmin(admin.ModelAdmin):#要存檔的model
...
...
def save_model(self, request, obj, form, change):
session_key = request.session.session_key
session = Session.objects.get(session_key=session_key)
uid = session.get_decoded().get('_auth_user_id')
user = User.objects.get(pk=uid)
obj.cuserid = user.username
obj.save()

2009年7月21日 星期二

使用mptt來建構treeview

  1. 下載mptt
  2. 修正mptt在執行django1.0下所發生的錯誤
  3. 使用範例如下

3.1在model.py


from django.db import models

import mptt

class Category(models.Model):
name = models.CharField(max_length=50)
parent = models.ForeignKey('self', null=True, blank=True, related_name='children')

def __unicode__(self):
return self.name

mptt.register(Category)

3.2執行snycdb

3.3輸入資料顯示結果

2009年7月19日 星期日

如何把分解結構用Tree view的方式顯示在html上


像一些WBS或是RBS等都需要利用Tree view的方式來顯示,顯示的內容如右。而在WEB GUI上一定會使用到Java script來顯示,可以使用jQuery的方式來作為顯示。

參考資料:http://www.hyperthot.com/pm_wbs_banquet.gif


jQuery的規則

首先是利用ul與li等來表示階層的概念,而第一的ul則class="treeview" id="tree"接下來再來設定各li的屬性如右圖所示。

而id則有四個要設定分別為main,sidetree,sidetreecontrol,tree