buntin.org

Flask routing tricks

While working on a project today I wondered how I might complete this particular task.  I needed user profiles. Each profile was going to require an extra form field or two and I wanted to have different urls but the same “view”.  So I am going to show what I did to solve my problem.

Since Flask routing is just a decorator, you can add as many decorators as you want to the view function:

users = Module(__name__, name="users", url_prefix="")

@users.route("/affiliates/new")
@users.route("/users/new")
def new():
    form = RegistrationForm()
    return render_template("users/new.html", form=form)

The route() function just passes keyword args onto werkzeug.routing.Rule which accepts a dictionary called defaults. As you would think, defaults sets default values to variables accepted by the view function. So I decided to use the defaults dictionary to set the value for profile_type. This will never be overwritten since I am not accepting profile_type as a url parameter.

users = Module(__name__, name="users", url_prefix="")

@users.route("/affiliates/new",
             defaults={'profile_type': 'affiliates'})
@users.route("/users/new",
             defaults={'profile_type': 'sales'})
def new(profile_type=None):
    # Handle form with profile_type variable...
    return render_template("users/new.html", form=form)

This might be a little hackish but it works like I thought it would.

Leiningen + Clojure + Google App Engine = Interesting…

So today I started messing around with Google App Engine for Java today.  I thought it would be interesting to see how I might get this done with Leiningen.  You can take a peek at what I did in the github repo.

Basically all you do is:

$ lein deps
$ lein compile
$ dev_appserver.sh war

And that serves up the project via the dev server.

Introducing Humongous

Yesterday I released a simple web interface to mongoDB named Humongous, and I mean simple.  It is written in Clojure and should be really simple to get up and running given you have leiningen installed.  I really don’t have any idea where it will go from here but I want to maintain its simplicity and usefulness.  Props to Wilkes for his a really good mongoDB clojure library.

Moved to Wordpress

I got so sick and tired of dealing with comment spam I have moved my blog to Wordpress.  I plan on migrating all of the content (that was actually looked at) in the next day or two.  Stay tuned…

Remote Pair Programming

I know I haven’t always been the biggest fan of pair programming but I don’t think [in my past life](http://www.cabedge.com) it wasn’t a feasible solution.  At [work](http://www.notifymd.com) the other day we came up with a solution for pair programming while we are working from home.  We knocked it out and man it is pretty sweet.
Here are the things you are going to need
1) Server/Computer (with ssh access) that is accessible by both parties.
2) The computer must have screen.
You will need to run:
:::bash
$ sudo chmod u+s /usr/bin/screen
so that screen will allow for multiple users accessing the same executable.
Here is the [gist](http://gist.github.com/71662) of .screenrc located in your home directory.
Once you have all of this setup you are ready to go.
1) User 1 should log into the computer and if a screen session isn’t already started run:
:::bash
$ screen
If a screen session is already started then run:
:::bash
$ screen -r
2) User 1 should allow User 2 to access their screen session.  This is done by pressing Ctrl-A : then:
:::bash
acladd *user2-ssh-username*
3) User 2 should ssh into the computer and run:
:::bash
screen -x *user1-username*/
That should be it, both users should be able to see each others work.  This functionality only works well with command line editors but Vim and Emacs are great and powerful tools that should be a part of every programmers arsenal.
**Note:**  This assumes that all network settings are correct for each individual to access the box accordingly.

I know I haven’t always been the biggest fan of pair programming but I don’t think [in my past life](http://www.cabedge.com) it wasn’t a feasible solution. At work the other day we came up with a solution for pair programming while we are working from home.  We knocked it out and man it is pretty sweet.

Here are the things you are going to need

  • Server/Computer (with ssh access) that is accessible by both parties.
  • The computer must have screen.

You will need to run:

$ sudo chmod u+s /usr/bin/screen

so that screen will allow for multiple users accessing the same executable.

Here is the gist of .screenrc located in your home directory.

Once you have all of this setup you are ready to go.

  • User 1 should log into the computer and if a screen session isn’t already started run:
$ screen

If a screen session is already started then run:

$ screen -r
  • User 1 should allow User 2 to access their screen session.  This is done by pressing Ctrl-A : then:
acladd *user2-ssh-username*
  • User 2 should ssh into the computer and run:
screen -x *user1-username*/

That should be it, both users should be able to see each others work.  This functionality only works well with command line editors but Vim and Emacs are great and powerful tools that should be a part of every programmers arsenal.

**Note:**  This assumes that all network settings are correct for each individual to access the box accordingly.

Authorize.net Clojure library

Here is my first stab at building a quick clojure library http://sethtrain.github.com/clojure-authorize/

Clojure

Website: http://www.clojure.org

A buddy of mine told me about a new Lisp dialect called Clojure. I have never used Lisp before (other than setting up my Emacs environment) but this looks cool, if you can get over looking at all the parentheses. The language is built on top of the JVM so you have access to any Java packages from Clojure.

I think this is really huge especially from the standpoint of “enterprise” development. I develop every day in Django and Django is awesome. One problem that I have experienced while working with a web development framework that isn’t built in .NET or Java is that you basically give up working with “enterprise” companies because they tend to not trust anything other than those two languages, which is a problem in and of itself. Since clojure runs on JVM anything written in clojure will run on any machine running the JVM, kinda side stepping the idea that developing for “enterprise” has to be .NET or Java.

So I am going to look at Clojure and see what it has to offer. I like learning new things especially when they make me more of a geek.

jQuery Polling plugin

I wanted to create a chatting application and decided to create a jQuery polling plugin specifically for that purpose. I don’t know for sure if this is useful to anyone else but it works for me.

(function($) {
    $.fn.poll = function(options){
       var $this = $(this);
       // extend our default options with those provided
       var opts = $.extend({}, $.fn.poll.defaults, options);
       setInterval(update, opts.interval);

       // method used to update element html
       function update(){
           $.ajax({
               type: opts.type,
               url: opts.url,
               success: opts.success
           });
       };
    };

    // default options
    $.fn.poll.defaults = {
       type: "POST",
       url: ".",
       success: '',
       interval: 2000
    };
})(jQuery);

*Usage*

$("#chat").poll({
    url: "/chat/ajax/1/messages/",
    interval: 3000,
    type: "GET",
    success: function(data){
        $("#chat").append(data);
    }
});