本文整理汇总了Python中tempfile.NamedTemporaryFile.filename方法的典型用法代码示例。如果您正苦于以下问题:Python NamedTemporaryFile.filename方法的具体用法?Python NamedTemporaryFile.filename怎么用?Python NamedTemporaryFile.filename使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tempfile.NamedTemporaryFile
的用法示例。
在下文中一共展示了NamedTemporaryFile.filename方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: add_book
# 需要导入模块: from tempfile import NamedTemporaryFile [as 别名]
# 或者: from tempfile.NamedTemporaryFile import filename [as 别名]
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
示例2: csv_file
# 需要导入模块: from tempfile import NamedTemporaryFile [as 别名]
# 或者: from tempfile.NamedTemporaryFile import filename [as 别名]
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
示例3: book_info
# 需要导入模块: from tempfile import NamedTemporaryFile [as 别名]
# 或者: from tempfile.NamedTemporaryFile import filename [as 别名]
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)
示例4: len
# 需要导入模块: from tempfile import NamedTemporaryFile [as 别名]
# 或者: from tempfile.NamedTemporaryFile import filename [as 别名]
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)
示例5: add_book
# 需要导入模块: from tempfile import NamedTemporaryFile [as 别名]
# 或者: from tempfile.NamedTemporaryFile import filename [as 别名]
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,
#.........这里部分代码省略.........
示例6: book_info
# 需要导入模块: from tempfile import NamedTemporaryFile [as 别名]
# 或者: from tempfile.NamedTemporaryFile import filename [as 别名]
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))