Url decorator

125 views
Skip to first unread message

!张沈鹏(电子科大 08年毕业)

unread,
Aug 8, 2007, 4:34:13 AM8/8/07
to django-d...@googlegroups.com
Url decorator

Some time we want to account the number of visitor by url or some url
only the logined in user can visit .

Yes , we can use url decorator do this thing .

I write a reuseable app for simplify those job . It can make decorator
more easy , can work with "include(xxx.urls)",
support multi-decorator .

Ok , we give a simplest example:

from django.conf.urls.defaults import *
from django.contrib.admin.views.decorators import staff_member_required
from django.views.generic.simple import direct_to_template

from url_decorate.url_decorate import url_decorate

staff_member_url=url_decorate(staff_member_required)

urlpatterns = patterns('',
staff_member_required(r'^$',direct_to_template, {'template': 'xxx.html'}),
)

Ok,You will find the / of this website require login in .
But , sometime we have a lot of url should be logined in ,
patterns_decorate will help you to do this more easy .
Just use as below:
staff_member_patterns=patterns_decorate(staff_member_required)
urlpatterns = staff_member_patterns('',
(r'xxx',direct_to_template, {'template': 'xxx.html'}),
(r'xxx',direct_to_template, {'template': 'xxx.html'}),
(r'xxx',direct_to_template, {'template': 'xxx.html'}),
)

For lazy people as me , I write staff_member_patterns,staff_member_url
and login_url,login_patterns in shortcuts.py , just import it to use
:)

Then , let's write a decorator by ourself .This decorator can account
the number of visitor for each pages .

First , write a model

models.py:
#----------------------------------------------------------------------------
from django.db import models
class PageAnalytic(models.Model):
url = models.URLField(max_length=2048,primary_key=True)
visit = models.IntegerField(default=0)

class Admin:
list_display= ('url','visit')

class Meta:
ordering = ['url']
#----------------------------------------------------------------------------

Second , write the view . You just need to write a function can
process the request .
The other thing "make_decorate" can take it over .

views.py:
#----------------------------------------------------------------------------
from models import PageAnalytic
from url_decorate import make_decorate

@make_decorate
def page_analytic(request):
page,is_create=PageAnalytic.objects.get_or_create(url=request.path)
page.visit+=1
page.save()
#----------------------------------------------------------------------------

At last , write two shortcuts

shortcuts.py:
#----------------------------------------------------------------------------
from url_decorate import url_decorate,patterns_decorate
from views import page_analytic

page_analytic_url=url_decorate(page_analytic)
page_analytic_patterns=patterns_decorate(page_analytic)
#----------------------------------------------------------------------------

Now , It's time to use this , we just accout the admin .


#----------------------------------------------------------------------------
from url_decorate.shortcuts import page_analytic_url

urlpatterns = patterns('',
page_analytic_url(r'^admin/', include('django.contrib.admin.urls')),
)


Ok , you can find screenshot and app at attachment .

A question : I should put this app at where ? google code ?I will
write a tag for the page account in the future .


--
欢迎访问我的博客:
http://zsp.javaeye.com/

--张沈鹏

ac.JPG
url_decorate.zip
Reply all
Reply to author
Forward
0 new messages