def create_new_project(self, title): doc_perm = Permission(key=Permission.create_key(), permissions=Permission.init_perm_struct( Document.operations_list)) pro_perm = Permission(key=Permission.create_key(), permissions=Permission.init_perm_struct( Project.operations_list)) self.document = Document(key=Document.create_key(), title='Outline', owner=[self.user.key], permissions=doc_perm.key, subtitle='Outline', author=self.user.username, version='v0.1', date=str(datetime.datetime.now().year), copyright_text='', description='') self.project = Project(key=Project.create_key(), title=title, distilled_document=self.document.key, permissions=pro_perm.key, owner=[self.user.key]) self.document.project = self.project.key self.document.parent_perms.append(pro_perm.key) if self.user.in_org(): self.document.organization = self.user.organization self.project.organization = self.user.organization doc_perm.artifact = self.document.key doc_perm.project = self.project.key pro_perm.artifact = self.project.key pro_perm.project = self.project.key ndb.put_multi([doc_perm, pro_perm, self.document, self.project])
def put(self, project_id=None): if self.json_request.get('title') is None: raise HttpErrorException.bad_request('invalid project title') if self.json_request.get('distilled_document') is None: raise HttpErrorException.bad_request('invalid document') distilled_document = self.json_request.get('distilled_document') if not distilled_document.get('title'): raise HttpErrorException.bad_request('invalid document title') doc_perm = Permission(permissions=Permission.init_perm_struct( Document.operations_list), key=Permission.create_key()) doc = Document( key=Document.create_key(), title=distilled_document.get('title'), subtitle=distilled_document.get('subtitle'), author=distilled_document.get('author', self.user.full_name), version=distilled_document.get('version', 'v0.1'), date=distilled_document.get('date', datetime.datetime.now().year), copyright_text=distilled_document.get('copyright', ''), description=distilled_document.get('description', ''), owner=[self.user.key], permissions=doc_perm.key, ) doc_perm.artifact = doc.key pro_perm = Permission(permissions=Permission.init_perm_struct( Project.operations_list), key=Permission.create_key()) pro = Project( key=Project.create_key(), title=self.json_request.get('title'), distilled_document=doc.key, permissions=pro_perm.key, owner=[self.user.key], ) pro_perm.artifact = pro.key pro_perm.project = pro.key doc_perm.project = pro.key doc.project = pro.key doc.parent_perms.append(pro_perm.key) if self.user.in_org(): doc.organization = self.user.organization pro.organization = self.user.organization ndb.put_multi([doc_perm, doc, pro_perm, pro]) index = self.user.get_put_index() doc.index(index) pro.index(index) self.write_json_response(pro.to_dict(self.user))
def restore_v1(self, project_json, request_user, zip_file): doc_perm = Permission(key=Permission.create_key(), permissions=Permission.init_perm_struct( Document.operations_list)) pro_perm = Permission(key=Permission.create_key(), permissions=Permission.init_perm_struct( Project.operations_list)) document = Document( key=Document.create_key(), title=project_json['distilled_document']['title'], author=project_json['distilled_document']['author'], subtitle=project_json['distilled_document']['subtitle'], date=project_json['distilled_document']['date'], copyright_text=project_json['distilled_document'] ['copyright_text'], description=project_json['distilled_document']['description'], owner=[request_user.key], permissions=doc_perm.key) project = Project(key=Project.create_key(), title=project_json['title'], distilled_document=document.key, permissions=pro_perm.key, owner=[request_user.key]) document.project = project.key document.parent_perms.append(pro_perm.key) if request_user.in_org(): document.organization = request_user.organization project.organization = request_user.organization doc_perm.artifact = document.key doc_perm.project = project.key pro_perm.artifact = project.key pro_perm.project = project.key ndb.put_multi([doc_perm, pro_perm, document, project]) # We do not want to re-use the old project, document, concept, ... ids so we will create a mapping # from old to new :) id_mapping = { project_json['id']: project.key.id(), project_json['distilled_document']['id']: project.distilled_document.id() } documents = { project.distilled_document.id(): project.distilled_document.get() } for doc_json in project_json['documents']: doc_json['project_id'] = project.key.id() doc_perm = Permission(key=Permission.create_key(), project=project.key, permissions=Permission.init_perm_struct( Document.operations_list)) doc_obj = Document(key=Document.create_key(), title=doc_json['title'], author=doc_json['author'], subtitle=doc_json['subtitle'], date=doc_json['date'], copyright_text=doc_json['copyright_text'], description=doc_json['description'], owner=[request_user.key], permissions=doc_perm.key, project=project.key) doc_perm.artifact = doc_obj.key doc_obj.put() doc_perm.put() project.documents.append(doc_obj.key) id_mapping[doc_json['id']] = doc_obj.key.id() documents[doc_obj.key.id()] = doc_obj project.put() model_array = [] project.children = self.restore_concept_from_json_v1( project, project_json['concepts'], id_mapping, request_user, model_array, None, documents, zip_file) ndb.put_multi(model_array) Phrasing.index_multi(model_array, request_user) project.put() return project