def filename(self): """Calculates unique wrapper filename. It is based on template and DATA_DIR settings. """ digest = calculate_checksum(self.SSH_WRAPPER_TEMPLATE, data_dir("ssh")) return ssh_file(f"ssh-weblate-wrapper-{digest}")
def get_cache_key(scope, request=None, address=None, user=None): """Generate cache key for request.""" if (request and request.user.is_authenticated) or user: if user: key = user.id else: key = request.user.id origin = "user" else: if address is None: address = get_ip_address(request) origin = "ip" key = calculate_checksum(address) return f"ratelimit-{origin}-{scope}-{key}"
def cdn_parse_html(files: str, selector: str, component_id: int): component = Component.objects.get(pk=component_id) source_translation = component.source_translation source_units = set( source_translation.unit_set.values_list("source", flat=True)) units = [] errors = [] for filename in files.splitlines(): filename = filename.strip() try: if filename.startswith("http://") or filename.startswith( "https://"): with request("get", filename) as handle: content = handle.read() else: with open(os.path.join(component.full_path, filename), "r") as handle: content = handle.read() except IOError as error: errors.append({"filename": filename, "error": str(error)}) continue document = html.fromstring(content) for element in document.cssselect(selector): text = element.text if (element.getchildren() or not text or text in source_units or text in units): continue units.append(text) # Actually create units if units: source_translation.new_unit( request=None, key=None, value=None, batch={calculate_checksum(text): text for text in units}, ) if errors: component.add_alert("CDNAddonError", occurrences=errors) else: component.delete_alert("CDNAddonError")
def test_calculate_checksum(self): self.assertEqual(calculate_checksum("Message"), "f5351ff85ab23173")
def get_hash(cls, request) -> Optional[str]: css = cls.get_css(request) if not css: return None return calculate_checksum(css)
def digest(self): return calculate_checksum(self.get_content())
def digest(self): return calculate_checksum(SSH_WRAPPER_TEMPLATE, data_dir("ssh"))