コード例 #1
0
 def create_new_concept(self, phrasing_text, parent=None, attribute=None):
     phr_perm = Permission(permissions=Permission.init_perm_struct(
         Phrasing.operations_list),
                           key=Permission.create_key(),
                           project=self.project.key)
     con_perm = Permission(permissions=Permission.init_perm_struct(
         Concept.operations_list),
                           key=Permission.create_key(),
                           project=self.project.key)
     phrasing = Phrasing(key=Phrasing.create_key(),
                         text=phrasing_text,
                         owner=[self.user.key],
                         permissions=phr_perm.key,
                         originating_document=self.document.key,
                         project=self.project.key)
     crawlcontext = CrawlContext(key=CrawlContext.create_key(),
                                 project=self.project.key,
                                 document=self.document.key,
                                 crawl=True)
     concept = Concept(key=Concept.create_key(),
                       owner=[self.user.key],
                       parent=parent.key if parent else None,
                       project=self.project.key,
                       distilled_phrasing=phrasing.key,
                       phrasings=[phrasing.key],
                       crawlcontext=[crawlcontext.key],
                       permissions=con_perm.key)
     self.last_concept = concept
     crawlcontext.concept = concept.key
     if attribute:
         concept.attributes = [attribute.key]
         attribute.concept = concept.key
     if parent:
         parent.children.append(concept.key)
         concept.parent_perms = parent.parent_perms
         concept.parent_perms.append(parent.permissions)
     else:
         self.project.children.append(concept.key)
         concept.parent_perms.append(self.project.permissions)
     phr_perm.artifact = phrasing.key
     con_perm.artifact = concept.key
     phrasing.concept = concept.key
     if self.user.in_org():
         phrasing.organization = self.user.organization
         concept.organization = self.user.organization
     self.concept_array[concept.key.id()] = concept
     self.crawlcontext_array.append(crawlcontext)
     self.phrasing_array.append(phrasing)
     self.prems_array += [phr_perm, con_perm]
     return concept
コード例 #2
0
    def restore_concept_from_json_v2(self, project, concepts, id_mapping,
                                     request_user, model_array, parent,
                                     documents, zip_file):
        concepts_key_array = []
        for con_json in concepts:
            con_json['distilled_phrasing_text'] = con_json[
                'distilled_phrasing']['text']
            phr_perm = Permission(permissions=Permission.init_perm_struct(
                Phrasing.operations_list),
                                  key=Permission.create_key(),
                                  project=project.key)
            con_perm = Permission(permissions=Permission.init_perm_struct(
                Concept.operations_list),
                                  key=Permission.create_key(),
                                  project=project.key)
            distilled_phrasing = Phrasing(
                key=Phrasing.create_key(),
                text=con_json['distilled_phrasing']['text'],
                owner=[request_user.key],
                permissions=phr_perm.key,
                originating_document=project.distilled_document,
                project=project.key)
            concept = Concept(key=Concept.create_key(),
                              owner=[request_user.key],
                              parent=parent.key if parent else None,
                              project=project.key,
                              distilled_phrasing=distilled_phrasing.key,
                              phrasings=[distilled_phrasing.key],
                              permissions=con_perm.key)

            if parent:
                parent.children.append(concept.key)
                concept.parent_perms = parent.parent_perms
                concept.parent_perms.append(parent.permissions)
            else:
                project.children.append(concept.key)
                concept.parent_perms.append(project.permissions)

            phr_perm.artifact = distilled_phrasing.key
            con_perm.artifact = concept.key
            distilled_phrasing.concept = concept.key

            if request_user.in_org():
                distilled_phrasing.organization = request_user.organization
                concept.organization = request_user.organization

            model_array += [phr_perm, con_perm, distilled_phrasing, concept]
            id_mapping[con_json['id']] = concept.key.id()
            id_mapping[con_json['distilled_phrasing']
                       ['id']] = distilled_phrasing.key.id()

            if con_json['id'] == "c3598ccc0b5511e4b7839bfd6cb32178":
                pass

            for attr in con_json['attributes']:
                try:
                    attribute = Attributes(
                        key=Attributes.create_key(),
                        project=project.key,
                        document=documents[id_mapping[attr['document']]].key,
                        attributes=attr['attributes'])
                    concept.attributes.append(attribute.key)
                    model_array.append(attribute)
                except KeyError:
                    continue

            for crawl in con_json['crawlcontexts']:
                try:
                    crawlcontext = CrawlContext(
                        key=CrawlContext.create_key(),
                        project=project.key,
                        document=documents[id_mapping[crawl['document']]].key,
                        crawl=crawl['crawl'])
                    concept.crawlcontext.append(crawlcontext.key)
                    model_array.append(crawlcontext)
                except KeyError:
                    continue

            new_phrasing_dict = {}
            for phrasing_dict in con_json['phrasings']:
                if phrasing_dict['id'] != con_json['distilled_phrasing']['id']:
                    try:
                        phr_perm = Permission(
                            permissions=Permission.init_perm_struct(
                                Phrasing.operations_list),
                            key=Permission.create_key(),
                            project=project.key)

                        phrasing = Phrasing(
                            key=Phrasing.create_key(),
                            concept=concept.key,
                            text=phrasing_dict['text'],
                            owner=[request_user.key],
                            permissions=phr_perm.key,
                            originating_document=project.distilled_document,
                            project=project.key)

                        phr_perm.artifact = phrasing.key
                        new_phrasing_dict[phrasing.key.id()] = phrasing
                        concept.phrasings.append(phrasing.key)
                        id_mapping[phrasing_dict['id']] = phrasing.key.id()
                        model_array.append(phrasing)
                        model_array.append(phr_perm)
                    except KeyError:
                        continue

                else:
                    new_phrasing_dict[
                        distilled_phrasing.key.id()] = distilled_phrasing

            for sel_phr in con_json['selected_phrasings']:
                try:
                    selected_phrasing = SelectedPhrasing(
                        key=SelectedPhrasing.create_key(),
                        project=project.key,
                        document=documents[id_mapping[
                            sel_phr['document']]].key,
                        phrasing=new_phrasing_dict[id_mapping[
                            sel_phr['phrsing']]].key)
                    concept.selected_phrasings.append(selected_phrasing.key)
                    model_array.append(selected_phrasing)
                except KeyError:
                    continue

            if 'is_media' in con_json.keys():
                if con_json['is_media']:
                    if 'image' in con_json['content_type']:
                        concept.media_id = server.create_uuid()
                        image = zip_file.open('images/' + con_json['id'], 'r')
                        filename = '/' + server.GCS_BUCKET_NAME + '/' + concept.media_id
                        f = gcs.open(filename,
                                     mode='w',
                                     content_type=con_json['content_type'])
                        f.write(image.read())
                        f.close()
                        concept.media_blob = blobstore.create_gs_key('/gs' +
                                                                     filename)

            if con_json['children'] is not None:
                self.restore_concept_from_json_v2(project,
                                                  con_json['children'],
                                                  id_mapping, request_user,
                                                  model_array, concept,
                                                  documents, zip_file)

            concepts_key_array.append(concept.key)

        return concepts_key_array