Coffeescript

Decided to try out Coffeescript. Why – to make javascript coding interesting again. It’s always fun to play around with new language, especially with one that has different syntax.

Installed in on my ubuntu following directions from Installing CoffeeScript on Ubuntu or Debian and Coffeescript.org. Installed node.js as well – wanted to try it out anyway after last devclub.eu meetup.

For now just playing around with it – trying to write javascript using coffeescript’s syntax with the help of Coffeescript Chrome Extension.

It’s definitely fun, but don’t see any practical use so far. Syntax is similar to Ruby, so maybe it’s easier for Ruby developers to switch to Coffeescript instead of learning Javascript. Will see if it turns useful later on.

Conditional stylesheets in symfony using view.yml

Was implementing BlueprintCSS on a website, and needed a way to put a conditional stylesheet. Remembered seeing it somewhere, but was not able to find it, so decided to do a little research on how it can be done. Here is a little reference on what can be used in view.yml stylesheet option.

If a stylesheet you include needs some options you should put them after a stylesheet url like this:

stylesheets:
  - blueprint/print.css : {media: print}

Everything you put there will be used as an attribute to link tag. Certain keys have a special meaning.

absolute: true|false – Will make CSS url absolute

condition: lt IE 8 – Will wrap link tag in a conditional comment

raw_name: true|false – Will use stylesheet name as a href directly. Ignores absolute option, does not attempt to add /css/ before it

BlueprintCSS example:

view.yml

stylesheets:
    - blueprint/screen.css
    - blueprint/print.css: { media: print, absolute: true }
    - blueprint/ie.css: { condition: lt IE 8}

will output

<link rel="stylesheet" type="text/css" media="screen" href="/css/blueprint/screen.css" />
<link rel="stylesheet" type="text/css" media="print" href="http://localhost/css/blueprint/print.css" />
<!--[if lt IE 8]><link rel="stylesheet" type="text/css" media="screen" href="/css/blueprint/ie.css" /><![endif]-->

Number formatting with thousands separator in Python

I am now learning Python and try to do all sorts of tasks I previously used PHP for using Python programming language.

This works quite well, especially with the help of a great website for PHP developers switching to Python

Was looking for a function similar to PHP’s number_format(), and to my surprise didn’t find any.

After more googling found that in Python3 format() and str.format() allow to do that. But I’m using Python2.7.1

Turned out, that it works as expected. The code I used in the end looks like this:

    # 12345 -> 12 345
    "{0:,d}".format(int(mystring)).replace(',', ' ')

How to return first element of a hash in PHP?

This question puzzled me for quite a lot of time now. It’s easy to get a first element of a “normal” array, but whenever I would need the first element of a hash I would go with code that just didn’t look clean to me.

Here’s how I did it before:

$example = array("one" => "ten", "two" => "twenty", "three" => "thirty");
$values = array_values($example);
$first = $values[0];

A lot of code for such a simple operation, right?

But recently I found out about side-effect of reset() function: it not only rewinds internal pointer to the beginning of an array, but also returns the first element.

So, if you want to get the first element of a hash, just do this:

$first = reset($example);

Instructions for setting up fresh symfony1.4 project

Since I happen do it all the time, decided to list all instructions here:

[above_docroot]$ mkdir -p lib/vendor && cd lib/vendor
[lib/vendor]$ wget http://www.symfony-project.org/get/symfony-1.4.13.tgz
[lib/vendor]$  tar zxpf symfony-1.4.13.tgz
[lib/vendor]$ mv symfony-1.4.13 symfony
[lib/vendor]$ rm symfony-1.4.1.tgz
[lib/vendor]$ cd ../../
[above-docroot]$ lib/vendor/symfony/data/bin/symfony generate:project ProjectName

URL for latest version you can get from here: http://www.symfony-project.org/installation

Howto: allow Facebook (or some other site) to bypass .htaccess Basic Authentication

So, I’m building a website and need to check if OpenGraph tags are correct.

I’m using Facebook’s URL Linter for that, but the site is protected with Basic Authentication, and Facebook cannot connect to it.

Turned out that it’s quite easy to allow Facebook in, just add

Order allow,deny
Allow from facebook.com
Satisfy Any

after require valid-user, so your .htaccess will look something like:

<Files ~ "^\.(htaccess|htpasswd)$">
deny from all
</Files>
AuthUserFile /path/to/.htpasswd
AuthGroupFile /dev/null
AuthName "Protected area"
AuthType Basic
require valid-user 
 
order allow,deny
Allow from facebook.com
Satisfy Any

Satisfy any means “Allow access if user enters valid username and password OR if they pass IP address restriction”.

I also did add

Allow from 192.168

so that I don’t have to enter password myself.

Fixing SSH login delay (how to disable gssapi-with-mic) on Ubuntu Linux

Had a problem with ssh login for some time now – it was taking way too long to connect to our staging machine. Normally it’s not a problem – after all, you connect once and work for all day.

But for rsync and git push this is annoying. Everytime you git push you have to wait almost a minute. Reminds me of days I was using SVN :)

Today I had enough of that and decided to fix the problem.

First, I put ssh client into verbose mode:

ssh german@dev-server -v

Turned out that it was stalling after trying gssapi-with-mic authentication method. Had several “Unspecified GSS failure” messages with several seconds delay between them.

Well, I am using publickey for authentication anyway, so I needed to disable gssapi auth.

There are two ways to do that:

Fix #1: pass an option when trying to connect

ssh -o GSSAPIAuthentication=no german@dev-server

Fix #2: add a line to your ~/.ssh/config

...
GSSAPIAuthentication=no
...

I prefer second method, because this way you don’t have to configure every program that connects to SSH to use those options, they will all just work (rsync, git, Nautilus)

How to add jQuery (Sizzle) selectors to Selenium

First, download Sizzle, which is a selector engine for jQuery and unpack sizzle.js to a convenient location.

Second, create empty user-extensions.js file. Name can be whatever you want by the way.

Add this to user-extensions.js

PageBot.prototype.locateElementBySizzle = function(locator, inDocument) {
  var results = [];
  window.Sizzle(locator, inDocument, results);
  return results.length > 0 ? results[0] : null;
}

Third, go to Selenium IDE, Options -> Options… and add sizzle.js and user-extensions.js to “Selenium Core extensions”.

Restart Selenium IDE (just close all instances of it and open it again), and now you can use sizzle=(locator) everywhere, where “locator” is needed

Adding jQueryUI Autocomplete to jqGrid edit form

Here is an example

For this to work you need to include jQueryUI 1.8+, since 1.7 doesn’t have autocomplete in it.

Adding Autocomplete to a form is done using edittype:"custom" option of a colModel. You are required to specify editoptions, which is just an object with two keys: custom_element and custom_value

custom_element is a callback that will create HTML element for use in form, custom_value allows to retrieve and set your element value.

function autocomplete_element(value, options) {
  // creating input element
  var $ac = $('<input type="text"/>');
  // setting value to the one passed from jqGrid
  $ac.val(value);
  // creating autocomplete
  $ac.autocomplete({source: "autocomplete.php"});
  // returning element back to jqGrid
  return $ac;
}
 
function autocomplete_value(elem, op, value) {
  if (op == "set") {
    $(elem).val(value);
  }
  return $(elem).val();
}

Your resulting colModel should look like this:

  ...
  colModel: [
    {
      name          : 'field1',
      'index'       : 'field1',
      'edittype'    : 'custom',
      'editoptions' : {
        'custom_element' : autocomplete_element,
        'custom_value'   : autocomplete_value
      }
    }
  ]
  ...

Garry, thank you for pointing out problem with form and next/prev buttons, I wasn’t even aware of that problem! After digging through source code I found out how custom_value is intended to work.

If you find this snippet useful or have something to add/correct, please leave a comment. I need your feedback!

Doctrine migrations

Finally learned how to use Doctrine migrations.

You have development and staging servers. Your development server moved forward – you did some database changes, removed some fields, added some indexes. Now it’s tricky to update staging server without rebuilding the db (and losing all the data).

Sure, you can alter database by hand, but if you made a lot of changes, that would be a long and error-prone process.

That’s when you are supposed to use migrations.

There was only one problem – I didn’t know how to use them correctly. Now I figured it out.

  1. Get a “staging” server model files (checkout or copy)
  2. Put new schema.yml into config/doctrine directory replacing old one.
  3. and run
    ./symfony doctrine:generate-migrations-diff

Symfony will now generate a migration file that brings your database up to date.

Now, when you deploy your changes just run

./symfony doctrine:migrate

Now I just have to remember doing generate-migrations-diff before every doctrine:build –all, so I will have all the necessary files before uploading.