19 Mar
In a Django project, I have a template that is used by two urls, which is quite common (generic views, using ‘create_object’ and ‘update_object’). The problem is that I had to add a supplementary menu just when the template is loaded from the ‘update’ generic view, and not from the ‘create’ generic view.
Making the difference between the two urls calls at the template level is a problem because it’s managed by generic views, so the same template is used.
Anyways, there are several possibilities:
In urls.py, use the ‘template_name’ variable, where you can speficy a specific template for this url(). That is instead of using the default <model>_form.html.
What I don’t like in this situation, is that I will have two nearly similar templates, just for an added menu. Not cool. Another problem is that I use a loop to create all my urls. So if I add a special template, I’ll add it to ALL my models :-(.
Another solution, is to find a way to use a variable in the template that would be different wether the template has been loaded by update_object or create_object.
In our urlpatterns in urls.py, we can use the ‘extra_context‘ variable (takes a dictionnary as parameter). It is correctly managed, even when using generic views. So, you’ll have :
url(r‘foo/%s/(?P<object_id>\d+)/modifier/$’,
‘django.views.generic.create_update.update_object’,
dict(form_class=modelForm,
extra_context={‘usage’:‘modify’},
name=‘foo_update’))
We can also use, in urls.py, the ‘context_processors’ variable. For more information about the context processors, have a look at this tutorial. The goal is to add ‘django.core.context_processors.request’, like this:
and in the url(), add context_processors:
The last possiblity is a more global solution. It’s like the context_processors usage above, but added into every templates automatically.
To do this, you’ll have to edit the list of Template Processors in your settings.py file. That list is run each time a template is loaded, and allows one to add any variable to the template automatically. By default (on Django 1.0.x) this list is commented out, so it has by default the list:
You’ll have to uncomment it, and add ‘django.core.context_processors.request’. By doing this, you get the variable ‘request.path’ available in your template.
Finally, you’ll be able to test your variable with {% ifequal %} and display your conditional elements.
28 Jan
Mediawiki allows one to send Recent Changes (RC) to a UDP port.
The ip address is defined by the variable wgRC2UDPAddress, and the port by the variable wgRC2UDPPort. It’s a really good idea that it’s a UDP transfer, since there is no need for any ACK, so Mediawiki doesn’t block if there is nothing listening on that port.
These parameters must be set in the LocalSettings.php file, like this, for example.
I wrote a really simple python program (more a proof of concept) that receives the data, cleans it, and then prints it to stdout :
HOST,PORT = ”, 9390
class MyUDPHandler(SocketServer.BaseRequestHandler):
def handle(self):
print re.sub(r‘\x03\d{0,2}’, ”, self.request[0])
if __name__ == ‘__main__’:
server = SocketServer.UDPServer((HOST, PORT), MyUDPHandler)
server.serve_forever()
I plan to make it into a complete project that will send the data/notifications using Jabber(XMPP).
17 Dec
I didn’t find any example in Python on how to Dial a number from an Asterisk server and link it to another channel. So here’s a quick code to do it. You just need to have a manager defined in your /etc/asterisk/manager.conf defined.
HOST="192.168.1.116"
PORT=5038
p = """Action: login
Events: off
Username: %(username)s
Secret: %(password)s
Action: originate
Channel: SIP/%(local_user)s
WaitTime: 60
CallerId: 600
Exten: %(phone_to_dial)s
Context: default
Priority: 1
Action: Logoff
"""
def click_to_call(phone_to_dial, username, password, local_user):
pattern = p % {
‘phone_to_dial’: phone_to_dial,
‘username’: username,
‘password’: password,
‘local_user’: local_user}
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
data = s.recv(1024)
for l in pattern.split(‘n’):
print "Sending>", l
s.send(l+‘rn’)
if l == "":
data = s.recv(1024)
print data
data = s.recv(1024)
s.close()
if __name__ == ‘__main__’:
click_to_call(phone_to_dial=‘123456789′,
username=‘manager_login’, password=‘yourpass’,
local_user=‘600′)
27 Jun
Mark Ramm, one of TurboGears’s core developers announced on TurboGears’s mailing list that they will merge with Pylons! To be more precise, the API of TurboGears will be implemented on top of Pylons. It seems they already made some test/proof of concept that are, as they say “a huge success”. That’s really good news for Python web frameworks development, and a good news for me, as I’ll not have to choose between the two :-
As a sidenote, it seems another framework, CleverHarold has disappeared without anybody noticing. Its domain is parked, and today its Google Group page went off (the last messages were from people asking if the project was still alive).
Update: Noah Gift wrote a nice article about the merge.
12 Jun
As the title says it, each presentation was filmed, and has just been uploaded for everyone’s pleasure! Here is the two-part video presentation of Twisted, done by Michael SCHERER.
Oh, by the way, please take a minute to vote for your favourite Internet Engine!
3 Jun
I went today to the Journée Python 2007 Conference in France. I managed to see half of the Twisted intro, some lightning talks, and most of the afternoon presentations (thanks to Ido’s mid-day nap).
Most of the talks were introductory type, but they were finely presented (alas most of the audience already knew python). I hope I’ll find the time to prepare some more advanced Twisted presentation for next year’s Conference (there’s one, right?)
7 May
Pylons is a cool web framework (one more, besides TurboGears, Django, Zope, etc.). Someone posted on the mailing list a reference to a nice introduction.
25 Apr
I found a nicely written article about the problems with Ruby, written by a Ruby user, and why he found Python to be really good. There’s even a quote about Twisted!
And, there are a bunch of things available to a Python guy that Ruby just can’t compete with that are of particular interest to me. Two that come to mind immediately are Twisted and Stackless Python. The former was used by others at TurnTide for creating a really powerful SMTP testing tool and the latter was used by TurnTide’s competitor IronPort to build one of the industry’s best MTAs.
I didn’t knew that IronPort was done in Python, even in Stackless Python!
10 Apr
Finally, after such painful setups, I really never could find a really suitable configuration that could satisfy me. Here are the different methods I tried to implement TurboGears/Pylons or similar WSGI/Python projects (MoinMoin for example):
Now here’s a new contender, mod_wsgi written by Graham Dumpleton. That’s right, it will not work for every app. Zope, not being WSGI aware, is out of the way, except for Zope 3.x. But most of Python apps can be modded to be WSGI aware (MoinMoin is an example).
28 Mar
PyPy version 1.0 is now available. That’s really a great news, and a milestone for the Python community (and the rest of the programming world). It’s not yet recommended to use it in production, but we’re not that far from that.
Please read the announcement and go in the different links referenced there to learn more about PyPy and what it will change in your life.
Python is so cool.
Search engine optimization by SEO Design Solutions
Recent Comments