示例#1
0
def my_books (request, username):
    """
    Django View. Show user books.

    @type request: C{django.http.HttpRequest}
    @param request: Django Request
    @type username: C{string}
    @param username: Username.
    """

    from django.contrib.auth.models import User
    from booki.editor import models

    try:
        user = User.objects.get(username=username)
    except User.DoesNotExist:
        return pages.ErrorPage(request, "errors/user_does_not_exist.html", {"username": username})
        
    books = models.Book.objects.filter(owner=user)

    if request.POST.get("action") == "hide":
        book = models.Book.objects.get(url_title=request.POST.get("book"))
        book.hidden = True
        book.save()
        transaction.commit()
    elif request.POST.get("action") == "unhide":
        book = models.Book.objects.get(url_title=request.POST.get("book"))
        book.hidden = False
        book.save()
        transaction.commit()

    if request.method == 'POST' and not request.POST.get("action"):
        project_form = BookForm(request.POST)
        import_form = ImportForm(request.POST)

        if import_form.is_valid() and import_form.cleaned_data["id"]:
            project_form = BookForm() # reset the other form

            try:
                ID = import_form.cleaned_data["id"]
                import_type = import_form.cleaned_data["type"]
                rename_title = import_form.cleaned_data["rename_title"]

                extraOptions = {}
                if rename_title:
                    extraOptions['book_title'] = rename_title

                if import_form.cleaned_data["hidden"]:
                    extraOptions['hidden'] = True

                import_sources = {   # base_url    source=
                    'flossmanuals': (TWIKI_GATEWAY_URL, "en.flossmanuals.net"),
                    "archive":      (ESPRI_URL, "archive.org"),
                    "wikibooks":    (ESPRI_URL, "wikibooks"),
                    "epub":         (ESPRI_URL, "url"),
                    }

                if import_type == "booki":
                    ID = ID.rstrip("/")
                    booki_url, book_url_title = ID.rsplit("/", 1)
                    base_url = "%s/export/%s/export" % (booki_url, book_url_title)
                    source = "booki"
                else:
                    base_url, source = import_sources[import_type]

                common.importBookFromUrl2(user, base_url,
                                          args=dict(source=source,
                                                    book=ID),
                                          **extraOptions
                                          )
            except Exception:
                transaction.rollback()
                logError(traceback.format_exc())
                return render_to_response('account/error_import.html',
                                          {"request": request, "user": user})
            else:
                transaction.commit()

        #XXX should this be elif? even if the POST is valid as both forms, the
        # transaction will end up being commited twice.
        if project_form.is_valid() and project_form.cleaned_data["title"]:
            import_form = ImportForm() # reset the other form
            
            from booki.utils.book import createBook
            title = project_form.cleaned_data["title"]

            try:
                book = createBook(user, title)

                license   = project_form.cleaned_data["license"]
                lic = models.License.objects.get(abbrevation=license)
                book.license = lic
                book.hidden = project_form.cleaned_data["hidden"]
                book.save()
            except:
                transaction.rollback()
            else:
                transaction.commit()

            return HttpResponseRedirect(reverse("my_books", args=[username]))
    else:
        project_form = BookForm()
        import_form = ImportForm()


    try:
        return render_to_response('account/my_books.html', {"request": request,
                                                            "user": user,
                                                            
                                                            "project_form": project_form,
                                                            "import_form": import_form,
                                                            
                                                            "books": books,})
    except:
        transaction.rollback()
    finally:
        transaction.commit()
示例#2
0
文件: views.py 项目: tuukka/booki-old
def my_books (request, username):
    """
    Django View. Show user books.

    @type request: C{django.http.HttpRequest}
    @param request: Django Request
    @type username: C{string}
    @param username: Username.
    """

    from django.contrib.auth.models import User
    from booki.editor import models
    from django.template.defaultfilters import slugify

    try:
        user = User.objects.get(username=username)
    except User.DoesNotExist:
        return pages.ErrorPage(request, "errors/user_does_not_exist.html", {"username": username})
        
    books = models.Book.objects.filter(owner=user)

    if request.method == 'POST':
        project_form = BookForm(request.POST)
        import_form = ImportForm(request.POST)

        if import_form.is_valid() and import_form.cleaned_data["id"]:
            try:
                ID = import_form.cleaned_data["id"]
                import_type = import_form.cleaned_data["type"]

                import_sources = {   # base_url    source=
                    'flossmanuals': (TWIKI_GATEWAY_URL, "en.flossmanuals.net"),
                    "archive":      (ESPRI_URL, "archive.org"),
                    "wikibooks":    (ESPRI_URL, "wikibooks"),
                    "epub":         (ESPRI_URL, "url"),
                    }
                base_url, source = import_sources[import_type]
                common.importBookFromUrl2(user, base_url,
                                          source=source,
                                          book=ID
                                          )
            except Exception:
                transaction.rollback()
                logError(traceback.format_exc())
                return render_to_response('account/error_import.html',
                                          {"request": request, "user": user})
            else:
                transaction.commit()

        #XXX should this be elif? even if the POST is valid as both forms, the
        # transaction will end up being commited twice.
        if project_form.is_valid() and project_form.cleaned_data["title"] != "":
            from booki.utils.book import createBook
            title = project_form.cleaned_data["title"]

            try:
                book = createBook(user, title)

                license   = project_form.cleaned_data["license"]
                lic = models.License.objects.get(abbrevation=license)
                book.license = lic
                book.save()
            except:
                transaction.rollback()
            else:
                transaction.commit()

            return HttpResponseRedirect(reverse("my_books", args=[username]))
    else:
        project_form = BookForm()
        import_form = ImportForm()


    return render_to_response('account/my_books.html', {"request": request,
                                                            "user": user,

                                                            "project_form": project_form,
                                                            "import_form": import_form,

                                                            "books": books,})