def _serve_page(self): self._create_analytic_session() self.project.record_analytic('pro_opn', self.analytic_session) doc = None if self.request.get('doc') != '': doc = Document.get_by_id(self.request.get('doc')) if not doc: raise HttpErrorException.bad_request('invalid doc id given') if doc.project != self.project.key: raise HttpErrorException.bad_request('invalid doc id given') if self.request.get('active_concept') != '': act_con_path = self.request.get('active_concept') elif self.request.get('act_con') != '': act_con_path = self.request.get('act_con') else: act_con_path = None if self.request.get('debug').lower() == 'true': self.client_debug = True if self.request.get('check_auth').lower() == 'false': self.client_check_auth = False self._get_concept_loader_configs() if self.user.is_world_user(): self._serve_worldshare_page(doc, act_con_path) else: self._serve_project_page(doc, act_con_path)
def post(self, concept_id=None): if not concept_id and not Concept.valid_id(concept_id): raise HttpErrorException.bad_request('invalid concept id') concept = Concept.get_by_id(concept_id) if not concept: raise HttpErrorException.bad_request('invalid concept id') if not self.json_request.get('document_id') and not Document.valid_id( self.json_request.get('document_id')): raise HttpErrorException.bad_request('invalid document id') document = Document.get_by_id(self.json_request.get('document_id')) if not document: raise HttpErrorException.bad_request('invalid document id') if not document.has_permission_set_crawlcontext(self.user): raise HttpErrorException.forbidden() if document.presentation_document is None: raise HttpErrorException.bad_request('document has not summary') presentation_document = document.presentation_document.get() if document.presentation_document is None: raise HttpErrorException.bad_request('document has not summary') project = document.project.get() if not isinstance(self.json_request.get('crawl'), bool): raise HttpErrorException.bad_request('invalid crawl') temp = self.json_request.get('temp', None) if temp is None: raise HttpErrorException.bad_request('no temp giving') crawlcontexts = ndb.get_multi(concept.presentation_crawlcontext) if temp: for crawl in crawlcontexts: if crawl.document == presentation_document.key: concept.presentation_crawlcontext.remove(crawl.key) concept.put() crawl.key.delete() break else: for crawl in crawlcontexts: if crawl.document == presentation_document.key: crawl.crawl = self.json_request.get('crawl') crawl.put() break else: crawl = PresentationCrawlContext( key=PresentationCrawlContext.create_key(), project=project.key, document=presentation_document.key, crawl=self.json_request.get('crawl')) crawl.put() concept.presentation_crawlcontext.append(crawl.key) concept.put() project.pw_modified_ts = datetime.datetime.now() project.put()
def get_publish(self, document, group): document = Document.get_by_id(document) if not document: raise HttpErrorException.bad_request('invalid document id') if not document.has_permission_read(self.user): raise HttpErrorException.forbidden() group = Group.get_by_id(group) if not group: raise HttpErrorException.bad_request('invalid group id') self.project = document.project.get() version = self.request.get('v', 'latest') pub = PublishDocument.get(document, group, version) if not pub: raise HttpErrorException.not_found() if pub.group not in self.user.groups and pub.group != Group.get_worldshare_key( ): raise HttpErrorException.forbidden() self._create_analytic_session() self.project.record_analytic('pro_opn', self.analytic_session) template_index = JINJA_ENVIRONMENT.get_template('document_public.html') return template_index.render({ 'title': self.project.title, 'version': pub.version, 'created_at': pub.created_ts, 'published_to': pub.group.get().name, 'an_token': self.analytic_session.key.id(), 'project_id': self.project.id, 'html': pub.html, })
def get(self, document='none'): document = Document.get_by_id(document) if not document: raise HttpErrorException.bad_request('invalid document id') if not document.has_permission_read(self.user): raise HttpErrorException.forbidden() self.write_json_response(document.to_dict(self.user))
def post(self): doc = Document.get_by_id(self.json_request.get('document', 'none')) if not doc: raise HttpErrorException.bad_request('invalid document id') if not doc.has_permission_write(self.user): raise HttpErrorException.forbidden() if not doc.summary_document: raise HttpErrorException.bad_request('no summary document') sum_doc = doc.summary_document.get() if not sum_doc: raise HttpErrorException.bad_request('no summary document') word_count = self.json_request.get('word_count') try: word_count = int(word_count) except (ValueError, TypeError): raise HttpErrorException.bad_request('word_count must be int') if word_count < 100 or word_count > 2500 or word_count % 50 != 0: raise HttpErrorException.bad_request( 'word_count must be 100 to 2500 in 50 inc') sum_doc.word_count = word_count sum_doc.put()
def post(self, phrasing_id=None): if not phrasing_id: raise HttpErrorException.bad_request('no phrasing_id given') phrasing = Phrasing.get_by_id(phrasing_id) if not phrasing: raise HttpErrorException.bad_request('invalid phrasing_id given') if not self.json_request.get('document'): raise HttpErrorException.bad_request('no document given') document = Document.get_by_id(self.json_request.get('document')) if not document: raise HttpErrorException.bad_request('invalid document given') if not document.has_permission(self.user, 'manage_phrasings'): raise HttpErrorException.forbidden() if not document.presentation_document: raise HttpErrorException.bad_request( 'document does not have a presentation') sum_doc = document.presentation_document.get() if not sum_doc: raise HttpErrorException.bad_request( 'document does not have a presentation') concept = phrasing.concept.get() if concept.has_presentation_selected_phrasing(document=sum_doc): selected_phrasing = concept.get_presentation_selected_phrasing( sum_doc) selected_phrasing.phrasing = phrasing.key selected_phrasing.put() else: selected_phrasing = PresentationSelectedPhrasing( id=PresentationSelectedPhrasing.create_uuid(), project=concept.project, document=sum_doc.key, phrasing=phrasing.key) selected_phrasing.put() concept.presentation_selected_phrasings.append( selected_phrasing.key) concept.put() project = document.project.get( ) # Don't want concept's project, this could be a linked concept project.pw_modified_ts = datetime.datetime.now() project.put() self.get_analytic_session() concept.record_analytic('con_phr_cha', self.analytic_session) if selected_phrasing: self.write_json_response(selected_phrasing.to_dict())
def get(self): if self.json_request.get('project') is None: raise HttpErrorException.bad_request('invalid project id') project = Project.get_by_id(self.json_request.get('project')) if not project: raise HttpErrorException.bad_request('invalid project id') if self.json_request.get('document') is None: raise HttpErrorException.bad_request('invalid document id') document = Document.get_by_id(self.json_request.get('document')) if not document: raise HttpErrorException.bad_request('invalid document id') if not document.has_permission_read(self.user): raise HttpErrorException.forbidden() try: limit = int(self.request.get('limit', 30)) except ValueError: raise HttpErrorException.bad_request('limit must be int') try: before_date = int(self.request.get('before_date', 0)) except ValueError: raise HttpErrorException.bad_request('before_date must be int') if limit > 100: limit = 100 if before_date and before_date > 0: before_date = datetime.fromtimestamp(before_date / 1000.0) chat_query = ChatMessage.query( ndb.AND(ChatMessage.project == project.key, ChatMessage.document == document.key, ChatMessage.created_ts < before_date)) else: chat_query = ChatMessage.query( ndb.AND(ChatMessage.project == project.key, ChatMessage.document == document.key)) chat_messages = chat_query.order( ChatMessage.created_ts).fetch(limit=limit) chat_messages_dict = [] for chat in chat_messages: chat_messages_dict.append(chat.to_dict()) self.write_json_response(chat_messages_dict)
def get(self, selected_phrasing_id=None): selected_phrasing_dict = {} q = SelectedPhrasing.query() if self.request.get('project_id').strip() != '': q = q.filter(SelectedPhrasing.project == Project.get_by_id( self.request.get('project_id').strip()).key) if self.request.get('document_id').strip() != '': q = q.filter(SelectedPhrasing.document == Document.get_by_id( self.request.get('document_id').strip()).key) if self.request.get('phrasing_id').strip() != '': q = q.filter(SelectedPhrasing.phrasing == Phrasing.get_by_id( self.request.get('phrasing_id').strip()).key) for phrase in q.iter(): phrase_dict = phrase.to_dict() selected_phrasing_dict[phrase.key.id()] = phrase_dict self.write_json_response(selected_phrasing_dict)
def post(self): if self.json_request.get('project') is None: raise HttpErrorException.bad_request('invalid project id') project = Project.get_by_id(self.json_request.get('project')) if not project: raise HttpErrorException.bad_request('invalid project id') if self.json_request.get('document') is None: raise HttpErrorException.bad_request('invalid document id') document = Document.get_by_id(self.json_request.get('document')) if not document: raise HttpErrorException.bad_request('invalid document id') if not document.has_permission_read(self.user): raise HttpErrorException.forbidden() msg = self.json_request.get('msg') if not msg or msg.strip() == '': raise HttpErrorException.bad_request('no message given') chat = ChatMessage(key=ChatMessage.create_key(), project=project.key, document=document.key, user=self.user.key, message=msg) chat.put() self.get_channel_token() channel_tokens = ChannelToken.get_by_project_key( project.key, self.user_channel_token) channel_tokens = ChannelToken.remove_unauthorized_users( channel_tokens, [project, document]) message = { 'user': self.get_user_channel_data(), 'chat': chat.to_dict() } ChannelToken.broadcast_message(channel_tokens, message)
def delete(self, document_id=None): if not document_id and not Document.valid_id(document_id): raise HttpErrorException.bad_request('invalid document id') self.document = Document.get_by_id(document_id) if not self.document: raise HttpErrorException.bad_request('invalid document id') if not self.document.has_permission_write(self.user): raise HttpErrorException.forbidden() if self.document.is_distilled_document(): raise HttpErrorException.bad_request( 'can not delete distilled document') trans = Transaction(action='doc_del', user=self.user.key, artifact=self.document.key, project=self.document.project) trans.put() self.get_channel_token() channel_tokens = ChannelToken.get_by_project_key( self.document.project, self.user_channel_token) channel_tokens = ChannelToken.remove_unauthorized_users( channel_tokens, [self.document]) message = { 'user': self.get_user_channel_data(), 'transaction': trans.to_dict(self.user) } ChannelToken.broadcast_message(channel_tokens, message) self._delete_document() project = self.document.project.get() project.modified_ts = datetime.datetime.now() project.pw_modified_ts = datetime.datetime.now() project.put()
def put(self): pro = Project.get_by_id(self.json_request.get('project', 'none')) if not pro: raise HttpErrorException.bad_request('invalid project id') doc = Document.get_by_id(self.json_request.get('document', 'none')) if not doc: raise HttpErrorException.bad_request('invalid document id') if not doc.has_permission_write(self.user): raise HttpErrorException.forbidden() if doc.key != pro.distilled_document and doc.key not in pro.documents: raise HttpErrorException.bad_request( 'document does not belong to project') pres_doc = PresentationDocument(key=PresentationDocument.create_key(), project=pro.key, document=doc.key) doc.presentation_document = pres_doc.key pro.pw_modified_ts = datetime.datetime.now() ndb.put_multi([pres_doc, doc, pro]) self.write_json_response(pres_doc.to_dict())
def _add_attribute(self): r_attr = self.json_request.get('add_attribute') if not r_attr: raise ValueError('no attribute given') if not isinstance(r_attr, basestring): raise ValueError('attributes must be a string') if not self.json_request.get('document'): raise ValueError('no document id given') if not Document.valid_id(self.json_request.get('document')): raise HttpErrorException.bad_request('invalid document id') doc = Document.get_by_id(self.json_request.get('document')) if not doc.has_permission_write(self.user): raise HttpErrorException.forbidden() if not doc: raise ValueError('invalid document id given') attr = self.project.get_attr(doc.key) if attr: if r_attr == 'h' and 'noh' in attr.attributes: attr.attributes.remove('noh') if r_attr == 'noh' and 'h' in attr.attributes: attr.attributes.remove('h') if r_attr == 'ol' and 'nol' in attr.attributes: attr.attributes.remove('nol') if r_attr == 'ol' and 'ul' in attr.attributes: attr.attributes.remove('ul') if r_attr == 'ul' and 'nol' in attr.attributes: attr.attributes.remove('nol') if r_attr == 'il' and 'ol' in attr.attributes: attr.attributes.remove('ol') if r_attr == 'nol' and 'ol' in attr.attributes: attr.attributes.remove('ol') if r_attr == 'nol' and 'ul' in attr.attributes: attr.attributes.remove('ul') if r_attr not in attr.attributes: attr.attributes.append(r_attr) attr.put() else: attr = [r_attr] if r_attr != 'attr' else [] attr = Attributes(project=self.project.project, document=doc.key, attributes=attr) attr.put() self.project.attributes.append(attr.key) action_data = {'attribute': r_attr, 'document': doc.key.id()} trans = Transaction(action='pro_attr_add', user=self.user.key, artifact=self.project.key, project=self.project.key, action_data=action_data) trans.put() channel_tokens = ChannelToken.get_by_project_key( self.project.key, self.user_channel_token) channel_tokens = ChannelToken.remove_unauthorized_users( channel_tokens, [self.project, doc]) message = { 'user': self.get_user_channel_data(), 'transaction': trans.to_dict(self.user) } ChannelToken.broadcast_message(channel_tokens, message)
def post(self, document_id=None): self.get_channel_token() modify_ts = True if not document_id and not Document.valid_id(document_id): raise HttpErrorException.bad_request('invalid document id') self.document = Document.get_by_id(document_id) if not self.document: raise HttpErrorException.bad_request('invalid document id') self.project = self.document.project.get() if not self.document.has_permission_read(self.user): raise HttpErrorException.forbidden() if self.json_request.get('active_document'): modify_ts = False self._activate_document() else: if not self.document.has_permission_write(self.user): raise HttpErrorException.forbidden() if self.json_request.get('permission'): self._add_perm() values = {} values_changed = False if not self.json_request.get( 'permission') and self.json_request.get('group_id'): self._rm_perm() if self.json_request.get('remove_group'): self._remove_group() if self.json_request.get('title') is not None: self._set_title() values_changed = True values['title'] = self.json_request.get('title', '') if self.json_request.get('subtitle') is not None: self._set_subtitle() values_changed = True values['subtitle'] = self.json_request.get('subtitle', '') if self.json_request.get('author') is not None: self._set_author() values_changed = True values['author'] = self.json_request.get('author', '') if self.json_request.get('date') is not None: self._set_date() values_changed = True values['date'] = self.json_request.get('date', '') if self.json_request.get('version') is not None: self._set_version() values_changed = True values['version'] = self.json_request.get('version', '') if self.json_request.get('copyright') is not None: self._set_copyright() values_changed = True values['copyright'] = self.json_request.get('copyright', '') if self.json_request.get('description') is not None: self._set_description() values_changed = True values['description'] = self.json_request.get( 'description', '') if values_changed: action_data = {'values': values} trans = Transaction(action='doc_edit', user=self.user.key, artifact=self.document.key, project=self.document.project, action_data=action_data) trans.put() self.get_channel_token() channel_tokens = ChannelToken.get_by_project_key( self.document.project, self.user_channel_token) channel_tokens = ChannelToken.remove_unauthorized_users( channel_tokens, [self.document]) message = { 'user': self.get_user_channel_data(), 'transaction': trans.to_dict(self.user) } ChannelToken.broadcast_message(channel_tokens, message) if modify_ts: project = self.document.project.get() project.pw_modified_ts = datetime.datetime.now() project.put() self.document.put()
def post(self, concept_id=None): if not concept_id and not Concept.valid_id(concept_id): raise HttpErrorException.bad_request('invalid concept id') concept = Concept.get_by_id(concept_id) if not concept: raise HttpErrorException.bad_request('invalid concept id') if not self.json_request.get('document_id') and not Document.valid_id( self.json_request.get('document_id')): raise HttpErrorException.bad_request('invalid document id') document = Document.get_by_id(self.json_request.get('document_id')) if not document: raise HttpErrorException.bad_request('invalid document id') if not document.has_permission_set_crawlcontext(self.user): raise HttpErrorException.forbidden() project = document.project.get() if not isinstance(self.json_request.get('crawl'), bool): raise HttpErrorException.bad_request('invalid crawl') crawlcontexts = ndb.get_multi(concept.crawlcontext) for crawl in crawlcontexts: if crawl.document == document.key: crawl.crawl = self.json_request.get('crawl') crawl.put() crawlcontext = crawl break else: crawl = CrawlContext(key=CrawlContext.create_key(), project=project.key, document=document.key, crawl=self.json_request.get('crawl')) crawl.put() concept.crawlcontext.append(crawl.key) concept.put() crawlcontext = crawl project.pw_modified_ts = datetime.datetime.now() project.put() self.get_analytic_session() if crawl.crawl: concept.record_analytic('con_cc_t', self.analytic_session) else: concept.record_analytic('con_cc_f', self.analytic_session) action = 'con_hid' if self.json_request.get('crawl'): action = 'con_shw' action_data = { 'concept': concept.key.id(), 'document': document.key.id() } trans = Transaction(action=action, user=self.user.key, artifact=crawlcontext.key, project=project.key, document=document, action_data=action_data) trans.put() self.get_channel_token() channel_tokens = ChannelToken.get_by_project_key( project.key, self.user_channel_token) channel_tokens = ChannelToken.remove_unauthorized_users( channel_tokens, [concept, document]) message = { 'user': self.get_user_channel_data(), 'transaction': trans.to_dict(self.user) } ChannelToken.broadcast_message(channel_tokens, message)
def get(self, project, document, group): if not project: raise HttpErrorException.bad_request('invalid project given') project = Project.get_by_id(project) if not project: raise HttpErrorException.bad_request('invalid project given') if not project.has_permission_read(self.user): raise HttpErrorException.forbidden() if not document: raise HttpErrorException.bad_request('invalid document given') document = Document.get_by_id(document) if not document: raise HttpErrorException.bad_request('invalid document given') if not document.has_permission_read(self.user): raise HttpErrorException.forbidden() if not group: raise HttpErrorException.bad_request('invalid group given') group = Group.get_by_id(group) if not group: raise HttpErrorException.bad_request('invalid group given') temp_user = User() temp_user.groups = [group.key, Group.get_worldshare().key] org = self.user.organization if self.user.organization else None if org: temp_user.organization = org if not document.has_permission_read(temp_user): raise HttpErrorException.bad_request( 'Group does not have permission to read the document') if document.key not in project.documents and document.key != project.distilled_document: raise HttpErrorException.bad_request( 'document does not belong to project') pubs = document.get_published(group=group) version_int = PublishDocument.get_largest_version(pubs) if version_int is None: version_int = 1 else: version_int += 1 version = self.request.get('version', str(version_int)) if version == 'latest': raise HttpErrorException.bad_request('invalid version given') pubs = document.get_published(group=group) for pub in pubs: if pub.version == version: raise HttpErrorException.bad_request( 'version name already taken') publisher = DocumentPublisherThread() publisher.id = server.create_uuid() publisher.request = self publisher.project = project publisher.document = document publisher.group = group publisher.user = self.user publisher.version = version if publisher.is_lock(): raise HttpErrorException.bad_request('publisher already running') publisher.start() self.write_json_response({'id': publisher.id}) self.get_analytic_session() document.record_analytic('doc_publish', self.analytic_session, project=project.key)
def post(self, phrasing_id=None): if not phrasing_id: raise HttpErrorException.bad_request('no phrasing_id given') phrasing = Phrasing.get_by_id(phrasing_id) if not phrasing: raise HttpErrorException.bad_request('invalid phrasing_id given') if not self.json_request.get('document'): raise HttpErrorException.bad_request('no document given') document = Document.get_by_id(self.json_request.get('document')) if not document: raise HttpErrorException.bad_request('invalid document given') if not document.has_permission(self.user, 'manage_phrasings'): lr = tt_logging.construct_log( msg_short='User does not have manage_phrasing perm', log_type=tt_logging.SECURITY, request=self.request, artifact=document, request_user=self.user) log.info(lr['dict_msg']['msg'], extra=lr) raise HttpErrorException.forbidden() concept = phrasing.concept.get() project = concept.project.get() selected_phrasing = None if document.is_distilled_document( ) and project.key == document.project: concept.distilled_phrasing = phrasing.key concept.put() else: if concept.has_selected_phrasing(document=document): selected_phrasing = concept.get_selected_phrasing(document) selected_phrasing.phrasing = phrasing.key selected_phrasing.put() else: selected_phrasing = SelectedPhrasing(id=Phrasing.create_uuid(), project=concept.project, document=document.key, phrasing=phrasing.key) selected_phrasing.put() concept.selected_phrasings.append(selected_phrasing.key) concept.put() project = document.project.get( ) # Don't want concept's project, this could be a linked concept project.pw_modified_ts = datetime.datetime.now() project.put() self.get_analytic_session() concept.record_analytic('con_phr_cha', self.analytic_session) if selected_phrasing: self.write_json_response(selected_phrasing.to_dict()) action_data = {'document': document.key.id()} trans = Transaction(action='phr_chg', user=self.user.key, artifact=phrasing.key, project=project.key, action_data=action_data) trans.put() self.get_channel_token() channel_tokens = ChannelToken.get_by_project_key( project.key, self.user_channel_token) channel_tokens = ChannelToken.remove_unauthorized_users( channel_tokens, [concept, document, phrasing]) message = { 'user': self.get_user_channel_data(), 'transaction': trans.to_dict(self.user) } ChannelToken.broadcast_message(channel_tokens, message)
def get(self, project, document, group): if not project: raise HttpErrorException.bad_request('invalid project given') project = Project.get_by_id(project) if not project: raise HttpErrorException.bad_request('invalid project given') if not project.has_permission_read(self.user): raise HttpErrorException.forbidden() if not document: raise HttpErrorException.bad_request('invalid document given') document = Document.get_by_id(document) if not document: raise HttpErrorException.bad_request('invalid document given') if not document.has_permission_read(self.user): raise HttpErrorException.forbidden() if not group: raise HttpErrorException.bad_request('invalid group given') group = Group.get_by_id(group) if not group: raise HttpErrorException.bad_request('invalid group given') if document.key not in project.documents and document.key != project.distilled_document: raise HttpErrorException.bad_request( 'document does not belong to project') temp_user = User() temp_user.groups = [group.key, Group.get_worldshare().key] org = self.user.organization if self.user.organization else None if org: temp_user.organization = org if not document.has_permission_read(temp_user): raise HttpErrorException.bad_request( 'Group does not have permission to read the document') pubs = document.get_presentation_published(group=group) version_int = PublishDocument.get_largest_version(pubs) if version_int is None: version_int = 1 else: version_int += 1 version = self.request.get('version', str(version_int)) if version == 'latest': raise HttpErrorException.bad_request('invalid version given') for pub in pubs: if pub.version == version: raise HttpErrorException.bad_request( 'version name already taken') try: slide_count = int(self.request.get('slide_count', 15)) except ValueError: raise HttpErrorException.bad_request( 'invalid slide count, must be integer') if slide_count < 1: raise HttpErrorException.bad_request( 'invalid slide_count given min 1') if slide_count > 100: raise HttpErrorException.bad_request( 'invalid slide_count given max 100') try: min_bullet = int(self.request.get('min_bullet', 4)) except ValueError: raise HttpErrorException.bad_request( 'invalid min bullet, must be integer') if min_bullet < 1: raise HttpErrorException.bad_request( 'invalid min_bullet given min 1') if min_bullet > 15: raise HttpErrorException.bad_request( 'invalid min_bullet given max 15') try: max_bullet = int(self.request.get('max_bullet', 6)) except ValueError: raise HttpErrorException.bad_request( 'invalid max bullet, must be integer') if max_bullet < 1: raise HttpErrorException.bad_request( 'invalid max_bullet given min 1') if max_bullet > 15: raise HttpErrorException.bad_request( 'invalid max_bullet given max 15') if min_bullet > max_bullet: raise HttpErrorException.bad_request( 'min_bullet can not be greater than max_bullet') publisher = PresentationPublisherThread() publisher.id = server.create_uuid() publisher.request = self publisher.project = project publisher.document = document publisher.slide_count = slide_count publisher.min_bullet = min_bullet publisher.max_bullet = max_bullet publisher.group = group publisher.user = self.user publisher.version = version if publisher.is_lock(): raise HttpErrorException.bad_request('publisher already running') publisher.start() self.write_json_response({'id': publisher.id}) self.get_analytic_session() document.record_analytic('pres_publish', self.analytic_session, project=project.key)
def put(self, phrasing_id=None): self.get_analytic_session() phrasing_text = self.json_request.get('text') if not phrasing_text: raise HttpErrorException.bad_request('invalid phrasing text') if not self.json_request.get('concept') and not Concept.valid_id( self.json_request.get('concept')): raise HttpErrorException.bad_request('invalid concept id') concept = Concept.get_by_id(self.json_request.get('concept')) if not concept: raise HttpErrorException.bad_request('invalid concept id') if not self.json_request.get('document') and not Document.valid_id( self.json_request.get('document')): raise HttpErrorException.bad_request('invalid document id') document = Document.get_by_id(self.json_request.get('document')) if not document: raise HttpErrorException.bad_request('invalid document id') sum_doc = None if self.json_request.get('summary', False): if not document.summary_document: HttpErrorException.bad_request( 'no document does not have summary') sum_doc = document.summary_document.get() if not sum_doc: HttpErrorException.bad_request( 'no document does not have summary') pres_doc = None if self.json_request.get('presentation', False): if not document.presentation_document: HttpErrorException.bad_request( 'no document does not have presentation') pres_doc = document.presentation_document.get() if not pres_doc: HttpErrorException.bad_request( 'no document does not have presentation') phr_perm = Permission(permissions=Permission.init_perm_struct( Phrasing.operations_list), key=Permission.create_key(), project=document.project) phrasing = Phrasing(key=Phrasing.create_key(), text=phrasing_text, concept=concept.key, owner=[self.user.key], permissions=phr_perm.key, project=document.project) phr_perm.artifact = phrasing.key concept.phrasings.append(phrasing.key) sel_phr = None if (document.is_distilled_document() and document.project == concept.project and sum_doc is None and pres_doc is None): concept.distilled_phrasing = phrasing.key else: if sum_doc is not None: if concept.has_summary_selected_phrasing(document=sum_doc): sel_phr = concept.get_summary_selected_phrasing(sum_doc) sel_phr.phrasing = phrasing.key else: sel_phr = SummarySelectedPhrasing( key=SummarySelectedPhrasing.create_key(), project=concept.project, document=sum_doc.key, phrasing=phrasing.key) concept.summary_selected_phrasings.append(sel_phr.key) elif pres_doc is not None: if concept.has_presentation_selected_phrasing( document=pres_doc): sel_phr = concept.get_presentation_selected_phrasing( pres_doc) sel_phr.phrasing = phrasing.key else: sel_phr = PresentationSelectedPhrasing( key=PresentationSelectedPhrasing.create_key(), project=concept.project, document=pres_doc.key, phrasing=phrasing.key) concept.presentation_selected_phrasings.append(sel_phr.key) else: if concept.has_selected_phrasing(document=document): sel_phr = concept.get_selected_phrasing(document=document) sel_phr.phrasing = phrasing.key else: sel_phr = SelectedPhrasing( key=SelectedPhrasing.create_key(), document=document.key, phrasing=phrasing.key, project=document.project) concept.selected_phrasings.append(sel_phr.key) sel_phr.put() phrasing.originating_document = document.key project = phrasing.project.get() project.pw_modified_ts = datetime.datetime.now() ndb.put_multi([phr_perm, phrasing, concept, project]) index = self.user.get_put_index() phrasing.index(index, concept=concept) concept.record_analytic('con_phr_new', self.analytic_session) self.write_json_response({ 'phrasing': phrasing.to_dict(self.user), 'selected_phrasing': sel_phr.to_dict() if sel_phr else None }) action_data = { 'concept': concept.key.id(), 'document': document.key.id(), 'phrasing': phrasing.to_dict(self.user) } trans = Transaction(action='phr_new', user=self.user.key, artifact=phrasing.key, project=project.key, action_data=action_data) trans.put() self.get_channel_token() channel_tokens = ChannelToken.get_by_project_key( project.key, self.user_channel_token) channel_tokens = ChannelToken.remove_unauthorized_users( channel_tokens, [concept, phrasing, document]) message = { 'user': self.get_user_channel_data(), 'transaction': trans.to_dict(self.user) } ChannelToken.broadcast_message(channel_tokens, message)
def post(self): query_dict = self.json_request.get('query') if not query_dict: raise HttpErrorException.bad_request('no query given') if not query_dict['pro']: raise HttpErrorException.bad_request('no project id given') project = Project.get_by_id(query_dict['pro']) if not project: raise HttpErrorException.bad_request('invalid project id given') if not project.has_permission(self.user, 'read'): raise HttpErrorException.forbidden() index = project.get_indexes(create_new=False) self.get_analytic_session() query_string = 'pro=%s typ=(phr OR anno_reply) (reply=(%s) OR phr=(%s))' % ( project.key.id(), query_dict.get('string', ''), query_dict.get('string', '')) if index is not None and len(index) > 0: search_results = ttindex.ttsearch(index, query_string, limit=1000, use_cursor=False, user=self.user) concepts = [] concept_ids = [] while len(concepts) < 20 and search_results is not None: for sr in search_results: if sr['fields']['con'] not in concept_ids: concept_ids.append(sr['fields']['con']) if sr['fields']['typ'] == 'phr': concept = Concept.get_by_id(sr['fields']['con']) phrasing = Phrasing.get_by_id(sr['id']) if concept.has_permission_read( self.user ) and phrasing.has_permission_read(self.user): concepts.append({ 'con_id': sr['fields']['con'], 'phr_text': sr['fields']['phr'] }) elif sr['fields']['typ'] == 'anno_reply': concept = Concept.get_by_id(sr['fields']['con']) document = Document.get_by_id(sr['fields']['doc']) if concept.has_permission_read(self.user) and \ document.has_permission_read(self.user) and \ document.has_permission_annotation_read(self.user): concepts.append({ 'con_id': sr['fields']['con'], 'phr_text': sr['fields']['reply'] }) if len(concepts) >= 20: break else: search_results = ttindex.ttsearch(index, query_dict, limit=1000, user=self.user) self.write_json_response({'concepts': concepts}) else: self.write_json_response({'concepts': ''}) project.record_analytic('pro_search', self.analytic_session, meta_data={'sch_query': str(query_dict)})