Friday, April 16, 2010

Installing Django (Python Framework) on an Ubuntu Linux Server

Install server software

Install Apache, Mod_Python, MySQL and MySQLdb. MySQLdb is the database bindings for MySQL. Django also supports PostgreSQL, Oracle and SQLite. If you choose to use a different database server, be sure to install the appropriate Python bindings.
#sudo apt-get install apache2 libapache2-mod-python
#sudo apt-get install mysql-server python-mysqldb
 

Install the Django source code

cd ~/
 
svn co http://code.djangoproject.com/svn/django/trunk/ django_src
 
Python won’t recognize Django unless it is installed in the “site-packages” directory, so instead we just create a symbolic link to the source code in our home directory. Run the first command to find out the path to your “site-packages” directory. Then use it in the second command, in place of “YOUR-DIR”. Lastly, copy the django-admin.py file into /usr/local/bin so that we don’t have to qualify the command with the full path to the file.

python -c "from distutils.sysconfig import get_python_lib; print get_python_lib()"
 
ln -s `pwd`/django_src/django YOUR-DIR/django
 
sudo cp ~/django_src/django/bin/django-admin.py /usr/local/bin
 

Create Django’s directories

cd ~/
 
mkdir django_projects
 
mkdir django_templates
 
mkdir media

 

Then we need to create some symbolic links in your webroot. The default webroot for an Apache2 installation on Ubuntu is /var/www. We are going to create a link to the media folder in your home directory, and a link to the admin_media folder which is provided in the Django source code.
cd /var/www
 
sudo ln -s  ~/media media
 
sudo ln -s ~/django_src/django/contrib/admin/media admin_media
 

Create a Django project

Move into your Django projects directory that we just created. We will be starting a new project using Django’s command line utility. This will give us a basic directory structure and the necessary configuration files. In my example I named the project “myproject.” Feel free to choose any name, as long as it does not conflict with any built-in Python or Django components. In particular, this means you should avoid using names like django (which will conflict with Django itself) or site (which conflicts with a built-in Python package).

cd ~/django_projects
 
django-admin.py startproject myproject
 
Edit the myproject/settings.py file and change the following sections:
  1. Uncomment and change the ADMINS setting



    6
    7
    8
    9
    10
    ADMINS = (
     
         ('Your Name', 'your_email@domain.com'),
     
    )
  2. Enter your database settings. You will need your database, username and password. Most likely your database server is running on the same server, so leave DATABASE_HOST blank



    DATABASE_ENGINE = 'mysql # 'postgresql_psycopg2','postgresql',mysql''sqlite3' or 'oracle'. 
    DATABASE_NAME = 'django_databae' # Or path to database file if using sqlite3. 
    DATABASE_USER = 'you'            # Not used with sqlite3. 
    DATABASE_PASSWORD = 'yourpassword'         # Not used with sqlite3. 
    DATABASE_HOST = ''# Set to empty string for localhost. Not used with sqlite3.
     
    DATABASE_PORT = ''# Set to empty string for default. Not used with sqlite3.

  3. Change your timezone if necesary.



    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    # Local time zone for this installation. Choices can be found here:
     
    # http://www.postgresql.org/docs/8.1/static/datetime-keywords.html
    #DATETIME-TIMEZONE-SET-TABLE
     
    # although not all variations may be possible on all operating systems.
     
    # If running in a Windows environment this must be set to the same as your
     
    # system time zone.
     
    TIME_ZONE = 'America/Chicago'
  4. Point Django at the template directory we created.



    TEMPLATE_DIRS = (
     
     "/home/YOUR_USERNAME/django_templates" 
    # Put strings here, like "/home/html/django_templates" or 
     "C:/www/django/templates".
     )
  5. Do the same thing for the media url and directory we created earlier.



    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    # Absolute path to the directory that holds media.
     
    # Example: "/home/media/media.lawrence.com/"
     
    MEDIA_ROOT = '/home/YOUR_USERNAME/media/' 
    # URL that handles the media served from MEDIA_ROOT. Make sure to use a
     
    # trailing slash if there is a path component (optional in other cases).
     
    # Examples: "http://media.lawrence.com", "http://example.com/media/"
     
    MEDIA_URL = 'http://yourdomain.com/media/'
  6. Set the admin media directory that we created in your webroot



    45
    46
    47
    48
    49
    50
    51
    # URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
     
    # trailing slash.
     
    # Examples: "http://foo.com/media/", "/media/".
     
    ADMIN_MEDIA_PREFIX = '/admin_media/'
  7. And finally, add the admin application to your install applications



    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    INSTALLED_APPS = (
     
        'django.contrib.auth',
     
        'django.contrib.contenttypes',
     
        'django.contrib.sessions',
     
        'django.contrib.sites',
     
        'django.contrib.admin',
     
    )
      
Then go to the directory which have file 
manage.py  
and run command on terminal 
(please first remember the creating database name "django" in mysql)  
#manage.py syncdb
  
 
Edit the URL configuration file and uncomment the admin line. This will allow you to access the admin section later.
nano ~/django_projects/myproject/urls.py
1
2
3
   # Uncomment this for admin:
     (r'^admin/', include('django.contrib.admin.urls')),
 

Configure Apache and mod_python

 sudo nano /etc/apache2/httpd.conf
 
MaxRequestsPerChild 1
<location "/">
 
    SetHandler python-program
 
    PythonHandler django.core.handlers.modpython
 
    SetEnv DJANGO_SETTINGS_MODULE myproject.settings
 
    PythonPath "['/home/YOUR_USERNAME/django_projects'] + sys.path"
 
</location>
 
<location "/media">
 
    SetHandler None
 
</location>
 
<location "/admin_media">
 
    SetHandler None
 
</location>
 
<location "/phpmyadmin">
 
    SetHandler None
 
</location >
 
<locationmatch ".(jpg|gif|png)$">
 
    SetHandler None
 
</locationmatch> 

Restart Apache and pray

sudo /etc/init.d/apache2 restart

Sunday, March 21, 2010

Activating the User's public_html Directory with userdirectory (Using Apache2 )

Create a directory (folder) called "public_html" in your home directory, with your file browser or the command below. Do NOT use the sudo command.
me@myhost$ mkdir public_html

I have found that on a simple development machine, it is easier to have your work all within your own home directory and not have to worry about permissions and folders owned by the root user. What it gets you is the ability to access your websites in your home directory by a URL in the form http://localhost/~USER_NAME. We can improve upon this with fake domains, but first things first.
There are only two commands you need to enter to activate the User Directory feature, and then one command to reload the configuration files. The last command includes an absolute path, so it doesn't matter where you execute it from. The first two "ln" commands assume you are in the directory /etc/apache2/mods-enabled. What you need to do is create two symbolic links (soft links, symlinks) in the stated directory pointing to the corresponding module in /etc/apache2/mods-available. So, if "$" is your prompt,
 
me@myhost$ cd /etc/apache2/mods-enabled
me@myhost$ sudo ln -s ../mods-available/userdir.conf userdir.conf
me@myhost$ sudo ln -s ../mods-available/userdir.load userdir.load
me@myhost$ sudo /etc/init.d/apache2 restart

On your home system, restarting apache will give you a warning "apache2:

Wednesday, March 3, 2010

.odt on fly

What is odtPHP ? 
OdtPHP is an oriented object libary for PHP 5+. It allows you to generate automatically OpenOffice text documents from templates. You can use it directly within your PHP scripts (without OpenOffice).
OdtPHP is very easy to use : with a minimum of code, you will be able to generate simple documents by replacing tags from template and by inserting some images. You can also take advantage from advanced features in order to create complex OpenOffice files by repeating parts of the document or lines of array.

Features

OdtPHP allows you to replace tags from your OpenOffice template by text content or pictures. The library also handles loops that can be imbricated as you want. Moreover, odtPHP allows you to simply repeat lines of OpenOffice tables. Eventually, you can export the final result in a file on your server or directly to the client browser.

 


Download odtPHP 

OdtPHP is released under the terms of the GNU Public License. To get more informations about the GPL license, For download   Click Here

Working Experience  :-

Its is very easy to run  ,by using the library .

But  there is one Problem that is

> By default there is no permission to access some labrary files 

Run by http://localhost/odtphp/tests/tutoriel1.php link on 

There error is  on the  bowers when we convert file into odt format :- 

> Warning: mkdir() [function.mkdir]: Permission denied in /var/www/odtphp/library/zip/PclZipProxy.php on line 50

> Warning: mkdir() [function.mkdir]: Permission denied in /var/www/odtphp/library/zip/PclZipProxy.php on line 50

> Warning: file_put_contents(./tmp/content.xml) [function.file-put-contents]: failed to open stream: No such file or directory in /var/www/odtphp/library/zip/PclZipProxy.php on line 94

> Fatal error: Uncaught exception 'OdfException' with message 'Error during file export' in /var/www/odtphp/library/odf.php:250 Stack trace: #0 /var/www/odtphp/library/odf.php(266): Odf->_save() #1 /var/www/odtphp/tests/tutoriel1.php(31): Odf->exportAsAttachedFile() #2 {main} thrown in /var/www/odtphp/library/odf.php on line 250


How to remove this errors :-  
> we can remove this errors by change the file permission by usibg this commonds on terminal of ubuntu   

#chmod -R 777 /var/www/odtphp/



than its working .....................


Friday, February 26, 2010

MySQL Basic Commands

Below when you see "#" or "$" it means from the unix shell. When you see mysql> it means from a MySQL prompt after logging into MySQL.

To login (from Linux shell) use -h only if needed.

#mysql -h hostname -u root -p

Create a database on the sql server.

mysql> create database [databasename];

List all databases on the sql server.

mysql> show databases;

Switch to a database.

mysql> use [db name];

To see all the tables in the db.

mysql> show tables;

To see database's field formats.

mysql> describe [table name];

To delete a db.

mysql> drop database [database name];

To delete a table.

mysql> drop table [table name];

Show all data in a table.

mysql> SELECT * FROM [table name];

Returns the columns and column information pertaining to the designated table.

mysql> show columns from [table name];

Show certain selected rows with the value "whatever".

mysql> SELECT * FROM [table name] WHERE [field name] = "whatever";

Show all records containing the name "Bob" AND the phone number '3444444'.

mysql> SELECT * FROM [table name] WHERE name = "Bob" AND phone_number = '3444444';

Show all records not containing the name "Bob" AND the phone number '3444444' order by the phone_number field.

mysql> SELECT * FROM [table name] WHERE name != "Bob" AND phone_number = '3444444' order by phone_number;

Show all records starting with the letters 'bob' AND the phone number '3444444'.

mysql> SELECT * FROM [table name] WHERE name like "Bob%" AND phone_number = '3444444';

Show all records starting with the letters 'bob' AND the phone number '3444444' limit to records 1 through 5.

mysql> SELECT * FROM [table name] WHERE name like "Bob%" AND phone_number = '3444444' limit 1,5;

Use a regular expression to find records. Use "REGEXP BINARY" to force case-sensitivity. This finds any record beginning with a.

mysql> SELECT * FROM [table name] WHERE rec RLIKE "^a";

Show unique records.

mysql> SELECT DISTINCT [column name] FROM [table name];

Show selected records sorted in an ascending (asc) or descending (desc).

mysql> SELECT [col1],[col2] FROM [table name] ORDER BY [col2] DESC;

Return number of rows.

mysql> SELECT COUNT(*) FROM [table name];

Sum column.

mysql> SELECT SUM(*) FROM [table name];

Join tables on common columns.

mysql> select lookup.illustrationid, lookup.personid,person.birthday from lookup left join person on lookup.personid=person.personid=statement to join birthday in person table with primary illustration id;

Creating a new user. Login as root. Switch to the MySQL db. Make the user. Update privs.

# mysql -u root -p
mysql> use mysql;
mysql> INSERT INTO user (Host,User,Password) VALUES('%','username',PASSWORD('password'));
mysql> flush privileges;

Change a users password from Linux shell.

# mysqladmin -u username -h hostname.blah.org -p password 'new-password'

Change a users password from MySQL prompt. Login as root. Set the password. Update privs.

# mysql -u root -p
mysql> SET PASSWORD FOR 'user'@'hostname' = PASSWORD('passwordhere');
mysql> flush privileges;

Recover a MySQL root password. Stop the MySQL server process. Start again with no grant tables. Login to MySQL as root. Set new password. Exit MySQL and restart MySQL server.

# /etc/init.d/mysql stop
# mysqld_safe --skip-grant-tables &
# mysql -u root
mysql> use mysql;
mysql> update user set password=PASSWORD("newrootpassword") where User='root';
mysql> flush privileges;
mysql> quit
# /etc/init.d/mysql stop
# /etc/init.d/mysql start

Set a root password if there is on root password.

# mysqladmin -u root password newpassword

Update a root password.

# mysqladmin -u root -p oldpassword newpassword

Allow the user "bob" to connect to the server from localhost using the password "passwd". Login as root. Switch to the MySQL db. Give privs. Update privs.

# mysql -u root -p
mysql> use mysql;
mysql> grant usage on *.* to bob@localhost identified by 'passwd';
mysql> flush privileges;

Give user privilages for a db. Login as root. Switch to the MySQL db. Grant privs. Update privs.

# mysql -u root -p
mysql> use mysql;
mysql> INSERT INTO db (Host,Db,User,Select_priv,Insert_priv,Update_priv,Delete_priv,Create_priv,Drop_priv) VALUES ('%','databasename','username','Y','Y','Y','Y','Y','N');
mysql> flush privileges;

or

mysql> grant all privileges on databasename.* to username@localhost;
mysql> flush privileges;

To update info already in a table.

mysql> UPDATE [table name] SET Select_priv = 'Y',Insert_priv = 'Y',Update_priv = 'Y' where [field name] = 'user';

Delete a row(s) from a table.

mysql> DELETE from [table name] where [field name] = 'whatever';

Update database permissions/privileges.

mysql> flush privileges;

Delete a column.

mysql> alter table [table name] drop column [column name];

Add a new column to db.

mysql> alter table [table name] add column [new column name] varchar (20);

Change column name.

mysql> alter table [table name] change [old column name] [new column name] varchar (50);

Make a unique column so you get no dupes.

mysql> alter table [table name] add unique ([column name]);

Make a column bigger.

mysql> alter table [table name] modify [column name] VARCHAR(3);

Delete unique from table.

mysql> alter table [table name] drop index [colmn name];

Load a CSV file into a table.

mysql> LOAD DATA INFILE '/tmp/filename.csv' replace INTO TABLE [table name] FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' (field1,field2,field3);
Example 2 : LOAD DATA INFILE '/var/lib/mysql-files/test.csv' INTO TABLE test FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n' (field1,field2,field3);

Dump all databases for backup. Backup file is sql commands to recreate all db's.


# mysqldump -u root -p --single-transaction --quick --lock-tables=false --all-databases > full-backup-$(date +\%F).sql
or
$ mysqldump -u [uname] -p[pass] [dbname] | gzip -9 > [backupfile.sql.gz]

if you want to extract the .gz file, use the command below:
$ gunzip [backupfile.sql.gz]

Dump one database for backup.

# mysqldump -u username -ppassword --databases databasename >/tmp/databasename.sql

Dump a table from a database.

# mysqldump -c -u username -ppassword databasename tablename > /tmp/databasename.tablename.sql

Restore database (or database table) from backup.

# mysql -u username -ppassword databasename < /tmp/databasename.sql


To restore compressed backup files you can do the following:
#gunzip < [backupfile.sql.gz] | mysql -u [uname] -p[pass] [dbname]



If you need to restore a database that already exists, you'll need to use mysqlimport command. The syntax for mysqlimport is as follows:

#mysqlimport -u [uname] -p[pass] [dbname] [backupfile.sql]



Create Table Example 1.
mysql> CREATE TABLE [table name] (firstname VARCHAR(20), middleinitial VARCHAR(3), lastname VARCHAR(35),suffix VARCHAR(3),officeid VARCHAR(10),userid VARCHAR(15),username VARCHAR(8),email VARCHAR(35),phone VARCHAR(25), groups VARCHAR(15),datestamp DATE,timestamp time,pgpemail VARCHAR(255));

Create Table Example 2.

mysql> create table [table name] (personid int(50) not null auto_increment primary key,firstname varchar(35),middlename varchar(50),lastnamevarchar(50) default 'bato');

Queries 
UPDATE table1 t1 SET table_field = (SELECT tabel_field from table2 t2 WHERE t1.job_no = t2.job_no)

Create user and database  , set password 
#
# Connect to the local database server as user root
# You will be prompted for a password.
#
mysql -h localhost  -u root -p

#
# Now we see the 'mysql>' prompt and we can run
# the following to create a new database for Paul.
#
mysql> create database pauldb;
Query OK, 1 row affected (0.00 sec)

#
# Now we create the user paul and give him full 
# permissions on the new database
mysql> grant CREATE,INSERT,DELETE,UPDATE,SELECT on pauldb.* to paul@localhost;
Query OK, 0 rows affected (0.00 sec)

#
# Next we set a password for this new user
#
mysql> set password for paul@localhost = password('mysecretpassword');
Query OK, 0 rows affected (0.00 sec)

#
# Cleanup and ext
mysql> flush privileges;
mysql> exit;

Saturday, February 13, 2010

Working Experience on "office automation" project."



We are working on office automation project , this is the project will reduce the office work with help of computer applications. There will be no need of calculating funds again and again , also no need of making paper records for the various funds transactions.
Our seniors had also tried their hands on this project and they were very close in completion. but, their were some problems in implementation of the project.
I along with my classmate Daljeet Singh Pathania again start working on this project under Dr. H.S.Rai sir's guidance.We started our journey of making project from the collection of previous work done and other details of the project available with Dr. H.S Rai sir. We both were very excited to work on this project and we goes as following:-

First we can try to run on Linux its difficult because our seniors made this on Windows . So our main problem is Linux is case sensitive and Windows not . So we can rename all the files according to file name is used in other files . So we can read all the files and modified according to need . And also make links between the files .
Second major problem is different version of same software a used by the our seniors and Now a we are use different Version . So there are some Keywords which are not support by older or new version .Some of the statement are Not working in new Version software . So we are modified these files .
In between these problems we are also get help from our respected teacher Dr. Hsrai sir . I was facing the problem of using username and password use in this software . Me used the Database username and password on that place by misunderstanding . So I told our sir , about this problem than they will tell to me thats username & password was not databases username & password , its a username which are written by in database tables .


Rai sir's guidance and motivation was always there to help and support us throughout the project.
We were facing the problem in “logging in“ in the software . We were using the database user and password in logging in the project but Rai sir suggest us the write user name and password (i.e. Which stored in the database table).
Now we are able to solve these problem and our project is on 90% worked condition.

Problems still facing

  1. How to add new users in the project and how to set the password for the new users.
  2. we also have to make amendments in the project according to the current policies of the college.
  3. Create the .odt file .
Now I can able to add the user  user ,but how to set the different password for new users , we cannot able to set this. but try in future ..
Now go to second Problem .
Our seniors are make this software according to  that time it need , but now there are many changes which we have to change
1. Add the one new field  in final reports which is generate that is " Higher Education tax" .
To add that field first  we change the database TABLE NAME "amt"  we can add new field that is "hedu" .
After this we can change the  whole system  files to display and also make calculations to add that data in TOTAL AMOUNT  and change the Service TAX ,EDUCATION TAX according to need.
Thats  diffcult job to read and modifly the whole system files ,but we can do it
2. The  SUSPENCE REPORT  is not Working  for ALL  the fields Which user required.
For this  we can modify some programming  files in which the SUSPENCE is not working .
3. In SUSPENCE REPORT  we are not able to get the DATE of CHECK /DD NUMBER .
For this we first add the DATE option in which the user can add  the date of CHECK / DD NUMBER  , than  its this date  can go to database TABLE NAME suspence  ,than  we can retrieve the date form database than Display on the Final SUSPENCE REPORT..
4. User also want to display the extra field in SUSPENCE REPROT  like TDS Amonut , Balance , Service tax and Total Amount.
so we can change this account to requirement .
5. Now the big issue is to generate the "" PROFORMA BILL REPROT"", BILL FEE REPORT" .For the addition of Proforma Bill & Bill Report we can again working with database file and add two tables PROFROBILL and BILL in database TC .After this we can add a new option in MENU.PHP ,than Create on the user interface for this field to add our data in database. Than we can retrieve the data form database tables and able to Create the report according to the user need.
6 .User also required to add the "SOIL" Field in the INSTITUTIONAL phase
7 .Problem with _POST method is working with the string value in PHP ....... This can be solved with help of Hsrai sir....... by using Inter values can be used without any quite, but string values need to be enclosed in single quote.
So Now we can working on this ...............