Archive for the ‘Programming’ Category

Limit user email domains in BuddyPress

Friday, March 4th, 2011

One of my upcoming projects is an intranet. For many reasons, I have chosen to use BuddyPress as the starting point and customize it for specific needs.

One such need is a restriction on which email address domains can be used to register a user. For instance, if I were to create such a system for old.webit.ca, I would want only users with a @old.webit.ca email address to register. Anyone else would simply not be allowed.

After some digging around, it seemed like there was no plugin that would quickly do what I was looking for. There were options, but most felt complicated or hacks. That is, until I found the following line in bp-core-signup.php:

// in function bp_core_validate_user_signup
$limited_email_domains =
    get_site_option( 'limited_email_domains', 'buddypress' );

Great! All that is left is a quick plugin to populate ‘limited_email_domains’.

<?php
/*
Plugin Name: Restricted Email Domains
Description: Restricts registration user email addresses to @old.webit.ca
*/
add_option('limited_email_domains', array('old.webit.ca'));

Activate the plugin and you’re good to go.

NoSQL Primer for Python

Monday, January 31st, 2011

Bellow is a video of my presentation at MontrealPython 18. It is a short introduction to NoSQL databases for the Python programmer.

The slides

PIL 1.1.7, Python 2.6 and Mac 10.5

Tuesday, May 11th, 2010

Here’s how it’s done.

http://countergram.com/articles/install-pil-intel-mac/

Extending the Django User model

Wednesday, May 5th, 2010

There appears to be 3 ways to extend the functionality of the Django User model:

  • Through sub-classing
  • With a profile class
  • By monkey-patching

(more…)

A case for Test Driven Development

Thursday, March 25th, 2010

I will be honest: TDD is hard. It’s hard to get into, it’s hard to keep up. It’s all too easy to revert to a time when programming was about creativity, experimentation and the gratifying feeling of printing ‘Hello, world!’.

(more…)

What is good software?

Thursday, January 21st, 2010

I have been going through some change. Good change. The kind of change which makes a professional question the merit and quality of their work. It’s the kind of change which leaves the inquisitive pondering for days. Am I writing good software? What is good software, anyway?

(more…)

Uploaded Excel mime type

Thursday, November 5th, 2009

I’ve been strugling to find the reason behind a malfunctioning Excel upload feature. The problem was that some times, the file would not have the application/vnd.ms-excel mime type. This was a bit of an issue, it was required for invaliding the type of the file.

By luck, a coworker discovered that the Excel file would return a different mime type if the Excel file was open in Excel when it was uploaded. Under these conditions, the mime type is application/octet-stream. In other words, when uploading an open Excel doc, Internet Explorer would have trouble identifying the file type and send along the generic mime-type application/octet-stream.

Bellow is a quick fix that should take care of future problems:

if (   $_FILES['excel_file']['type'] == 'application/vnd.ms-excel'
    || preg_match('/\\.xls$/', $_FILES['excel_file']['name']) ) {
    // continue working on the uploaded file...
}

PHP isset()

Sunday, September 20th, 2009

The PHP function isset() is often used to determine the presence of a variable. When the variable is not defined, false is returned. Using isset() is also an easy way to determine whether an array has a given key.

$assoc = array('key' => 'value');
echo isset($assoc['key']) ? 'true' : 'false';
// true
echo isset($assoc['missing']) ? 'true' : 'false';
// false

There is a catch: isset() called on a null variable will return false.

$var = null;
echo isset($var) ? 'true' : 'false';
// false
 
$array = array('key' => null);
echo isset($array['key']) ? 'true' : 'false';
// false

For checking the presence of a key in an array, use the PHP function array_key_exists() instead.

echo array_key_exists('key', $array) ? 'true' : 'false';
// true
 
function has_empty($key, &$array) {
    return array_key_exists($key, $array) && !$array[$key];
}
echo has_empty('key', $array) ? 'true' : 'false';
// true

To determine the presence of a null variable, a custom function is required.

function isvar($var) {
    return isset($var) || @is_null($var);
}
$null = null;
echo isvar($null) ? 'true' : 'false';
// true

On Website Depreciation

Thursday, February 26th, 2009

Is a site ever finished? In a limited way, yes. As the months pass, however, the value of a site degrades. Worse still, with enough time, a site may drive your audience away with it’s dated look, old-style functionality or lack of modern features. How do we handle this depreciation? The key is to become ‘light-weight’: not bogged down with tools and structure, but remain open and flexible to change.

(more…)

On Programming Alone

Friday, February 20th, 2009

Early Cowboy StarOften we developers find ourselves working on projects as the only programmers. It may be a personal project or a company with enough work for only one. Such work often present challenges in way of responsibility and advantages like control and intimate knowledge of the system. In response to the challenges of programming alone, I have come up with the 5 rules that keep me and my work sane:

(more…)