Django 1.7, PyCharm, and Apache

I’ve had some ongoing problems with PyCharm support for Django since the 1.7 release. Here is a summary.

WSGI Broke

The entire app failed to run under Apache. This caused some moments of terror. The WSGI file needed to be edited.

This code started on version 1.3. The old WSGI configuration worked until 1.7. This discussion thread described the behaviour, which bites projects started pre-1.4. See also Issue #23437 describes the WSGI problem, and the official release notes under app-loading changes.

The correct call to initialize WSGI is:

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

 

PyCharm Testing Broke

Tests in PyCharm broke. This a case of being bitten by older code again. Django uses the standard Python unittest module now. I had to go through dozens of files and tweak the import statements.

Update: LH asked for more information.

In my unit testing, I used the old-style Django test classes and had import statements like:

from django.test import TestCase

which must be changed to:

from unittest import TestCase

in order to use the built-in Python unit testing core.

PyCharm Django Console Broke

This is kind of annoying. The problem isn’t so much PyCharm itself but Django “fixed” a long-standing “bug”. Now what used to work fine doesn’t. Per the app-loading changes in the 1.7 release notes, one must, every time they start the Django console from within PyCharm, execute the following commands:

import django
django.setup()

This became annoying enough I hacked the Django helper

/Applications/PyCharm.app/helpers/pycharm/django_manage_shell.py

and added the two lines to that file. Problem resolved.

 

Posted in Programming | Tagged , , , , , , , | 2 Comments

Hair Donated

I’ve been blessed with a full head of thick hair and decided to share that blessing with others. Today I had my pony tail cut off, harvesting approximately 13 inches for donating to charity. It will be used in making wigs for children with hair loss due to cancer or other conditions.

 

My hair, before harvesting it for charity.

My hair, before harvesting it for charity.

Posted in Extra Mile | Tagged , , , , , | Leave a comment

Control VMware Fusion from the Command Line

Background

Virtualization has been a key technology for me as a contractor. Because I have multiple clients, keeping their projects sequestered from each other is a snap: one or more VMs (virtual machines) per project. Development, testing, and in some cases deployment is all on separate VMs.

I’ve been using VMware Workstation since it first came out 1999. The ability for me to seamlessly move VMs between Windows, Linux, and OS X has been a key reason I’ve stuck with them for so long.

Automating tasks via scripting one of many reasons I insist that my workstation run  Un*x of some flavour. I’ve been using OS X for quite a while quite happily as a software developer, however VMware Fusion (for OS X) doesn’t present the full set of features available on VMware Workstation (for Linux and Windows). Fortunately hidden under the hood are the same tools.

The Executable vmrun

VMware provides a tool called vmrun that allows common operations to be performed on VMs — starting, suspending, taking snapshots, etc. On OS X it’s tucked away in the VMware Fusion bundle under Contents/Library.

In my shell startup I add the directory to PATH:

# VMware Fusion
if [ -d "/Applications/VMware Fusion.app/Contents/Library" ]; then
    export PATH=$PATH:"/Applications/VMware Fusion.app/Contents/Library"
fi

Find the Virtual Machine’s .vmx File

To use vmrun you need to have the path to the .vmx file that resides inside of the VM bundle on OS X. For example, I have a VM with an install of RedHat Enterprise Linux 7:

RHEL7 VM

This is actually not a single file, but a directory called RHEL7.vmwarevm. The contents can be seen in the Finder by right clicking:

Show Contents

This will open up the directory and show the various files that make up the virtual machine.

Inside the Bundle

As can be seen, the .vmx file is prominently displayed.

Put the Pieces Together

To start the VM from the command line one uses the “start” parameter. For example, if the above virtual machine were in one’s home directory one can type:

$ vmrun start ~/RHEL7.vmwarevm/RHEL7.vmx

vmrun Commands

If one runs vmrun without parameters it gives a farily long summary of commands that it accepts. One can:

  1. control the power state of the VM,
  2. control snapshots,
  3. perform various operations inside a running VM, and
  4. other operations such as installing tools and cloning.

A short list of common operations:

Description Command Parameters
List running VMs list
Start a VM start /path/to/vmx/file
Suspend a VM suspend /path/to/vmx/file
Take a snapshot snapshot /path/to/vmx/file snapshot name

 Additional Information

VMware has a PDF on vmrun here.

 

Posted in SysAdmin | Tagged , , , , | Leave a comment

Controlling RHEL 7 Services

One change from RHEL/CentOS 6 to the RHEL 7 beta is how services are controlled. The old service and chkconfig commands are replaced with systemctl. These are my quick and dirty notes compiled from the Fedora Project systemd and SysVinit to Systemd Cheatsheet pages.

Basic Control

The old system command’s replacement is very similar, with services having .service appended:

systemctl start|stop|restart|status name.service

For example:

systemctl restart httpd.service

Service Boot time Control

To get a list of available services and their boot time status:

systemctl list-unit-files --type=service

To set a service to start (or not) at boot time:

systemctl enable|disable <em>service</em>.service

For example:

systemctl enable mariadb.service
systemctl enable httpd.service

Run Levels

Run levels are called targets, have been simplified, and have names now. An incomplete list:

  1. poweroff.target (run level 0)
  2. rescue.target (single-user mode; run level 1)
  3. multi-user.target (normal run level 3)
  4. graphical.target (normal run level 5)

To set the default run level:

systemctl set-default multi-user.target

To change the run level:

systemctl isolate name.target

For example, to enter single user mode:

systemctl isolate rescue.target

And the appropriate services will be stopped and started.

Additional Reading

  • A description of how systemd fits into the boot process here.
  • Another nice summary here.

Updates

2014-07-17
Updated setting the default run level per CertDepot’s suggestion. Added the “Additional Reading” section.
Posted in SysAdmin | Tagged , , , , , , , | 2 Comments

Display a Server-Supplied Drop Down List Using AngularJS

These are my notes on displaying a list of server-supplied objects in a drop down list using AngularJS.

Background

I have a server that supplies lists of lookup objects that are used in an AngularJS-based single-page application (SPA). The SPA obtains a list through an API call to the server. The server returns an ordered list of JSON objects. Every object in every list includes a key value, a display value, and supplementary data. For the purposes of this article, only the key and display values are of any concern.

For example, the SPA needs a list of units of measure. The server supplies a list of objects along these lines. They key value is called code and the display value is called display:

[
    {
        code: "L",
        display: "L",
        description: "litres"
    },
    {
        code: "ML",
        display: "mL",
        description: "millilitres"
    },
    ... etc ...
]

In the SPA code, each lookup table is wrapped in its own Angular service.

From List of Objects to Dropdown Using <select>

Angular can be told to create a dropdown list using an array of objects thus:

<select ng-model="product.uom"
        ng-options="u.display for u in units" />

Here ng-options tells Angular to build the dropdown list showing the display attribute of each object. Whenever the user chooses an item, the entire associated object is stored in $scope.product.uom (uom means units of measure). For my purposes this is very handy since I want access to the entire object.

Defaulting to a Value

This works beautifully until an edit page is shown. When displaying data from the server, the dropdown shows a blank selection even though $scope.product.uom contains an object with all the correct values!

The problem is that Angular matches based on object references, not object contents. This can be illustrated thus:

var a = {foo: "bar"};
var b = {foo: "bar"};
var c = a;

Variables a and b contain two separate objects that by chance have attributes with the same values. Variables a and c contain the same object pointer.

In the example above, Angular will recognize the value in $scope.product.uom only if it points to an object in the master list $scope.units. The fact that the server-supplied object has identical attributes is irrelevant — Angular only cares whether the object pointers are identical.

[Edit]

There are a couple of ways around this. If there is a unique key on the object, one can use the AngularJS track by clause to tell AngularJS to compare the keys. Continuing the above examples, each object has a code attribute that has a unique string which JavaScript can compare.

<select ng-model="product.uom"
        ng-options="u.display for u in units track by u.code" />

[Original Article]

Otherwise,  when an object is loaded from the server for editing, the lookup values are replaced with pointers to the corresponding objects in the dropdown list. An unsophisticated but functional bit of code to perform this substitution might be:

// Wrapper function to retrieve a product
// from the server, keyed on productId.
apiProduct.lookup(productId, function(product) {
    $scope.product = product;

    // Replace the server-supplied lookup value
    // with the matching value
    // in the $scope.units array.
    $scope.product.uom = lookup_by_code(product.uom.code, $scope.units);
});

function lookup_by_code(code, data) {
    for(var i=0; i
        if(data[i].code == code)
            return data[i];

    return null;
};

Update

There is a JSFiddle that demonstrates the value/reference problem concisely.

Posted in Programming | Tagged , , , , , , | 5 Comments

Upgrading Node.js using npm

The Node.js ecosystem provides a tool to update Node from within npm called, simply, “n”.

Install n thus:

sudo npm cache clean -f
sudo npm install -g n

I don’t know that clearing the cache is actually necessary, but a number of people have recommended doing so.

Update to the latest version of node using:

sudo n stable

n allows node versions to be changed easily. The n package listing has details.

Posted in Programming | Tagged , , , | Leave a comment

Strange npm Errors

I’ve gotten some strange errors with npm which were resolved by clearing npm’s cache. The brute force method is:

sudo npm cache clean -f

This falls under the same category as strange C/C++ behaviour resolved by removing all .o files, strange Python behaviour resolved by removing all .pyc files. Caching or otherwise keeping around intermediaries is a boon for speed, but can bite when the cache gets stuffed up.

Posted in Programming | Tagged , | Leave a comment