Yes,It's mE.

Hi,I am here.

Django Create Inmemoryfile

| Comments

say we have a model like that:

class Data(models.Model):
    file_name = models.CharField(max_length=100)
    upload_file = models.FileField(upload_to='data')

we what create a file in back,but not save to filesystem. so use StringIO create string first

from cStringIO import StringIO

my_str = 'qwer\nasdf\m\n'

buff = StringIO(my_str)

buff.seek(0,2)

use InMemoryUploadFile to create the string file

from django.core.files.uploadedfile import InMemoryUploadedFile
from app.models import Data
file_name = 'myfile.txt'
file_data = InMemoryUploadedFile(buf,'file',file_name,None,buff.tell(),None)

d = Data(file_name = file_name)
d.upload_file.save(file_name,file_data)
d.save() 

untile now we create an InMemoryUploadedFile object and save it to our model.

by the way we can use simpler way to create the file

from django.core.files.base import ContentFile

file_data = ContentFile(buff.getvalue())

Eclipse Tips

| Comments

1.fix unicode garbled

right click project->properties->Resource->Text file encoding->other->UTF-8

2.auto complete

Windows->Preferences->Java->Editor->Content Asist->replace auto activation triggers for Java “.” to “.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ”

Django Use Filefield in SAE

| Comments

sina 有个云平台叫sina app enginee,当然,是山寨google app enginee的。虽然这个东西还在alpha阶段,但是对于我等叼丝程序员来说,免去了购买vps麻烦,免费的总归是好的,即使有点问题,也总比没有的强吧。

最近研究了好几天的sae的storage。总觉得很不爽,因为官方api给的很笼统,看不明白,网上demo不多,几乎都同一个人转载,当然也有牛人,不过没找到牛人的源码,所以只好自己推敲了。

再这里要感谢下falcon同学,自己写了一个ready4sae的storage方法,在我纠结了好久以后,用了一下,发现竟然能够用,真是大喜啊.

下面就先来说说如何使用吧:

1.先建立一个store.py的文件:

#! coding:utf-8

import itertools import sae.storage from datetime import datetime from os.path import basename,splitext from sae.const import APP_NAME from django.conf import settings from django.core.files.storage import Storage from django.utils.http import urlquote

class SaeStorage(Storage):

"""
SAE storage
"""

def __init__(self, domain=None, app=None):
    if app is None:
        app = APP_NAME
    if domain is None:
        domain = settings.SAE_STORAGE_DOMAIN
    self.domain = domain
    self.base_url = "http://%s-%s.stor.sinaapp.com/" % (app,self.domain)
    self.client = sae.storage.Client()

def _open(self, name, mode='rb'):
    """
    not allow in sae, or maybe get the object content and store in memery using StringIO is a good idea. 
    """
    raise sae.storage.PermissionDeniedError('not allow to do this')

def _save(self, name, content):
    name = self.get_available_name(name)
    data = ''.join(content.chunks())
    ob = sae.storage.Object(data)        
    return self.client.put(self.domain, name, ob)

def delete(self, name):
    return self.delete(self.domain, name)

def exists(self, name):
    return name in [ob['name'] for ob in self.client.list(self.domain)]

def listdir(self, path):        
    return [ob['name'] for ob in self.client.list(self.domain)]

def path(self, name):        
    return self.url

def size(self, name):
    ob = self.clien.stat(self.domain,name)
    return ob['length']

def url(self, name):        
    url = self.client.url(self.domain,name)
    return url.replace(urlquote(self.base_url),'')

def accessed_time(self, name):
    return self.created_time()    

def created_time(self, name):
    ob = self.clien.stat(self.domain,name)
    return datetime.fromtimestamp(ob['datetime'])

def modified_time(self, name):
    return self.created_time()    

def get_available_name(self, name):
    count = itertools.count(1)
    while name in (ob['name'] for ob in self.client.list(self.domain)):
        file_root,file_ext = splitext(basename(name))
        name = '%s_%s%s' % (file_root,count.next(),file_ext)
    return name

2.然后在settings.py 里面写好sae mysql 的设置代码,这些网上请自己搜索。

3.建立自己的models.py

from django.db import models
from store import SaeStorage
class Persion(models.Model):
    name = models.CharField(max_length=30)
    photo=models.FileField(
storage=SaeStorage(domain='your_storage_domain',app='your_app_name'))
#    photo = models.FileField(upload_to='xxx')

这里注意下的说,在本地syncdb的时候,要使用默认的storage,不然会报错,但是用dev_server.py的时候是没关系的。

4.就这么简单,本地测试通过的话记得上传到svn,记得把mysql的tables导入到sae先。

5.ps.在此感谢 http://blog.csdn.net/vesslan1029/article/details/7594545 和falcon同学提供的思路。 以上。

Os X Lion Mysql Python

| Comments

1.after install mysql on mac.pls be sure the version of mysql.

file /usr/local/mysql/bin/mysql

it may be show

/usr/local/mysql/bin/mysql: Mach-O 64-bit executable x86_64

2.download mysql-python package.

wget http://download.sourceforge.net/sourceforge/mysql-python/MySQL-python-1.2.3.tar.gz

tar zxvf MySQL-python-1.2.3.tar.gz
cd MySQL-python1.2.3

3.change the site.cfg in line 13

#mysql_config = /usr/local/bin/mysql_config

to:

mysql_config = /usr/local/mysql/bin/mysql_config

4.build and install from source.

sudo ARCHFLAGS='-arch x86_64' python setup.py build
sudo ARCHFLAGS='-arch x86_64' python setup.py install

if mysql is a version of i386,use the command:

sudo ARCHFLAGS='-arch i386' python setup.py build
sudo ARCHFLAGS='-arch i386' python setup.py install

5.go to python shell

import MySQLdb

if raise error:

ImportError: dlopen(/Users/cgy8888/.python-eggs/MySQL_python-1.2.3-py2.6-macosx-10.7-x86_64.egg-tmp/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib
Referenced from: /Users/cgy8888/.python-eggs/MySQL_python-1.2.3-py2.6-macosx-10.7-x86_64.egg-tmp/_mysql.so
Reason: image not found

fixed it by doing the following:

sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib
sudo ln -s /usr/local/mysql/lib /usr/local/mysql/lib/mysql

now go to django project and run:

python manage.py syncdb

it will work now.

Mysql on Mac

| Comments

1.download mysql from mysql website.it’s a dmg file, open the dmg file 4 files in it.install the pkg file and the prefPane file

2.to use mysql command in the terminal,modify the /etc/bashrc file

#mysql
alias mysql='/usr/local/mysql/bin/mysql'
alias mysqladmin='/usr/local/mysql/bin/mysqladmin'

add in the end of bashrc file.

3.reopen the terminal,try use mysql command.

4.use mysqladmin to set root user

mysqladmin -u root password mysqlpassword 

Octopress to Github on Mac

| Comments

1.install xcode

2.install homebrew

$ ruby -e "$(curl -fsSkL raw.github.com/mxcl/homebrew/go)"
$ brew update

3.install git

$ brew install git

4.install RVM

$ bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)

5.install ruby

$ rvm install 1.9.3

6.install Pow

curl get.pow.cx | sh

7.download octopress

$ git clone git://github.com/imathis/octopress.git octopress
$ cd octopress

8.install some tools

$ gem install bundler
$ rbenv rehash
$ bundle install
$ rake install

9.test localhost view

rake preview

10.go to github website,and new a repository with the name “yourusername.github.com”

11.set octopress data

$ rake setup_github_pages

12.generate html files

$ rake generate

13.deploy it

$ rake deploy

then wait several minutes for github update the site.

14.post an article

rake new_post['article_title']

in octopress/source/_post/ will make a markdown file