Category: Life & Work

长期维护生产环境的数据库,恐怕很多人会犯这样的错误:

update tableA set field1value = VVVV (where …);

忘记了加括号中的约束条件,等反应过来,Shell 中显示已经更新了几万条数据。

如何挽救这样的失误呢?

  1. 每天进行数据备份,我们的方案是每天 dump 整库,保存在 S3 中,服务器上只保存昨天的数据
  2. 假如有前一天的备份数据,可以选择性恢复除了今天以外的数据。Shell 中命令行失误往往被损坏的数据都涉及一张表的一个字段或者几个字段。
  3. Drupal 中可以利用其支持多库的特性,新建一个旧数据的库,从旧数据库中读取数据,覆盖到新库上。以下是恢复数据的脚本例子

假如不是 Drupal 系统,也可以直接用 PHP 连接2个数据库,进行类似操作。

Drupal 7 profile menu

There 2 methods to add a menu in Drupal, hookmenu or hookmenu_alert

We can define a menu like this:

  $items['user/%user/facebook'] = array(
    'title' => 'Facebook',
    'description' => 'Facebook Profile',
    'page callback' => 'facebook_user_settings',
    'file' => 'facebook.pages.inc',
    'page arguments' => array(1),
    'access callback' => 'facebook_user_access', //access arguments don't support multiple arguments, so create our access handler
    'access arguments' => array(1),
    'type' => MENU_LOCAL_TASK,
  );

Because the menu type is MENULOCALTASK, this will add a new tab “Facebook” right after the “edit” tab. 

But how to add the “facebook” tab in secondary level, after “Account”, just do as the “Linkedin” tab?

The following code is NOT right:

  $items['user/%user/edit/facebook'] = array(
    'title' => 'Facebook',
    'description' => 'Facebook Profile',
    'page callback' => 'facebook_user_settings',
    'file' => 'facebook.pages.inc',
    'page arguments' => array(1),
    'access callback' => 'facebook_user_access', //access arguments don't support multiple arguments, so create our access handler
    'access arguments' => array(1),
    'type' => MENU_LOCAL_TASK,
  );

 

This will lead some errors. You should do as the following steps, (Linkedin codes):

Step 1, show a tab menu after “Account”:

function linkedin_user_categories() {
  return array(
    array(
      'name' => 'linkedin',
      'title' => 'Linkedin',
      'weight' => 3,
    ),
  );
}

Step 2, define the menu callback functions:

function linkedin_menu_alter(&$callbacks) {
  $callbacks['user/%user_category/edit/linkedin']['page callback'] = 'linkedin_user_settings';
  $callbacks['user/%user_category/edit/linkedin']['module'] = 'linkedin';
  $callbacks['user/%user_category/edit/linkedin']['page arguments'] = array(1);
  $callbacks['user/%user_category/edit/linkedin']['file'] = 'linkedin.pages.inc';
}

There is no document about hookusercategories for Drupal 7, so this article shows How to use it.

Hope to help you.

Django

Introduction:

Django is the modern base stack for web development. Compared with Ruby on Rails, I think I like Django better:

  • Build-in ready to use admin interface
  • Written in Python, with better performance
  • Simpler and reasonable project struct
  • Smaller base stack, but you can add what you want (ajax, south, REST, oauth …)

Essential tools for Django development:

virtualenv

We use this to manage the library for the project. You can have different environments (different python modules, different python versions) for every projects.

Create new clean environment foobar

virtualenv –no-site-packages foobar

Active the environment foobar

source foobar/bin/activate

Then install library in the environment foobar

pip …

At the end leave the environment foobar

deactivate

pip or easy_install

To install python libraries.

Django south

Database migration tool. Alertanative tool for syncdb

Create migration for your app

./manage.py schemamigration myapp –initial

Apply the migration

./manage.py migrate myapp

Then change the model myapp, and make a new migratation

./manage.py schemamigration myapp –auto

And apply it

./manage.py migrate myapp django-annoying

Useful decorators:

rederto ajaxrequest JSONField getobjectorNone

django-compressor

Compresses linked and inline javascript or CSS into a single cached file. And support coffeescript and less css framework.

django-pagination

pagination tools

django-piston

A mini-framework for Django for creating RESTful APIs.

django-celery

Celery integration for Django.

Celery is a task queue/job queue based on distributed message passing.

django-mongodb

MongoDB backend

django-admin-tools

Custom user interface, customizable dashboard

django-socialauth

oauth for social sites, login with twitter account, facebook account or gmail account

django-storages

Amazon S3, MogileFS file storage

django-grappelli

A jazzy skin for the Django Admin-Interface

django-profiles

A simple application which provides basic features for working with custom user profiles in Django projects.

django-registration

User registration and login

simplejson

JSON encoding and decoding

django-imagekit

Image process, support S3.

Beautiful Soup

Web Scrapping tool

django-workflows

django-workflows provides a generic workflow engine for Django.

The Django development environment steps:

How to install the virtualenv & django:

sudo apt-get install python-virtualenv

virtualenv –no-site-packages env_nm

source env_nm/bin/activate

pip install django

… ( install other modules )

deactivate

Export the libraries of the environment to a configuration file

pip freeze > requirements.txt

Install the environment in a clean environment

pip install -r requirements.txt

The missing part of Django settings.py

import os

ROOT_PATH = os.path.dirname(file)

MEDIAROOT = os.path.join(ROOTPATH, ‘static’)

TEMPLATE_DIRS = (

os.path.join(ROOT_PATH, ‘templates’)

)

Django Signals – You should pay attentation to this

This is the ECA (Aka: Event Condition Action) part of Django. 

ECA is the essentail part of modern web appliactions.

Other useful modules:

SQLAlchemy – use this when the django ORM is not enough.

Haystack – Modular search for Django.

Django-mptt – Preorder tree management