from django import template
#import urllib,urlparse
from django.utils.http import urlencode
register = template.Library()

class HttpGetArgsAddOne(template.Node):
    def __init__(self, cname,cvalue):
        self.cname = cname
        self.cvalue = cvalue
    def render(self, context):
        dsrc=context['request'].GET.dict()
        value=self.cvalue.resolve(context, True)
        name=self.cname.resolve(context, True)
        dopt=dict([(name,value)])
        dsrc.update(dopt)
        return urlencode(dsrc)

@register.tag(name="HttpGetArgsAddOne")
def do_HttpGetArgsAddOne(parser, token):
    try:
        tag_name,name,value = token.split_contents()
    except ValueError:
        msg = '%r tag requires 2 arguments' % token.split_contents()[0]
        raise template.TemplateSyntaxError(msg)
    cvalue=parser.compile_filter(value)
    cname=parser.compile_filter(name)
    return HttpGetArgsAddOne(cname,cvalue)