03
Oct 10

Django HttpResponseRedirect returns 301 instead of 302

If you have some code that redirects like this…

def index(request):
    return HttpResponseRedirect('/widget/create')

And you want to test it with assertRedirects() like this…

def test_widget_redirect(self)
    response = self.client.get('/')
    self.assertRedirects(response, '/widget/create')

And you can’t figure out why your test fails like this…

AssertionError: Couldn't retrieve redirection page '/widget/create': response 
    code was 301 (expected 200)

Then you should read this.

In short, add a trailing slash to your url.

'/widget/create/'
21
Sep 10

Use Notational Velocity, MacVim, WriteRoom, and Vimpress for the ultimate WordPress Blogging Workflow

My most recent post about Switching To Markdown got me started on a quest to find better tooling for writing, editing, and posting to fzysqr.com. As a .NET developer by trade, I have grown accustomed to one of the best development environments ever created (Visual Studio). After switching my home computing environment to OSX a few years ago, I haven’t been able to settle into a comfortable set of tooling for coding and blogging on my Mac. This causes me to go off on wild tangents every time I sit down at my Macbook Pro to try to be productive:

Me: I'll work on a new post for my blog.

(starts tapping away at the keyboard for a few minutes.)

Me: Ugh, it is such a pain to post and format code on WordPress. I'll just
    see if there is a better way.

(4 hours later)

Me: I've got it! Using these 10 new plugins and some custom python scripts 
    I found online, I now have the ultimate setup!

(Next day)

Me: Ugh, these custom scripts suck. There has to be a better way...

(repeat forever)

I am somewhere around iteration 50 in this hellish loop and I think I may have found a workflow worth keeping.

Continue reading →

10
Sep 10

Switching to Markdown

I am thirteen posts into my blogging career and I just discovered Markdown! As someone who writes html all day long for work, I find it strangely relieving to put away the angle brackets once and awhile (without resorting to yet another bad WYSIWYG editor). So there are going to be a few changes:

  • I am now writing my posts in Markdown
  • Using TextMate and the blogging bundle
  • I had to ditch the code syntax highlighting. (I haven’t found one that works with Markdown and WordPress)

The first two items are awesome. The third not so much. I would love to hear from anyone that has figured out a way to have their cake and eat it too.

So, if you see any garbled code on my old posts, please let me know and I will clean them up.

Thanks for reading!

10
Sep 10

ASP.NET MVC2 Plugin Architecture Tutorial Part 3

This is part 3 of a multi-part tutorial:

So here it is. Part 3. When I said coming soon, I meant it. Honest! But I had a busy summer. Really busy. I made a list:

Continue reading →

31
Aug 10

Good read on the life and times of a Ninject object

I was working on refactoring our Ninject 2.0 IoC implementation yesterday when I came across this great post from the author of himself: Ninject 2.0 Object Lifcycle. The bug I was trying to squash turned out to be completely related to the scope of one of my Ninjected objects.

12
Aug 10

Unit testing updatemodel and tryupdatemodel in ASP.NET MVC2

I was trying to write unit tests in ASP.NET MVC2 against action methods that call UpdateModel() or TryUpdateModel() and I kept getting the exception below from the MVC framework in the controller class:

System.ArgumentNullException: Value cannot be null.
Parameter name: collection

Here is the offending MVC source:

protected internal bool TryUpdateModel<TModel>(TModel model, string prefix) where TModel : class {
    return TryUpdateModel(model, prefix, null, null, ValueProvider);
}

ValueProvider was returning null despite my best efforts to mock the HttpRequest object in the controller’s context (as seen in a bazillion examples online). As it turns out, in MVC2, you now have to create a ValueProviderCollection and assign it directly to your controller in order to mock the form post. The following code does the trick:

private static ValueProviderCollection SetupValueProvider(Dictionary<string, string> formValues)
    {
        List<IValueProvider> valueProviders = new List<IValueProvider>();

        FormCollection form = new FormCollection();
        if (formValues != null)
        {
            foreach (string key in formValues.Keys)
            {
                form.Add(key, formValues[key]);
            }
        }

        valueProviders.Add(form);
        return new ValueProviderCollection(valueProviders);
}

Call it during your test setup like this:

controller.ValueProvider = SetupValueProvider(formValues);
20
May 10

ASP.NET MVC2 Plugin Architecture Tutorial Part 2

This is part 2 of a multi-part tutorial:

After much anticipation and speculation (read mild interest from Google searches) I am ready to get on with the second part of the ASP.NET MVC2 Plugin Architecture Tutorial. In this post we will take the solution developed in part 1 of this tutorial and extend it so that we can make the plugins less aware of their plugin “status”. One of our primary goals will be to remove the need to use fully qualified views such as:

View("~/Plugins/FzySqrPlugin.dll/FzySqrPlugin.Views.HelloWorld.Index.aspx");

We will do this by ever so slightly extending the MVC framework to bend it to our will. Let’s get started shall we?

Continue reading →

05
May 10

Copying a file with msbuild in Visual Studio 2010

This morning I finally became fed up with manually copying the compiled assembly for one of my new plugins. Each time I make a change to the plugin, I need to move it to the plugin host’s project folder. Not a big deal, but unacceptable according to question #2 of the Joel Test

This is a quick fix using msbuild. First right click on the project in Visual Studio and select “Unload Project”. Once unloaded, right click it again and select “Edit Project.csproj”.

Scroll down to the bottom of the project file. You should see something like:

  <!-- To modify your build process, add your task inside one of the targets below and 
  uncomment it. Other similar extension points exist, see Microsoft.Common.targets.
  <Target Name="BeforeBuild">
  </Target>
  <Target Name="AfterBuild">
  </Target>
  -->

Uncomment the AfterBuild target and modify it to look like the following:

<Target Name="AfterBuild" >
    <Copy SourceFiles="bin\FzySqrApp.dll" DestinationFolder="C:\some\special\path\"></Copy>
</Target>

Or, if you only want to have the copy executed for a specific build configuration (in this case BuildAndCopy) you can add a condition:

<Target Name="AfterBuild" Condition="'$(Configuration)' == 'BuildAndCopy'">
    <Copy SourceFiles="bin\FzySqrApp.dll" DestinationFolder="C:\some\special\path\"></Copy>
 </Target>

But what if we are already using the AfterBuild target for something else? Well, it is quite easy to add a new target. Below, I have added the CopyFile target to my project file and set it to run for my BuildAndCopy configuration.

<Target Name="CopyFile" Condition="'$(Configuration)' == 'BuildAndCopy'">
    <Copy SourceFiles="bin\FzySqrApp.dll" DestinationFolder="C:\some\special\path\"></Copy>
</Target>

By default, msbuild will not execute the new target. We can fix this by adding CopyFile to the DefaultTargets property in the tag at the top of the file:

<Project ToolsVersion="4.0" DefaultTargets="Build;CopyFile" 
    xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

After you have made your changes, right click on the Project name and select “Reload Project” and build away!*

*If you need to debug your msbuild configuration, you might find it helpful to increase MSBuild’s verbosity. Go to Tools->Options, Projects And Solutions->Build and Run to set this.

26
Apr 10

ASP.NET MVC2 Plugin Architecture Tutorial

This is part 1 of a multi-part tutorial:

I recently started a new project with a requirement that the software be able support plugin modules. I assumed that this sort of functionality would be well documented and demonstrated on the internet, but had a surprisingly hard time finding information.   Throw Visual Studio 2010 and ASP.NET MVC2 into the mix and all I could really find were some proof-of-concept examples, the best being J Wynia’s example which ended up forming the basis for my solution. I took Wynia’s example (and some of the subsequent discussion in the comments) and expanded it to meet my requirements:

  • I want to use Visual Studio 2010, .NET 4.0,  ASP.NET MVC2.
  • Plugins are not a PITA to develop and test.
  • Plugins live in separate Visual Studio solutions.
  • Plugins can stand alone as applications (primarily for ease of development)
  • Plugins can be loaded without restarting/redeploying the main application.
  • Plugins can provide information to the hosting application.

In this series of posts, I will walk through creating a basic plugin framework from scratch, starting with describing Wynia’s VirtualPathProvider concept and then building on it to form a solution that can be used in the real world (at least my twisted definition of real world!).

Continue reading →

22
Apr 10

Snippet: Use CURL with NTLM authentication on an Active Directory domain

C:\>curl --ntlm -u domain\user:password http://localhost/somesite/