Debugging NoReverseMatch failures in Django

If you get an error like this:

NoReverseMatch at /account/my_webpage

And it points to and error like:

Reverse for 'password_reset_confirm' with arguments '()' and keyword arguments '{u'uidb64': '7ae9a6e950c08946', u'token': u'4g4-2cbcc2bd7311dc9de06520399f4adbe8'}'

And links to some code like:

This message from Django is really not descriptive. What Django is trying to say is:

I cannot find a matching expression in your urls.py file. You will need to add a matching expression like the below to your urls.py file.

However there still an error:

NoReverseMatch at /account/password_reset
...
1 pattern(s) tried: ['account/password_reset_confirm']

So Django is saying that we have tried a pattern but it does not match. It looks like our pattern does not accept keyword arguments.

Per the Django notes:

https://docs.djangoproject.com/en/1.9/ref/templates/builtins/

Do not mix both positional and keyword syntax in a single call. All arguments required by the URLconf should be present.

We should be good, we do not have mixed arguments and we have just a keyword argument. Let’s set it to handle keyword arguments:

However it still throws error:

NoReverseMatch: Reverse for 'password_reset_confirm' with arguments '()' and keyword arguments '{u'uidb64': '7ae9a6e950c08946', u'token': u'4g4-2cbcc2bd7311dc9de06520399f4adbe8'}' not found. 1 pattern(s) tried: ['account/password_confirm/(?P\\w+)/(?P\\w+)/']

That doesn’t work either, I wonder what is wrong?

Look at the token. Does “\w+” match a dash? NOPE! Let’s match the dash.

Now everything works!

Whenever you have a NoReverseMatch error check the following:

  1. Have existing matching url
  2. Match the keyword or positional arguments with that url
  3. Make sure that the argument regular expressions match what you are trying to pass

Ordering Tests in Android UIAutomator Testing

UIAutomator does not normally tests in a specific sequence. This is actually good behavior because your tests in your testing code SHOULD be sequence invariant.

It should not matter what order your tests should run in. If all of your tests are sequence specific, it makes it very difficult to parallelize test in the future and speaks of brittle code.

However in the real world there are often business or code dependencies which force us to order tests. To order tests add the @FixMethodOrder and specify the sorting order. We usually just use name ascending and then name or tests in alphabetical order.

  • t01_checkPreconditions
  • t02_emailNotUpdated

Code Example
The following example will then run in alphabetical sort order:

Getting Resource Id’s for Android UIAutomator Testing

When you are testing Android applications you can lookup the object to receive user input either via text or via the resource id.

The resource id is more stable since the text may change dramatically between versions and upon the user interface language.

The best tool is the uiautomatorviewer viewer. It is included in the android sdk tools directory. Launch it via:

Then click the second icon from the left which will dump the screen. You can then lookup the resource-id from lower right:

uiautomatorviewer-screenshot

Mutable Deep Copy By Using Categories

Ever run into this error:

mutating method sent to immutable object

The above error means that the program has tried to modify an immutable object. No problem you think. I’ll just make the object mutable. Now the normal way to deal with this is to do:

[obj mutableCopy];

Depending on the calling object, this call returns a mutable copy of the primitive or the objects of an array or hash.

deep-mutable-copy-successful

Great, solution solved! Press the easy button and go to the next problem.

Still reading… No worky?

If you are still reading this then I guess the easy solution did not work for you. If the data structure in question is comprised of deep objects such as trees, JSON objects or other recursive structures you need something more.

Mutable Deep Copy will clone the data structure recursively and allow updating of contained objects no matter how deep the structure.

deep-mutable-copy-fail

In Objective-C, we can attach methods to existing classes via Categories. For deep mutable copy, We attach categories upon the following classes:

  • NSObject
  • NSArray
  • NSDictionary
  • NSSet

This should handle arrays, dictionaries, sets and most primitive objects but one will still need to implement the mutableDeepCopy method on your own custom classes if they are used in the data structure.

Add the following two files into your Xcode project and include them when you need to clone deep recursive structures.

DeepMutableCopy.h

DeepMutableCopy.m

Updating the MySQL ENUM Data Type

Now I am not a big fan of the ENUM data type, it I usually like VARCHAR’s or INT’s better but they do possess a certain value in constraining data to legal values.

However modifying ENUM’s are problematic. Because of several restrictions:

  • You can only append to the end of the enums.
  • You cannot delete a enum.
  • You may not reorder enums.

To modify an enum of ('WHEEL','LEG') try the following:

However these are not valid changes:

Null Pointer Exception in Android UIAutomator Testing

Are you getting a null pointer exception in UIAutomator testing code? Is it occurring during setText() or some other action on the UIObject2?

In our UIAutomater test code here we try to set the text of an EditText here:

Which throws this exception:

If we look into this we see that we are blowing up on:

Ugh, I think we are failing on

currentText.length() > 0

Because the text was null for this field and we are getting the length of a null object.

No much we can do now except to either wait for Google to update the UIAutomator code.

For me the failing version is:
uiautomator-v18:2.1.2

Included build.gradle here:
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.+'

It looks like there is ticket opened already for this issue:

https://code.google.com/p/android/issues/detail?id=201469


Updated Sept 30, 2016

There apparently is a workaround. Instead of looking up an object using BySelector

Use the lookup by UiSelector instead which will return an UIObject instead of UIObject2

Example

Note that there is a click() and a delay() added. If I didn’t add it then it would just keep filling in the first text field when I had multiple EditText fields in the view.

Android Apks, Zipfiles and Small Bits of Nastiness

Today we had some issues with apk handling. One of our platforms which we were trying to upload apks kept rejecting them on us. The error we got was:
The APK must be signed with jarsigner

Now an *.apk file is just a renamed *.zip so just rename the file back to *.zip and then extract the files with normal zip tools. We spent hours trying to figure out why our most recently updated apk is different than the previous one and found nothing conclusive. We looked at all the signing and it all looked correct.

You know things are grim when you start manually diffing all the files but even that did not help us.

Time to bring out the big guns. Lets look at the beginning of the files and see if we see something obvious.

Let us lookup the format for the zipfile and lets decode it to something more human readable.

That seems strange, the previous file looked like this…

Now we are getting somewhere, let’s search for the central directory segment 50 4b 01 02

Notice that the compression method has changed from:
00 08 -> 08 08

Well… it looks like Android Studio 2.2 RC2 changes the compression type of zipfile and our platform processor can’t deal with it.

What a pain, to fix we just rezip the apk with no compression and the upload works fine:

Why was this so painful
We got a red herring! Red herrings are extraordinarily painful in software development and while a developer cannot avoid them, it behooves us to not lay these traps for others.

A common red herring occurs during during exception handling. Whenever I see code like this:

I know that eventually one day some other error will occur and turn this small error into something that takes another developer two days to solve.

Moral
Bad errors hurt way more than unknown errors.

Throw an “Unknown Error” or “Error 500” instead of returning a red herring.

Clearing rePasta Installs

To clear a repasta install, do the following:

1. Remove the app from your machine
https://support.apple.com/en-us/HT202235

2. Remove all rePasta data

(WARNING) This will wipe all rePasta data and should only be done if you intend to delete all rePasta data and are comfortable with the command line.

For version 2.0 of rePasta

For version 1.0 of rePasta

Migrate posts to a different WordPress user

A lot of times you start a WordPress blog and publish with the original admin user.  You later decide to create a new user and you need to move the posts over.

The easiest way that I can figure out how to do it is just to update the tables via SQL.

Note Please check your WordPress table prefix and modify before running these commands. Normally it is wp_xxxxx but your admin might have changed it.

Look at the data before running them

Run the update to migrate the posts to user 2;

Django Gives Bad Request (400) Upon Rolling to Production

If the code run in development but fails with a Bad Request (400) error when you roll it to production.

And there is no indication of what is wrong in the logs.

Try toggling
DEBUG = True
from
DEBUG = False
in settings.py

Restart the webserver
sudo service httpd restart

And retry reloading the page. If it works then is is possibly an
ALLOWED_HOSTS issue.

Bad Fix (don’t do this)! This can cause security issues later as one should limit to the relevant hosts.
ALLOWED_HOSTS = ['*']

Doesn’t work I have seen this answer several places, it is wrong and doesn’t work with version 1.9.4 of Django. Do NOT attempt to mix the asterisk with the host parts
ALLOWED_HOSTS = ['repasta.com', '*.repasta.com', 'localhost']

Works
ALLOWED_HOSTS = ['repasta.com', '.repasta.com', 'localhost']

Web page now shows correctly. Yay!