Oct 29 2008

Webfaction Limited Review

Tag: YadaDustin @ 5:48 am

I’ve seen a lot of recommendations for Webfaction for new Django developers who want an easy to use hosting solution. As you can see from previous posts that I have a VPS that I’ve set up Django on. I was having problems getting one of my applications to work, and I wondered if it was because of my setup. So I decided to try out Webfaction. After all, they offered a 60 day money back guarantee. So I signed up and set up my account. I must admit it was much easier to set up than my VPS, plus I could choose to use Python 2.5 (which was never really able to get working on my server setup) and you can use mod_python or WSGI. I was quite impressed with their setup options and the ease of setup. It turns out there there was a problem in my code because I ended up getting the same error on their servers as I did mine. I was just confused because it was working on my windows develpement machine, by not my linux servers. 

So having lost the need for Webfaction, I decided to exercise their money back guarantee. I think this was the best part of the process. I sent them an email. The responded quickly telling me how to cancle the account from the control panel. I think within a half hour I had the money back in my PayPal account and an email confirmation letting me know and saying thanks for trying their service.

Surely, If I ever need another Django host or if it comes to the point where I need to use the latest version of Python, I will look to Webfaction. If I have a client looking for hosting, I will recommend Webfaction.


Oct 03 2008

Django Tip: get_FIELD_display()

Tag: YadaDustin @ 3:45 pm

I’m posting this because it seems rather simple, but it took me a while to find - even with some tips from some helpful people in the #django IRC channel.

Let’s say you have a ChoiceField set up like the documents describe:

 

class Foo(models.Model):
    GENDER_CHOICES = (
        ('M', 'Male'),
        ('F', 'Female'),
    )
    gender = models.CharField(max_length=1, choices=GENDER_CHOICES)

Now, you want to display the gender field in a template. If you use the {{ person.gender }} variable, you would get “M” or “F” to display on your page. But what if you want “Male” or “Female”? Then you would use {{ person.get_gender_display }}.

Nifty.


Oct 03 2008

Dynamic Filtered Drop-Down Choice Fields With Django

Tag: YadaDustin @ 10:05 am

I’m enjoying the Django framework. Sometimes, what was seems rather easy (relatively speaking) gets sort of complicated when working within the boundaries of a framework. Such was the case with my latest endeavor to filter on drop-down based on the selection of another drop-down.

Here is the situation: I have a database with car makes and models. When a user selects a make, I want to update the models drop-down with only the models associated with that make. Sounds pretty common, right? The database has a lot of entries, so I don’t want to do it with pure javascript because that could make for big script, and maybe I don’t want to give away all my data so easily so some hacker can just parse a simple javascript file to get all the make/model data out of my database (not that I care, but some people might). Therefore I want to use Ajax to populate the data.

First, the proof of concept. I created this simple html file to test how it might work:

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
<html>
    <head>
        <meta http-equiv=”Content-type” content=”text/html; charset=utf-8″ />
        <title>Update Drop-Down Without Refresh</title>
        <script type=”text/javascript” charset=”utf-8″>
            function FilterModels() {
                var makeslist = document.getElementById(’makes’);
                var modelslist = document.getElementById(’models’);
                var make_id = makeslist.options[makeslist.selectedIndex].value;
                var modelstxt = new Array();
                modelstxt[1] = “1\tEscort\n2\tTaurus”;
                modelstxt[2] = “1\tAltima\n2\tMaxima”;
                var models = modelstxt[make_id].split(”\n”);
                for (var count = modelslist.options.length-1; count >-1; count–){
                    modelslist.options[count] = null;
                }
                for (i=0; i<models.length; i++){
                    var modelvals = models[i].split(”\t”);
                    var option = new Option(modelvals[1], modelvals[2], false, false);
                    modelslist.options[modelslist.length] = option;
                }
            }
        </script>
    </head>
    <body>
        <p>This is a proof of concept to update a select (drop-down) list of values, based on the selection of another select (drop-down) element using ajax.</p>
        <form action=”" method=”get” accept-charset=”utf-8″>
            <select name=”makes” onchange=”FilterModels()” id=”makes”>
                <option>–</option>
                <option value=”1″>Ford</option>
                <option value=”2″>Nissan</option>
            </select>
            <select name=”models” id=”models”>
                <option>Choose Make</option>
            </select>
        </form>
    </body>
</html>

 

 

That basically helped me organize the code I needed to change the drop-down, now I just need to plug in the Ajax and server side code to make it happen.

Notice, in the proof of concept, I used a tab delimited format for my models. I really didn’t want to mess with parsing xml or json or whatever. So I wrote a model feed based on my automobile make id.

URL:

(r'^feeds/models-by-make-id/(\d+)/$', 'autos.views.feed_models'),

View:

def feed_models(request, make_id):
    make = AutoMake.objects.get(pk=make_id)
    models = AutoModel.objects.filter(make=make)
    return render_to_response('feeds/models.txt', {'models':models}, mimetype="text/plain")

Template:

{% for model in models %}{{ model.id }}Â Â Â  {{ model.model }}
{% endfor %}

That gave me the data feed that I needed to request via Ajax. Now for my form. Because the model field loads dynamically, I had to override the clean function in the ChoiceField class and use it instead so I didn’t get invalid choice errors:

 

class DynamicChoiceField(forms.ChoiceField):Â 
    def clean(self, value): 
        return value

 

 

class MyForm(forms.Form):Â 
    make = ModelChoiceField(AutoMake.objects, widget=forms.Select(attrs={'onchange':'FilterModels();'})) 
    model = DynamicChoiceField(widget=forms.Select(attrs={'disabled':'true'}), choices=(('-1','Select Make'),))

Notice that I am making a call to FilterModels() on my ModelChoiceField’s onchange event. So, here is that code as well:

function FilterModels(sel_val) {
        var modelList = $('id_model');
        for (var count = modelList.options.length-1; count >-1; count--){
                modelList.options[count] = null;
        }
        modelList.options[0] = new Option('Loading...', '-1', false, false);
        modelList.disabled = true;

        var makeList = $('id_make');
        var make_id = makeList.options[makeList.selectedIndex].value;
        if (make_id > 0) {
                new Ajax.Request('/feeds/models-by-make-id/' + make_id + '/', {
                        method: 'get',
                        onSuccess: function(transport){
                                var response = transport.responseText || 'no response text';
                                var kvpairs = response.split("\n");
                                for (i=0; i<kvpairs.length - 1; i++) {
                                        m = kvpairs[i].split("\t");
                                        var option = new Option(m[1], m[0], false, false);
                                        modelList.options[i] = option;
                                }
                                modelList.disabled = false;
                                if (sel_val > 0) {
                                        modelList.value = sel_val;
                                }
                        },
                        onFailure: function(){
                                alert('An error occured trying to filter the model list.');
                                modelList.options[0] = new Option('Other', '0', false, false);
                                modelList.disabled = false;
                        }
                });
        }
        else {
                modelList.options[0] = new Option('Select Make', '-1', false, false);
                modelList.disabled = true;
        }
}

I also make a call to this function in my template after the form loads. This is incase the form is refreshed, or it fails validation and a make is still selected. It will populate the model list and select the value that was selected previously:

<script type="text/javascript" charset="utf-8">
        FilterModels({{ model_id }});
</script>

Note that in order to get the model_id, I had to set it in my view:

def add_classified(request):
        if request.method == 'POST':
                form = AdForm(request.POST)
        if request.POST.has_key('model'):
                model_id = request.POST['model']
        else:
                model_id = 0
        return render_to_response('add_classified.html', {'form':form,'model_id':model_id}, context_instance=RequestContext(request))

I think that’s about it… Did I miss anything?


Sep 22 2008

I Hate RC Willey

Tag: YadaDustin @ 9:49 am

image So it was labor day. For some reason - likely do to excessive advertising, I think of RC Willey on Labor day. My wife has been wanting a bedroom set. We were planning on ordering from Canopy, but I thought it would be fun to go look at RC Willey and see if they had any sales going on. Big mistake!

So we found a set we liked and decided to order. We were told we could pick it up at the warehouse, way out west, so we drove down to Alpine and borrowed my father-in-law’s truck, since my truck was not registered at the moment (a long crazy story).

Once we get there we were told that the bed frame was on back order and won’t be available until next month. Frustrated and angry, I told them to cancel the order. One the way back I called the salesman (Michael F. Langeland) and asked if he knew that information and didn’t bother telling us just so he could get his commission. He said he wasn’t aware of that. He pulled it up in the computer and said, yeah, the bed is on back-order.

We looked around at other furniture sets and we decided to order the Canopy one we wanted originally, but found that one piece we wanted was also not in stock. So as long as we were going to wait, we figured we would get the RC Willey furniture because it was a little cheaper and we wouldn’t have to put it together. So we called back and said, OK we’ll get this set as long as you deliver it free. We were still mad about wasting all the gas to go pick it up. So that deal was made. In the process of that deal they double billed us.

A few days later my wife is wondering when they are going to arrive with the furniture that was in stock. So she called and learned that no delivery was ever scheduled and now all of the furniture set was on back order. We were really annoyed with our salesman now, so we went in to another store to talk with another salesman (we didn’t want Michael to get any commission at this point) and look around at other furniture sets. We found something we like, but a bit more expensive. So we decided to just cancel our order and save a bit longer. We stood in line and eventually had them cancel our order and fix the double billing problem and return all the money to our account.

So now its two weeks later. We’ve had one charge returned but still have $1100+ that needs to be returned to our account. It shouldn’t take two weeks to get a return right? So I called again to find out what is going on. It turns out they never canceled our order. The question was “so you don’t want your bed?” Uh, no! We were in the store and we canceled that. The response was. “No, you didn’t. Do you want to cancel it now?” Oh, I didn’t huh? Whatever. “YES. CANCEL IT!” Now it can’t just be that easy. She said, “you’ll have to call back at ten o’clock to speak with a cashier to get your money back.” Seriously, it was 9:56!

So I called back and I think I got it taken care of. But something that urks me about the whole process is how everyone seems to treat me like I’M stupid.

“What’s your account number?”

“I don’t know”

“What’s your social security number… You account doesn’t come up with that. What do you need?”

I need my money back. Then I swear, I’ll never BOTHER you people again!


Sep 19 2008

Windows Live Writer

Tag: YadaDustin @ 8:53 am

This is a test post from Windows Live Writer.

Someone said it was the "bomb diggity" so I had to check it out!

Not bad indeed!

It certainly looks cool:
image 

And it’s like editing directly in my blog as it uses my existing CSS!

Wow!


Jul 31 2008

Star Trek Meets Monty Python

Tag: YadaDustin @ 7:18 am
This page contained an embedded video. Click here to view it.

Star Trek Meets Monty Python

Wow, someone definitely had a LOT of time on their hands! Thanks for the effort though. It was certainly enjoyable ;)

Technorati Tags: monty python, star trek, mashup


Jun 19 2008

Remember TRBL

Tag: YadaDustin @ 10:34 am

This post (like many I write) is mainly for myself.

When using CSS padding or margin, I generally will write margin-top, margin-bottom, margin-left, etc. I know there is easier syntax where I can specify all for with one line such as

margin: 0 10px 88px 10px;

The problem is, I can never remember which one is where. Know I’m not going to forget. It’s simply top, right, bottom, left… TRBL, TRouBLe, trouble. Who can forget trouble?

Sometime I want the top and bottom the same and the left and right the same. It is also a one liner:

margin: 10px 20px; /* top-bottom, right-left */

No, I’m not going to remember TBRL, that would be stupid. I’ll just remember to start at Top. See, no TRouBLe at all!


Nov 12 2007

PHP to Excel

Tag: YadaDustin @ 5:12 pm

I was tasked with exporting report to Microsoft Excel files. There are a number of ways to do this. One of the easiest I have found it to create a tab delimited file and give it an .xls extension. You can also create a table using html and give it an .xls extension for similar results. The main issue with doing this is that it is not really an excel file so you may get an error like this: excel-error.gif

So, not wanted to re-invent the wheel, I went searching for a good php class I could use. I ended up selected this one. What I liked most about it was that I could just pass in a multi-dimensional array (which my data was already in) and it would create a Excel file from that.

Well, soon I got complaints that number fields were being stored as strings so it was back to the drawing board. I then came across this port of a popular Perl package. It had a lot more features I could use and it wrote numbers as numbers. I wrote a simple function that converted an array to an Excel file. I’ll post it here for your enjoyment.

$test = array(
array (
'Text' => 'abc',
'Number' => 123,
'Date' => '28345.345'
),
array (
'Text' => 'def',
'Number' => 456,
'Date' => '8/8/1977 7:00 AM'
),
array (
'Text' => 'ghi',
'Number' => 789,
'Date' => '1/1/2007'
)
);
echo '

'; print_r($test);
Array2Xls($test, 'test.xls', 'Report');

function Array2Xls($array, $filename, $worksheetname = 'Sheet1') {
        require_once "php_writeexcel-0.3.0/class.writeexcel_workbook.inc.php";
        require_once "php_writeexcel-0.3.0/class.writeexcel_worksheet.inc.php";

        $workbook = &new writeexcel_workbook($filename);
        $worksheet =& $workbook->addworksheet($worksheetname);
        $heading =& $workbook->addformat(array('align' => 'center', 'bold' => 1, 'bg_color' => 'black', 'color' => 'white'));
        $date_format =& $workbook->addformat(array(num_format => 'mm/dd/yyyy hh:mm:ss AM/PM'));

        $worksheet->set_column(0, 3, 15);

        // heading
        $i = 0;
        $h = array();
        foreach(array_keys($array[0]) as $val) {
                $worksheet->write(0, $i++, $val, $heading);
                array_push($h, $val);
        }

        // values
        for ($i=0; $iwrite($i+1, $j, $array[$i][$h[$j]], $date_format);
                        }
                        else {
                                $worksheet->write($i+1, $j, $array[$i][$h[$j]]);
                        }
                }
        }

        $workbook->close();
}

There is still one issue I have yet to resolve and that is concerning dates. I’ve been searching for a PHP script that can convert Unix timestamps or date strings to Excel date numbers. I gave up on it figured I will implement it when or if the request comes in to do so ;)


Jun 26 2007

Blog Publishing In Word?!

Tag: YadaDustin @ 7:21 am

So I just upgraded to Microsoft Office 2007. I saw this feature to publish a document to a blog. You know me, I just had to try…

So let’s see how well it performs:

Lists

One Two Three
Bullet 1 Bullet 2 Bullet 3

Big
Colorful
Text

And an image:


May 30 2007

Syncing Google Calendar with Outlook/PDA

Tag: YadaDustin @ 6:01 am

I always thought it would be nice to sync Outlook with the Google Calendar. I found out today that it is now possible thanks to OGG Sync.

Check it out and give it a try. There is not very much documentation, but be sure to read what documentation they do have on installing it so you can figure out how to use it after installing.

I should also mention that the freeware is limited to syncing the 3 days only - so it is not a time trial, but a free version with limitations.

[nms:pocket pc,5,1]


Next Page »


ss_blog_claim=4973c51f5e7bec57951f995fed1b85f3ss_blog_claim=4973c51f5e7bec57951f995fed1b85f3


You are viewing a mobilized version of this site...
View original page here

How do you rate mobile version of this page?

Mobilized by Mowser Mowser