WebIT.ca

Montreal Web Development

Archive for the ‘General Programming’ Category

MySQL’s “Warning: Field ‘id’ doesn’t have a default value”

Tuesday, January 31st, 2012

The following error came up on a project built on Django and MySQL:
“Warning: Field ‘id’ doesn’t have a default value”

At first, it seemed as though Django was responsible. However, a quick Google search proved this was an issue with MySQL.

Some solutions returned by the search required the programmer/administrator to export and recreate the database, but the solution proved a lot more simple. For a table named auth_user, the following MySQL command fixes the above error message:

ALTER TABLE auth_user MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

The above command resets the ID field of the auth_user table to an auto-incremented integer of length 11.

Variable height UIView

Sunday, September 4th, 2011

In the latest iPhone app, it was necessary for a particular view to grow or shrink with respect to its visible subviews. Bellow is a quick snippet from a class called VariableHeightView, a subclass of UIView.

- (CGRect)frame
{
    CGRect frame = [super frame];
 
    float height = 0.0f;
    for (UIView *asubview in self.subviews) {
        if (![asubview isHidden]) {
            float subviewBottom = asubview.frame.size.height
                                    + asubview.frame.origin.y;
            if (subviewBottom > height) {
                height = subviewBottom;
            }
        }
    }
 
    CGRect newFrame = CGRectMake(frame.origin.x, frame.origin.y,
                                 frame.size.width, height);
    return newFrame;
}

The code is fairly simple. For a view built using the Interface Builder, the VariableHeightView’s (a superview) frame function will iterative over all subviews, looking for the one lowest in the screen. Once found, it will expand to match that sub-view’s Y origin plus height.

The drawRect function of UIView will take care of rendering the VariableHeightView using the dimension of the frame function.

PHP array_remove and array_remove_assoc

Thursday, August 25th, 2011

The array_remove and array_remove_assoc PHP functions allow your code to remove an element from an array (or associative array) given the element’s value. See the comments in the code bellow on how to use the two functions.

Download the code

<?php
/**
 * Removes the given value from the given array.
 *
 * Returns either FALSE if the value was not found in the array
 * or the index at which the value was found and removed.
 * 
 * $array = array('a', 'b', 'c', 'd');
 * assert( array_remove( 'b', $array ) == 1 );
 * assert( array( 'a', 'c', 'd' ) == $array );
 * assert( array_remove( 'z', $array ) === false );
 * assert( array( 'a', 'c', 'd' ) == $array );
 *
 * @param mixed $val The value to remove
 * @param array $array The array from which to remove the value
 * @author Dimitry Zolotaryov, http://webit.ca
 * @returns FALSE or the index at which the value was found
 */
function array_remove( $val, &$array ) {
    foreach ( $array as $i => $v ) {
        if ( $v == $val ) {
            array_splice( $array, $i, 1 );
            return $i;
        }
    }
    return false;
}
 
/**
 * Removes the given value from the given associative array.
 *
 * Returns either FALSE if the value was not found in the array
 * or the key at which the value was found and removed.
 *
 * $array2 = array( 'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4 );
 * assert( array_remove_assoc( 1, $array2 ) == 'a' );
 * assert( array( 'b' => 2, 'c' => 3, 'd' => 4 ) == $array2 );
 * 
 * @param mixed $val The value to remove
 * @param array $array The associative array from which to remove the value
 * @author Dimitry Zolotaryov, http://webit.ca
 * @returns FALSE or the index at which the value was found
 */
function array_remove_assoc( $val, &$array ) {
    foreach ( $array as $key => $value ) {
        if ( $value == $val ) {
            unset( $array[ $key ] );
            return $key;
        }
    }
    return false;
}

Download the code

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 webit.ca, I would want only users with a @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 @webit.ca
*/
add_option('limited_email_domains', array('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...
}