Beispiel #1
0
def edit_book(request, bookid, version=None):
    """
    Django View. Default page for Booki editor.

    @param request: Django Request
    @param bookid: Book ID
    @param version: Book Version or None
    """

    from booki.utils import security

    try:
        book = models.Book.objects.get(url_title__iexact=bookid)
    except models.Book.DoesNotExist:
        return pages.ErrorPage(request, "errors/book_does_not_exist.html", {"book_name": bookid})

    book_version = getVersion(book, version)

    bookSecurity = security.getUserSecurityForBook(request.user, book)

    hasPermission = security.canEditBook(book, bookSecurity)

    if not hasPermission:
        return pages.ErrorPage(request, "errors/editing_forbidden.html", {"book": book})    

    chapters = models.Chapter.objects.filter(version=book_version)

    tabs = ["chapters"]

    if True: # bookSecurity.isAdmin():
        tabs += ["attachments"]

    tabs += ["history", "versions", "notes"]

    if bookSecurity.isAdmin():
        tabs += ["settings"]

    tabs += ["export"]

    isBrowserSupported = True
    browserMeta = request.META.get('HTTP_USER_AGENT', '')

    if browserMeta.find('MSIE') != -1:
        isBrowserSupported = False
        
    return render_to_response('editor/edit_book.html', {"book": book, 
                                                        "book_version": book_version.getVersion(),
                                                        "version": book_version,

                                                        ## this change will break older versions of template
                                                        "statuses": [(s.name.replace(' ', '_'), s.name) for s in models.BookStatus.objects.filter(book = book)],
                                                        "chapters": chapters, 
                                                        "security": bookSecurity,
                                                        "is_admin": bookSecurity.isGroupAdmin() or bookSecurity.isBookAdmin() or bookSecurity.isSuperuser(),
                                                        "is_owner": book.owner == request.user,
                                                        "tabs": tabs,
                                                        "is_browser_supported": isBrowserSupported,
                                                        "request": request})
Beispiel #2
0
def edit_book(request, bookid, version=None):
    """
    Django View. Default page for Booki editor.

    @param request: Django Request
    @param bookid: Book ID
    @param version: Book Version or None
    """

    from booki.utils import security

    try:
        book = models.Book.objects.get(url_title__iexact=bookid)
    except models.Book.DoesNotExist:
        return pages.ErrorPage(request, "errors/book_does_not_exist.html", {"book_name": bookid})

    book_version = getVersion(book, version)

    bookSecurity = security.getUserSecurityForBook(request.user, book)

    hasPermission = security.canEditBook(book, bookSecurity)

    if not hasPermission:
        return pages.ErrorPage(request, "errors/editing_forbidden.html", {"book": book})    

    chapters = models.Chapter.objects.filter(version=book_version)

    tabs = ["chapters"]

    if True: # bookSecurity.isAdmin():
        tabs += ["attachments"]

    tabs += ["history", "versions", "notes"]

    if bookSecurity.isAdmin():
        tabs += ["settings"]

    tabs += ["export"]

    isBrowserSupported = True
    browserMeta = request.META.get('HTTP_USER_AGENT', '')

    if browserMeta.find('MSIE') != -1:
        isBrowserSupported = False
        
    return render_to_response('editor/edit_book.html', {"book": book, 
                                                        "book_version": book_version.getVersion(),
                                                        "version": book_version,

                                                        ## this change will break older versions of template
                                                        "statuses": [(s.name.replace(' ', '_'), s.name) for s in models.BookStatus.objects.filter(book = book)],
                                                        "chapters": chapters, 
                                                        "security": bookSecurity,
                                                        "is_admin": bookSecurity.isGroupAdmin() or bookSecurity.isBookAdmin() or bookSecurity.isSuperuser(),
                                                        "is_owner": book.owner == request.user,
                                                        "tabs": tabs,
                                                        "is_browser_supported": isBrowserSupported,
                                                        "request": request})
Beispiel #3
0
def book_chapter(request, bookid, chapter, version=None):
    """
    Django View. Shows chapter for the published version of a book.

    @type request: C{django.http.HttpRequest}
    @param request: Client Request object
    @type bookid: C{string}
    @param bookid: Unique Book ID
    @type chapter: C{string}
    @param chapter: Chapter name
    @type version: C{string}
    @param verson: Book version
    """

    try:
        book = models.Book.objects.get(url_title__iexact=bookid)
    except models.Book.DoesNotExist:
        return pages.ErrorPage(request, "errors/book_does_not_exist.html", {"book_name": bookid})

    book_version = getVersion(book, version)

    from booki.utils import security
    bookSecurity = security.getUserSecurityForBook(request.user, book)

    hasPermission = security.canEditBook(book, bookSecurity)

    if book.hidden and not hasPermission:
        return pages.ErrorPage(request, "errors/no_permissions.html")


    chapters = []

    for chap in  models.BookToc.objects.filter(version=book_version).order_by("-weight"):
        if chap.isChapter():
            chapters.append({"url_title": chap.chapter.url_title,
                             "name": chap.chapter.title})
        else:
            chapters.append({"url_title": None,
                             "name": chap.name})

    try:
        content = models.Chapter.objects.get(version=book_version, url_title = chapter)
    except models.Chapter.DoesNotExist:
        return pages.ErrorPage(request, "errors/chapter_does_not_exist.html", {"chapter_name": chapter, "book": book})
    except models.Chapter.MultipleObjectsReturned:
        return pages.ErrorPage(request, "errors/chapter_duplicate.html", {"chapter_name": chapter, "book": book})


    return render_to_response('reader/book_chapter.html', {"chapter": chapter, 
                                                      "book": book, 
                                                      "book_version": book_version.getVersion(),
                                                      "chapters": chapters, 
                                                      "has_css": _customCSSExists(book.url_title),
                                                      "request": request, 
                                                      "content": content})
Beispiel #4
0
def view_book(request, bookid):
    from booki.utils import misc

    try:
        book = models.Book.objects.get(url_title__iexact=bookid)
    except models.Book.DoesNotExist:
        return pages.ErrorPage(request, "errors/book_does_not_exist.html", {"book_name": bookid})

    book_version = book.getVersion(None)

    # this only shows info for latest version
    book_history = models.BookHistory.objects.filter(version=book_version).order_by("-modified")[:20]
    book_collaborators = [
        e.values()[0]
        for e in models.BookHistory.objects.filter(version=book_version, kind=2).values("user__username").distinct()
    ]

    from booki.utils import security

    bookSecurity = security.getUserSecurityForBook(request.user, book)
    isBookAdmin = bookSecurity.isAdmin()

    import sputnik

    channel_name = "/booki/book/%s/%s/" % (book.id, book_version.getVersion())
    online_users = sputnik.smembers("sputnik:channel:%s:users" % channel_name)

    book_versions = models.BookVersion.objects.filter(book=book).order_by("created")

    from django.utils.html import escape

    bookDescription = escape(book.description)

    attachmentDirectory = "%s/books/%s" % (settings.DATA_ROOT, book.url_title)
    attachmentsSize = misc.getDirectorySize(attachmentDirectory)

    return render_to_response(
        "booktypecontrol/book.html",
        {
            "request": request,
            "admin_options": ADMIN_OPTIONS,
            "book": book,
            "book_version": book_version.getVersion(),
            "book_versions": book_versions,
            "book_history": book_history,
            "book_collaborators": book_collaborators,
            "is_book_admin": isBookAdmin,
            "online_users": online_users,
            "book_description": "<br/>".join(bookDescription.replace("\r", "").split("\n")),
            "attachments_size": attachmentsSize,
        },
        context_instance=RequestContext(request),
    )
Beispiel #5
0
def view_full(request, bookid, version=None):
    """
    Django View. Shows full content of book on one page. This is published version of a book.

    @type request: C{django.http.HttpRequest}
    @param request: Django Request
    @type bookid: C{string}
    @param bookid: Unique Book ID
    @type version: C{string}
    @param version: Version of the book
    """

    chapters = []

    try:
        book = models.Book.objects.get(url_title__iexact=bookid)
    except models.Book.DoesNotExist:
        return pages.ErrorPage(request, "errors/book_does_not_exist.html",
                               {"book_name": bookid})

    book_version = book.getVersion(version)

    from booki.utils import security
    bookSecurity = security.getUserSecurityForBook(request.user, book)

    hasPermission = security.canEditBook(book, bookSecurity)

    if book.hidden and not hasPermission:
        return pages.ErrorPage(request, "errors/no_permissions.html")

    for chapter in models.BookToc.objects.filter(
            version=book_version).order_by("-weight"):
        if chapter.isChapter():
            chapters.append({
                "type": "chapter",
                "title": chapter.chapter.title,
                "content": chapter.chapter.content,
                "chapter": chapter.chapter
            })
        else:
            chapters.append({"type": "section", "title": chapter.name})

    return render_to_response(
        'reader/full.html', {
            "book": book,
            "book_version": book_version.getVersion(),
            "chapters": chapters,
            "has_css": _customCSSExists(book.url_title),
            "request": request
        })
Beispiel #6
0
def remote_attachments_delete(request, message, bookid, version):
    # TODO: must check security
    book = models.Book.objects.get(id=bookid)
    bookSecurity = security.getUserSecurityForBook(request.user, book)
    
    if bookSecurity.isAdmin():
        for att_id in message['attachments']:
            att = models.Attachment.objects.get(pk=att_id)
            att.delete()
            
        transaction.commit()

        return {"result": True}

    return {"result": False}
Beispiel #7
0
def draft_book(request, bookid, version=None):
    """
    Django View. Shows main page for the draft version of a book.

    @type request: C{django.http.HttpRequest}
    @param request: Client Request object
    @type bookid: C{string}
    @param bookid: Unique Book ID
    @type version: C{string}
    @param verson: Book version
    """

    try:
        book = models.Book.objects.get(url_title__iexact=bookid)
    except models.Book.DoesNotExist:
        return pages.ErrorPage(request, "errors/book_does_not_exist.html",
                               {"book_name": bookid})

    book_version = book.getVersion(version)

    from booki.utils import security
    bookSecurity = security.getUserSecurityForBook(request.user, book)

    hasPermission = security.canEditBook(book, bookSecurity)

    if book.hidden and not hasPermission:
        return pages.ErrorPage(request, "errors/no_permissions.html")

    chapters = []

    for chapter in models.BookToc.objects.filter(
            version=book_version).order_by("-weight"):
        if chapter.isChapter():
            chapters.append({
                "url_title": chapter.chapter.url_title,
                "name": chapter.chapter.title
            })
        else:
            chapters.append({"url_title": None, "name": chapter.name})

    return render_to_response(
        'reader/draft_book.html', {
            "book": book,
            "book_version": book_version.getVersion(),
            "chapters": chapters,
            "has_css": _customCSSExists(book.url_title),
            "request": request
        })
Beispiel #8
0
def book_info(request, bookid, version=None):
    """
    Django View. Shows single page with all the Book info.

    @type request: C{django.http.HttpRequest}
    @param request: Client Request object
    @type bookid: C{string}
    @param bookid: Unique Book ID
    @type version: C{string}
    @param verson: Book version
    """

    try:
        book = models.Book.objects.get(url_title__iexact=bookid)
    except models.Book.DoesNotExist:
        return pages.ErrorPage(request, "errors/book_does_not_exist.html", {"book_name": bookid})


    book_version = getVersion(book, version)

    book_history =  models.BookHistory.objects.filter(version = book_version).order_by("-modified")[:20]

    book_collaborators =  [e.values()[0] for e in models.BookHistory.objects.filter(version = book_version, kind = 2).values("user__username").distinct()]

    from booki.utils import security
    bookSecurity = security.getUserSecurityForBook(request.user, book)
    isBookAdmin = bookSecurity.isAdmin()
    
    import sputnik
    channel_name = "/booki/book/%s/%s/" % (book.id, book_version.getVersion())
    online_users = sputnik.smembers("sputnik:channel:%s:users" % channel_name)

    book_versions = models.BookVersion.objects.filter(book=book).order_by("created")

    from django.utils.html import escape
    bookDescription = escape(book.description)

    return render_to_response('reader/book_info.html', {"book": book, 
                                                        "book_version": book_version.getVersion(),
                                                        "book_versions": book_versions,
                                                        "book_history": book_history, 
                                                        "book_collaborators": book_collaborators,
                                                        "has_css": _customCSSExists(book.url_title),
                                                        "is_book_admin": isBookAdmin,
                                                        "online_users": online_users,
                                                        "book_description": '<br/>'.join(bookDescription.replace('\r','').split('\n')),
                                                        "request": request})
Beispiel #9
0
def view_full(request, bookid, version=None):
    """
    Django View. Shows full content of book on one page. This is published version of a book.

    @type request: C{django.http.HttpRequest}
    @param request: Django Request
    @type bookid: C{string}
    @param bookid: Unique Book ID
    @type version: C{string}
    @param version: Version of the book
    """

    chapters = []

    try:
        book = models.Book.objects.get(url_title__iexact=bookid)
    except models.Book.DoesNotExist:
        return pages.ErrorPage(request, "errors/book_does_not_exist.html", {"book_name": bookid})

    book_version = getVersion(book, version)

    from booki.utils import security
    bookSecurity = security.getUserSecurityForBook(request.user, book)

    hasPermission = security.canEditBook(book, bookSecurity)

    if book.hidden and not hasPermission:
        return pages.ErrorPage(request, "errors/no_permissions.html")

    for chapter in  models.BookToc.objects.filter(version=book_version).order_by("-weight"):
        if chapter.isChapter():
            chapters.append({"type": "chapter",
                             "title": chapter.chapter.title,
                             "content": chapter.chapter.content,
                             "chapter": chapter.chapter})
        else:
            chapters.append({"type": "section",
                             "title": chapter.name})

    return render_to_response('reader/full.html', {"book": book, 
                                                   "book_version": book_version.getVersion(),
                                                   "chapters": chapters, 
                                                   "has_css": _customCSSExists(book.url_title),
                                                   "request": request})
Beispiel #10
0
def book_delete(request, bookid, version=None):
    """
    Django View. 

    @type request: C{django.http.HttpRequest}
    @param request: Client Request object
    @type bookid: C{string}
    @param bookid: Unique Book ID
    @type version: C{string}
    @param verson: Book version
    """

    try:
        book = models.Book.objects.get(url_title__iexact=bookid)
    except models.Book.DoesNotExist:
        try:
            resp = pages.ErrorPage(request, "errors/book_does_not_exist.html", {"book_name": bookid})
        except:
            transaction.rollback()
            raise
        else:
            transaction.commit()

        return resp

    book_version = book.getVersion(version)

    if request.method == 'POST':
        title = request.POST.get('title', '')

        try:
            from booki.utils import security

            bookSecurity = security.getUserSecurityForBook(request.user, book)

            resp = render_to_response('reader/book_delete_error.html', {"request": request})

            if bookSecurity.isAdmin():
                if title.strip() == book.title.strip():
                    from booki.utils.book import removeBook
                
                    removeBook(book)

                    resp = render_to_response('reader/book_delete_redirect.html', {"request": request})
        except:
            transaction.rollback()
            raise
        else:
            transaction.commit()    

        return resp

        
    try:
        resp = render_to_response('reader/book_delete.html', {"request": request,
                                                              "book": book})
    except:
        transaction.rollback()
        raise
    else:
        transaction.commit()    

    return resp
Beispiel #11
0
def book_chapter(request, bookid, chapter, version=None):
    """
    Django View. Shows chapter for the published version of a book.

    @type request: C{django.http.HttpRequest}
    @param request: Client Request object
    @type bookid: C{string}
    @param bookid: Unique Book ID
    @type chapter: C{string}
    @param chapter: Chapter name
    @type version: C{string}
    @param verson: Book version
    """

    try:
        book = models.Book.objects.get(url_title__iexact=bookid)
    except models.Book.DoesNotExist:
        try:
            resp = pages.ErrorPage(request, "errors/book_does_not_exist.html", {"book_name": bookid})
        except:
            transaction.rollback()
            raise
        else:
            transaction.commit()

        return resp

    book_version = book.getVersion(version)

    from booki.utils import security
    bookSecurity = security.getUserSecurityForBook(request.user, book)

    hasPermission = security.canEditBook(book, bookSecurity)

    if book.hidden and not hasPermission:
        try:
            resp = pages.ErrorPage(request, "errors/no_permissions.html")
        except:
            transaction.rollback()
            raise
        else:
            transaction.commit()

        return resp

    chapters = []

    for chap in  models.BookToc.objects.filter(version=book_version).order_by("-weight"):
        if chap.isChapter():
            chapters.append({"url_title": chap.chapter.url_title,
                             "name": chap.chapter.title})
        else:
            chapters.append({"url_title": None,
                             "name": chap.name})

    try:
        content = models.Chapter.objects.get(version=book_version, url_title = chapter)
    except models.Chapter.DoesNotExist:
        try:
            resp = pages.ErrorPage(request, "errors/chapter_does_not_exist.html", {"chapter_name": chapter, "book": book})
        except:
            transaction.rollback()
            raise
        else:
            transaction.commit()

        return resp
    except models.Chapter.MultipleObjectsReturned:
        try:
            resp = pages.ErrorPage(request, "errors/chapter_duplicate.html", {"chapter_name": chapter, "book": book})
        except:
            transaction.rollback()
            raise
        else:
            transaction.commit()

    try:
        resp = render_to_response('reader/book_chapter.html', {"chapter": chapter, 
                                                               "book": book, 
                                                               "book_version": book_version.getVersion(),
                                                               "chapters": chapters, 
                                                               "has_css": _customCSSExists(book.url_title),
                                                               "request": request, 
                                                               "content": content})
    except:
        transaction.rollback()
    else:
        transaction.commit()

    return resp
Beispiel #12
0
def book_view(request, bookid, version=None):
    """
    Django View. Shows main book page for the published version of a book.

    @type request: C{django.http.HttpRequest}
    @param request: Client Request object
    @type bookid: C{string}
    @param bookid: Unique Book ID
    @type version: C{string}
    @param verson: Book version
    """

    try:
        book = models.Book.objects.get(url_title__iexact=bookid)
    except models.Book.DoesNotExist:
        try:
            resp = pages.ErrorPage(request, "errors/book_does_not_exist.html", {"book_name": bookid})
        except:
            transaction.rollback()
            raise
        else:
            transaction.commit()

        return resp

    book_version = book.getVersion(version)

    from booki.utils import security
    bookSecurity = security.getUserSecurityForBook(request.user, book)

    hasPermission = security.canEditBook(book, bookSecurity)

    if book.hidden and not hasPermission:
        try:
            resp = pages.ErrorPage(request, "errors/no_permissions.html")
        except:
            transaction.rollback()
            raise
        else:
            transaction.commit()

        return resp

    chapters = []
    firstChapter = None

    for chapter in  models.BookToc.objects.filter(version=book_version).order_by("-weight"):
        if chapter.isChapter():
            if not firstChapter:
                firstChapter = chapter.chapter

            chapters.append({"url_title": chapter.chapter.url_title,
                             "name": chapter.chapter.title})
        else:
            chapters.append({"url_title": None,
                             "name": chapter.name})
        

    try:
        if firstChapter:
            resp = redirect('book_chapter', bookid = book.url_title, chapter=firstChapter.url_title) 
        else:
            resp = render_to_response('reader/book_view.html', {"book": book, 
                                                                "book_version": book_version.getVersion(),
                                                                "chapters": chapters, 
                                                                "has_css": _customCSSExists(book.url_title),
                                                                "request": request})
    except:
        transaction.rollback()
        raise
    else:
        transaction.commit()

    return resp
Beispiel #13
0
def draft_book(request, bookid, version=None):
    """
    Django View. Shows main page for the draft version of a book.

    @type request: C{django.http.HttpRequest}
    @param request: Client Request object
    @type bookid: C{string}
    @param bookid: Unique Book ID
    @type version: C{string}
    @param verson: Book version
    """

    try:
        book = models.Book.objects.get(url_title__iexact=bookid)
    except models.Book.DoesNotExist:
        try:
            resp = pages.ErrorPage(request, "errors/book_does_not_exist.html", {"book_name": bookid})
        except:
            transaction.rollback()
            raise
        else:
            transaction.commit()

        return resp

    book_version = book.getVersion(version)

    from booki.utils import security
    bookSecurity = security.getUserSecurityForBook(request.user, book)

    hasPermission = security.canEditBook(book, bookSecurity)

    if book.hidden and not hasPermission:
        try:
            resp = pages.ErrorPage(request, "errors/no_permissions.html")
        except:
            transaction.rollback()
            raise
        else:
            transaction.commit()

        return resp

    chapters = []
    firstChapter = None

    for chapter in  models.BookToc.objects.filter(version=book_version).order_by("-weight"):
        if chapter.isChapter():
            if not firstChapter:
                firstChapter = chapter.chapter

            chapters.append({"url_title": chapter.chapter.url_title,
                             "name": chapter.chapter.title})
        else:
            chapters.append({"url_title": None,
                             "name": chapter.name})
        

    try:
        editingEnabled = False

        if request.user.is_authenticated() and book.version == book_version:
            editingEnabled = True

        if firstChapter:
            resp = redirect('draft_chapter', bookid = book.url_title, version=book_version.getVersion(), chapter=firstChapter.url_title) 
        else:
            resp = render_to_response('reader/draft_book.html', {"book": book, 
                                                                 "book_version": book_version.getVersion(),
                                                                 "chapters": chapters, 
                                                                 "editing_enabled": editingEnabled,
                                                                 "has_css": _customCSSExists(book.url_title),
                                                                 "request": request})
    except:
        transaction.rollback()
        raise
    else:
        transaction.commit()

    return resp
Beispiel #14
0
def book_info(request, bookid, version=None):
    """
    Django View. Shows single page with all the Book info.

    @type request: C{django.http.HttpRequest}
    @param request: Client Request object
    @type bookid: C{string}
    @param bookid: Unique Book ID
    @type version: C{string}
    @param verson: Book version
    """

    try:
        book = models.Book.objects.get(url_title__iexact=bookid)
    except models.Book.DoesNotExist:
        try:
            resp = pages.ErrorPage(request, "errors/book_does_not_exist.html", {"book_name": bookid})
        except:
            transaction.rollback()
            raise
        else:
            transaction.commit()

        return resp

    book_version = book.getVersion(version)

    book_history =  models.BookHistory.objects.filter(version = book_version).order_by("-modified")[:20]

    book_collaborators =  [e.values()[0] for e in models.BookHistory.objects.filter(version = book_version, kind = 2).values("user__username").distinct()]

    from booki.utils import security
    bookSecurity = security.getUserSecurityForBook(request.user, book)
    isBookAdmin = bookSecurity.isAdmin()
    
    import sputnik
    channel_name = "/booki/book/%s/%s/" % (book.id, book_version.getVersion())
    online_users = sputnik.smembers("sputnik:channel:%s:users" % channel_name)

    book_versions = models.BookVersion.objects.filter(book=book).order_by("created")

    from django.utils.html import escape
    bookDescription = escape(book.description)

    try:
        resp = render_to_response('reader/book_info.html', {"book": book, 
                                                            "book_version": book_version.getVersion(),
                                                            "book_versions": book_versions,
                                                            "book_history": book_history, 
                                                            "book_collaborators": book_collaborators,
                                                            "has_css": _customCSSExists(book.url_title),
                                                            "is_book_admin": isBookAdmin,
                                                            "online_users": online_users,
                                                            "book_description": '<br/>'.join(bookDescription.replace('\r','').split('\n')),
                                                            "request": request})
    except:
        transaction.rollback()
        raise
    else:
        transaction.commit()

    return resp
Beispiel #15
0
def edit_book(request, bookid, version=None):
    """
    Django View. Default page for Booki editor.

    @param request: Django Request
    @param bookid: Book ID
    @param version: Book Version or None
    """

    from booki.utils import security

    try:
        book = models.Book.objects.get(url_title__iexact=bookid)
    except models.Book.DoesNotExist:
        return pages.ErrorPage(request, "errors/book_does_not_exist.html", {"book_name": bookid})

    """%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"""
    """
    Modificacion UCSP
    """
    discussions_list = models.DiscussionTheme.objects.all().order_by('-created')
    group = book.group
    members = []
    if (group):
        members = group.members.all()
    is_member = request.user in members


    """%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"""

    book_version = book.getVersion(version)

    bookSecurity = security.getUserSecurityForBook(request.user, book)

    hasPermission = security.canEditBook(book, bookSecurity)

    if not hasPermission:
        return pages.ErrorPage(request, "errors/editing_forbidden.html", {"book": book})    

    chapters = models.Chapter.objects.filter(version=book_version)

    tabs = ["chapters"]
    #tabs += ["attachments"]
    tabs += ["covers"]
    #tabs += ["history", "versions", "notes"]
    tabs += ["history", "notes"]

    if bookSecurity.isAdmin():
        tabs += ["settings"]

    #tabs += ["export"]
    tabs += ["discussions"]

    isBrowserSupported = True
    browserMeta = request.META.get('HTTP_USER_AGENT', '')

    if browserMeta.find('MSIE') != -1:
        isBrowserSupported = False

    try:
        publish_options = settings.PUBLISH_OPTIONS
    except AttributeError:
        from booki import constants
        publish_options = constants.PUBLISH_OPTIONS
        
    return render_to_response('editor/edit_book.html', {"book": book, 
                                                        "book_version": book_version.getVersion(),
                                                        "version": book_version,

                                                        ## this change will break older versions of template
                                                        "statuses": [(s.name.replace(' ', '_'), s.name) for s in models.BookStatus.objects.filter(book = book)],
                                                        "chapters": chapters, 
                                                        "security": bookSecurity,
                                                        "is_admin": bookSecurity.isGroupAdmin() or bookSecurity.isBookAdmin() or bookSecurity.isSuperuser(),
                                                        "is_owner": book.owner == request.user,
                                                        "tabs": tabs,
                                                        "is_browser_supported": isBrowserSupported,
                                                        "publish_options": publish_options,
                                                        "discussions_list": discussions_list, #MODIFICATION UCSP
                                                        "is_member": is_member, #MODIFICATION UCSP
                                                        "request": request})
Beispiel #16
0
def draft_chapter(request, bookid, chapter, version=None):
    """
    Django View. Shows chapter for the draft version of a book.

    @type request: C{django.http.HttpRequest}
    @param request: Client Request object
    @type bookid: C{string}
    @param bookid: Unique Book ID
    @type chapter: C{string}
    @param chapter: Chapter name
    @type version: C{string}
    @param verson: Book version
    """

    try:
        book = models.Book.objects.get(url_title__iexact=bookid)
    except models.Book.DoesNotExist:
        try:
            resp =pages.ErrorPage(request, "errors/book_does_not_exist.html", {"book_name": bookid})
        except:
            transaction.rollback()
            raise
        else:
            transaction.commit()

        return resp

    book_version = book.getVersion(version)

    from booki.utils import security
    bookSecurity = security.getUserSecurityForBook(request.user, book)

    hasPermission = security.canEditBook(book, bookSecurity)

    if book.hidden and not hasPermission:
        try:
            resp = pages.ErrorPage(request, "errors/no_permissions.html")
        except:
            transaction.rollback()
            raise
        else:
            transaction.commit()

        return resp


    chapters = []
    subchapters = []

    for chap in  models.BookToc.objects.filter(version=book_version).order_by("-weight"):
        if chap.isChapter():
            chapters.append({"url_title": chap.chapter.url_title,
                             "name": chap.chapter.title,
                             "isSection": 0,
                             "id":  chap.chapter.id,
                             "parent":chap.chapter.parent})
            for subchap in  models.Chapter.objects.filter(parent=chap.chapter.id).order_by("sc_position"):
                    subchapters.append({"url_title": subchap.url_title,
                                 "name": subchap.title,
                                 "isSection": 0,
                                 "parent":subchap.parent})
        else:
            chapters.append({"url_title": chap.chapter.url_title,
                             "name": chap.name,
                             "isSection": 1,
                             "parent":0})




    try:
        content = models.Chapter.objects.get(version=book_version, url_title = chapter)
    except models.Chapter.DoesNotExist:
        try:
            resp = pages.ErrorPage(request, "errors/chapter_does_not_exist.html", {"chapter_name": chapter, "book": book})
        except:
            transaction.rollback()
            raise
        else:
            transaction.commit()

        return resp
    except models.Chapter.MultipleObjectsReturned:
        try:
            resp = pages.ErrorPage(request, "errors/chapter_duplicate.html", {"chapter_name": chapter, "book": book})
        except:
            transaction.rollback()
            raise
        else:
            transaction.commit()

        return resp
        
    try:
        editingEnabled = False

        if request.user.is_authenticated() and book.version == book_version:
            editingEnabled = True

        resp = render_to_response('reader/draft_chapter.html', {"chapter": chapter, 
                                                                "book": book, 
                                                                "book_version": book_version.getVersion(),
                                                                "chapters": chapters, 
								"subchapters": subchapters,
                                                                "editing_enabled": editingEnabled,
                                                                "has_css": _customCSSExists(book.url_title),
                                                                "request": request, 
                                                                "content": content})
    except:
        transaction.rollback()
        raise
    else:
        transaction.commit()

    return resp