comparison roundup/cgi/client.py @ 6977:ff2c8b430738

flake8 - remove re.compile from method arg + test + doc changed 2 methods defined like: def method(..., dre=re.compile(r'...')): moved re.compile to module variables and passed the var name def method(..., dre=var_name): while doing this I found out that a url of .../issue0001 will behave like .../issue1. Who knew. Documented in customizing. Tested same in test_liveserver. Added msg1 as well so I could verify msg0001 worked. Also added some range tests as well.
author John Rouillard <rouilj@ieee.org>
date Wed, 14 Sep 2022 17:48:51 -0400
parents 3917ae82fb24
children 7259ce224d65
comparison
equal deleted inserted replaced
6976:3917ae82fb24 6977:ff2c8b430738
1177 for r in token['roles']: 1177 for r in token['roles']:
1178 if r.lower() not in all_rolenames: 1178 if r.lower() not in all_rolenames:
1179 raise LoginError("Token roles are invalid.") 1179 raise LoginError("Token roles are invalid.")
1180 1180
1181 # will be used later to override the get_roles method 1181 # will be used later to override the get_roles method
1182 override_get_roles = lambda self: iter_roles( 1182 # having it defined as truthy allows it to be used.
1183 override_get_roles = lambda self: iter_roles( # noqa: E731
1183 ','.join(token['roles'])) 1184 ','.join(token['roles']))
1184 1185
1185 # if user was not set by http authorization, try session lookup 1186 # if user was not set by http authorization, try session lookup
1186 if not user: 1187 if not user:
1187 user = self.session_api.get('user') 1188 user = self.session_api.get('user')
1630 self.db.tx_Source = "web" 1631 self.db.tx_Source = "web"
1631 # The old session API refers to the closed database; 1632 # The old session API refers to the closed database;
1632 # we can no longer use it. 1633 # we can no longer use it.
1633 self.session_api = Session(self) 1634 self.session_api = Session(self)
1634 1635
1635 def determine_context(self, dre=re.compile(r'([^\d]+)0*(\d+)')): 1636 # match designator in URL stripping leading 0's. So:
1637 # https://issues.roundup-tracker.org/issue002551190 is the same as
1638 # https://issues.roundup-tracker.org/issue2551190
1639 # Note: id's are strings not numbers so "02" != "2" but 02 == 2
1640 dre_url = re.compile(r'([^\d]+)0*(\d+)')
1641
1642 def determine_context(self, dre=dre_url):
1636 """Determine the context of this page from the URL: 1643 """Determine the context of this page from the URL:
1637 1644
1638 The URL path after the instance identifier is examined. The path 1645 The URL path after the instance identifier is examined. The path
1639 is generally only one entry long. 1646 is generally only one entry long.
1640 1647
1741 1748
1742 # see if we have a template override 1749 # see if we have a template override
1743 if template_override is not None: 1750 if template_override is not None:
1744 self.template = template_override 1751 self.template = template_override
1745 1752
1746 def serve_file(self, designator, dre=re.compile(r'([^\d]+)(\d+)')): 1753 # re for splitting designator, see also dre_url above this one
1754 # doesn't strip leading 0's from the id. Why not??
1755 dre = re.compile(r'([^\d]+)(\d+)')
1756
1757 def serve_file(self, designator, dre=dre):
1747 """ Serve the file from the content property of the designated item. 1758 """ Serve the file from the content property of the designated item.
1748 """ 1759 """
1749 m = dre.match(str(designator)) 1760 m = dre.match(str(designator))
1750 if not m: 1761 if not m:
1751 raise NotFound(str(designator)) 1762 raise NotFound(str(designator))
2512 return None 2523 return None
2513 # Get the first and last bytes. 2524 # Get the first and last bytes.
2514 first = self.http_strip(pos[0]) 2525 first = self.http_strip(pos[0])
2515 last = self.http_strip(pos[1]) 2526 last = self.http_strip(pos[1])
2516 # We do not handle suffix ranges. 2527 # We do not handle suffix ranges.
2528 # Note this also captures atempts to make first
2529 # element of range a negative number.
2517 if not first: 2530 if not first:
2518 return None 2531 return None
2519 # Convert the first and last positions to integers. 2532 # Convert the first and last positions to integers.
2520 try: 2533 try:
2521 first = int(first) 2534 first = int(first)
2525 last = length - 1 2538 last = length - 1
2526 except ValueError: 2539 except ValueError:
2527 # The positions could not be parsed as integers. 2540 # The positions could not be parsed as integers.
2528 return None 2541 return None
2529 # Check that the range makes sense. 2542 # Check that the range makes sense.
2543 # Note, if range is -1-10, first = '', so this code will never
2544 # be reached. if range = 1--10, this code is reached.
2530 if (first < 0 or last < 0 or last < first): 2545 if (first < 0 or last < 0 or last < first):
2531 return None 2546 return None
2532 if last >= length: 2547 if last >= length:
2533 # RFC 2616 10.4.17: 416 Requested Range Not Satisfiable 2548 # RFC 2616 10.4.17: 416 Requested Range Not Satisfiable
2534 # 2549 #

Roundup Issue Tracker: http://roundup-tracker.org/