Monday, August 17th, 2009
Django Templates and Dictionaries
Ever needed to access a python dictionary from a django template? It's pretty straightforward:
{{dictionary.key}} will get you that.
but ever needed to do it inside a loop, with a variable key? django will not allow you to do this default (after finding a solution that worked, i can't BELIEVE that it isn't a part of django already. maybe it is in the new version?)
anyway, i wrote a custom template tag which allows me to access the keys of a dictionary using a variable:
from django import template
register = template.Library()
def dict_get(value, arg):
#custom template tag used like so:
#{{dictionary|dict_get:var}}
#where dictionary is duh a dictionary and var is a variable representing
#one of it's keys
return value[arg]
register.filter('dict_get',dict_get)
now, {{dictionary|dict_get:var}}, where var is one of the dictionary keys, will give me exactly what i want.
update:
there is a big difference that i didn't make obvious in my post.
in view:
dict = {'1':True,'3':False,'5':True}
arr = [1,2,3,4,5]
in template:
{{dict.5}} #evaluates to True
{% for key in arr %}
{{dict.key}} #does not work
{{dict|dict_get:key}} #WORKS!
{% endfor %}
12:35pm by Brandon //comment 2492 views Monday, July 27th, 2009
django blog project
i'm developing a django blog here: http://brandons.homeip.net/
right now it has only it's very core. i need to skin it, and add a few features. probably most of the posting i do will be test posts over there.
thanks.
10:53pm by Brandon //comment 2721 views Friday, July 17th, 2009
Almost Done
I think that I am done modeling my models file for a new project. Can you see where this is going? Is it stupid? Are there other (better) ways to do this? Let me know in the comments please.
-Brandon
#this app started on 7/14/09
from django.db import models
from django.contrib.auth.models import User, Group
from django.contrib.localflavor.us import models as usmodels
from datetime import datetime
class Client(models.Model):
#choices for the time_zone
TIME_ZONES = [
(1,'Eastern'),
(2,'Central'),
(3,'Mountain'),
(4,'Pacific'),
(5,'Alaska'),
(6,'Hawaii'),
]
clientID = models.IntegerField(unique=True) #the main account number
name = models.CharField(max_length=255)
location = models.CharField(max_length=255,blank=True,null=True)
contact_number = usmodels.PhoneNumberField(null=True,blank=True)
contact_email = models.EmailField(max_length=255,null=True,blank=True)
time_zone = models.IntegerField(choices=TIME_ZONES,blank=True,null=True)
active = models.BooleanField()
def unicode(self):
return "%s - %s" % (self.clientID,self.name)
class Meta:
ordering = ['clientID',]
# Create your models here.
class Answer(models.Model):
answer = models.CharField(max_length=255)
active = models.BooleanField()
"""
weight will be a percentage of the related topic's value
(ie a weight of .5 on a topic with a value of 10 = a score of 5/10)
with the exception of these hard-coded weights
-3 will give Fail behavior (0/possible)
-2 will give Pass behavior (scored/possible i.e. ignored)
-1 will give NA behavior (0/0 i.e. ignored)
"""
weight = models.FloatField(help_text="Percentage of Topic Value <br/>\n-3=auto zero, -2=ignored, -1=ignored")
def unicode(self):
return "%s : %s" % (self.answer,self.weight)
class Meta:
ordering = ['-weight',]
class Section(models.Model):
name = models.CharField(max_length=255)
def unicode(self):
return self.name
class Meta:
ordering = ['name',]
class Topic(models.Model):
section = models.ForeignKey(Section)
topic = models.CharField(max_length=255)
description = models.TextField(null=True,blank=True)
value = models.FloatField()
active = models.BooleanField()
possible_answers = models.ManyToManyField(Answer,related_name='possible_answers')
default_answer = models.ForeignKey(Answer,related_name='default_answer')
def unicode(self):
answers, div = '', ''
for answer in self.possible_answers.all():
answers = answers + div + answer.answer
div = '/'
return "%s: %s - %s (%s)" % (self.section.name, self.topic, self.value, answers)
class Meta:
ordering = ['section','-value','topic',]
class Questionnaire(models.Model):
name = models.CharField(max_length=255,unique=True)
active = models.BooleanField()
topics = models.ManyToManyField(Topic)
def unicode(self):
return self.name
class Meta:
ordering = ['name',]
class TopicOrder(models.Model):
questionnaire = models.ForeignKey(Questionnaire)
priority = models.IntegerField(help_text="'1' is first listed")
section = models.ForeignKey(Section)
def unicode(self):
return "%s: %s" % (self.priority,self.section)
class Meta:
ordering = ['questionnaire','priority']
class ScoreCard(models.Model):
operator = models.ForeignKey(User,related_name='Operator')
evaluator = models.ForeignKey(User,related_name='Evaluator')
client = models.ForeignKey(Client)
call_time = models.DateTimeField()
input_time = models.DateTimeField()
notes = models.TextField()
possible = models.FloatField()
scored = models.FloatField()
percentage = models.FloatField()
questionnaire = models.ForeignKey(Questionnaire)
def unicode(self):
return "%s - %s/%s" % (self.operator.name,self.scored,self.possible)
class SelectedAnswer(models.Model):
scorecard = models.ForeignKey(ScoreCard)
topic = models.ForeignKey(Topic)
answer = models.ForeignKey(Answer)
def unicode(self):
return "%s - %s" % (self.topic,self.answer)
11:58pm by Brandon //comment 2028 views Saturday, June 6th, 2009
i love jquery
this is the beginning of a beautiful relationship!
1:31am by Brandon //comment 1212 views Tuesday, March 31st, 2009
Fire Up Firefox
How many web developers are using FireFox and still writing apps that have to look good in IE? Almost all of you?! WHAT? This has to stop. Companies, when will you see that FireFox is the answer. Keep your employees organized and on task with tabbed browsing (screw IE 7 and 8).
Also, save your developers time so they don't have to design their apps multiple times. While I'm designing, if I go too long without checking how it looks in Internet Explorer, I have to retrace my footsteps to find out where the mysterious IE bugs are. Let's just write in standards compliant mode, and use standards compliant browsers!
please everyone visit this site whether you use firefox or not:
FireFox Tips
Also, if you are NOT a firefox user, get with it and go here:
Get FireFox
Thanks for listening.
2:44am by Brandon //comment 488 views Tuesday, March 17th, 2009
HTML colors
I know I could always just go to the Complete HTML True Color Chart which comes up when you feel lucky with the search term HTML Colors, but I was just playing with some loops in Python and made this short script.
html = open('colors.html','w')
html.write("<html><body><table style='width:100%;'>\n")
colors = ['00','33','66','99','cc','ff']
for r in colors:
for g in colors:
html.write("<tr>")
for b in colors:
html.write("<td style='background-color:#%s%s%s;'><span style='color:#%s%s%s;'>#%s%s%s</span></td>" % (r,g,b,r,g,b,r,g,b))
html.write("</tr>")
html.write("</table></body></html>")
html.close()
If you put that in a text file called makecolors.py and then run it (assuming of course that you have python installed), you will get an html file with all the colors (browser safe). The reason I made the text the same color as the background is I specifically wanted to see the colors alone. Then I can just double click a cell and see what HTML color it is (or triple click if I want to get the '#' for easy copy and paste).
2:18pm by Brandon //comment 645 views Monday, February 23rd, 2009
M2Crypto FTPS
I have been trying a ton to find something about FTPS with python. I desperately need to automate a process that I have to manually do twice daily, and at very inconvenient hours. I finally came up with this:
from M2Crypto import ftpslib
def handleDownload(block):
file.write(block)
print ".",
f = ftpslib.FTP_TLS()
f.connect('ftp.server.com',21)
f.login('testaccount', 'testaccount')
f.auth_tls()
f.set_pasv(0)
file = open('localfile.txt', 'wb')
f.retrbinary('RETR test.txt', handleDownload)
file.close()
f.quit()
I'm not positive how secure it actually is, but it seems to me that M2Crypto should hold it's ground, since every internet search regarding FTPS with Python returns something about M2Crypto.
9:10pm by Brandon //comment 769 views