def get(request): if not request.is_ajax() or request.method != 'POST': return JsonResponse({}, status=405) id = int(request.POST['id']) if id == 0: doc_template = DocumentTemplate() doc_template.user = request.user doc_template.save() status = 201 else: doc_template = DocumentTemplate.objects.filter( id=id, user=request.user).first() status = 200 if doc_template is None: return JsonResponse({}, status=405) serializer = PythonWithURLSerializer() export_templates = serializer.serialize( doc_template.exporttemplate_set.all()) document_styles = serializer.serialize( doc_template.documentstyle_set.all(), use_natural_foreign_keys=True, fields=['title', 'slug', 'contents', 'documentstylefile_set']) response = { 'template': { 'id': doc_template.id, 'title': doc_template.title, 'definition': doc_template.definition, 'export_templates': export_templates, 'document_styles': document_styles }, } return JsonResponse(response, status=status)
def send_styles(self): doc_db = self.doc['db'] response = dict() response['type'] = 'styles' serializer = PythonWithURLSerializer() export_temps = serializer.serialize( doc_db.template.export_templates.all() ) document_styles = serializer.serialize( doc_db.template.document_styles.all(), use_natural_foreign_keys=True ) cite_styles = serializer.serialize( doc_db.template.citation_styles.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)
def get_template(request): id = int(request.POST["id"]) if id == 0: doc_template = DocumentTemplate() doc_template.user = request.user doc_template.save() status = 201 else: doc_template = DocumentTemplate.objects.filter( id=id, user=request.user ).first() status = 200 if doc_template is None: return JsonResponse({}, status=405) serializer = PythonWithURLSerializer() export_templates = serializer.serialize( doc_template.exporttemplate_set.all() ) document_styles = serializer.serialize( doc_template.documentstyle_set.all(), use_natural_foreign_keys=True, fields=["title", "slug", "contents", "documentstylefile_set"], ) response = { "id": doc_template.id, "title": doc_template.title, "content": doc_template.content, "doc_version": doc_template.doc_version, "export_templates": export_templates, "document_styles": document_styles, } return JsonResponse(response, status=status)
def send_styles(self): doc_db = self.doc['db'] response = dict() response['type'] = 'styles' serializer = PythonWithURLSerializer() export_temps = serializer.serialize( doc_db.template.exporttemplate_set.all(), fields=['file_type', 'template_file', 'title']) document_styles = serializer.serialize( doc_db.template.documentstyle_set.all(), use_natural_foreign_keys=True, fields=['title', 'slug', 'contents', 'documentstylefile_set']) document_templates = {} for obj in DocumentTemplate.objects.filter( Q(user=self.user) | Q(user=None)).order_by( F('user').desc(nulls_first=True)): document_templates[obj.import_id] = { 'title': obj.title, 'id': obj.id } response['styles'] = { 'export_templates': [obj['fields'] for obj in export_temps], 'document_styles': [obj['fields'] for obj in document_styles], 'document_templates': document_templates } self.send_message(response)
def save_export_template(request): response = {} if not request.is_ajax() or request.method != 'POST': return JsonResponse(response, status=405) template_id = int(request.POST['template_id']) if request.user.is_staff: template = DocumentTemplate.objects.filter(id=template_id).first() else: template = DocumentTemplate.objects.filter(id=template_id, user=request.user).first() if not template: return JsonResponse(response, status=405) id = int(request.POST['id']) if id > 0: export_template = ExportTemplate.objects.filter( id=id, document_template=template).first() status = 200 else: export_template = ExportTemplate() export_template.document_template = template status = 201 if not export_template: return JsonResponse(response, status=405) export_template.template_file = request.FILES['added_file'] export_template.file_type = request.POST['added_file_type'] try: export_template.full_clean() export_template.save() except ValidationError as e: response['errors'] = e.message_dict return JsonResponse(response, status=400) serializer = PythonWithURLSerializer() response['export_template'] = serializer.serialize( [export_template], fields=['file_type', 'template_file', 'title']) return JsonResponse(response, status=status)
def save_export_template(request): response = {} template_id = int(request.POST['template_id']) template_selector = {'id': template_id} if not request.user.is_staff: template_selector['user'] = request.user template = DocumentTemplate.objects.get(**template_selector) id = int(request.POST['id']) if id > 0: export_template = ExportTemplate.objects.get( id=id, document_template=template) status = 200 else: export_template = ExportTemplate() export_template.document_template = template status = 201 export_template.template_file = request.FILES['added_file'] export_template.file_type = request.POST['added_file_type'] try: export_template.full_clean() export_template.save() except ValidationError as e: response['errors'] = e.message_dict return JsonResponse(response, status=400) serializer = PythonWithURLSerializer() response['export_template'] = serializer.serialize( [export_template], fields=['file_type', 'template_file', 'title']) return JsonResponse(response, status=status)
def save_document_style(request): response = {} if not request.is_ajax() or request.method != 'POST': return JsonResponse(response, status=405) template_id = int(request.POST['template_id']) if request.user.is_staff: template = DocumentTemplate.objects.filter(id=template_id).first() else: template = DocumentTemplate.objects.filter(id=template_id, user=request.user).first() if not template: return JsonResponse(response, status=405) id = int(request.POST['id']) if id > 0: document_style = DocumentStyle.objects.filter( id=id, document_template=template).first() status = 200 else: document_style = DocumentStyle() document_style.document_template = template status = 201 if not document_style: return JsonResponse(response, status=405) document_style.title = request.POST['title'] document_style.slug = request.POST['slug'] document_style.contents = request.POST['contents'] try: document_style.full_clean() document_style.save() except ValidationError as e: response['errors'] = e.message_dict return JsonResponse(response, status=400) deleted_files = request.POST.getlist('deleted_files[]') added_files = request.FILES.getlist('added_files[]') for file in added_files: dsf = DocumentStyleFile() dsf.file = file dsf.style = document_style dsf.save() for file in deleted_files: dsf = DocumentStyleFile.objects.filter(style=document_style, filename=file).first() if dsf: dsf.delete() serializer = PythonWithURLSerializer() response['doc_style'] = serializer.serialize( [document_style], use_natural_foreign_keys=True, fields=['title', 'slug', 'contents', 'documentstylefile_set']) return JsonResponse(response, status=status)
def get_documentlist(request): response = {} status = 200 response['documents'] = documents_list(request) response['team_members'] = [] for team_member in request.user.leader.all(): tm_object = {} tm_object['id'] = team_member.member.id tm_object['name'] = team_member.member.readable_name tm_object['username'] = team_member.member.get_username() tm_object['avatar'] = get_user_avatar_url(team_member.member) response['team_members'].append(tm_object) serializer = PythonWithURLSerializer() doc_styles = serializer.serialize( DocumentStyle.objects.filter( Q(document_template__user=None) | Q(document_template__user=request.user) ), use_natural_foreign_keys=True, fields=['title', 'slug', 'contents', 'documentstylefile_set'] ) response['document_styles'] = [obj['fields'] for obj in doc_styles] doc_templates = DocumentTemplate.objects.filter( Q(user=request.user) | Q(user=None) ).order_by(F('user').desc(nulls_first=True)) response['document_templates'] = {} for obj in doc_templates: response['document_templates'][obj.import_id] = { 'title': obj.title, 'id': obj.id } return JsonResponse( response, status=status )
def get_template_for_doc(request): doc_id = request.POST.get("id") doc = ( Document.objects.filter(id=doc_id) .filter(Q(owner=request.user) | Q(accessright__user=request.user)) .first() ) if doc is None: return JsonResponse({}, status=405) doc_template = doc.template serializer = PythonWithURLSerializer() export_templates = serializer.serialize( doc_template.exporttemplate_set.all() ) document_styles = serializer.serialize( doc_template.documentstyle_set.all(), use_natural_foreign_keys=True, fields=["title", "slug", "contents", "documentstylefile_set"], ) response = { "id": doc_template.id, "title": doc_template.title, "content": doc_template.content, "doc_version": doc_template.doc_version, "export_templates": export_templates, "document_styles": document_styles, } return JsonResponse(response, status=200)
def get_documentlist_js(request): response = {} status = 405 if request.is_ajax() and request.method == 'POST': status = 200 response['documents'] = documents_list(request) response['team_members'] = [] for team_member in request.user.leader.all(): tm_object = {} tm_object['id'] = team_member.member.id tm_object['name'] = team_member.member.readable_name tm_object['username'] = team_member.member.get_username() tm_object['avatar'] = avatar_url(team_member.member, 80) response['team_members'].append(tm_object) serializer = PythonWithURLSerializer() export_temps = serializer.serialize(ExportTemplate.objects.all()) response['export_templates'] = [obj['fields'] for obj in export_temps] cit_styles = serializer.serialize(CitationStyle.objects.all()) response['citation_styles'] = [obj['fields'] for obj in cit_styles] cit_locales = serializer.serialize(CitationLocale.objects.all()) response['citation_locales'] = [obj['fields'] for obj in cit_locales] response['user'] = {} response['user']['id'] = request.user.id response['user']['name'] = request.user.readable_name response['user']['username'] = request.user.get_username() response['user']['avatar'] = avatar_url(request.user, 80) response['access_rights'] = get_accessrights( AccessRight.objects.filter(document__owner=request.user)) return JsonResponse(response, status=status)
def get_documentlist_js(request): response = {} status = 405 if request.is_ajax() and request.method == 'POST': status = 200 response['documents'] = documents_list(request) response['team_members'] = [] for team_member in request.user.leader.all(): tm_object = {} tm_object['id'] = team_member.member.id tm_object['name'] = team_member.member.readable_name tm_object['username'] = team_member.member.get_username() tm_object['avatar'] = get_user_avatar_url( team_member.member)['url'] response['team_members'].append(tm_object) serializer = PythonWithURLSerializer() export_temps = serializer.serialize(ExportTemplate.objects.all()) response['export_templates'] = [obj['fields'] for obj in export_temps] cit_styles = serializer.serialize(CitationStyle.objects.all()) response['citation_styles'] = [obj['fields'] for obj in cit_styles] cit_locales = serializer.serialize(CitationLocale.objects.all()) response['citation_locales'] = [obj['fields'] for obj in cit_locales] doc_styles = serializer.serialize(DocumentStyle.objects.all(), use_natural_foreign_keys=True) response['document_styles'] = [obj['fields'] for obj in doc_styles] response['access_rights'] = get_accessrights( AccessRight.objects.filter(document__owner=request.user)) doc_templates = DocumentTemplate.objects.filter( Q(user=request.user) | Q(user=None)) response['document_templates'] = [{ 'id': obj.id, 'title': obj.title } for obj in doc_templates] return JsonResponse(response, status=status)
def get_template_extras(request): id = request.POST['id'] doc_template = DocumentTemplate.objects.filter( id=id ).first() status = 200 if doc_template is None: return JsonResponse({}, status=405) serializer = PythonWithURLSerializer() export_templates = serializer.serialize( doc_template.exporttemplate_set.all() ) document_styles = serializer.serialize( doc_template.documentstyle_set.all(), use_natural_foreign_keys=True, fields=['title', 'slug', 'contents', 'documentstylefile_set'] ) response = { 'export_templates': export_templates, 'document_styles': document_styles, } return JsonResponse( response, status=status )
def save_document_style(request): response = {} template_id = int(request.POST["template_id"]) template_selector = {"id": template_id} if not request.user.is_staff: template_selector["user"] = request.user template = DocumentTemplate.objects.get(**template_selector) id = int(request.POST["id"]) if id > 0: document_style = DocumentStyle.objects.get( id=id, document_template=template ) status = 200 else: document_style = DocumentStyle() document_style.document_template = template status = 201 document_style.title = request.POST["title"] document_style.slug = request.POST["slug"] document_style.contents = request.POST["contents"] try: document_style.full_clean() document_style.save() except ValidationError as e: response["errors"] = e.message_dict return JsonResponse(response, status=400) deleted_files = request.POST.getlist("deleted_files[]") added_files = request.FILES.getlist("added_files[]") for file in added_files: dsf = DocumentStyleFile() dsf.file = file dsf.style = document_style dsf.save() for file in deleted_files: dsf = DocumentStyleFile.objects.filter( style=document_style, filename=file ).first() if dsf: dsf.delete() serializer = PythonWithURLSerializer() response["doc_style"] = serializer.serialize( [document_style], use_natural_foreign_keys=True, fields=["title", "slug", "contents", "documentstylefile_set"], ) return JsonResponse(response, status=status)
def send_styles(self): doc_db = self.doc['db'] response = dict() response['type'] = 'styles' serializer = PythonWithURLSerializer() export_temps = serializer.serialize( doc_db.template.exporttemplate_set.all(), fields=['file_type', 'template_file', 'title']) document_styles = serializer.serialize( doc_db.template.documentstyle_set.all(), use_natural_foreign_keys=True, fields=['title', 'slug', 'contents', 'documentstylefile_set']) response['styles'] = { 'export_templates': [obj['fields'] for obj in export_temps], 'document_styles': [obj['fields'] for obj in document_styles] } self.send_message(response)
def get_documentlist(request): response = {} status = 200 response["documents"] = documents_list(request) response["contacts"] = [] for contact in request.user.contacts.all(): contact_object = { "id": contact.id, "name": contact.readable_name, "username": contact.get_username(), "avatar": contact.avatar_url, "type": "user", } response["contacts"].append(contact_object) for contact in request.user.invites_by.all(): contact_object = { "id": contact.id, "name": contact.username, "username": contact.username, "avatar": contact.avatar_url, "type": "userinvite", } response["contacts"].append(contact_object) serializer = PythonWithURLSerializer() doc_styles = serializer.serialize( DocumentStyle.objects.filter( Q(document_template__user=None) | Q(document_template__user=request.user) ), use_natural_foreign_keys=True, fields=["title", "slug", "contents", "documentstylefile_set"], ) response["document_styles"] = [obj["fields"] for obj in doc_styles] doc_templates = DocumentTemplate.objects.filter( Q(user=request.user) | Q(user=None) ).order_by(F("user").desc(nulls_first=True)) response["document_templates"] = {} for obj in doc_templates: response["document_templates"][obj.import_id] = { "title": obj.title, "id": obj.id, } return JsonResponse(response, status=status)
def get_documentlist(request): response = {} status = 405 if request.is_ajax() and request.method == 'POST': status = 200 response['documents'] = documents_list(request) response['team_members'] = [] for team_member in request.user.leader.all(): tm_object = {} tm_object['id'] = team_member.member.id tm_object['name'] = team_member.member.readable_name tm_object['username'] = team_member.member.get_username() ''' tm_object['avatar'] = get_user_avatar_url( team_member.member )['url'] ''' tm_object['avatar'] = get_user_avatar_url(team_member.member) response['team_members'].append(tm_object) serializer = PythonWithURLSerializer() export_temps = serializer.serialize( ExportTemplate.objects.filter( Q(document_template__user=None) | Q(document_template__user=request.user)), fields=['file_type', 'template_file', 'title']) response['export_templates'] = [obj['fields'] for obj in export_temps] doc_styles = serializer.serialize( DocumentStyle.objects.filter( Q(document_template__user=None) | Q(document_template__user=request.user)), use_natural_foreign_keys=True, fields=['title', 'slug', 'contents', 'documentstylefile_set']) response['document_styles'] = [obj['fields'] for obj in doc_styles] doc_templates = DocumentTemplate.objects.filter( Q(user=request.user) | Q(user=None)) response['document_templates'] = [{ 'id': obj.id, 'title': obj.title } for obj in doc_templates] return JsonResponse(response, status=status)
def get_template_admin(request, type="all"): template_id = request.POST.get("id") doc_template = DocumentTemplate.objects.filter(pk=int(template_id)).first() if doc_template is None: return JsonResponse({}, status=405) response = {} if type in ["all", "base"]: response["id"] = doc_template.id response["title"] = doc_template.title response["content"] = doc_template.content response["doc_version"] = doc_template.doc_version if type in ["all", "extras"]: serializer = PythonWithURLSerializer() export_templates = serializer.serialize( doc_template.exporttemplate_set.all() ) document_styles = serializer.serialize( doc_template.documentstyle_set.all(), use_natural_foreign_keys=True, fields=["title", "slug", "contents", "documentstylefile_set"], ) response["export_templates"] = export_templates response["document_styles"] = document_styles return JsonResponse(response, status=200)
def send_styles(self): doc_db = self.doc['db'] response = dict() response['type'] = 'styles' serializer = PythonWithURLSerializer() export_temps = serializer.serialize( doc_db.template.export_templates.all()) document_styles = serializer.serialize( doc_db.template.document_styles.all(), use_natural_foreign_keys=True) cite_styles = serializer.serialize( doc_db.template.citation_styles.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)
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()
def list(request): response = {} status = 200 avatars = Avatars() response["documents"] = documents_list(request) books = (Book.objects.filter( Q(owner=request.user) | Q( bookaccessright__holder_id=request.user.id, bookaccessright__holder_type__model="user", )).select_related("owner").prefetch_related( Prefetch( "chapter_set", queryset=Chapter.objects.select_related("text").only( "id", "number", "part", "text_id", "text__title", "text__updated", ), )).only( "id", "title", "path", "added", "updated", "metadata", "settings", "cover_image", "owner_id", "owner__first_name", "owner__last_name", "owner__username", ).order_by("-updated").distinct()) response["books"] = [] for book in books: if book.owner_id == request.user.id: access_right = "write" path = book.path else: access_right_object = BookAccessRight.objects.get( holder_id=request.user.id, holder_type__model="user", book=book) access_right = access_right_object.rights path = access_right_object.path added = time.mktime(book.added.utctimetuple()) updated = time.mktime(book.updated.utctimetuple()) is_owner = False if book.owner_id == request.user.id: is_owner = True chapters = [] for chapter in book.chapter_set.all(): chapters.append({ "text": chapter.text_id, "number": chapter.number, "part": chapter.part, "title": chapter.text.title, }) chapter_updated = time.mktime(chapter.text.updated.utctimetuple()) if chapter_updated > updated: updated = chapter_updated book_data = { "id": book.id, "title": book.title, "path": path, "is_owner": is_owner, "owner": { "id": book.owner_id, "name": book.owner.readable_name, "avatar": avatars.get_url(book.owner), }, "added": added, "updated": updated, "rights": access_right, "chapters": chapters, "metadata": book.metadata, "settings": book.settings, } if book.cover_image: image = book.cover_image book_data["cover_image"] = image.id field_obj = { "id": image.id, "added": time.mktime(image.added.utctimetuple()) * 1000, "checksum": image.checksum, "file_type": image.file_type, "title": "", "cats": "", "image": image.image.url, } if image.thumbnail: field_obj["thumbnail"] = image.thumbnail.url field_obj["height"] = image.height field_obj["width"] = image.width book_data["cover_image_data"] = field_obj response["books"].append(book_data) response["contacts"] = [] for contact in request.user.contacts.all(): contact_object = { "id": contact.id, "name": contact.readable_name, "username": contact.get_username(), "avatar": avatars.get_url(contact), "type": "user", } response["contacts"].append(contact_object) for contact in request.user.invites_by.all(): contact_object = { "id": contact.id, "name": contact.username, "username": contact.username, "avatar": None, "type": "userinvite", } response["contacts"].append(contact_object) serializer = PythonWithURLSerializer() book_styles = serializer.serialize( BookStyle.objects.all(), use_natural_foreign_keys=True, fields=["title", "slug", "contents", "bookstylefile_set"], ) response["styles"] = [obj["fields"] for obj in book_styles] return JsonResponse(response, status=status)