Pyramid+SQLAlchemy: create filter with multiple OR conditions

…my first step with Python…

When you add filter with multiple criterion conditions joined by AND, you simply put all conditions one after one separated by comma:

query = DBSession.query(SomeTable).filter(
                SomeTable.name.contains(search_str),
                SomeTable.description.contains(search_str),
                SomeTable.another_field.contains(search_str)
        )

But in case you need to join filter conditions with OR, the way is more tricky… I found this approach working for me:

Add import:

from sqlalchemy import or_

And then, your filter should look like this:

query = DBSession.query(SomeTable).filter(
            or_(
                SomeTable.name.contains(search_str),
                SomeTable.name.contains(search_string2),
                SomeTable.description.contains(search_str),
                SomeTable.another_field.contains(search_str)
            )
        )

Enjoy!

Internet Explorer limits number of external CSS files to 32

Do you know IE has a limit of 32 external CSS files? Do you know each file should be up to 288Kb to get parsed?

I didn’t know and found this just a little bit of time ago. As Google says IE has a bug (well, someone names this a limitation which makes sense): number of external CSS files you can import is 32, everything beyond the 32th file will be ignored. In addition to this – CSS file size cannot exceed 288Kb, otherwise it will be ignored as well. This limit is present in Internet Explorer 6/7 and 8.

Seems IE is the only browser with such a limit, other browsers worked well for me with 33 external style sheets (Firefox, Opera, Safari, Google Chrome, Konqueror).

Solution: aggregate multiple CSS files in bigger ones. As my experience shows – most files are 5-20Kb in size, and one 40Kb file is faster than 8x5Kb.





Reset form with jQuery (part II)

Here I noted how to reset a form with jQuery, however, later I noticed some fields aren’t reset() correctly: in my form I have hidden fields with initial value of “0″ (or just an empty value) and these fields are supposed to be changed interactively upon a user’s input using jQuery. These fields weren’t reset correctly for some reason, at least in Firefox, thus I had to change my original function to something like this:

1
2
3
4
5
function resetForm(id) {
	$('#' + id + ' :input').each(function(){  // I decided not to have different reset routine for different field types
			$(this).val('');
	});
}

Probably less elegant, but works… ;)

How to .reset() form with jQuery

Such jQuery call won’t work:

1
2
	$('#formId').reset();
	// error: $("#formId").reset() is not a function

As the tutorial shows (yes, I never read manuals before I get into troubles as well, but always google answers before think, lol), we can have the following:

1
2
3
4
5
function resetForm(id) {
	$('#'+id).each(function(){
	        this.reset();
	});
}

…and somewhere on a page this:

1
	<input type="button" onclick="resetForm('formId');" />




Propel: Call to a member function XXXX on a non-object

Want to write down a simple trick I use to avoid errors like the subject says.

Let’s take we have described tables in our schema.xml:
Listing 1

1
2
3
4
5
6
7
8
9
10
11
12
13
  <table name="User" phpName="User" idMethod="native">
    <column name="id" phpName="Id" peerName="ID" type="INTEGER" required="true" autoIncrement="true" primaryKey="true"></column>
    <column name="username" phpName="UserName" peerName="USERNAME" type="VARCHAR" size="32" required="true" default=""></column>
    <column name="state_id" phpName="StateId" peerName="STATEID" type="INTEGER" required="true" default="0"></column>
    <foreign -key foreignTable="State">
      <reference local="state_id" foreign="id"/>
    </foreign>
  </table>
 
  <table name="State" phpName="State" idMethod="native">
    <column name="id" phpName="Id" peerName="ID" type="INTEGER" required="true" autoIncrement="true" primaryKey="true"></column>
    <column name="state_name" phpName="StateName" peerName="NAME" type="CHAR" size="25" required="true" default=""></column>
  </table>

Thanx to relation definition in lines 5-7 we can do a call like this:
Listing 2

1
2
// assuming $user is a valid instance of User
echo $user->getState()->getStateName();

But what happens if a certain user has no `state_id` set for some reason? We’ll get the message: Error: Call to a member function getName() on a non-object…
(Obviously as $user->getState() doesn’t produce a correct State instance without `state_id`)

In case `state_id` isn’t something nitty-gritty in our application we can extend our User class with the following:
Read the rest of this entry »

PHP 5 scope resolution problem

I was working a lot with PHP 4, with PHP5 I work for not very long time. For now the most disappointing thing in PHP 5 is scope resolution problem. It is described in ian at [first name]henderson dot org comment:

Please note that methods called by the scope resolution operator which are defined by a superclass of the first operand are called in the scope of the SUPERCLASS. For example,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?php
 
class ExampleSuperclass
{
    static function classType()
    {
        return "superclass";
    }
 
    static function doSomething()
    {
        echo "doing something with " . self::classType();
    }
}
 
class ExampleClass extends ExampleSuperclass
{
    static function classType()
    {
        return "subclass";
    }
}
 
ExampleClass::doSomething();
// output is "doing something with superclass"!
 
?>

This can be surprising (it surprised me!) when coming from other object-oriented languages, which would output “doing something with subclass” in this case.

The answer was:

The functionality you’ve expected maybe will be possible in PHP6….

That’s pity.





How to access iframe in jQuery

Assuming you have

1
  <iframe id="iframeID" ...></iframe>

Iframe contains div with id=”someID”:

1
  <div id="someID">Hello world!</div>

Need get div’s text?

1
  $('#iframeID').contents().find('#someID').html();