August 4th, 2008
So I pulled the latest Django codebase from svn today and I noticed that my old pagination setup no longer works. Not really surprising as a ton of stuff is being updated due to the coming release of 1.0. So I did a bit of digging into the new pagination code to see if I could rework my wrapper class to work with the new release. It turns out that with the updates there really is no good reason to wrap the classes any more. They have done a good job of working out all the kinks in the pagination setup and it does everything I need it to do out of the box.
So here is a quick nutshell example of how I am using the new django.core.paginator classes to replace the old ObjectPaginator wrapper from my previous example.
in your view:
from django.core.paginator import Paginator
#create a new paginator instance by passing it a collection of objects and the per page count
paginator = Paginator(Contact.objects.all().order_by('last_name'), 10)
#get the page number from a get param
#if param is blank then set page to 1
page = int(request.GET.get('page', '1'))
#grab the current page from the paginator...
contacts = paginator.page(page)
#render the template and pass the contacts page into the template
return render_to_response('contacts.html',{'contacts':contacts})
in your template:
{% for contact in contacts.object_list %}
{{ contact.name }}
{% endfor %}
I’d say that’s pretty easy. Here is a link to a simple snippet that you can use to create a paging footer.
Notice to get the page of pages info all you have to do is output the page object like so… {{ contacts }} . Pretty cool.
Posted in development, django, python | No Comments »
July 22nd, 2008
Posted in development | No Comments »
June 20th, 2008
Here is a decorator that I came up with to augment the already available login_required and permission_required decorators. This one takes a group name and makes sure the logged in user is a part of it.
def group_required(group_name, login_url='/accounts/login/'):
def wrap(view_func):
def in_group(request, *args, **kwargs):
from django.contrib.auth.models import Group
try:
group = request.user.groups.get(name=group_name)
except Group.DoesNotExist:
from django.http import HttpResponseRedirect
return HttpResponseRedirect(login_url)
else:
return view_func(request, *args, **kwargs)
return in_group
return wrap
to use
@group_required('groupies')
def index(request):
...
I’m posting this with a disclaimer up front. Every time I post what I think is a clever snippet someone always tells me that I am reinventing the wheel and it already exists in django. So if this is another case of me being tragically uninformed then please let me know as I would most likely use the official way instead.
Posted in development, django, python | 2 Comments »
April 27th, 2008
So awhile back (when I switched to Vista which had built in windows media center) I started getting into streaming videos from my pc to my xbox over the network so I could watch them on my tv. I did some searching and found that you have to convert everything to wmv first. So since all the files I have were avi files I found this tutorial to help me convert the files without having to pay for some shady converter software. Following that tutorial I got up and running fairly quickly and everything was peachy. Well sorta. My only issue was with this method you can only do one video at a time. So since I have a huge library of episodic anime this quickly became a prob. So I wrote a little add on to the instructions above that allows you to batch convert your avi files to wmv file using vlc player.
First the convert.bat file. Copy this code to a file and name it convert.bat. Put it in the directory where all your avi files are.
"C:\Program Files\VideoLAN\VLC\vlc" -vvv %1 --sout-ffmpeg-qscale 1 :sout=#transcode{vcodec=WMV2,scale=1,acodec=wma,ab=96,channels=2}:duplicate{dst=std{access=file,mux=asf,dst=%1.wmv}} vlc:quit
note
You may have to change this part C:\Program Files\VideoLAN\VLC\vlc to point at the path you installed your alc player if you didn’t go with the default install location.
Next the the bulk-convert script. Save the following code into a file and name it bulk-convert.bat. Put this script in the same directory as you convert.bat and all your avi files.
lfnfor off
for %%x in (*.avi) do call convert.bat %%x
From there all you have to do is click on the bulk-convert.bat file and it will cycle through all the avi files in the directory and feed them into your convert.bat file.
Enjoy 
Posted in tutorials, video, xbox | 3 Comments »