[Tutor] suggestion on improving script

bob gailer bgailer at alum.rpi.edu
Sun Mar 23 17:20:42 CET 2008


Norman Khine wrote:
> Hello,
>
> Please excuse me in advance if this post is long winded. I have the 
> following nagging issue for which I have found a work around, but wanted 
> a better solution.
>   

The reason
http://uk.expert.travel/companies/abakuc/;view?batchstart=5
fails while
http://uk.expert.travel/;view?t2=5
succeeds does not have to do with using a global method.

t2=5 tells the CGI program which tab was clicked, whereas batchstart=5 
does not.
You only need one "t" function as long as you use the t2=5 approach (or 
some other way to
communicate WHICH tab and WHERE to start (e.g. ?tab=2,batchstart=5.

Also - 99% of list_news, list_jobs, et. al. can be factored out into 
common code.

Does this make sense? Would you like more help or can you start figuring 
it out yourself?
> I am using jQuery to populate tabs with some data, such as news and jobs 
> posts, as can be seen at http://uk.expert.travel
>
> In my application, I have the following structure (only partially listed):
>
> $ tree -L 2
> .
> |-- expert_travel.py
> |-- ui
> |   `-- ui.tabs.js
> ...
> `-- utils.py
>
>
>
> #expert_travel.py
>
> from utils import t1, t2, t3, t4
>
> ...
>
>      ####################################################################
>      # News - List
>      ####################################################################
>
>      def list_news(self, context):
>          namespace = {}
>          namespace['batch'] = ''
>          #Search the catalogue, list all news items in company
>
> ...
>
> # Set batch informations
>
> [t1]        batch_start = int(context.get_form_value('t1', default=0))
>
>          batch_size = 5
>          batch_total = len(news_items)
>          batch_fin = batch_start + batch_size
>          if batch_fin > batch_total:
>              batch_fin = batch_total
>          news_items = news_items[batch_start:batch_fin]
>          # Namespace
>          if news_items:
>              msgs = (u'There is one news item.',
>                      u'There are ${n} news items.')
>
> [t1]            news_batch = t1(context.uri, batch_start, batch_size,
>                                batch_total, msgs=msgs)
>
>              msg = None
>          else:
>              news_batch = None
>              msg = u'Currently there is no news.'
>
>
>      ####################################################################
>      # List jobs
>      list_jobs__label__ = u'List jobs'
>      list_jobs__access__ = True
>      def list_jobs(self, context):
> ...
>
>          # Set batch informations
>
> [t2]        batch_start = int(context.get_form_value('t2', default=0))
>
>          batch_size = 5
>          batch_total = len(jobs)
>          batch_fin = batch_start + batch_size
>          if batch_fin > batch_total:
>              batch_fin = batch_total
>          jobs = jobs[batch_start:batch_fin]
>          # Namespace
>          if jobs:
>              msgs = (u'There is one job.',
>                      u'There are ${n} jobs.')
>
> [t2]            job_batch = t2(context.uri, batch_start, batch_size,
>                                batch_total, msgs=msgs)
>
>              msg = None
>          else:
>              job_table = None
>              job_batch = None
>              msg = u'Sorry but there are no jobs'
> ...
>
>
>
> ####
>
> #utils.py
>
> def t1(uri, start, size, total, gettext=Handler.gettext,
>            msgs=(u"There is 1 object.", u"There are ${n} objects.")):
>
>      # Plural forms
>      if total == 1:
>          msg1 = gettext(msgs[0])
>      else:
>          msg1 = gettext(msgs[1])
>          msg1 = Template(msg1).substitute(n=total)
>      msg1 = msg1.encode('utf-8')
>
>      # Calculate end
>      end = min(start + size, total)
>
>      # Previous
>      previous = None
>      if start > 0:
>          previous = max(start - size, 0)
>          previous = str(previous)
>
> [t1]        previous = uri.replace(t1=previous)
>
>          previous = str(previous)
>          previous = XMLAttribute.encode(previous)
>          previous = '<a href="%s" title="%s">&lt;&lt;</a>' \
>                     % (previous, gettext(u'Previous'))
>      # Next
>      next = None
>      if end < total:
>          next = str(end)
>
> [t1]        next = uri.replace(t1=next)
>
>          next = str(next)
>          next = XMLAttribute.encode(next)
>          next = '<a href="%s" title="%s">&gt;&gt;</a>' \
>                 % (next, gettext(u'Next'))
>
>      # Output
>      if previous is None and next is None:
>          msg = msg1
>      else:
>          # View more
>          if previous is None:
>              link = next
>          elif next is None:
>              link = previous
>          else:
>              link = '%s %s' % (previous, next)
>
>          msg2 = gettext(u"View from ${start} to ${end} (${link}):")
>          msg2 = Template(msg2)
>          msg2 = msg2.substitute(start=(start+1), end=end, link=link)
>          msg2 = msg2.encode('utf-8')
>
>          msg = '%s %s' % (msg1, msg2)
>
>      # Wrap around a paragraph
>      return Parser('<p class="batchcontrol">%s</p>' % msg, namespaces)
>
>
> #Second TAB
>
> def t2(uri, start, size, total, gettext=Handler.gettext,
>            msgs=(u"There is 1 object.", u"There are ${n} objects.")):
>      # Plural forms
> ...
>
>
> ###
>
> I would like to alter this batch control so that I don't have to create 
> a new method everytime I need a new tab.
>
> For example, I would like to change t1, t2 ... t[n] depending on the tab 
> I am at.
>
> You can see it in action at http://uk.expert.travel/;view
>
> If you click on the [Jobs] tab, there are 6 jobs, if you then click on 
> the 'There are 6 jobs. View from 1 to 5 (>>):'
>
> you will get a list of the last post.
>
> Note the URI http://uk.expert.travel/;view?t2=5
>
>
> If I only use one global method, the problem would be that when I 
> clicked on the News tab, I will get no items displayed as can be seen here:
>
> http://uk.expert.travel/companies/abakuc/;view
>
> I hope this makes sense ;)
>
> Any advice would be much appreciated.
>
> Cheers
>
> Norman
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>   


-- 
Bob Gailer
919-636-4239 Chapel Hill, NC



More information about the Tutor mailing list