0

Well I have some old python code that seems to not work right, I have researched to the ends of the internet trying to find a fix.

def getURL(self, context):
    # Make this an absolute URL- currently it's required for
    # links placed in the RSS and XML feeds, and won't
    # hurt elsewhere.
    req = context['request']                                                                         
    port = req.host[2]
    hostname = req.getRequestHostname()
    if req.isSecure():
        default = 443
    else:
        default = 80
    if port == default:
        hostport = ''
    else:
        hostport = ':%d' % port
    path = posixpath.join('/stats',
                          *(tuple(self.target.pathSegments) + self.relativePathSegments))
    return quote('http%s://%s%s%s' % (
        req.isSecure() and 's' or '',
        hostname,
        hostport,
        path), "/:")

now I think its just the context['request'] giving me issues but I'm not sure. This code block was from the CIA.vc project (link.py to be exact), so if something doesn't make sense check there

also the 1st error i get from python is:

File "/home/justasic/cia/cia/LibCIA/Web/Stats/Link.py", line 41, in getURL port = req.host[2] exceptions.TypeError: unindexable object

but I got more about the context['request'] not being defined after I found what I think was a simple fix

Justasic
  • 1
  • 1
  • 3
    Would it be possible to give a slightly more concrete example of what you expected to happened, what failed, and the indication that it did? – Peter Rowell Jun 18 '11 at 22:38
  • The first error I got was `File "/home/justasic/cia/cia/LibCIA/Web/Stats/Link.py", line 41, in getURL port = req.host[2] exceptions.TypeError: unindexable object` but I got more about the `context['request']` after I found what i think was a simple fix – Justasic Jun 20 '11 at 12:41
  • Unless you start working harder on your end of this conversation, we have no reason to work hard on our end. Exactly where in the processing of the request does this routine occur? Is this part of a template tag? Where did that context come from? Is it a Django Context object? Is that a Twisted Request object? What version of Twisted was this created for? What version are you using now? Same questions for Django. etc. etc. etc. – Peter Rowell Jun 20 '11 at 15:42
  • @Peter I honestly have little clue on how this works, I am using CIA as a starting point because it was used in IRC. I know that @andzep's answer may have worked but I cannot directly include django code into the file, leading me to assume its apart of twisted. I know it was created for at least Twisted 8.0.0 or earlier so its really old code and I would say the same era for Django. I am sorry I am not very helpful but I am doing my best, I have a full error output here if it helps any: http://pastebin.com/g8QfqDUQ and I have been looking for where this function is called and cant find it. – Justasic Jun 21 '11 at 10:12
  • Ok it looks like I just need it to get the port that the request is requested on. I replaced `req.host[8]` with just `80` and it worked perfectly.. – Justasic Jun 27 '11 at 01:48

1 Answers1

0

It seems to me like Context['request'] doesn't fit on there... where does Context come from? As param you get context all lowercase. Probably you sould use the param 'context' instead, so ...

a) make Context['request'] to context['request']

... or, if your are already using context in lowercase, and it's only a typo here on the post, then

b) I searched a while and found this snippet http://djangosnippets.org/snippets/2428/... so maybe something like this might work:

from django.template import resolve_variable

...

def getURL(self, context):
    req = resolve_variable('request', context)
andzep
  • 1,877
  • 24
  • 35