import datetime

from booki.utils.misc import bookiSlugify
from django.utils.translation import ugettext_lazy as _

from booki.editor import models
from booki.utils.log import logBookHistory


def checkBookAvailability(bookTitle):
    """
    Checks if the book name is available or not.

    @type bookTitle: C{string}
    @param bookTitle: Title for the book.

    @rtype: C{bool}
    @return: Returns true or false
    """

    url_title = bookiSlugify(bookTitle)

    if url_title == '':
        return False

    try:
        book = models.Book.objects.get(url_title = url_title)
    except models.Book.DoesNotExist:
        return True

    return False


def createBook(user, bookTitle, status = "imported", bookURL = None):
    """
    Creates book.

    @todo: Do something about status.

    @type user: C{django.contrib.auth.models.User}
    @param user: Booki user who will be book owner
    @type bookTitle: C{string}
    @param bookTitle: Title for the book. If bookURL is omitted it will slugify title for the url version
    @type status: C{string} 
    @param status: String name for the status (optional)
    @type bookURL: C{string}
    @param bookURL: URL title for the book (optional)

    @rtype: C{booki.editor.models.Book}
    @return: Returns book object
    """

    if bookURL:
        url_title = bookURL
    else:
        url_title = bookiSlugify(bookTitle)

    book = models.Book(url_title = url_title,
                       title = bookTitle,
                       owner = user, 
                       published = datetime.datetime.now())

    book.save()

    # put this in settings file
    status_default = ["published", "not published", "imported"]
    n = len(status_default)

    for statusName in status_default:
        status = models.BookStatus(book=book, name=statusName, weight=n)
        status.save()
        n -= 1

    # not use "not published" but first in the list maybe, or just status
    book.status = models.BookStatus.objects.get(book=book, name="not published")
    book.save()
    
    
    version = models.BookVersion(book = book,
                                 major = 1,
                                 minor = 0,
                                 name = 'initial',
                                 description = '')
    version.save()

    book.version = version
    book.save()

    logBookHistory(book = book, 
                   version = version,
                   user = user,
                   kind = 'book_create')

    import booki.editor.signals    
    booki.editor.signals.book_created.send(sender = user, book = book)

    return book


class BookiGroupExist(Exception):
    def __init__(self, groupName):
        self.groupName = groupName

    def __str__(self):
        return 'Booki group already exists'

def createBookiGroup(groupName, groupDescription, owner):
    """
    Create Booki Group.

    @type groupName: C{string}
    @param groupName: Group name
    @type groupDescription: C{string}
    @param groupDescription: Group name
    @type owner: C{django.contrib.auth.models.User}
    @param owner: Group owner

    @rtype: C{booki.editor.models.BookiGroup}
    @return: Returns group object
    """

    import datetime

    try:
        bg = models.BookiGroup.objects.get(url_name = bookiSlugify(groupName))
    except models.BookiGroup.MultipleObjectsReturned:
        raise BookiGroupExist(groupName)
    except models.BookiGroup.DoesNotExist:
        group = models.BookiGroup(name = groupName,
                                  url_name = bookiSlugify(groupName),
                                  description = groupDescription,
                                  owner = owner,
                                  created = datetime.datetime.now())
        group.save()

        return group

    raise BookiGroupExist(groupName)


    

@@ -107,16 +107,47 @@
 azy as _
+%0Afrom django.db.models import Q
 %0A%0Afrom b
@@ -582,24 +582,47 @@
 objects.get(
+Q(title=bookTitle) %7C Q(
 url_title = 
@@ -631,16 +631,17 @@
 l_title)
+)
 %0A    exc
@@ -3879,14 +3879,513 @@
 pName)%0A%0A
+def checkGroupAvailability(groupName):%0A    %22%22%22%0A    Checks if the group name is available or not.%0A%0A    @type bookName: C%7Bstring%7D%0A    @param bookName: Name of the group.%0A%0A    @rtype: C%7Bbool%7D%0A    @return: Returns true or false%0A    %22%22%22%0A%0A    url_name = bookiSlugify(groupName)%0A%0A    if url_name == '':%0A        return False%0A%0A    try:%0A        group = models.BookiGroup.objects.get(Q(name=groupName) %7C Q(url_name = url_name))%0A    except models.BookiGroup.DoesNotExist:%0A        return True%0A%0A    return False%0A%0A
 %0A    %0A

Add group availability function.