Exemple #1
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),
    )
Exemple #2
0
def frontpage(request):

    # check all active online users and what are they doing
    import sputnik

    clientList = sputnik.rkeys("ses:*:username")
    onlineUsers = []

    for us in clientList:
        clientID = us[4:-9]

        channelList = []
        for chan in sputnik.smembers("ses:%s:channels" % clientID):
            if chan.startswith("/booki/book/"):
                _s = chan.split("/")
                if len(_s) > 3:
                    bookID = _s[3]
                    b = models.Book.objects.get(pk=bookID)
                    channelList.append(b)

        _u = sputnik.get(us)
        onlineUsers.append((_u, channelList))

    # Check the attachment size.
    # This should not be here in the future. It takes way too much time.
    from booki.utils import misc

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

    # Number of books and number of groups
    number_of_books = len(models.Book.objects.all())
    number_of_groups = len(models.BookiGroup.objects.all())

    # Number of all users on the system.
    # This should somehow check only the active users
    from django.contrib.auth.models import User

    number_of_users = len(User.objects.all())

    # check the database size
    from django.db import connection

    cursor = connection.cursor()

    try:
        # This will not work if user has new style of configuration for the database
        # This will also only work for PostgreSQL. Should make another method for checking sqlite database size.
        cursor.execute("SELECT pg_database_size(%s)", [settings.DATABASES["default"]["NAME"]])
        databaseSize = cursor.fetchone()[0]
    except:
        databaseSize = 0

    # Book activity
    activityHistory = models.BookHistory.objects.filter(kind__in=[1, 10]).order_by("-modified")[:20]

    # Booktype version
    import booki

    booktypeVersion = ".".join([str(num) for num in booki.version])

    return render_to_response(
        "booktypecontrol/frontpage.html",
        {
            "request": request,
            "booktype_version": booktypeVersion,
            "admin_options": ADMIN_OPTIONS,
            "online_users": onlineUsers,
            "attachments_size": attachmentsSize,
            "number_of_books": number_of_books,
            "number_of_users": number_of_users,
            "number_of_groups": number_of_groups,
            "database_size": databaseSize,
            "activity_history": activityHistory,
        },
    )