Running Django tests in PyCharm for standalone apps

Over the last couple of years I have created several Django projects designed to be standalone apps. While it’s not to hard to create a custom test running to handle unit tests using:

django.core.management.call_command('test', APP_TO_TEST)

This technique unfortunately does not integrate very well into PyCharm’s built in testing tools (you also cannot debug).

You can however trick PyCharm into thinking it is running a Django project and everything works!

Create a manage.py file in the root of your project inside add the following:

# coding=utf-8
# Helper test runner for running tests for django standalone apps
import os
from optparse import OptionParser
from django.conf import settings

APP_TO_TEST = 'warthog'

parser = OptionParser()
parser.add_option('--DATABASE_ENGINE', dest='DATABASE_ENGINE', default='django.db.backends.sqlite3')
parser.add_option('--DATABASE_NAME', dest='DATABASE_NAME', default='test.db')parser.add_option('--DATABASE_USER', dest='DATABASE_USER', default='')parser.add_option('--DATABASE_PASSWORD', dest='DATABASE_PASSWORD', default='')
parser.add_option('--DATABASE_HOST', dest='DATABASE_HOST', default='')
parser.add_option('--DATABASE_PORT', dest='DATABASE_PORT', default='')
parser.add_option('--SITE_ID', dest='SITE_ID', type='int', default=1)
options, args = parser.parse_args()
settings.configure(**{
 'DATABASES': {
 'default': {
 'ENGINE': options.DATABASE_ENGINE,
 'NAME': options.DATABASE_NAME,
 'USER': options.DATABASE_USER,
 'PASSWORD': options.DATABASE_PASSWORD,
 'HOST': options.DATABASE_HOST,
 'PORT': options.DATABASE_PORT,
 },
 },
 'SITE_ID': options.SITE_ID,
 'ROOT_URLCONF': '',
 'TEMPLATE_LOADERS': (
 'django.template.loaders.filesystem.load_template_source',
 'django.template.loaders.app_directories.load_template_source',
 ),
 'TEMPLATE_DIRS': (
 os.path.join(os.path.dirname(__file__), 'templates'),
 ),
 'INSTALLED_APPS': (
 "django.contrib.auth",
 "django.contrib.contenttypes",
 "django.contrib.sessions", 
 "django.contrib.sites", 
 APP_TO_TEST,
 ),
})

Now enable Django support pointing at manage.py, create a Django Unit test and your good to go.

Posted in Django, Python | Tagged , , | Leave a comment

XCSS Parser

Working with CSS can be painful at times, and while there is the option of switching to SASS or similar I personally find these more even more annoying. Being able to quickly preview your work is useful.

CSS does actually have a few built in constructs to break up work better @import does allow you to split up multiple files although this is not recommended for a production site as it increases the number HTTP requests.

My simple solution is a python app that will take your CSS files and combine them into a single file, at the same time removing any blank lines, this is not as advanced as a CSS minification tool but does a similar job. Combined with gzip compression on the server side for best results.

My biggest dislike with SASS and co is that the syntax is quite different from standard CSS, this means you cannot preview the output in your browser without a compile stage, slowing down the development workflow. My solution (thus far) does not introduce anything that is not supported already by your browser.

The only feature I added (which is entirely optional) is support for variables, again I wanted to ensure that the syntax is as similar as possible to standard CSS (they actually work more like a Make MACRO). A variable is defined using @define NAME VALUE; and can then be referenced with $NAME.

Anyway if your interested check out the project on BitBucket:

https://bitbucket.org/timsavage/xcss-parser/overview

Posted in Development, Python | Tagged , , | Leave a comment

Django Ajax form's updated

With the release of Django 1.2 the newforms module has had a re-factor, with validation pulled out into its own module (django.core.validation). Changes where required to the form introspection code that works out what validation is required client site. The upshot of these changes is that generating a JSON representation of the rules now requires a lot less code and is a lot simpler. =o)

The updated version code is now in the Django1.2 branch until I can work out how I will merge back into trunk while maintaining backward compatibility.

More to come soon…

Posted in Development, Django, jQuery, Python | Leave a comment

Hacking up Gypsy

I recently came across a great little project (part of freedesktop.org) called Gypsy. Gypsy is a dbus enabled replacement for gpsd.

My own efforts are writing a GPS daemon are hosted on Google Code. My daemon was written entirely in python and similar to Gypsy published information via dbus. But Gypsy has a number of advantages:

  • Support for the garmin protocol
  • More complete implementation of NMEA and all DBUS information
  • Designed to be efficient for both client and server
  • Includes all configuration to run as a DBus service

My only concern was a feature that I had written into my daemon and this was the ability to record an NMEA stream and replay the stream at any time. This becomes very useful when testing client software.

To this end I have written two patches for Gypsy to firstly add support for TCP and secondly add support for writing a time delta to the NMEA logs output by Gypsy as well as providing a Python tool for replaying these logs via a TCP socket.

The patches have been submitted to the Gypsy dev’s or can be fetched from the Freedesktop bugzilla.

Update: after feedback from the gypsy maintainers I have updated my patch to simply record a delta to the log file. As of Gypsy 0.8 gypsy can now read from a named pipe the patch also includes a python script that can be used to replay the log.

Posted in Uncategorized | Tagged , , , | 2 Comments

More on IEnumerable and ICollection

Following up from my post yesterday on enhancing IEnumerable with additional features Phil Haack (Program Manager on ASP.NET for MS) posted a comment on the use of the Count Linq extension method and some of the dangers of using it with generic IEnumerables. With this in mind I would suggest updating my code to enhance ICollection which would remove this risk.

See Phil Haack’s post here.

Posted in ASP.NET MVC, Development | Tagged , | Leave a comment

Enhancing the ASP.NET MVC View fornext loop

A common problem with the default ASP.NET MVC view engine, when rendering collections in a for/next construct is the inability to easily determine where you are in the loop. For example a common way of displaying a menu is to use unordered lists and produce the following markup:

<ul class="menu>
    <li class="first"><a href="/home">Home</a></li>
    <li class="selected"><a href="/page-1">Page 1</a></li>
    <li><a href="/page-2">Page 2</a></li>
    <li class="last"><a href="/page-3">Page 3</a></li>
</ul>

With the basic for/next loop over an ICollection producing this output is rather difficult.

My solution is an extension method for ICollection that provides additional information about the loop.

public static class CollectionExtensions
{
    public static IEnumerable<LoopItem<TSource>> Loop<TSource>(this ICollection<TSource> self)
    {
        if (self == null) throw new ArgumentNullException("self");

        var index = 0;
        var count = self.Count();

        foreach (var item in self)
        {
            yield return new LoopItem<TSource>(item, index, count);
            index++;
        }
    }
}

public class LoopItem<TSource>
{
    private int _count;

    internal LoopItem(TSource item, int index, int count)
    {
        this.Value  = item;
        this.Index  = index;
        this._count = count;
    }

    public TSource Value { get; private set; }

    public int Index { get; private set; }

    public bool IsOdd
    {
        get { return !this.IsEven; }
    }

    public bool IsEven
    {
        get { return this.Index % 2 == 0; }
    }

    public bool IsFirst
    {
        get { return this.Index == 0; }
    }

    public bool IsLast
    {
        get { return this.Index == (this._count - 1); }
    }
}

With this code in place we can now do generate the menu from a collection with:

<ul>
    <% foreach(var item in menuList.Loop()) {
        var classes = "";
        if (item.IsFirst) classes += "first ";
        if (item.IsLast) classes += "last ";%>
        <li class="<%: classes %>" >
            <%: Html.ActionLink(item.Value.Action, item.Value.Title) %>
        </li>
    <% } %>
</ul>

This makes displaying lists of data with specific classes a lot easier and simplifies your views. This same piece of code can also be used for rendering tables with odd/even rows.

Enjoy!

Posted in ASP.NET MVC, Development | Tagged ,

Templatrix 1.1

About two years ago I built the first version of Templatrix. To put it simply it’s a tool for generating the base HTML/XHTML template that you start with every time you do a new website cut up, or have a brilliant idea you want to try out. Over that time I have used it a lot, but it was starting to show it’s age, it still referenced jQuery 1.2.x and still didn’t support optional extras (i.e. jQuery UI) that had been planned. So without further ado I present Templatrix 1.1.1.

This version includes the following enhancements:

  • References to all JavaScript and CSS libraries hosted by Google.
  • References to optional extra’s that certain JS and CSS libraries offer.
  • Tweaked UI (no more popup to select code) and enhancements from later releases of jQuery UI.
  • Everything minified and optomised.

It’s been quite a busy weekend. First installing WordPress, crafting a template and a rebuild/new version of an old tool.

Posted in Development, Joocey Labs | Tagged , , | 1 Comment