def get_text(self, sha=None): if self.chapter.tutorial: tutorial = self.chapter.tutorial else: tutorial = self.chapter.part.tutorial repo = Repo(tutorial.get_path()) # find hash code if sha is None: sha = tutorial.sha_draft manifest = get_blob(repo.commit(sha).tree, "manifest.json") tutorial_version = json_reader.loads(manifest) if "parts" in tutorial_version: for part in tutorial_version["parts"]: if "chapters" in part: for chapter in part["chapters"]: if "extracts" in chapter: for extract in chapter["extracts"]: if extract["pk"] == self.pk: path_ext = extract["text"] break if "chapter" in tutorial_version: chapter = tutorial_version["chapter"] if "extracts" in chapter: for extract in chapter["extracts"]: if extract["pk"] == self.pk: path_ext = extract["text"] break if path_ext: return get_blob(repo.commit(sha).tree, path_ext) else: return None
def get_conclusion(self, sha=None): # find hash code if sha is None: sha = self.sha_draft repo = Repo(self.get_path()) manifest = get_blob(repo.commit(sha).tree, "manifest.json") tutorial_version = json_reader.loads(manifest) if "introduction" in tutorial_version: path_tuto = tutorial_version["conclusion"] if path_tuto: return get_blob(repo.commit(sha).tree, path_tuto)
def load_version(self, sha=None, public=None): """Using git, load a specific version of the content. if ``sha`` is ``None``, the draft/public version is used (if ``public`` is ``True``). .. attention:: for practical reason, the returned object is filled with information from DB. :param sha: version :param public: if set with the right object, return the public version :type public: PublishedContent :raise BadObject: if sha is not None and related version could not be found :raise IOError: if the path to the repository is wrong :raise NotAPublicVersion: if the sha does not correspond to a public version :return: the versioned content :rtype: zds.tutorialv2.models.models_versioned.VersionedContent """ # load the good manifest.json if sha is None: if not public: sha = self.sha_draft else: sha = self.sha_public max_title_length = PublishableContent._meta.get_field('title').max_length if public and isinstance(public, PublishedContent): # use the public (altered and not versioned) repository path = public.get_prod_path() slug = public.content_public_slug if not os.path.isdir(path): raise IOError(path) if sha != public.sha_public: raise NotAPublicVersion manifest = open(os.path.join(path, 'manifest.json'), 'r') json = json_reader.loads(manifest.read()) versioned = get_content_from_json(json, public.sha_public, slug, public=True, max_title_len=max_title_length) else: # draft version, use the repository (slower, but allows manipulation) path = self.get_repo_path() slug = self.slug if not os.path.isdir(path): raise IOError(path) repo = Repo(path) data = get_blob(repo.commit(sha).tree, 'manifest.json') try: json = json_reader.loads(data) except ValueError: raise BadManifestError( _(u'Une erreur est survenue lors de la lecture du manifest.json, est-ce du JSON ?')) versioned = get_content_from_json(json, sha, self.slug, max_title_len=max_title_length) self.insert_data_in_versioned(versioned) return versioned
def load_json_for_public(self, sha=None): if sha is None: sha = self.sha_public repo = Repo(self.get_path()) mantuto = get_blob(repo.commit(sha).tree, 'manifest.json') data = json_reader.loads(mantuto) return data
def load_json_for_public(self, sha=None): if sha is None: sha = self.sha_public repo = Repo(self.get_path()) mantuto = get_blob(repo.commit(sha).tree, 'manifest.json') data = json_reader.loads(mantuto) if 'licence' in data: data['licence'] = Licence.objects.filter( code=data['licence']).first() return data
def get_conclusion(self, sha=None): tutorial = self.tutorial # find hash code if sha is None: sha = tutorial.sha_draft repo = Repo(tutorial.get_path()) manifest = get_blob(repo.commit(sha).tree, "manifest.json") tutorial_version = json_reader.loads(manifest) if "parts" in tutorial_version: for part in tutorial_version["parts"]: if part["pk"] == self.pk: path_part = part["conclusion"] break if path_part: return get_blob(repo.commit(sha).tree, path_part) else: return None
def load_json_for_public(self): repo = Repo(self.get_path()) mantuto = get_blob(repo.commit(self.sha_public).tree, 'manifest.json') data = json_reader.loads(mantuto) return data
def load_version(self, sha=None, public=None): """Using git, load a specific version of the content. if ``sha`` is ``None``, the draft/public version is used (if ``public`` is ``True``). .. attention:: for practical reason, the returned object is filled with information from DB. :param sha: version :param public: if set with the right object, return the public version :type public: PublishedContent :raise BadObject: if sha is not None and related version could not be found :raise OSError: if the path to the repository is wrong :raise NotAPublicVersion: if the sha does not correspond to a public version :return: the versioned content :rtype: zds.tutorialv2.models.versioned.VersionedContent """ # load the good manifest.json if sha is None: if not public: sha = self.sha_draft else: sha = self.sha_public max_title_length = PublishableContent._meta.get_field('title').max_length if public and isinstance(public, PublishedContent): # use the public (altered and not versioned) repository path = public.get_prod_path() slug = public.content_public_slug if not os.path.isdir(path): raise OSError(path) if sha != public.sha_public: raise NotAPublicVersion with open(os.path.join(path, 'manifest.json'), 'r', encoding='utf-8') as manifest: json = json_handler.loads(manifest.read()) versioned = get_content_from_json( json, public.sha_public, slug, public=True, max_title_len=max_title_length, hint_licence=self.licence, ) else: # draft version, use the repository (slower, but allows manipulation) path = self.get_repo_path() if not os.path.isdir(path): raise OSError(path) repo = Repo(path) data = get_blob(repo.commit(sha).tree, 'manifest.json') try: json = json_handler.loads(data) except ValueError: raise BadManifestError( _('Une erreur est survenue lors de la lecture du manifest.json, est-ce du JSON ?')) versioned = get_content_from_json(json, sha, self.slug, max_title_len=max_title_length) self.insert_data_in_versioned(versioned) return versioned