def add_book(request, isbn=None, template_name="ebooks/add/index.html", *args, **kwargs): """ Begins the add book wizard process """ from q.ebooks.forms import BookForm ctx = {} if request.POST.has_key('title'): book = models.Book() book.title = request.POST['title'] book.tags = request.POST['tags'] book.isbn10 = request.POST['isbn10'] book.isbn13 = request.POST['isbn13'] book.gid = request.POST['gid'] book.description = request.POST['description'] book.save() for gauthor in request.POST['authors'].split(','): try: author = models.Author.objects.get(firstname=" ".join(gauthor.split(" ")[:-1]).strip(), lastname=gauthor.split(" ")[-1]) except models.Author.DoesNotExist, e: author = models.Author() author.firstname = " ".join(gauthor.split(" ")[:-1]).strip() author.lastname = gauthor.split(" ")[-1] author.save() book.authors.add(author) if book.cover == "": cover_link = request.POST['cover_url'] headers = {'User-Agent': settings.DEFAULT_HTTP_HEADERS} f = NamedTemporaryFile(delete=False) f.write(urllib2.urlopen(urllib2.Request(cover_link, headers=headers)).read()) f.filename = f.name f.close() book.cover.save( "temp_filename.jpg", File(open(f.name)) ) os.unlink(f.name) if request.POST["series"] != "": series_name = request.POST["series"] series_num = request.POST["series_num"] try: series = models.Series.objects.get(name__exact=series_name) except models.Series.DoesNotExist, e: series = models.Series() series.name = series_name series.save() book.series = series book.series_num = series_num
def csv_file(): # create a tmp csv file csv = NamedTemporaryFile("w+", suffix=".csv", prefix="tmp_", delete=False) csv.write("[email protected];userone;userone;manager\n") csv.write("[email protected];usertwo;usertwo;member\n") csv.write("[email protected];userseven;userseven;member\n") # writing a wrong line csv.write("[email protected];userthree;userthree\n") csv.write("example.com;example;userfour;member\n") csv.seek(0) csv.filename = csv.name return csv
def book_info(request, template_name="ebooks/book_info.html", *args, **kwargs): """ Display the information for the book """ ctx = {} book_slug = kwargs.get('book_slug') book = get_object_or_404(models.Book, slug=book_slug) error = None if request.FILES.has_key("book"): # We are getting a book uploaded filename = request.FILES["book"].name ext = os.path.splitext(filename)[1].replace('.','') if (ext, ext) not in models.FORMAT_CHOICES: error = "invalid filetype: %s" % filename if not error: try: format = models.Format.objects.get(ebook=book, format=ext) error = "Format exists: %s" % format.format except models.Format.DoesNotExist, e: format = models.Format() format.ebook = book format.format = ext format.uploaded_by = request.user f = NamedTemporaryFile(delete=False) f.write(request.FILES["book"].read()) f.filename = filename f.close() format.ebook_file.save( "temp_filename.%s" % ext, File(open(f.name)) ) format.save() os.unlink(f.name) #activity stream create_activity_item('upload', request.user, format)
def out_file_path(self): """This method creates the output file and outputs the temporary path. Note that the process happens is several passes. """ self.get_works_and_writers() # the second pass of processing self.file.seek(0) csv_reader = csv.reader(self.file) f = NamedTemporaryFile(mode='w+', delete=False, encoding='utf8') csv_writer = csv.writer(f) csv_writer.writerow(self.fieldnames) for row in csv_reader: for out_row in self.process_row(row): csv_writer.writerow(out_row) f.filename = self.filename f.close() return f.name
if id.startswith("ISBN:"): isbn = id.replace("ISBN:", "") if len(isbn) == 13 and self.isbn13 == "": self.isbn13 = isbn elif len(isbn) == 10 and self.isbn10 == "": self.isbn10 = isbn else: if self.gid == "": self.gid = id if cover_link and self.cover is None or self.cover == "" and save_cover: headers = {'User-Agent': settings.DEFAULT_HTTP_HEADERS} f = NamedTemporaryFile(delete=False) f.write(urllib2.urlopen(urllib2.Request(cover_link, headers=headers)).read()) f.filename = f.name f.close() self.cover.save( "temp_filename.jpg", File(open(f.name)) ) os.unlink(f.name) def save(self, *args, **kwargs): cache_book_info=False if kwargs.has_key('cache_book_info'): cache_book_info = bool(kwargs['cache_book_info']) if self.gid != "" and cache_book_info: self.cache_book_info(self.gid)
def add_book(request, isbn=None, template_name="ebooks/add/index.html", *args, **kwargs): """ Begins the add book wizard process """ from ebooks.forms import BookForm ctx = {} book_form = BookForm() book = models.Book() if request.POST.has_key('title'): try: book = models.Book.objects.get(isbn13=request.POST['isbn13']) except models.Book.DoesNotExist: book = None if book is not None: messages.error(request, "You are trying to add a duplicate book.") ctx.update({'book_form': BookForm(request.POST)}) return render_to_response(template_name, RequestContext(request, ctx)) book = models.Book() book.title = request.POST['title'] book.isbn10 = request.POST['isbn10'] book.isbn13 = request.POST['isbn13'] book.gid = request.POST['gid'] book.description = request.POST['description'] book.tags = '' #book.save() #book.tags = request.POST['tags'] book.save() for gauthor in request.POST['authors'].split(','): try: author = models.Author.objects.get(firstname=" ".join(gauthor.split(" ")[:-1]).strip(), lastname=gauthor.split(" ")[-1]) except models.Author.DoesNotExist: author = models.Author() author.firstname = " ".join(gauthor.split(" ")[:-1]).strip() author.lastname = gauthor.split(" ")[-1] author.save() book.authors.add(author) if book.cover == "" and request.POST.has_key('cover_url') and request.POST['cover_url'] != "": cover_link = request.POST['cover_url'] headers = {'User-Agent': settings.DEFAULT_HTTP_HEADERS} f = NamedTemporaryFile(delete=False) f.write(urllib2.urlopen(urllib2.Request(cover_link, headers=headers)).read()) f.filename = f.name f.close() book.cover.save( "temp_filename.jpg", File(open(f.name)) ) os.unlink(f.name) if request.POST["series"] != "": series_name = request.POST["series"] series_num = request.POST["series_num"] try: series = models.Series.objects.get(name__exact=series_name) except models.Series.DoesNotExist: series = models.Series() series.name = series_name series.save() book.series = series book.series_num = series_num book.save() return HttpResponseRedirect(reverse(book_info, kwargs={'book_slug': book.slug})) if request.POST.has_key("isbn") and isbn is None: isbn = re.sub("\D", "", request.POST['isbn']) if isbn: from gdata.books import BookFeed from urllib2 import urlopen search_xml = urlopen("http://books.google.com/books/feeds/volumes?q=ISBN%s" % isbn).read() search_feed = BookFeed.FromString(search_xml) google_id = search_feed.entry[0].identifier[0].text #volume_xml = urlopen("http://www.google.com/books/feeds/volumes/%s" % google_id).read() #book_feed = Book.FromString(volume_xml) #cover_link = book_feed.GetThumbnailLink().href.replace('zoom=5','zoom=1') book.cache_book_info(google_id, save_cover=False) book_form = BookForm( { 'title':book.title, 'authors': ",".join(book._authors), 'isbn10': book.isbn10, 'isbn13': book.isbn13, 'gid': book.gid, 'description': book.description, 'metarating': 0.0 if book.metarating is None else book.metarating, } ) # Dupe check try: book = models.Book.objects.get(isbn13=book.isbn13) messages.error(request, "You are trying to add a duplicate book.") except models.Book.DoesNotExist: pass ctx.update({'book': book, 'book_form': book_form}) return render_to_response(template_name, RequestContext(request, ctx))
def book_info(request, template_name="ebooks/book_info.html", *args, **kwargs): """ Display the information for the book """ ctx = {} book_slug = kwargs.get('book_slug') book = get_object_or_404(models.Book, slug=book_slug) error = None if request.FILES.has_key("book"): # We are getting a book uploaded filename = request.FILES["book"].name ext = os.path.splitext(filename)[1].replace('.','') if (ext, ext) not in models.FORMAT_CHOICES: error = "invalid filetype: %s" % filename if not error: try: format = models.Format.objects.get(ebook=book, format=ext) error = "Format exists: %s" % format.format except models.Format.DoesNotExist: format = models.Format() format.ebook = book format.format = ext format.uploaded_by = request.user f = NamedTemporaryFile(delete=False) f.write(request.FILES["book"].read()) f.filename = filename f.close() format.ebook_file.save( "temp_filename.%s" % ext, File(open(f.name)) ) format.save() os.unlink(f.name) #activity stream action.send(request.user, verb="uploaded", action_object=format, target=format.ebook) # see if the logged in user has read this book to display the check try: read = models.Read.objects.get(user=request.user, book=book) except models.Read.DoesNotExist: read = None checkouts = models.CheckOut.objects.filter(book__book=book).order_by('-create_time') format_form = forms.UploadFormatForm() try: my_ownership = models.Ownership.objects.get(book=book, user=request.user) except models.Ownership.DoesNotExist: my_ownership = None ctx.update( { 'book': book, 'checkouts':checkouts, 'my_ownership': my_ownership, 'format_form': format_form, 'error': error, 'read': read, } ) return render_to_response(template_name, RequestContext(request, ctx))