本文整理汇总了Python中app.models.Image.objects方法的典型用法代码示例。如果您正苦于以下问题:Python Image.objects方法的具体用法?Python Image.objects怎么用?Python Image.objects使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类app.models.Image
的用法示例。
在下文中一共展示了Image.objects方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: import_from_directory
# 需要导入模块: from app.models import Image [as 别名]
# 或者: from app.models.Image import objects [as 别名]
def import_from_directory(path_to_images):
connect("eventum")
creator = User.objects().get(gplus_id="super")
filenames = os.listdir(path_to_images)
filenames = [fn for fn in filenames if not fn.startswith(".")]
failures = []
for filename in filenames:
if Image.objects(filename=filename).count() > 0:
img = Image.objects().get(filename=filename)
img.delete()
old_path = os.path.join(path_to_images, filename)
shutil.copy(old_path, config["UPLOAD_FOLDER"])
default_path = config["RELATIVE_UPLOAD_FOLDER"] + filename
image = Image(filename=filename, default_path=default_path, creator=creator)
try:
image.save()
except ValidationError as e:
failures.append(filename)
print "FAIL: %s" % filename
print e
print "Processed %s images." % len(filenames)
print "%s success." % (len(filenames) - len(failures))
print "%s failures." % len(failures)
示例2: new
# 需要导入模块: from app.models import Image [as 别名]
# 或者: from app.models.Image import objects [as 别名]
def new():
"""Create a new blog post.
**Route:** ``/admin/posts/new``
**Methods:** ``POST``
"""
form = CreateBlogPostForm(request.form)
form.author.choices = [
(str(u.id), u.name + " (You)" if u == g.user else u.name)
for u in User.objects()]
form.author.data = str(g.user.id)
upload_form = UploadImageForm()
if form.validate_on_submit():
author = User.objects().get(id=ObjectId(form.author.data))
post = BlogPost(title=form.title.data,
slug=form.slug.data,
images=[Image.objects().get(filename=fn) for fn in form.images.data],
markdown_content=form.body.data,
author=author,
posted_by=g.user)
post.save()
if form.published.data:
post.publish()
else:
post.unpublish()
return redirect(url_for('.index'))
images = Image.objects()
return render_template('admin/posts/edit.html', user=g.user, form=form,
images=images, upload_form=upload_form)
示例3: delete
# 需要导入模块: from app.models import Image [as 别名]
# 或者: from app.models.Image import objects [as 别名]
def delete(filename):
if Image.objects(filename=filename).count() == 1:
image = Image.objects().get(filename=filename)
image.delete()
else:
flash('Invalid filename')
pass
return redirect(url_for('.index'))
示例4: edit
# 需要导入模块: from app.models import Image [as 别名]
# 或者: from app.models.Image import objects [as 别名]
def edit(post_id):
try:
object_id = ObjectId(post_id)
except InvalidId:
return abort(404)
try:
post = BlogPost.objects().with_id(object_id)
except (DoesNotExist, ValidationError):
flash('Cannot find blog post with id %s.' % post_id)
return redirect(url_for('.index'))
if request.method == 'POST':
form = CreateBlogPostForm(request.form)
form.author.choices = [
(str(u.id), u.name + " (You)" if u == g.user else u.name)
for u in User.objects()]
form.author.default = str(g.user.id)
if form.validate_on_submit():
was_published = post.published
should_be_published = form.published.data
post.title = form.title.data
post.author = User.objects.get(id=ObjectId(form.author.data))
post.slug = form.slug.data
post.markdown_content = form.body.data
post.images = [Image.objects().get(filename=fn) for fn in form.images.data]
if form.featured_image.data:
post.featured_image = Image.objects().get(filename=form.featured_image.data)
else:
post.featured_image = None
post.save()
if was_published != should_be_published:
if was_published:
set_published_status(post.id, False)
else:
set_published_status(post.id, True)
return redirect(url_for('.index'))
upload_form = UploadImageForm()
featured_image = post.featured_image.filename if post.featured_image else None
form = CreateBlogPostForm(request.form,
title=post.title,
slug=post.slug,
published=post.published,
body=post.markdown_content,
images=[image.filename for image in post.images],
author=str(post.author.id),
featured_image=featured_image)
form.author.choices = [
(str(u.id), u.name + " (You)" if u == g.user else u.name)
for u in User.objects()]
form.author.default = str(g.user.id)
images = [image for image in Image.objects() if image not in post.images]
return render_template('admin/posts/edit.html', user=g.user, form=form,
post=post, images=images, upload_form=upload_form)
示例5: delete
# 需要导入模块: from app.models import Image [as 别名]
# 或者: from app.models.Image import objects [as 别名]
def delete(filename):
"""View all of the uploaded images.
**Route:** ``/admin/media/delete/<filename>``
**Methods:** ``POST``
"""
if Image.objects(filename=filename).count() == 1:
image = Image.objects().get(filename=filename)
image.delete()
else:
flash('Invalid filename', ERROR_FLASH)
return redirect(url_for('.index'))
示例6: create
# 需要导入模块: from app.models import Image [as 别名]
# 或者: from app.models.Image import objects [as 别名]
def create():
"""Create a new event.
**Route:** ``/admin/events/create``
**Methods:** ``GET, POST``
"""
form = CreateEventForm(request.form)
if form.validate_on_submit():
try:
EventsHelper.create_event(form, g.user)
except EventumError.GCalAPI as e:
flash(e.message, ERROR_FLASH)
return redirect(url_for('.index'))
if form.errors:
for error in form.errors:
for message in form.errors[error]:
flash(message, ERROR_FLASH)
upload_form = UploadImageForm()
delete_form = DeleteEventForm()
images = Image.objects()
return render_template('admin/events/create.html', form=form,
delete_form=delete_form, upload_form=upload_form,
images=images)
示例7: _image
# 需要导入模块: from app.models import Image [as 别名]
# 或者: from app.models.Image import objects [as 别名]
def _image(self):
"""Gets an image to associate with the event, from the database.
:returns: The image.
:rtype: :class:'~app.models.Image'
"""
return random.choice(Image.objects())
示例8: edit
# 需要导入模块: from app.models import Image [as 别名]
# 或者: from app.models.Image import objects [as 别名]
def edit(event_id):
""""""
try:
event = Event.objects().get(id=event_id)
except (DoesNotExist, ValidationError):
flash('Cannont find event with id "%s"' % event_id)
return redirect(url_for('.index'))
form = EditEventForm(request.form) if request.method == 'POST' else \
EventsHelper.create_form(event, request)
if form.validate_on_submit():
try:
EventsHelper.update_event(event, form)
except GoogleCalendarAPIError as e:
flash(e.message)
return redirect(url_for('.index'))
if form.errors:
for error in form.errors:
for message in form.errors[error]:
flash(message)
delete_form = DeleteEventForm()
upload_form = UploadImageForm()
images = Image.objects()
return render_template('admin/events/edit.html', form=form, event=event,
delete_form=delete_form, upload_form=upload_form,
images=images)
示例9: view
# 需要导入模块: from app.models import Image [as 别名]
# 或者: from app.models.Image import objects [as 别名]
def view():
"""Displays all uploaded images.
**Route:** ``/admin/media/view``
**Methods:** ``GET``
"""
images = Image.objects()
return render_template('admin/media/view.html', images=images)
示例10: _images
# 需要导入模块: from app.models import Image [as 别名]
# 或者: from app.models.Image import objects [as 别名]
def _images(self):
"""Gets a list of images from the database if any exist.
:returns: The images.
:rtype: list(:class:`~app.models.Image`)
"""
# Fetch self.images if it hasn't been fetched
if self.images is None:
self.images = list(Image.objects().limit(5))
return self.images
示例11: index
# 需要导入模块: from app.models import Image [as 别名]
# 或者: from app.models.Image import objects [as 别名]
def index():
"""View all of the uploaded images.
**Route:** ``/admin/media``
**Methods:** ``GET``
"""
images = Image.objects()
form = UploadImageForm()
return render_template('admin/media/media.html', images=images, form=form)
示例12: image_with_same_name
# 需要导入模块: from app.models import Image [as 别名]
# 或者: from app.models.Image import objects [as 别名]
def image_with_same_name(form, field):
"""A validator that ensures that there is an image in the database with the
filename that is the same as the field's data.
:param form: The parent form
:type form: :class:`Form`
:param field: The field to validate
:type field: :class:`Field`
"""
if Image.objects(filename=field.data).count() != 1:
return ValidationError(
message="Can't find image '{}' in the database".format(field.data))
示例13: create_images
# 需要导入模块: from app.models import Image [as 别名]
# 或者: from app.models.Image import objects [as 别名]
def create_images(num_images, superuser, printer):
"""Creates ``num_images`` image objects in the database. It will download
sample images from http://lorempixel.com, and add database entries.
:param int num_images: The number of images to create
:param superuser: The superuser object to associate with the images.
:type superuser: :class:`~app.models.User`
:param printer: The object to manage progress printing.
:type printer: :class:`~script.cli.ProgressPrinter`
:returns: A list of images that now exist.
:rtype: list(:class:`~app.models.Image`)
"""
print "Generating images..."
printer.line()
successes = []
failures = []
skips = []
for width in range(400, 1600, (1600 - 400) / num_images):
height = width / 2
filename = BASE_FILENAME.format(width, height)
path = config['UPLOAD_FOLDER'] + filename
url = BASE_URL.format(width, height)
printer.begin_status_line(filename)
# Download image if it doesn't exist already
if not exists(path):
try:
urllib.urlretrieve(url, path)
except IOError:
failures.append((filename, ''))
printer.status_fail()
continue # Failed to download, move on to the next image.
# Insert or fetch image from database
if Image.objects(filename=filename).count() == 0:
image = Image(filename=filename,
default_path=path,
creator=superuser)
image.save()
successes.append((filename, path))
printer.status_success()
else:
skips.append((filename, path))
printer.status_skip()
printer.line()
printer.results(len(successes), len(skips), len(failures))
return successes + skips
示例14: __call__
# 需要导入模块: from app.models import Image [as 别名]
# 或者: from app.models.Image import objects [as 别名]
def __call__(self, form, field):
"""Called internally by :mod:`wtforms` on validation of the field.
:param form: The parent form
:type form: :class:`Form`
:param field: The field to validate
:type field: :class:`Field`
:raises: :class:`wtforms.validators.ValidationError`
"""
filename = '{}.'.format(field.data)
if Image.objects(filename__startswith=filename).count():
raise ValidationError(self.message)
示例15: index
# 需要导入模块: from app.models import Image [as 别名]
# 或者: from app.models.Image import objects [as 别名]
def index():
"""View and manage users
Whitelisted users are the only ones allowed to make user accounts.
"""
upload_form = UploadImageForm()
whitelist_form = AddToWhitelistForm()
return render_template('admin/users/users.html',
whitelist_form=whitelist_form,
upload_form=upload_form,
whitelist=Whitelist.objects(redeemed=False),
users=User.objects(),
images=Image.objects(),
current_user=g.user)