Wednesday, November 9, 2011

web2py - add a foreign key to aut_user

I'm still doing my project in web2py, and I wanted to add a foreign key in my auth_user table.

Problem: in the file models/db.py, you first declare the tables auth_* and then the other tables. So if you add your foreign key you'll get an error because the "foreign table" doesn't exist yet.
Example :

auth = Auth(db)
auth.settings.extra_fields['auth_user'] = [Field('myFK', db.foreignTable)]
auth.define_tables() 

db.define_table('foreignTable',Field('name', length=45) )

So I tried to define my foreignTable before settings.extra_fields, but it didn't work either...

Solution :
The only solution I could find was the following :
auth = Auth(db)
auth.settings.extra_fields['auth_user'] = [Field('myFK', integer)]
auth.define_tables() 

db.define_table('foreignTable', Field('name', length=45) )

db.auth_user.myFK.requires = IS_IN_DB(db, 'foreignTable.id', '%(name)s', zero=T('choose one')) 
At first I declare my column as an integer, and after I define my foreignTable. Then I link myFK to my foreignTable.

And that's it!

Friday, October 14, 2011

How to install web2py on a shared host

I'm working on a project with a friend using web2py
Free open source full-stack framework for rapid development of fast, scalable, secure and portable database-driven web-based applications. Written and programmable in Python.
Official website

Now that we are ready to upload our website, we were looking for good shared host. It was not easy to find a good one. Finally we choose Site5 because it supports Python and the server is in Montreal.

Problem:
how to install web2py on a shared host ?

Solution:
I could find this website : web2py on shared hosting.
I followed the instructions from "Installing web2py".

When you do this, here is the url to see your app :
http://www.mydomain.com/web2py/mySuperApp/default/index

But what we wanted was something like that :
http://www.mydomain.com

So here are the modifications :
- move all the web2py files directly to "public_html".
Before : $HOME/public_html/web2py/web2py.py
After : $HOME/public_html/web2py.py
(it's just an example with web2py.py file, but you should move all the files!)

- edit $HOME/public_html/routes.py, your file should just contains :
routers = dict(
    BASE = dict(
  default_application='mySuperApp',
  domains={'mydomain.com' : 'mySuperApp'},
    ),
) 
*routers is a new feature of web2py. This parameter-based system first appeared in web2py version 1.92.1.
If you want to know which version of web2py you're using, just open the file $HOME/public_html/VERSION
 
And that's it ! Now you can access your application simply by using : www.mydomain.com !

Friday, July 29, 2011

Adding Syntax Highlighting to Blogger

Problem
How to add syntax highlighting to Blogger ?

Solution
I couldn't find a better way than this website :
http://heisencoder.net/2009/01/adding-syntax-highlighting-to-blogger.html
That helped me a lot !

Android Mini Collectibles

*2011-08-09 The figures that I'm talking about are fake! Please read the comments

Today I'm not going to talk about software problem.
I want to talk about the Android Mini figures. I bought some and I'm not totally satisfied by the finishing.
Of course if you look at the official website (Dyzplastic) it looks very nice. And I think 80% of the time it is nice.
But here are some "bugs" that I could see.

1) Head
As you can see on the picture, there are 2 Reactor, but the one on the left has some strange stain on his head.

2) Paint
This looks like the painting was not done correctly

3) Dirty
This other Albino looks dirty (not as white as the other one)

4) Glue
The box opened by itself from the bottom. Cheap quality glue...


Well, the price of these figures is cheap but I was expecting something better from Google.


Saturday, June 11, 2011

Yii - Right join

I wanted to learn something new so I decided to create a website using the framework Yii (more info). It's written in PHP and I think it's pretty cool.

Yii comes with rich features: MVC, DAO/ActiveRecord, I18N/L10N, caching, authentication and role-based access control, scaffolding, testing, etc. It can reduce your development time significantly.

But I'm having a problem that I can't solve right now.

Problem

I have 2 tables :
- tbl_prono (id, game_id, ...) : there are 2 rows of data
- tbl_game (id, date, team_id,...) : there are 3 rows of data

In my model Prono I did the following relation:
return array(
'game'=>array(self::BELONGS_TO, 'Game', 'game_id'),
);


I created the following dataprovider in an action (PronoController):

$dataProvider=new CActiveDataProvider('Prono', array(
'criteria'=>array(
'with'=>array('game'=>array('joinType'=>'RIGHT JOIN')),
// please note the RIGHT JOIN
)));

$this->render('pronos',array('dataProvider'=>$dataProvider,));


It generates a correct sql querry. But it returns 2 rows instead of 3 !
It should return 3 rows because I'm doing a "right join" and tbl_game has 3 rows.

So, in my CgridView, I can see only 2 rows...

I couldn't find a solution to display 3 rows....

Thursday, April 14, 2011

ORA-01940 cannot drop user

Problem:
Oracle 10g, I want to drop a user using this command :
drop user alex cascade;

But I have this error:
ORA-01940: cannot drop a user that is currently logged in

Solution :
I checked on google and find this excellent Website : http://www.dba-oracle.com/t_ora_01940_cannot_drop_user.htm
It said that you should do this :

select s.sid, s.serial#, s.status, p.spid
from v$session s, v$process p
where s.username = 'myuser'
and p.addr (+) = s.paddr;

alter system kill session 'sid,serial#';


That's correct but your username should be in uppercase!
For example :
...
where s.username = 'ALEX'
...

And that's it !

You can do a quick test :

select username from v$session;
select * from v$session where username = 'alex';
select * from v$session where username = 'ALEX';

Wednesday, April 13, 2011

path oracle 10g

Problem:
You can't execute commands such as "imp", "exp", "impdp" or "expdp" even though the path is setted in the environment variable.

Solution:
In my case, there was a mistake. When I installed Oracle 10g, the path was automatically set to :
PATH=C:\oraclexe\app\oracle\product\10.2.0\server\bin;C:\Windows\system32...

But in reality it should be "BIN" (uppercase)!
PATH=C:\oraclexe\app\oracle\product\10.2.0\server\BIN;C:\Windows\system32...

After modifying the path, I could execute the command "impdp" anywhere from the command line.

Sunday, February 20, 2011

SQLiteDatabase created and never closed

Problem
When I'm using my application and I can see this error in the LogCat tab :


02-17 17:20:38.556: ERROR/Database(434): Leak found
02-17 17:20:38.556: ERROR/Database(434): java.lang.IllegalStateException: /data/data/databases/xx.db SQLiteDatabase created and never closed


Solution
Well, at first I thought it was because of my cursors. So I decided to check every cursor and close them all after having done with them.
But I still got the same problem.

So in fact, it's just that for every activity (onCreate) I was opening a connection to the database, but I never closed it.
So the solution was to close the connection in each activity:


@Override
protected void onStop() {
dbService.closeConnection();
super.onStop();
}


And that's it !

Sunday, February 13, 2011

Android Insufficient storage

Problem
I was debugging my application using an android virtual device in Eclipse.
When I run the application I could see this error in the console:

[2011-02-12 19:22:25 - shiro] Installation error: INSTALL_FAILED_INSUFFICIENT_STORAGE
[2011-02-12 19:22:25 - shiro] Please check logcat output for more details.
[2011-02-12 19:22:25 - shiro] Launch canceled!

The size of my app is around 22Mo (there are a lot of pictures).

Solution
The first time I configured the android virtual device, I put only 2 or 3Mo for "SD Card size".
So I had no choice to create a new virtual device and put something like 30Mo.
I ran my app for the first time. No problem. Then I wanted to run it again, and I got the same problem. I guess I could set the size of SD card to 100Mo, or in the virtual device, you can go to the menu > settings > manage applications. Then choose the application and uninstall it. Then you can run your app again in debug mode from Eclipse.

And that's it!