installing apache flood

Apache flood is a…reasonable load tester for HTTP servers.  You give it an xml files which says “get this page then this one then this one” then spawn some processes to do so.  A little bit non user friendly, as I was never able to get the tests to really…run the way I expected them to.  Turns out that installign apache flood has some…difficulties, but is not too bad.  Here is my post on it in case others run into the same difficulties.  Let me know if you have any feedback on this!

django passwordinput

So you want to use a django.newsforms for passwords? Turns out to be all right…once you figure it out

from django.newforms.widgets import * # passwordinput
class UserEditVitalsForm(forms.Form):
 previousPassword = forms.CharField(widget = PasswordInput)

Then it should display right 🙂
Note if you want a partial field to work with django newforms and a database entry, let me know, I have some code to help.

python sending email with TLS

So…you want to email? and your SMTP server requires TLS? No worries.
Download ssmtplib from http://aleph-null.tv/go.php?doc=20070216-1104-925.xml
then use it thus:

from ssmtplib import * 
def emailSSL(fromy, to, subject, body): 
    # Add the From: and To: headers at the start! 
    msg = "From: %srnTo: %srnSubject: %srn%sn" % (fromy, to, subject, body) 
    conn = SMTP_SSL('mail.wilkboardonline.com') 
    # From here, you can use any method in smtplib 
    conn.login('roger+wilkboardonline.com','yourpass') 
    conn.ehlo() 
    # sender is first 
    conn.sendmail(fromy, to, msg) 
    conn.close()

Much thanks to http://pydoc.gbrandl.de/modules/smtplib.html for help knowing how to do msg.

doing queries in django templates

Well it seems that you can’t do ‘generic’ queires from within a template — the only reason being as I can tell– that functions don’t take parameters when you call them from within a tempalte. Pity.

Therefore the way to query within a template is to make a ‘helper function’ that will do your query for you, if possible.

I think. According to this post
* Attribute access of a related object which hadn’t previously been
accessed (and select_related will work around that for you so you get
all the queries up-front).
* Method accesses which fetch data (these should be documented in your models).
* Template tags which execute SQL (these should be documented in the
template tags which are executing the SQL).

So just build a helper function and hope it helps 🙂

how to display forms only data

So say you want to just display a form — plaster it on the screen, without any boxes?

Add this to django/newforms/forms.py

    def as_data_only(self): # lodor tell world, django :)
        if not self.form.is_bound:
            data = self.form.initial.get(self.name, self.field.initial)
        else:
            data = self.data
        return data

and in the view template:

{% for field in userForm %}
{{ field.label }}:
{{ field.as_data_only }}
{% if field.help_text %}<dd>{{ field.help_text }}</dd>{% endif %}
{% if field.errors %}<dd class=”myerrors”>{{ field.errors }}</dd>{% endif %}
<br>
{% endfor %}

How to save with a newform into the DB with missing parameters

As django auto forms seem to not do anything except ‘many to many’ relationships yet (yeck) and, with foreign keys you probably don’t want the visitor just setting those anyway, you are left to use an autoform but ‘fill in’ bits and pieces of this ‘partial’ autoform for the user before saving it.

One way to do it is this (uses ‘editable=false’) to not display certain (necessary) things for the user


class ResponseToEvaluation(models.Model):
originating_evaluation = models.ForeignKey(Evaluation,editable=False) # lodo umm...foreginkey then just doesn't work with these things?
personal_comments = models.CharField(maxlength=2000)
pub_date = models.DateTimeField(editable=False)

ResponseToEvaluationAutoForm = form_for_model(ResponseToEvaluation)

Then in the view thus:

from django.newforms.models import * # save_instance
def comment_reply_view(request, id):
        if request.method == 'POST':
            form = ResponseToEvaluationAutoForm(request.POST)
            rNew = ResponseToEvaluation()
            rNew.originating_evaluation_id = id # muhaha
            rNew.pub_date = datetime.now() # we want that one, too
            if form.is_valid():
                save_instance(form, rNew) # stolen from modeltests/model_forms/models.py yeck
                return returnUserHomePage(request, extraHash = {'display_message': 'Response written'})
            else:
                return returnUserHomePage(request, extraHash = {'error_message': 'Trouble writing response'})
        else:
            form = ResponseToEvaluationAutoForm()
        originalEvaluation = Evaluation.objects.get(pk=id)
        repliesToIt = originalEvaluation.responsetoevaluation_set.order_by('pub_date')
        return render_to_response('user/comment_reply_to.html', {'form': form, 'originalEvaluation': originalEvaluation, 'replies': repliesToIt})

django fixtures

say you have a directory “main” (an app), with models.py containing “class Evaluation”
you can create a fixture thus:

[
    {
        "pk": "1",
        "model": "main.Evaluation",
        "fields": {
            "user_who_made_it": "1",
            "user_they_evaluated": "2",
            "personal_comments": "first evaluation from 1 to 2",
            "is_positive": "0",
            "pub_date": "2006-06-16 12:00:00"
        }
    }

]

And put it in the main directory and voila. Works.

django duplicate field weirdness

So in django if you duplicate field names in a model declaration “weird” things happen, like
ValueError at /admin/

invalid literal for int() with base 10: ‘False’

Request Method: POST
Request URL: http://localhost:8000/admin/
Exception Type: ValueError
Exception Value: invalid literal for int() with base 10: ‘False’
Exception Location: c:\Python25\lib\site-packages\django\db\backends\util.py in typecast_date, line 46

Go figure 🙂

how to download files in Ruby

# the ruby way to download files 🙂 — useful if you haven’t got wget on the system…
require ‘open-uri’
writeOut = open(whereItGoes, “wb”)
writeOut.write(open(urlToGet).read)
writeOut.close
puts “downloaded” + urlToGet + “\n”

Of course, this reads in the whole file before writing it (I bet), so be careful.

Roger's meanderings, notes to himself, bug reports, and other things