def expand_macro(self, formatter, name, content): # Create request context. context = Context.from_request(formatter.req)('screenshots-wiki') # Get database access. db = self.env.get_db_cnx() context.cursor = db.cursor() # Get API component. api = self.env[ScreenshotsApi] if name == 'Screenshot': # Check permission. if not formatter.req.perm.has_permission('SCREENSHOTS_VIEW'): return html.div('No permissions to see screenshots.', class_ = 'system-message') # Get macro arguments. arguments = content.split(',') # Get screenshot ID. try: screenshot_id = int(arguments[0]) except: raise TracError("Missing screenshot ID in macro arguments.") # Try to get screenshots of that ID. screenshot = api.get_screenshot(context, screenshot_id) # Build and return macro content. if screenshot: # Set default values of image attributes. attributes = {'align' : 'none', 'border' : '1', 'format' : 'raw', 'alt' : screenshot['description'], 'description' : self.default_description} # Fill attributes from macro arguments. for argument in arguments[1:]: argument = argument.strip() match = self.attributes_re.match(argument) if match: attributes[str(match.group(1))] = match.group(2) # Zero width or height means keep original. if attributes.has_key('width'): if attributes['width'] == 0: attributes['width'] = screenshot['width'] if attributes.has_key('height'): if attributes['height'] == 0: attributes['height'] = screenshot['height'] # If one dimension is missing compute second to keep aspect. if not attributes.has_key('width') and \ attributes.has_key('height'): attributes['width'] = int(int(attributes['height']) * ( float(screenshot['width']) / float(screenshot['height'])) + 0.5) if not attributes.has_key('height') and \ attributes.has_key('width'): attributes['height'] = int(int(attributes['width']) * ( float(screenshot['height']) / float(screenshot['width'])) + 0.5) # If both dimensions are missing keep original. if not attributes.has_key('width') and not \ attributes.has_key('height'): attributes['width'] = screenshot['width'] attributes['height'] = screenshot['height'] self.log.debug('attributes: %s' % (attributes,)) # Format screenshot description from template. attributes['description'] = self._format_description(context, attributes['description'], screenshot) # Make copy of attributes for image tag. img_attributes = {} for attribute in attributes.keys(): if attribute not in ('align', 'border', 'description', 'format', 'width', 'height'): img_attributes[attribute] = attributes[attribute] # Add CSS for image. add_stylesheet(formatter.req, 'screenshots/css/screenshots.css') # Build screenshot image and/or screenshot link. image = html.img(src = formatter.req.href.screenshots( screenshot['id'], format = 'raw', width = attributes['width'], height = attributes['height']), **img_attributes) link = html.a(image, href = formatter.req.href.screenshots( screenshot['id'], format = attributes['format']), title = screenshot['description'], style = 'border-width: %spx;' % ( attributes['border'],)) width_and_border = int(attributes['width']) + 2 * \ int(attributes['border']) description = html.span(attributes['description'], class_ = 'description', style = "width: %spx;" % (width_and_border,)) auxilary = html.span(link, description, class_ = 'aux', style = "width: %spx;" % (width_and_border,)) thumbnail_class = 'thumbnail' + ((attributes['align'] == 'left') and '-left' or (attributes['align'] == 'right') and '-right' or '') thumbnail = html.span(auxilary, class_ = thumbnail_class) return thumbnail else: return html.a(screenshot_id, href = formatter.req.href.screenshots(), title = content, class_ = 'missing') elif name == 'ScreenshotsList': # Check permission. if not formatter.req.perm.has_permission('SCREENSHOTS_VIEW'): return html.div('No permissions to see screenshots.', class_ = 'system-message') # Get desired list item description list_item_description = content or self.default_list_item # Get all screenshots. screenshots = api.get_screenshots_complete(context) # Create and return HTML list of screenshots. list_items = [] for screenshot in screenshots: list_item = self._format_description(context, list_item_description, screenshot) list_items.append(html.li(html.a(list_item, href = formatter.req.href.screenshots(screenshot['id'])))) return html.ul(list_items)
def render_macro(self, req, name, content): # Get database access. db = self.env.get_db_cnx() cursor = db.cursor() # Get API component. api = self.env[ScreenshotsApi] if name == 'Screenshot': # Check permission. if not req.perm.has_permission('SCREENSHOTS_VIEW'): return html.div('No permissions to see screenshots.', class_='system-message') # Get macro arguments. arguments = content.split(',') # Get screenshot ID. try: screenshot_id = int(arguments[0]) except: raise TracError("Missing screenshot ID in macro arguments.") # Try to get screenshots of that ID. screenshot = api.get_screenshot(cursor, screenshot_id) # Build and return macro content. if screenshot: # Set default values of image attributes. attributes = { 'align': 'none', 'border': '1', 'format': 'raw', 'width': screenshot['width'], 'height': screenshot['height'], 'alt': screenshot['description'], 'description': self.default_description } # Fill attributes from macro arguments. for argument in arguments[1:]: argument = argument.strip() match = self.attributes_re.match(argument) if match: attributes[str(match.group(1))] = match.group(2) self.log.debug('attributes: %s' % (attributes, )) # Format screenshot description from template. attributes['description'] = self._format_description( attributes['description'], screenshot) # Make copy of attributes for image tag. img_attributes = { 'align': 'center', 'style': 'border-width: %spx;' % (attributes['border'], ) } for attribute in attributes.keys(): if attribute not in ('align', 'border', 'description', 'format'): img_attributes[attribute] = attributes[attribute] # Add CSS for image. add_stylesheet(req, 'screenshots/css/screenshots.css') # Build screenshot image and/or screenshot link. image = html.img(src=req.href.screenshots( screenshot['id'], width=attributes['width'], height=attributes['height'], format='raw'), **img_attributes) link = html.a(image, href=req.href.screenshots( screenshot['id'], format=attributes['format']), title=screenshot['description']) description = html.span(attributes['description'], class_='description') thumbnail_class = 'thumbnail' + ( (attributes['align'] == 'left') and '-left' or (attributes['align'] == 'right') and '-right' or '') thumbnail = html.span(link, ' ', description, class_=thumbnail_class, style="width: %spx;" % (int(attributes['width']) + 2 * int(attributes['border'], ))) return thumbnail else: return html.a(screenshot_id, href=req.href.screenshots(), title=content, class_='missing') elif name == 'ScreenshotsList': # Check permission. if not req.perm.has_permission('SCREENSHOTS_VIEW'): return html.div('No permissions to see screenshots.', class_='system-message') # Get desired list item description list_item_description = content or self.default_list_item # Get all screenshots. screenshots = api.get_screenshots_complete(cursor) # Create and return HTML list of screenshots. list_items = [] for screenshot in screenshots: list_item = self._format_description(list_item_description, screenshot) list_items.append( html.li( html.a(list_item, href=req.href.screenshots(screenshot['id'])))) return html.ul(list_items)
def generate_captcha(self, req): resp, content = self.mollom._call('captcha', {'type': 'image'}) tree = ElementTree.fromstring(content) req.session['captcha_mollom_id'] = tree.find('./captcha/id').text url = tree.find('./captcha/url').text return '', html.img(src=url, alt='captcha')
def expand_macro(self, formatter, name, content): # Create request context. context = Context.from_request(formatter.req)('screenshots-wiki') # Get database access. db = self.env.get_db_cnx() context.cursor = db.cursor() # Get API component. api = self.env[ScreenshotsApi] if name == 'Screenshot': # Check permission. if not formatter.req.perm.has_permission('SCREENSHOTS_VIEW'): return html.div(_("No permissions to see screenshots."), class_='system-message') # Get macro arguments. arguments = content.split(',') # Get screenshot ID. try: screenshot_id = int(arguments[0]) except: raise TracError(_("Missing screenshot ID in macro arguments.")) # Try to get screenshots of that ID. screenshot = api.get_screenshot(context, screenshot_id) # Build and return macro content. if screenshot: # Set default values of image attributes. attributes = { 'align': 'none', 'border': '1', 'format': 'raw', 'alt': screenshot['description'], 'description': self.default_description } # Fill attributes from macro arguments. for argument in arguments[1:]: argument = argument.strip() match = self.attributes_re.match(argument) if match: attributes[str(match.group(1))] = match.group(2) # Zero width or height means keep original. if attributes.has_key('width'): if attributes['width'] == 0: attributes['width'] = screenshot['width'] if attributes.has_key('height'): if attributes['height'] == 0: attributes['height'] = screenshot['height'] # If one dimension is missing compute second to keep aspect. if not attributes.has_key('width') and \ attributes.has_key('height'): attributes['width'] = int( int(attributes['height']) * (float(screenshot['width']) / float(screenshot['height'])) + 0.5) if not attributes.has_key('height') and \ attributes.has_key('width'): attributes['height'] = int( int(attributes['width']) * (float(screenshot['height']) / float(screenshot['width'])) + 0.5) # If both dimensions are missing keep original. if not attributes.has_key('width') and not \ attributes.has_key('height'): attributes['width'] = screenshot['width'] attributes['height'] = screenshot['height'] self.log.debug('attributes: %s' % (attributes, )) # Format screenshot description from template. attributes['description'] = self._format_description( context, attributes['description'], screenshot) # Make copy of attributes for image tag. img_attributes = {} for attribute in attributes.keys(): if attribute not in ('align', 'border', 'description', 'format', 'width', 'height'): img_attributes[attribute] = attributes[attribute] # Add CSS for image. add_stylesheet(formatter.req, 'screenshots/css/screenshots.css') # Build screenshot image and/or screenshot link. image = html.img(src=formatter.req.href.screenshots( screenshot['id'], format='raw', width=attributes['width'], height=attributes['height']), **img_attributes) link = html.a(image, href=formatter.req.href.screenshots( screenshot['id'], format=attributes['format']), title=screenshot['description'], style='border-width: %spx;' % (attributes['border'], )) width_and_border = int(attributes['width']) + 2 * \ int(attributes['border']) description = html.span(attributes['description'], class_='description', style="width: %spx;" % (width_and_border, )) auxilary = html.span(link, description, class_='aux', style="width: %spx;" % (width_and_border, )) thumbnail_class = 'thumbnail' + ( (attributes['align'] == 'left') and '-left' or (attributes['align'] == 'right') and '-right' or '') thumbnail = html.span(auxilary, class_=thumbnail_class) return thumbnail else: return html.a(screenshot_id, href=formatter.req.href.screenshots(), title=content, class_='missing') elif name == 'ScreenshotsList': # Check permission. if not formatter.req.perm.has_permission('SCREENSHOTS_VIEW'): return html.div(_("No permissions to see screenshots."), class_='system-message') # Get desired list item description list_item_description = content or self.default_list_item # Get all screenshots. screenshots = api.get_screenshots_complete(context) # Create and return HTML list of screenshots. list_items = [] for screenshot in screenshots: list_item = self._format_description(context, list_item_description, screenshot) list_items.append( html.li( html.a(list_item, href=formatter.req.href.screenshots( screenshot['id'])))) return html.ul(list_items)
def _generate_avatar(self, req, author, class_, size): href = self.pictures_provider.get_src(req, author, size) return tag.img(src=href, class_='userpictures_avatar %s' % class_, width=size, height=size).generate()
def render_macro(self, req, name, content): # Get database access. db = self.env.get_db_cnx() cursor = db.cursor() # Get API component. api = self.env[ScreenshotsApi] if name == 'Screenshot': # Check permission. if not req.perm.has_permission('SCREENSHOTS_VIEW'): return html.div('No permissions to see screenshots.', class_ = 'system-message') # Get macro arguments. arguments = content.split(',') # Get screenshot ID. try: screenshot_id = int(arguments[0]) except: raise TracError("Missing screenshot ID in macro arguments.") # Try to get screenshots of that ID. screenshot = api.get_screenshot(cursor, screenshot_id) # Build and return macro content. if screenshot: # Set default values of image attributes. attributes = {'align' : 'none', 'border' : '1', 'format' : 'raw', 'width' : screenshot['width'], 'height' : screenshot['height'], 'alt' : screenshot['description'], 'description' : self.default_description} # Fill attributes from macro arguments. for argument in arguments[1:]: argument = argument.strip() match = self.attributes_re.match(argument) if match: attributes[str(match.group(1))] = match.group(2) self.log.debug('attributes: %s' % (attributes,)) # Format screenshot description from template. attributes['description'] = self._format_description( attributes['description'], screenshot) # Make copy of attributes for image tag. img_attributes = {'align' : 'center', 'style' : 'border-width: %spx;' % ( attributes['border'],)} for attribute in attributes.keys(): if attribute not in ('align', 'border', 'description', 'format'): img_attributes[attribute] = attributes[attribute] # Add CSS for image. add_stylesheet(req, 'screenshots/css/screenshots.css') # Build screenshot image and/or screenshot link. image = html.img(src = req.href.screenshots(screenshot['id'], width = attributes['width'], height = attributes['height'], format = 'raw'), **img_attributes) link = html.a(image, href = req.href.screenshots(screenshot['id'], format = attributes['format']), title = screenshot['description']) description = html.span(attributes['description'], class_ = 'description') thumbnail_class = 'thumbnail' + ((attributes['align'] == 'left') and '-left' or (attributes['align'] == 'right') and '-right' or '') thumbnail = html.span(link, ' ', description, class_ = thumbnail_class, style = "width: %spx;" % ( int(attributes['width']) + 2 * int(attributes['border'],))) return thumbnail else: return html.a(screenshot_id, href = req.href.screenshots(), title = content, class_ = 'missing') elif name == 'ScreenshotsList': # Check permission. if not req.perm.has_permission('SCREENSHOTS_VIEW'): return html.div('No permissions to see screenshots.', class_ = 'system-message') # Get desired list item description list_item_description = content or self.default_list_item # Get all screenshots. screenshots = api.get_screenshots_complete(cursor) # Create and return HTML list of screenshots. list_items = [] for screenshot in screenshots: list_item = self._format_description(list_item_description, screenshot) list_items.append(html.li(html.a(list_item, href = req.href.screenshots(screenshot['id'])))) return html.ul(list_items)
def make_png_element(self, url, **kwargs): kwargs['src'] = url return html.img(**kwargs)
def generate_captcha(self, req): challenge = ''.join(random.choice(self.alphabet) for i in xrange(self.letters)) return challenge, html.img(src=req.href('/captcha/image'), width='33%', alt='captcha')