def post(self, request, **kwargs): session_key = request.session.session_key dbdoc_id = kwargs['document_id'] controller = SharedVariables.get_dbd_controller(session_key, dbdoc_id) try: DBDPersistency.load(dbdoc_id, controller) except Exception as e: request.session['error'] = str(e) SocketUtil.notify(dbdoc_id, session_key=session_key, error=str(e)) return HttpResponse()
def upload(self, xmlcontent, safe=False): messages = [] try: if not safe: is_valid, messages = ValidationUtil.validate( ET.fromstring(xmlcontent)) else: is_valid = True except: return False, [ "The given string could not be converted into an xml." ] if is_valid: dbdoc = DBDoc(xmlcontent) SharedVariables.add_dbdoc(dbdoc, True) self.attached_id = dbdoc.get_id() self.dbdoc = dbdoc return True, "Document Uploaded." return False, messages
def post(self, request): # Create new document try: session_key = request.session.session_key dbd_controller = SharedVariables.add_dbd_controller( session_key, 'NEW') dbdoc_id = dbd_controller.get_id() return HttpResponseRedirect('/document/{}'.format(dbdoc_id)) except Exception as e: return render(request, 'docbook/index.html', { 'error': str(e), 'documents': self.get_dbdoc_list() })
def get(self, request, **kwargs): if not request.session.exists(request.session.session_key): return HttpResponseRedirect('/') session_key = request.session.session_key dbdoc_id = kwargs['document_id'] dbd_controller = SharedVariables.get_dbd_controller( session_key, dbdoc_id) if dbd_controller is None: request.session['error'] = None return HttpResponseRedirect('/') dbdoc = dbd_controller.dbdoc xml_content = str(dbdoc) form_insert_child = InsertChildForm() form_delete_element_by_id = DeleteElementByIdForm() form_set_name = SetNameForm() form_delete_element_attribute = DeleteElementAttributeForm() form_set_element_attr = SetElementAttributeForm() form_set_element_text = SetElementTextForm() form_insert_sibling = InsertSiblingForm() form_insert_text = InsertTextForm() form_save = SaveForm() form_upload = UploadForm() doc_name = dbdoc.get_name() try: error = request.session['error'] except KeyError: error = None request.session['error'] = None return render( request, 'docbook/document.html', { 'doc_name': doc_name, 'form_upload': form_upload, 'form_save': form_save, 'xml_content': xml_content, 'form_insert_child': form_insert_child, 'form_delete_element_by_id': form_delete_element_by_id, 'dbdoc_id': dbdoc.get_id(), 'form_set_name': form_set_name, 'form_delete_element_attribute': form_delete_element_attribute, 'form_set_element_attr': form_set_element_attr, 'form_set_element_text': form_set_element_text, 'form_insert_sibling': form_insert_sibling, 'form_insert_text': form_insert_text, 'error': error, 'session_key': session_key })
def get(self, request, **kwargs): dbdoc_id = kwargs['document_id'] session_key = request.session.session_key controller = SharedVariables.get_dbd_controller(session_key, dbdoc_id) doc_name = controller.get_name() if not session_key or controller is None: return HttpResponseRedirect('/') result_json = json.dumps(parse_xml_to_json(controller.dbdoc.content)) response = { 'xml': str(controller.dbdoc), 'doc_name': doc_name, 'json_xml': result_json } return HttpResponse(json.dumps(response), content_type='application/json')
def post(self, request, **kwargs): session_key = request.session.session_key form = SaveForm(request.POST) dbdoc_id = kwargs['document_id'] controller = SharedVariables.get_dbd_controller(session_key, dbdoc_id) if form.is_valid(): try: safe = form.cleaned_data['safe'] result, message = DBDPersistency.save(dbdoc_id, safe) except Exception as e: request.session['error'] = str(e) SocketUtil.notify(dbdoc_id, session_key=session_key, error=str(e)) return HttpResponse() if result is False: request.session['error'] = str(message) SocketUtil.notify(dbdoc_id, session_key=session_key, error=str(message)) return HttpResponse()
def post(self, request, **kwargs): session_key = request.session.session_key form = DeleteElementByIdForm(request.POST) dbdoc_id = kwargs['document_id'] controller = SharedVariables.get_dbd_controller(session_key, dbdoc_id) if form.is_valid(): try: id = form.cleaned_data['id'] result, message = controller.delete_element_by_id(id) except Exception as e: request.session['error'] = str(e) SocketUtil.notify(dbdoc_id, session_key=session_key, error=str(e)) return HttpResponse() if result is False: request.session['error'] = str(message) SocketUtil.notify(dbdoc_id, session_key=session_key, error=str(message)) return HttpResponse()
def post(self, request): session_key = request.session.session_key form = UploadForm(request.POST) # controller = SharedVariables.get_dbd_controller(session_key, dbdoc_id) if form.is_valid(): try: xml = form.cleaned_data['xml_content'] safe = form.cleaned_data['safe'] controller = SharedVariables.add_dbd_controller_with_upload( session_key, xml, safe) # In case same client is editing the same document in 1+ tabs, after uploading create a new controller. # SharedVariables.add_dbd_controller(session_key, controller.get_id()) # SharedVariables.add_dbd_controller(session_key, dbdoc_id) except Exception as e: request.session['error'] = str(e) print(e) return HttpResponseRedirect('/') return HttpResponseRedirect('/document/{}'.format( controller.get_id())) return HttpResponse("not valid")
def post(self, request, **kwargs): session_key = request.session.session_key form = InsertSiblingForm(request.POST) dbdoc_id = kwargs['document_id'] controller = SharedVariables.get_dbd_controller(session_key, dbdoc_id) if form.is_valid(): try: tag = form.cleaned_data['tag'] id = form.cleaned_data['id'] after = form.cleaned_data['after'] result, message = controller.insert_sibling(id, tag, after) except Exception as e: request.session['error'] = str(e) SocketUtil.notify(dbdoc_id, session_key=session_key, error=str(e)) return HttpResponse() if result is None: request.session['error'] = str(message) SocketUtil.notify(dbdoc_id, session_key=session_key, error=str(message)) return HttpResponse()
def __init__(self, attached_id="NEW", xml_content=None, safe=False): if xml_content is not None: messages = [] try: if not safe: is_valid, messages = ValidationUtil.validate( ET.fromstring(xml_content)) else: is_valid = True except: raise Exception( ["The given string could not be converted into an xml."]) if is_valid: self.count = DBDController.count DBDController.count += 1 self.notification = Condition() dbdoc = DBDoc(xml_content) SharedVariables.add_dbdoc(dbdoc, True) self.attached_id = dbdoc.get_id() self.dbdoc = dbdoc SharedVariables.add_dbdcontroller_notifier( self.attached_id, self.notification, self.on_dbdoc_change) else: raise Exception(messages) else: self.count = DBDController.count DBDController.count += 1 self.notification = Condition() if attached_id == "NEW": dbdoc = DBDoc() SharedVariables.add_dbdoc(dbdoc, True) self.attached_id = dbdoc.get_id() self.dbdoc = dbdoc SharedVariables.add_dbdcontroller_notifier( self.attached_id, self.notification, self.on_dbdoc_change) else: if SharedVariables.get_dbdoc(attached_id) is None: try: DBDPersistency.load(attached_id) self.attached_id = attached_id self.dbdoc = SharedVariables.get_dbdoc(attached_id) SharedVariables.add_dbdcontroller_notifier( self.attached_id, self.notification, self.on_dbdoc_change) except: raise Exception( 'Cannot be attached to a non existing dbdoc') else: self.attached_id = attached_id self.dbdoc = SharedVariables.get_dbdoc(attached_id) SharedVariables.add_dbdcontroller_notifier( self.attached_id, self.notification, self.on_dbdoc_change)
def on_upload_dbdoc_request(self, function): result = function() if result[0]: SharedVariables.notify_controllers_on_change( self.attached_id, self.dbdoc, False, self.notification) return result