本文整理汇总了Python中app.models.Image类的典型用法代码示例。如果您正苦于以下问题:Python Image类的具体用法?Python Image怎么用?Python Image使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Image类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: import_from_directory
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: upload
def upload():
"""Upload an image to Eventum
**Route:** ``/admin/media/upload``
**Methods:** ``POST``
"""
form = UploadImageForm(request.form)
uploaded_from = form.uploaded_from.data
if form.validate_on_submit():
f = request.files['image']
if f and allowed_file(f.filename.lower()):
filename = create_filename(f, request.form['filename'])
f.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
image = Image(filename=filename,
default_path=app.config['RELATIVE_UPLOAD_FOLDER']+filename,
creator=g.user)
image.save()
return redirect(url_for('.index'))
flash("Filename {} is invalid".format(f.filename))
if form.errors:
flash(form.errors)
if uploaded_from:
return redirect(uploaded_from)
return render_template('admin/media/upload.html', form=form)
示例3: new
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)
示例4: delete
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'))
示例5: save
def save(self):
super(ImageFile, self).save()
img = Image()
img.file_id = self.id
img.image = self.base + '.' + self.ext
img.save()
return self.base
示例6: test_create_image
def test_create_image(self):
image = Image()
image.height = 640
image.width = 640
image.url = 'http://scontent-b.cdninstagram.com/hphotos-xpa1/t51.2885-15/10522310_677385995673625_267718762_n.jpg'
image.type = 'standard_resolution'
db.session.add(image)
db.session.commit()
self.assertIn(image, db.session)
示例7: edit
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)
示例8: delete
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'))
示例9: create_images
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
示例10: _image
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())
示例11: remove_images
def remove_images(app):
from datetime import datetime
while True:
time.sleep(1800)
conf = app.config['IMAGE_DELETE']
with app.app_context():
if ( datetime.utcnow().hour in conf['TIME_OF_DAY'] and
datetime.utcnow().weekday() in conf['WEEKDAY'] ):
images = Img.get_all_imgs()
db_imgs = [img.location + img.filename for img in images]
posts = Post.get_all()
post_imgs = get_all_imgs((post.body_html for post in posts))
diff_imgs = set(db_imgs) - set(post_imgs)
if diff_imgs:
app.logger.debug('Images to delete: {}'.format(db_imgs))
for i in images:
if i.location + i.filename in diff_imgs:
if os.path.isfile(imgs.path(i.filename)):
os.remove(imgs.path(i.filename))
f, e = os.path.splitext(i.filename)
if os.path.isfile(imgs.path(f + '_crop' + e)):
os.remove(imgs.path(f + '_crop' + e))
db.session.delete(i)
db.session.commit()
示例12: create
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)
示例13: edit
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)
示例14: view
def view():
"""Displays all uploaded images.
**Route:** ``/admin/media/view``
**Methods:** ``GET``
"""
images = Image.objects()
return render_template('admin/media/view.html', images=images)
示例15: index
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)