Ejemplo n.º 1
0
def add_doc_form(request):
    """
    Displays form for converting and adding book in doc
    """
    if request.method == "POST":
        form = DocCreateForm(request.POST, request.FILES)
        if form.is_valid():
            temp_name = tempfile.mkstemp()[1]
            cd = form.cleaned_data
            properties = BookProperties(temp_name, cd['title'].encode("utf-8"))
            properties.language = cd['language']
            properties.author = request.user.first_name + ' ' + request.user.last_name

            properties.subject = cd['subject'].encode("utf-8")
            properties.description = cd['description'].encode("utf-8")
            properties.genre = cd['type'].encode("utf-8")
            properties.date = cd['date'].encode("utf-8")
            properties.rights = cd['rights'].encode("utf-8")
            properties.author = request.user.first_name + " " + request.user.last_name

            file = request.FILES['file']
            
            doc_file_name = tempfile.mkstemp()[1]
            doc_file = open(doc_file_name, 'wb')
            doc_file.write(file.read())
            doc_file.close()
            doc_parser = DocParser(doc_file_name, properties)
            out_file_name = tempfile.mkdtemp() + "tmp.html"
            doc_parser.parse()

            try:
                language = Language.objects.get(short=cd['language'])
            except:
                return render_response(request, "epub/add_doc_form.html", {'errors': [_('No such language.')],
                                                                      'user': request.user,
                                                                      'menu_on': True,
                                                                           'form': form})

            lang_code = LANG_CODE[0]
            for lang_code in LANG_CODE:
                if lang_code[0] == cd['language']:
                    break
            
            book_model = Book(language=language, title = cd['title'].encode("utf-8"), pagelink="")
            try:
                author = Author.objects.get(name=request.user.first_name + 
                                            ' ' + request.user.last_name)
            except:
                author = Author.objects.create(name=request.user.first_name + 
                                            ' ' + request.user.last_name)
            book_model.save()
            book_model.author = [author]
            
            ebook = EpubBook.objects.create(book=book_model)
            print "creating"
            ebook.name.save(cd['title'].encode("utf-8") + ".epub", _ExistingFile(temp_name))
            

            try:
                book_file = BookFile(link="/" + MEDIA_URL + "/uploads/" + quote(cd['title'].encode("utf-8")) + '.epub')
                book_file.type = 'epub'
                book_file.save()
            except:
                return render_response(request, "epub/add_doc_form.html", 
                                       {'errors': [_("Book name should be unique")],
                                        'menu_on': True, 'form': form})
            book_model.book_file = [book_file]
            book_model.save()
            ebook.save()
            hrr =  HttpResponseRedirect("/book/id%d" % book_model.id)
            return hrr
    else:
        form = DocCreateForm()

    return render_response(request, "epub/add_doc_form.html", {'user': request.user, 'menu_on': True, 'form': form})
Ejemplo n.º 2
0
def epub_form(request):
    """
    Displays form for converting from txt files and adding into db
    """
    messages = []
    if request.method == "POST":
        form = EpubAddForm(request.POST, request.FILES)
        if form.is_valid():
            cd = form.cleaned_data
            book_name = cd['caption']
            lang = cd['language']
            tags = cd['tags']
            subject = cd['subject']
            description = cd['description']
            type = cd['type']
            date = cd['date']
            rights = cd['rights']
            
            temp_name = tempfile.mkstemp()[1]
            properties = BookProperties(temp_name, book_name)
            properties.language = lang
            properties.subject = subject
            properties.description = description
            properties.genre = type
            properties.date = date
            properties.rights = rights
            properties.author = request.user.first_name + " " + request.user.last_name
            book = BookCreator(properties)
            file = request.FILES['file']
            toc = cd['toc']
            if toc:
                book.split_by_toc(toc, file.read())
            else:
                book.txt_to_html(file.read(), temp_name + "text.html")
                book.add_file(temp_name + "text.html", "c1", "")
                
            book.pack()

            try:
                language = Language.objects.get(short=lang)
            except:
                return render_response(request, "epub/epub_form.html", 
                                       {'errors': [_('No such language.')],
                                        'user': request.user, 'menu_on': True,
                                        'form': form})

            lang_code = LANG_CODE[0]
            for lang_code in LANG_CODE:
                if lang_code[0] == lang:
                    break
            
            book_model = Book.objects.create(language=language, pagelink="")
            book_model.title = book_name
            try:
                author = Author.objects.get(name=request.user.first_name + 
                                            ' ' + request.user.last_name)
            except:
                author = Author.objects.create(name=request.user.first_name + 
                                            ' ' + request.user.last_name)
            book_model.author = [author]
            book_model.save()
            
            ebook = EpubBook.objects.create(book = book_model)

            exfile = _ExistingFile(temp_name)
            ebook.name.save(book_name + ".epub", exfile)
            print ebook.name
            try:
                book_file = BookFile(link="/" + MEDIA_URL + "/" + quote(str(ebook.name)))
                book_file.type = 'epub'
                book_file.save()
            except Exception, e:
                print e
                return render_response(request, "epub/epub_form.html",
                                       {'errors': [_("Book name should be unique")],
                                        'menu_on': True, 'form': form})
            book_model.book_file = [book_file]
            book_model.save()
            
            
            ebook.save()
            
            messages += [_("Book successfully created.")]
            hrr =  HttpResponseRedirect("/book/id%d" % book_model.id)
            return hrr