本文整理汇总了Python中models.File.save方法的典型用法代码示例。如果您正苦于以下问题:Python File.save方法的具体用法?Python File.save怎么用?Python File.save使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类models.File
的用法示例。
在下文中一共展示了File.save方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: RegisterFile
# 需要导入模块: from models import File [as 别名]
# 或者: from models.File import save [as 别名]
def RegisterFile(Service=None, FileName=None, ProvisionedSpace="10G"):
if Service is None:
raise StorageError('RegisterFile(): Service can not be None')
if FileName is None:
raise StorageError('RegisterFile(): FileName can not be None')
vfilespace = StringSizeToBytes(ProvisionedSpace)
if Service.freespace - vfilespace > 0:
NewFile = File()
NewFile.vfilename = FileName
NewFile.ufid = GetUniqueFileID(FileName)
NewFile.pfilesize = 0
NewFile.vfilesize = vfilespace
NewFile.service = Service
NewFile.pfilename = GetPhysicalFileName(Service.localpath, FileName)
NewFile.status = 'O'
NewFile.save()
SFreeSpace = CalculateFreeSpace(Service)
Service.freespace = SFreeSpace
Service.save()
return NewFile
else:
raise StorageError('RegisterFile(): No have left space')
示例2: upload
# 需要导入模块: from models import File [as 别名]
# 或者: from models.File import save [as 别名]
def upload(request, uri):
if request.POST:
_uri = request.POST.get("uri")
_md5_hash = request.POST.get("hash")
_name = request.POST.get("name")
_description = request.POST.get("description")
_time = datetime.datetime.now()
_directory = request.POST.get("directory")
if _directory != "":
_rank = 2
# 判断文件夹是否存在,若存在,则其中文件数加一,否则创建新的文件夹
try:
d = Directory.objects.get(uri=uri + "/" + _directory)
except Directory.DoesNotExist:
d = None
if d:
d.file_num += 1
d.save()
else:
d = Directory(uri=uri + "/" + _directory, file_num=1, time=_time, name=_directory)
d.save()
else:
_rank = 1
pd = PrimaryDirectory.objects.get(uri=uri)
pd.file_num += 1
pd.save()
file = File(uri=_uri, md5_hash=_md5_hash, name=_name,
description=_description, time=_time,
url=getDownloadUrl(_uri), download_num=0, rank=_rank)
file.save()
return HttpResponse("ok")
return render_to_response("upload.html", {"uri": uri}, context_instance=RequestContext(request))
示例3: edit_person
# 需要导入模块: from models import File [as 别名]
# 或者: from models.File import save [as 别名]
def edit_person(slug):
try:
p = Person.objects.get(site=g.site.domain, slug=slug)
except Person.DoesNotExist:
abort(404)
if not g.site.domain == g.user:
abort(403)
if request.method == "POST":
reqfile = request.files.get('file')
if reqfile:
f = File()
f.site = g.site.domain
f.name = reqfile.filename
f.content_type = reqfile.mimetype
f.slug, f.content_length = save_file(reqfile, request.config["UPLOAD_FOLDER"])
f.save()
p.name = request.form.get("name")
p.title = request.form.get("title")
p.phone = request.form.get("phone")
p.email = request.form.get("email")
p.twitter = request.form.get("twitter")
p.description = request.form.get("description")
if reqfile:
p.image_slug = f.slug
p.save()
return redirect(url_for(".show_person", slug=p.slug))
return render_template("edit_person.html", person=p)
示例4: edit_post
# 需要导入模块: from models import File [as 别名]
# 或者: from models.File import save [as 别名]
def edit_post(year, month, day, slug):
try:
p = Post.objects.get(site=g.site.domain, year=year, month=month, day=day, slug=slug)
except Post.DoesNotExist:
abort(404)
if not g.site.domain == g.user:
abort(403)
if request.method == "POST":
reqfile = request.files.get("file")
if reqfile:
f = File()
f.site = g.site.domain
f.name = reqfile.filename
f.content_type = reqfile.mimetype
f.slug, f.content_length = save_file(reqfile, current_app.config["UPLOAD_FOLDER"])
f.save()
p.name = request.form.get("name")
p.text = request.form.get("text")
if reqfile:
p.image_slug = f.slug
p.save()
return redirect(url_for("post", year=p.year, month=p.month, day=p.day, slug=p.slug))
return render_template("edit_post.html", post=p)
示例5: test_model_file
# 需要导入模块: from models import File [as 别名]
# 或者: from models.File import save [as 别名]
def test_model_file(self):
"""Test File Model"""
folder = Folder(name='test')
folder.save()
obj = File(name='test', folder=folder)
obj.save()
self.assertEquals(folder, obj.folder)
self.assertNotEquals(obj.id, None)
obj.delete()
示例6: parse_pep8
# 需要导入模块: from models import File [as 别名]
# 或者: from models.File import save [as 别名]
def parse_pep8(run, git_path, output):
"""Parse the pep8 output, store the results"""
errfiles_set = set()
errortype_set = set()
lineno_set = set()
# Add all files in the project to the db
allfiles = set()
os.path.walk(git_path, add_file_to_set, allfiles)
for filename in allfiles:
filename = filename.replace(git_path + '/', '', 1)
runfile = File(filename=filename, run=run)
runfile.save()
# Generate a set of error types, error files, and lines
for line in output.readlines():
filename, lineno, errnum, errtext = string.split(line, ':', 3)
lineno = int(lineno)
filename = filename.replace(git_path + '/', '', 1)
# Create sets to remove duplicates
errfiles_set.add(filename)
# Add new err types to the db
if (errnum, errtext) not in errortype_set:
errortype_set.add((errnum, errtext))
if not Error.objects.filter(error_type=errnum):
err = Error(error_type=errnum, short_descr=errtext)
err.save()
# Create a set of line numbers for each file
for ln in range(max(1, lineno - 3), lineno + 4):
lineno_set.add((filename, ln))
# Add err instances to the db
runfile = File.objects.get(run=run, filename=filename)
errtype = Error.objects.get(error_type=errnum)
runerr = RunError(error=errtype, file=runfile, line_number=lineno,
error_descr=errtext)
runerr.save()
# Add lines to the db
for filename in errfiles_set:
runfile = File.objects.get(run=run, filename=filename)
f = open(git_path + '/' + filename, 'r')
lineno = 1
for line in f:
if (filename, lineno) in lineno_set:
linetext = Line(file=runfile, line_number=lineno, text=line)
linetext.save()
lineno = lineno + 1
f.close()
示例7: post_object
# 需要导入模块: from models import File [as 别名]
# 或者: from models.File import save [as 别名]
def post_object(request):
if request.method == "POST":
upload = request.FILES['file']
parent = request.POST.get('parent') if 'parent' in request.POST.keys() else None
f = File(name=upload.name, file=upload, owner=request.user, parent=parent, bytes=request.POST.get('size'), mime=request.POST.get('type'))
f.save()
return JsonResponse(f.get_client_inode(), safe=False)
return JsonResponse({'message':'Invalid file..'}, status=400, safe=False)
示例8: save_file
# 需要导入模块: from models import File [as 别名]
# 或者: from models.File import save [as 别名]
def save_file(mime, base64):
'''
Calculate hash of the file, look up in the database,
save if it's a new one and return its hash. The hash
can the be used as URL.
'''
hash_ = file_keeper_hash(mime, base64)
objects = File.objects.filter(hash=hash_)
if not objects:
file_ = File(hash=hash_, mime=mime, base64=base64)
file_.save()
return hash_
示例9: files
# 需要导入模块: from models import File [as 别名]
# 或者: from models.File import save [as 别名]
def files():
if request.method == "POST" and g.site.domain == g.user:
reqfile = request.files["file"]
f = File()
f.site = g.site.domain
f.name = reqfile.filename
f.slug = secure_filename(f.name)
f.content_type = reqfile.mimetype
f.slug, f.content_length = save_file(reqfile, app.config["UPLOAD_FOLDER"])
f.save()
files = File.objects(site=g.site.domain)
return render_template("files.html", files=files)
示例10: add_file
# 需要导入模块: from models import File [as 别名]
# 或者: from models.File import save [as 别名]
def add_file(request,prop_id, event_id, note_id):
if request.method == 'POST':
form = FileUploadForm(request.POST, request.FILES)
note = Note.objects.get(pk=note_id)
if form.is_valid():
newdoc = File(docfile=request.FILES['docfile'] )
newdoc.note = note
newdoc.save()
return HttpResponse("added file")
else:
form = FileUploadForm()
documents = File.objects.all()
context={'form':form, 'documents': documents,'event_id':event_id,
'prop_id':prop_id,"note_id":note_id}
return HttpResponseBadRequest(render (request,'main/note.html',context))
示例11: upload_html
# 需要导入模块: from models import File [as 别名]
# 或者: from models.File import save [as 别名]
def upload_html(request, submission):
"""
Helper function that processes the file upload. Used by the 'no flash' version.
:Return: Void
"""
if request.FILES:
for file in request.FILES:
upload = File()
upload.submission = submission
upload.file_upload = request.FILES[file]
upload.save()
return
示例12: upload_file
# 需要导入模块: from models import File [as 别名]
# 或者: from models.File import save [as 别名]
def upload_file(name, folder_id, f):
root = MEDIA_ROOT
folder = Folder.objects.get(id=folder_id)
# create a file
with open(folder.path + "/" + name, 'w') as destination:
for chunk in f.chunks():
destination.write(chunk)
file = File()
file.name = name
file.folder = folder
file.active = True
file.save()
return file.id
示例13: create_file
# 需要导入模块: from models import File [as 别名]
# 或者: from models.File import save [as 别名]
def create_file(name, folder_id):
root = MEDIA_ROOT
folder = Folder.objects.get(id=folder_id)
# Open a file
f = open(folder.path + "/" + name, "w")
# Close opend file
f.close()
file = File()
file.name = name
file.folder = folder
file.active = True
file.save()
return file.id
示例14: upload
# 需要导入模块: from models import File [as 别名]
# 或者: from models.File import save [as 别名]
def upload(request):
if request.POST:
userid = request.POST.get('userid')
# Queries all non-expired sessions
sessions = Session.objects.filter(expire_date__gte=datetime.now())
# Checks if session is active
for session in sessions:
data = session.get_decoded()
found_userid=data.get('_auth_user_id')
# Prceeds when user id is validated
if found_userid!=None and long(userid)==found_userid:
user = User.objects.filter(id=userid)[0]
faculty=None
faculty=Faculty.objects.filter(id=request.POST.get('fid'))[0]
transaction = Transaction.objects.get(id=request.POST.get('transaction'))
document = Dokument()
document.faculty= faculty
document.transaction= transaction
document.save()
#Generates a random alphanum string for filename template
while True:
fnameTemplate=''
fnameTemplate = ''.join(random.choice(string.ascii_lowercase))
fnameTemplate += ''.join(random.choice(string.ascii_lowercase + string.digits) for x in range(4)) + '_'
if len(File.objects.filter(filename__startswith = fnameTemplate))==0: break
#Processes uploaded files, page by page
for key in request.FILES:
files = request.FILES[key]
filename = fnameTemplate + key.split('_')[1] + '.bmp'
with open('DCSArchivingSystem/testapp/media/files/' + filename, 'wb+') as destination:
for chunk in files.chunks():
destination.write(chunk)
file = File()
file.filename = filename
file.file = 'files/' + filename
file.save()
document.files.add(file)
Log.create(user, "Uploaded file", file, document).save()
Log.create(user, "Created Document", None, document).save()
return HttpResponseRedirect("/dashboard/")
else:
return render_to_response('upload.html', context_instance=RequestContext(request))
示例15: compile_java
# 需要导入模块: from models import File [as 别名]
# 或者: from models.File import save [as 别名]
def compile_java(file_id):
#first traslate
traslate_java(file_id)
file = File.objects.get(id=file_id)
folder = file.folder
user = folder.user
galatea_code = GALATEA + "galatea.jar "
code = "javac -cp "+ galatea_code + folder.path + "/*.java"
#print code
value = subprocess.check_output([code], shell=True)
#print value
#get all the file in this folder
for root, dirs, files in os.walk(folder.path):
root_folder = Folder.objects.get(path=root, name = os.path.basename(root))
for dir in dirs:
folders = Folder.objects.filter(father=root_folder.id)
esta = False
for folder in folders:
if folder.name == dir:
esta = True
if not esta:
folder = Folder()
folder.name = dir
folder.path = root_folder.path + "/" + dir
folder.user = user
folder.father = root_folder.id
folder.active = True
folder.save()
#print dirs
for file in files:
files_folder = File.objects.filter(folder = root_folder.id)
esta = False
for f in files_folder:
if f.name == file:
esta = True
if not esta:
if (os.path.splitext(file)[1] != ".class"):
f = File()
f.name = file
f.folder = root_folder
f.active = True
f.save()
return value