def open(self, document_id): print('Websocket opened') current_user = self.get_current_user() self.user_info = SessionUserInfo() doc_db, can_access = self.user_info.init_access( document_id, current_user) if can_access: if doc_db.id in DocumentWS.sessions: self.doc = DocumentWS.sessions[doc_db.id] self.id = max(self.doc['participants']) + 1 print "id when opened %s" % self.id else: self.id = 0 self.doc = dict() self.doc['db'] = doc_db self.doc['participants'] = dict() self.doc['last_diffs'] = json_decode(doc_db.last_diffs) self.doc['comments'] = json_decode(doc_db.comments) self.doc['settings'] = json_decode(doc_db.settings) self.doc['contents'] = json_decode(doc_db.contents) self.doc['metadata'] = json_decode(doc_db.metadata) self.doc['version'] = doc_db.version self.doc['diff_version'] = doc_db.diff_version self.doc['comment_version'] = doc_db.comment_version self.doc['title'] = doc_db.title self.doc['id'] = doc_db.id DocumentWS.sessions[doc_db.id] = self.doc self.doc['participants'][self.id] = self response = dict() response['type'] = 'welcome' self.write_message(response)
def subscribe_doc(self, connection_count=0): self.user_info = SessionUserInfo(self.user) doc_db, can_access = self.user_info.init_access( self.document_id ) if not can_access or float(doc_db.doc_version) != FW_DOCUMENT_VERSION: self.access_denied() return if ( doc_db.id in WebSocket.sessions and len(WebSocket.sessions[doc_db.id]['participants']) > 0 ): logger.debug( f"Action:Serving already opened document. " f"URL:{self.endpoint} User:{self.user.id} " f" ParticipantID:{self.id}") self.doc = WebSocket.sessions[doc_db.id] self.id = max(self.doc['participants']) + 1 self.doc['participants'][self.id] = self else: logger.debug( f"Action:Opening document from DB. " f"URL:{self.endpoint} User:{self.user.id} " f"ParticipantID:{self.id}") self.id = 0 self.doc = { 'db': doc_db, 'participants': { 0: self }, 'last_diffs': json_decode(doc_db.last_diffs), 'comments': json_decode(doc_db.comments), 'bibliography': json_decode(doc_db.bibliography), 'contents': json_decode(doc_db.contents), 'version': doc_db.version, 'title': doc_db.title, 'id': doc_db.id, 'template': { 'id': doc_db.template.id, 'definition': json_decode(doc_db.template.definition) } } WebSocket.sessions[doc_db.id] = self.doc logger.debug( f"Action:Participant ID Assigned. URL:{self.endpoint} " f"User:{self.user.id} ParticipantID:{self.id}") self.send_message({ 'type': 'subscribed' }) if connection_count < 1: self.send_styles() self.send_document() if self.can_communicate(): self.handle_participant_update()
def open(self, arg): logger.debug('Websocket opened') response = dict() current_user = self.get_current_user() args = arg.split("/") self.messages = {'server': 0, 'client': 0, 'last_ten': []} if len(args) < 3 or current_user is None: response['type'] = 'access_denied' self.id = 0 self.send_message(response) IOLoop.current().add_callback(self.do_close) return document_id = int(args[0]) connection_count = int(args[1]) self.user_info = SessionUserInfo() doc_db, can_access = self.user_info.init_access( document_id, current_user) if not can_access or doc_db.doc_version != FW_DOCUMENT_VERSION: response['type'] = 'access_denied' self.id = 0 self.send_message(response) return response['type'] = 'welcome' if (doc_db.id in WebSocket.sessions and len(WebSocket.sessions[doc_db.id]['participants']) > 0): logger.debug("Serving already opened file") self.doc = WebSocket.sessions[doc_db.id] self.id = max(self.doc['participants']) + 1 self.doc['participants'][self.id] = self logger.debug("id when opened %s" % self.id) else: logger.debug("Opening file") self.id = 0 self.doc = { 'db': doc_db, 'participants': { 0: self }, 'last_diffs': json_decode(doc_db.last_diffs), 'comments': json_decode(doc_db.comments), 'bibliography': json_decode(doc_db.bibliography), 'contents': json_decode(doc_db.contents), 'version': doc_db.version, 'title': doc_db.title, 'id': doc_db.id } WebSocket.sessions[doc_db.id] = self.doc serializer = PythonWithURLSerializer() export_temps = serializer.serialize(ExportTemplate.objects.all()) document_styles = serializer.serialize(DocumentStyle.objects.all(), use_natural_foreign_keys=True) cite_styles = serializer.serialize(CitationStyle.objects.all()) cite_locales = serializer.serialize(CitationLocale.objects.all()) response['styles'] = { 'export_templates': [obj['fields'] for obj in export_temps], 'document_styles': [obj['fields'] for obj in document_styles], 'citation_styles': [obj['fields'] for obj in cite_styles], 'citation_locales': [obj['fields'] for obj in cite_locales], } self.send_message(response) if connection_count < 1: self.send_document() if self.can_communicate(): self.handle_participant_update()