def run(self, lines): new_text = [] previous_line = "" line_index = None previous_line_was_image = False image = None image_id = None alignment = None caption_lines = [] for line in lines: m = IMAGE_RE.match(line) if m: previous_line_was_image = True image_id = m.group('id').strip() alignment = m.group('align') try: image = models.Image.objects.get( article=self.markdown.article, id=image_id, current_revision__deleted=False) except models.Image.DoesNotExist: pass line_index = line.find(m.group(1)) line = line.replace(m.group(1), "") previous_line = line caption_lines = [] elif previous_line_was_image: if line.startswith(" "): caption_lines.append(line[4:]) line = None else: caption_placeholder = "{{{IMAGECAPTION}}}" html = render_to_string("wiki/plugins/images/render.html", context={ 'image': image, 'caption': caption_placeholder, 'align': alignment, }) html_before, html_after = html.split(caption_placeholder) placeholder_before = self.markdown.htmlStash.store( html_before, safe=True) placeholder_after = self.markdown.htmlStash.store( html_after, safe=True) new_line = placeholder_before + "\n".join( caption_lines) + placeholder_after + "\n" previous_line_was_image = False if previous_line is not "": if previous_line[line_index:] is not "": new_line = new_line[0:-1] new_text[-1] = (previous_line[0:line_index] + new_line + previous_line[line_index:] + "\n" + line) line = None else: line = new_line + line if line is not None: new_text.append(line) return new_text
def article_list(self, depth="2"): html = render_to_string( "wiki/plugins/macros/article_list.html", context={ 'article_children': self.markdown.article.get_children( article__current_revision__deleted=False), 'depth': int(depth) + 1, }) return self.markdown.htmlStash.store(html, safe=True)
def response_forbidden(request, article, urlpath): if request.user.is_anonymous(): qs = request.META.get("QUERY_STRING", "") if qs: qs = urlquote("?" + qs) else: qs = "" return redirect(settings.LOGIN_URL + "?next=" + request.path + qs) else: return HttpResponseForbidden( render_to_string( "wiki/permission_denied.html", context={"article": article, "urlpath": urlpath}, request=request ) )
def run(self, lines): new_text = [] for line in lines: m = ATTACHMENT_RE.match(line) if m: attachment_id = m.group('id').strip() before = self.run([m.group('before')])[0] after = self.run([m.group('after')])[0] try: attachment = models.Attachment.objects.get( articles__current_revision__deleted=False, id=attachment_id, current_revision__deleted=False, articles=self.markdown.article ) url = reverse( 'wiki:attachments_download', kwargs={ 'article_id': self.markdown.article.id, 'attachment_id': attachment.id, }) # The readability of the attachment is decided relative # to the owner of the original article. # I.e. do not insert attachments in other articles that # the original uploader cannot read, that would be out # of scope! article_owner = attachment.article.owner if not article_owner: article_owner = AnonymousUser() attachment_can_read = can_read( self.markdown.article, article_owner) html = render_to_string( "wiki/plugins/attachments/render.html", context={ 'url': url, 'filename': attachment.original_filename, 'attachment_can_read': attachment_can_read, }) line = self.markdown.htmlStash.store(html, safe=True) except models.Attachment.DoesNotExist: html = """<span class="attachment attachment-deleted">Attachment with ID #%s is deleted.</span>""" % attachment_id line = line.replace( m.group(2), self.markdown.htmlStash.store( html, safe=True)) line = before + line + after new_text.append(line) return new_text
def response_forbidden(request, article, urlpath): if request.user.is_anonymous(): qs = request.META.get('QUERY_STRING', '') if qs: qs = urlquote('?' + qs) else: qs = '' return redirect(settings.LOGIN_URL + "?next=" + request.path + qs) else: return HttpResponseForbidden( render_to_string( "wiki/permission_denied.html", context={'article': article, 'urlpath': urlpath}, request=request))
def run(self, lines): new_text = [] for line in lines: m = ATTACHMENT_RE.match(line) if m: attachment_id = m.group('id').strip() before = self.run([m.group('before')])[0] after = self.run([m.group('after')])[0] try: attachment = models.Attachment.objects.get( articles__current_revision__deleted=False, id=attachment_id, current_revision__deleted=False, articles=self.markdown.article) url = reverse('wiki:attachments_download', kwargs={ 'article_id': self.markdown.article.id, 'attachment_id': attachment.id, }) # The readability of the attachment is decided relative # to the owner of the original article. # I.e. do not insert attachments in other articles that # the original uploader cannot read, that would be out # of scope! article_owner = attachment.article.owner if not article_owner: article_owner = AnonymousUser() attachment_can_read = can_read(self.markdown.article, article_owner) html = render_to_string( "wiki/plugins/attachments/render.html", context={ 'url': url, 'filename': attachment.original_filename, 'attachment_can_read': attachment_can_read, }) line = self.markdown.htmlStash.store(html, safe=True) except models.Attachment.DoesNotExist: html = """<span class="attachment attachment-deleted">Attachment with ID #%s is deleted.</span>""" % attachment_id line = line.replace( m.group(2), self.markdown.htmlStash.store(html, safe=True)) line = before + line + after new_text.append(line) return new_text
def wrapper(request, *args, **kwargs): from . import models path = kwargs.pop("path", None) article_id = kwargs.pop("article_id", None) urlpath = None # fetch by urlpath.path if path is not None: try: urlpath = models.URLPath.get_by_path(path, select_related=True) except NoRootURL: return redirect("wiki:root_create") except models.URLPath.DoesNotExist: try: pathlist = list(filter(lambda x: x != "", path.split("/"))) path = "/".join(pathlist[:-1]) parent = models.URLPath.get_by_path(path) return HttpResponseRedirect( reverse("wiki:create", kwargs={"path": parent.path}) + "?slug=%s" % pathlist[-1] ) except models.URLPath.DoesNotExist: return HttpResponseNotFound( render_to_string( "wiki/error.html", context={"error_type": "ancestors_missing"}, request=request ) ) if urlpath.article: # urlpath is already smart about prefetching items on article # (like current_revision), so we don't have to article = urlpath.article else: # Be robust: Somehow article is gone but urlpath exists... # clean up return_url = reverse("wiki:get", kwargs={"path": urlpath.parent.path}) urlpath.delete() return HttpResponseRedirect(return_url) # fetch by article.id elif article_id: # TODO We should try to grab the article form URLPath so the # caching is good, and fall back to grabbing it from # Article.objects if not articles = models.Article.objects article = get_object_or_404(articles, id=article_id) try: urlpath = models.URLPath.objects.get(articles__article=article) except (models.URLPath.DoesNotExist, models.URLPath.MultipleObjectsReturned): urlpath = None else: raise TypeError("You should specify either article_id or path") if not deleted_contents: # If the article has been deleted, show a special page. if urlpath: if urlpath.is_deleted(): # This also checks all ancestors return redirect("wiki:deleted", path=urlpath.path) else: if article.current_revision and article.current_revision.deleted: return redirect("wiki:deleted", article_id=article.id) if article.current_revision.locked and not_locked: return response_forbidden(request, article, urlpath) if can_read and not article.can_read(request.user): return response_forbidden(request, article, urlpath) if (can_write or can_create) and not article.can_write(request.user): return response_forbidden(request, article, urlpath) if can_create and not (request.user.is_authenticated() or settings.ANONYMOUS_CREATE): return response_forbidden(request, article, urlpath) if can_delete and not article.can_delete(request.user): return response_forbidden(request, article, urlpath) if can_moderate and not article.can_moderate(request.user): return response_forbidden(request, article, urlpath) kwargs["urlpath"] = urlpath return func(request, article, *args, **kwargs)
def run(self, lines): new_text = [] previous_line = "" line_index = None previous_line_was_image = False image = None image_id = None alignment = None caption_lines = [] for line in lines: m = IMAGE_RE.match(line) if m: previous_line_was_image = True image_id = m.group('id').strip() alignment = m.group('align') try: image = models.Image.objects.get( article=self.markdown.article, id=image_id, current_revision__deleted=False) except models.Image.DoesNotExist: pass line_index = line.find(m.group(1)) line = line.replace(m.group(1), "") previous_line = line caption_lines = [] elif previous_line_was_image: if line.startswith(" "): caption_lines.append(line[4:]) line = None else: caption_placeholder = "{{{IMAGECAPTION}}}" html = render_to_string( "wiki/plugins/images/render.html", context={ 'image': image, 'caption': caption_placeholder, 'align': alignment, }) html_before, html_after = html.split(caption_placeholder) placeholder_before = self.markdown.htmlStash.store( html_before, safe=True) placeholder_after = self.markdown.htmlStash.store( html_after, safe=True) new_line = placeholder_before + "\n".join( caption_lines) + placeholder_after + "\n" previous_line_was_image = False if previous_line is not "": if previous_line[line_index:] is not "": new_line = new_line[0:-1] new_text[-1] = (previous_line[0:line_index] + new_line + previous_line[line_index:] + "\n" + line) line = None else: line = new_line + line if line is not None: new_text.append(line) return new_text
def wrapper(request, *args, **kwargs): from . import models path = kwargs.pop('path', None) article_id = kwargs.pop('article_id', None) urlpath = None # fetch by urlpath.path if not path is None: try: urlpath = models.URLPath.get_by_path(path, select_related=True) except NoRootURL: return redirect('wiki:root_create') except models.URLPath.DoesNotExist: try: pathlist = list( filter( lambda x: x != "", path.split("/"), )) path = "/".join(pathlist[:-1]) parent = models.URLPath.get_by_path(path) return HttpResponseRedirect( reverse("wiki:create", kwargs={ 'path': parent.path, }) + "?slug=%s" % pathlist[-1]) except models.URLPath.DoesNotExist: return HttpResponseNotFound( render_to_string( "wiki/error.html", context={'error_type': 'ancestors_missing'}, request=request)) if urlpath.article: # urlpath is already smart about prefetching items on article # (like current_revision), so we don't have to article = urlpath.article else: # Be robust: Somehow article is gone but urlpath exists... # clean up return_url = reverse('wiki:get', kwargs={'path': urlpath.parent.path}) urlpath.delete() return HttpResponseRedirect(return_url) # fetch by article.id elif article_id: # TODO We should try to grab the article form URLPath so the # caching is good, and fall back to grabbing it from # Article.objects if not articles = models.Article.objects article = get_object_or_404(articles, id=article_id) try: urlpath = models.URLPath.objects.get(articles__article=article) except models.URLPath.DoesNotExist as noarticle: models.URLPath.MultipleObjectsReturned = noarticle urlpath = None else: raise TypeError('You should specify either article_id or path') if not deleted_contents: # If the article has been deleted, show a special page. if urlpath: if urlpath.is_deleted(): # This also checks all ancestors return redirect('wiki:deleted', path=urlpath.path) else: if article.current_revision and article.current_revision.deleted: return redirect('wiki:deleted', article_id=article.id) if article.current_revision.locked and not_locked: return response_forbidden(request, article, urlpath) if can_read and not article.can_read(request.user): return response_forbidden(request, article, urlpath) if (can_write or can_create) and not article.can_write(request.user): return response_forbidden(request, article, urlpath) if can_create and not (request.user.is_authenticated() or settings.ANONYMOUS_CREATE): return response_forbidden(request, article, urlpath) if can_delete and not article.can_delete(request.user): return response_forbidden(request, article, urlpath) if can_moderate and not article.can_moderate(request.user): return response_forbidden(request, article, urlpath) kwargs['urlpath'] = urlpath return func(request, article, *args, **kwargs)