def view_deed( request, license_code, version, jurisdiction=None, language_code=None ): legalcode = get_object_or_404( LegalCode, deed_url=request.path, ) license = legalcode.license language_code = legalcode.language_code # CC language code languages_and_links = get_languages_and_links_for_legalcodes( license.legal_codes.all(), language_code, "deed" ) if license.license_code == "CC0": body_template = "includes/deed_cc0_body.html" elif license.version == "4.0": body_template = "includes/deed_40_body.html" else: # Default to 4.0 - or maybe we should just fail? body_template = "includes/deed_40_body.html" translation = legalcode.get_translation_object() with active_translation(translation): return render( request, "deed.html", { "additional_classes": "", "body_template": body_template, "fat_code": legalcode.license.fat_code(), "languages_and_links": languages_and_links, "legalcode": legalcode, "license": legalcode.license, }, )
def get_metadata(self): """ Return a dictionary with the metadata for this license. """ data = { "license_code": self.license_code, "version": self.version, "title_english": self.title_english, } if self.jurisdiction_code: data["jurisdiction"] = self.jurisdiction_code data["permits_derivative_works"] = self.permits_derivative_works data["permits_reproduction"] = self.permits_reproduction data["permits_distribution"] = self.permits_distribution data["permits_sharing"] = self.permits_sharing data["requires_share_alike"] = self.requires_share_alike data["requires_notice"] = self.requires_notice data["requires_attribution"] = self.requires_attribution data["requires_source_code"] = self.requires_source_code data["prohibits_commercial_use"] = self.prohibits_commercial_use data[ "prohibits_high_income_nation_use"] = self.prohibits_high_income_nation_use data["translations"] = {} for lc in self.legal_codes.order_by("language_code"): language_code = lc.language_code with active_translation(lc.get_translation_object()): data["translations"][language_code] = { "license": lc.license_url, "deed": lc.deed_url, "title": gettext(self.title_english), } return data
def view_license( request, license_code, version, jurisdiction=None, language_code=None, # CC language code is_plain_text=False, ): if is_plain_text: legalcode = get_object_or_404( LegalCode, plain_text_url=request.path, ) else: legalcode = get_object_or_404( LegalCode, license_url=request.path, ) language_code = legalcode.language_code # CC language code languages_and_links = get_languages_and_links_for_legalcodes( legalcode.license.legal_codes.all(), language_code, "license" ) kwargs = dict( template_name="legalcode_page.html", context={ "fat_code": legalcode.license.fat_code(), "languages_and_links": languages_and_links, "legalcode": legalcode, "license": legalcode.license, }, ) translation = legalcode.get_translation_object() with active_translation(translation): if is_plain_text: response = HttpResponse(content_type='text/plain; charset="utf-8"') html = render_to_string(**kwargs) soup = BeautifulSoup(html, "lxml") plain_text_soup = soup.find(id="plain-text-marker") # FIXME: prune the "img" tags from this before saving again. with tempfile.NamedTemporaryFile(mode="w+b") as temp: temp.write(plain_text_soup.encode("utf-8")) output = subprocess.run( [ "pandoc", "-f", "html", temp.name, "-t", "plain", ], encoding="utf-8", capture_output=True, ) response.write(output.stdout) return response return render(request, **kwargs)
def test_active_translation(self): # Third-party from django.utils.translation.trans_real import _active translation_object = getattr(_active, "value", None) with active_translation(translation_object): self.assertEqual( getattr(_active, "value", None), translation_object, ) del _active.value self.assertEqual(getattr(_active, "value", None), None) with active_translation(translation_object): self.assertEqual( getattr(_active, "value", None), translation_object, )