本文整理汇总了Python中models.File类的典型用法代码示例。如果您正苦于以下问题:Python File类的具体用法?Python File怎么用?Python File使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了File类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: expose_path
def expose_path(group):
form_keys = ["path", "size", "hash", "hash_function", "modified", "signature"]
for key in form_keys:
if key not in request.form:
raise ValueError("Missing form value %s" % key)
path = request.form["path"]
signature = request.form["signature"]
# Don't include signature
d = {k: request.form[k] for k in form_keys[:-1]}
if not security.check_json_sig(d, app.config["BASEJUMP_KEY"], signature):
raise ValueError("Invalid signature provided.")
key = security.sign_path(path, app.config["SECRET_KEY"])
meta = request.form
with db_session() as s:
f = s.query(File).filter(File.key == key).all()
if f:
raise ValueError("This path is already exposed.")
last_modified = datetime.fromtimestamp(int(meta["modified"]))
f = File(path=path, group=group, key=key, size=meta["size"], checksum=meta["hash"], checksumType=meta["hash_function"], modified=last_modified)
s.add(f)
s.commit()
url_path = f.queue_url()
url = request.url_root + url_path
return jsonify({"queue_url": url})
示例2: fstream
def fstream(name):
fstream_count = File.objects(name=name).count()
if fstream_count:
fstream = File.objects(name=name).first()
return fstream.data.read()
else:
return ''
示例3: file_upload
def file_upload(user):
_file = request.files['file']
cont = ''
for d in _file.stream:
cont += d
from binascii import hexlify
cont = hexlify(cont)
name = _file.filename
root = models.get_dir(name=user, path='/')
db_f = g.db.query(File).filter_by(owner=user, name=name, dir=root.inode).first()
if db_f is not None:
return "File already exists", 409
new_file = File(name=name, owner=user, content=cont, dir=root.inode)
new_file.directory = root
tx = Transaction(user=user,
action="CREATE",
type="FILE",
pathname=new_file.pathname(),
ip_address=request.remote_addr)
g.db.add(new_file)
g.db.add(tx)
return "Success"
示例4: get
def get(self):
if not self.check_user(False):
#self.logged_user_home()
self.redirect('/')
return
max_results = 10
tmp = self.request.get("max")
if len(tmp):
max_results = int(tmp)
query = File.gql("WHERE author = :1", self.get_current_user())
files = query.fetch(max_results);
if len(files) > 0:
head = files[0].head
else:
head = Revision()
user = users.User(base.SUGGESTIONS_USER)
query = File.gql("WHERE author = :1", user)
public_files = query.fetch(50);
files = files + public_files
template_values = {
'user' : self.get_current_user(),
'files' : files,
'head' : head,
'login_url' : users.create_login_url(self.request.uri)
}
path = self.get_template_path( 'files.html' )
self.response.out.write(template.render(path, template_values))
示例5: upload
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))
示例6: download
def download():
'''Grabs the latest.'''
# Download Files
date_str = date.today().strftime("%Y_%m_%d")
new_folder_path = "data/downloaded_%s" % date_str
download_files(new_folder_path)
# Delete if already exist in database
for path, subdirs, files in os.walk(new_folder_path):
for f in files:
if already_downloaded(path + '/' + f):
print "Didn't save '%s' because it was already in the database." % f
os.remove(path + '/' + f)
else:
print "Saved new file '%s/%s'" % (path, f)
File.create(
name = f,
years=next(re.finditer(r'\d{4}_\d{4}', f)),
sha1 = sha1OfFile(path + '/' + f),
updated = date.today(),
ingested = False
)
示例7: doShare
def doShare(path):
is_private = False
is_public = False
try:
f = File.get(File.public_share_url == path)
is_public = True
except peewee.DoesNotExist:
try:
f = File.get(File.private_share_url == path)
is_private = True
except peewee.DoesNotExist:
return jsonify(message='error'), 404
if not ((is_public and f.open_public_share) or (is_private and f.open_private_share)):
return jsonify(message='error'), 404
args = request.args
if 'password' in args:
if args['password'] == f.private_share_password:
return jsonify(message='OK')
else:
return jsonify(message='error'), 401
s = Serializer(app.config['SECRET_KEY'])
token = s.dumps({'path': path})
payload = {
'filename': f.filename,
'folder': f.folder.name,
'openPublic': f.open_public_share,
'openPrivate': f.open_private_share,
'token': token,
}
return jsonify(message='OK', payload=payload)
示例8: add
def add(request, key=None, type=FILE):
to = key # lame but it does the trick for now
if type == FOLDER:
form = FolderForm(request.form)
else:
form = FileForm(request.form)
if request.method == "POST" and form.validate():
if len(form.slug.data) < 1:
form.slug.data = slugify(form.name.data)
if type == FOLDER:
file = File.add(to=to,type=type, name=form.name.data,
slug=form.slug.data,
breadcrumb=form.breadcrumb.data,
state=form.state.data,
active=form.active.data,
author=users.get_current_user(),
updated=datetime.now())
elif type == FILE:
file = request.files.get('file')
data = db.Blob(file.read())
file = File.add(to=to,type=type, name=form.name.data,
slug=form.slug.data,
breadcrumb=form.breadcrumb.data,
state=form.state.data,
active=form.active.data,
author=users.get_current_user(),
updated=datetime.now(),
content_type=file.content_type,
data=data, size=len(data))
if form.save.data is True:
return redirect(url_for('nut:files/list'), 301)
if form.cont.data is True:
return redirect(url_for('nut:files/edit', key=file.key()), 301)
return render_template('app:files/form.html', form=form)
示例9: RegisterFile
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')
示例10: search
def search(query, offset = 0, limit = 10):
results = None
search_type = None
if query:
if len(query) > 10 and lower_hex_regex.match(query):
for d in hash_sizes:
if len(query) == hash_sizes[d]:
search_type = '%s search' % d.upper()
results = File.all().filter('%s =' % d, query)
break
if not search_type:
search_type = 'SHA1 prefix search'
results = File.all().filter('sha1 >= ', query).filter('sha1 < ', query + u'\ufffd')
else: #Last resort: basename matching
match_case = 0
if query.lower() == query:
results = File.all().filter('name_lower >= ', query).filter('name_lower < ', query + u'\ufffd')
else:
match_case = 1
results = File.all().filter('name >= ', query).filter('name < ', query + u'\ufffd')
search_type = 'Filename prefix search%s' % ['', ', matching case,'][match_case]
#db.GqlQuery("SELECT * FROM MyModel WHERE prop >= :1 AND prop < :2", "abc", u"abc" + u"\ufffd")
if results and limit:
results.fetch(limit)
return {'results': results, 'search_type': search_type, 'query': query, 'search_limit': limit, 'search_offset': offset}
示例11: file_path
def file_path(slug):
f = File.all().filter('abs_path = ', slug).get()
if f is None:
f = File.all().filter('slug = ', slug).get()
if f is None:
return u''
return f.get_absolute_url()
示例12: GetOutput
def GetOutput(self):
# if not self.check_user():
# return
file_id = self.request.get("id")
file = self.get_file(file_id)
if file is None:
file = File()
file.put()
else:
if self.get_file_permission(file) < base.ACCESS_READ:
self.redirect('/')
return
head = file.head
if head is None:
file_text = "Welcome to ZenTxt!"
revisions = []
else:
#file_text = cgi.escape(head.content)
file_text = head.content
revisions = self.get_revisions(file)
template_values = {
'user' : self.get_current_user(),
'file_id' : file_id,
'revisions' : revisions,
'file_text' : file_text,
'login_url' : users.create_login_url(self.request.uri)
}
path = self.get_template_path( 'file.html' )
return template.render(path, template_values)
示例13: test_model_file
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()
示例14: create_file
def create_file(self, filename, user=None):
file = File()
if user is None:
user = self.get_current_user()
file.author = user
file.name = filename
key = file.put()
self.create_permission(file, file.author, ACCESS_WRITE)
#self.redirect('/file?' + urllib.urlencode({'id': key}))
return key
示例15: already_downloaded
def already_downloaded(filepath):
'''
Return true if we already have this version of the file
(check date and file hash). False otherwise
'''
try:
File.get(File.sha1 == sha1OfFile(filepath))
return True
except peewee.DoesNotExist:
return False