Error with prewarming VersatileImageField - django

I've implemented django-versatileimagefield and have gotten it to a state where I can successfully resize images in templates with the use of the following tag, for example:
As a result, I'm confident that my form.py, view.py, and template works. I have, however, recently tried to 'prewarm' the images so that when a user saves an image, a set of resized images are saved to my 'media' folder. This prevents the server from having to resize dynamically on every page load and can just grab the correctly sized image.
My problem is that when the user goes to save an image within the image upload form, they get an error.
Settings.py
VERSATILEIMAGEFIELD_SETTINGS = {
'cache_length': 2592000,
'cache_name': 'versatileimagefield_cache',
'jpeg_resize_quality': 70,
'sized_directory_name': '__sized__',
'filtered_directory_name': '__filtered__',
'placeholder_directory_name': '__placeholder__',
'create_images_on_demand': False,
'image_key_post_processor': None,
'progressive_jpeg': False
}
VERSATILEIMAGEFIELD_RENDITION_KEY_SETS = {
'image_gallery': [
('image_large', 'thumbnail__400x400'),
('image_small', 'thumbnail__900x900')
]
}
models.py
from django.db import models
from django.db.models.signals import post_save
from versatileimagefield.fields import VersatileImageField
from django.dispatch import receiver
from versatileimagefield.image_warmer import VersatileImageFieldWarmer
class ImageGallery(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE, default=None)
image = VersatileImageField('image_gallery', upload_to=upload_location)
def __str__(self):
return self.author.username
#receiver(models.signals.post_save, sender=ImageGallery)
def warm_gallery_images(sender, instance, **kwargs):
gallery_img_warmer = VersatileImageFieldWarmer(
instance_or_queryset=instance,
rendition_key_set='image_gallery',
image_attr='image_small'
)
num_created, failed_to_create = gallery_img_warmer.warm()
Traceback
Environment:
Traceback:
File "C:\Users\jason\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\exception.py" in inner
35. response = get_response(request)
File "C:\Users\jason\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\base.py" in _get_response
128. response = self.process_exception_by_middleware(e, request)
File "C:\Users\jason\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\base.py" in _get_response
126. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\jason\AppData\Local\Programs\Python\Python36\lib\site-packages\django\contrib\auth\decorators.py" in _wrapped_view
21. return view_func(request, *args, **kwargs)
File "C:\Users\jason\Desktop\jason\accounts\views.py" in imagegallery
513. instance.save()
File "C:\Users\jason\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\base.py" in save
729. force_update=force_update, update_fields=update_fields)
File "C:\Users\jason\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\base.py" in save_base
769. update_fields=update_fields, raw=raw, using=using,
File "C:\Users\jason\AppData\Local\Programs\Python\Python36\lib\site-packages\django\dispatch\dispatcher.py" in send
178. for receiver in self._live_receivers(sender)
File "C:\Users\jason\AppData\Local\Programs\Python\Python36\lib\site-packages\django\dispatch\dispatcher.py" in <listcomp>
178. for receiver in self._live_receivers(sender)
File "C:\Users\jason\Desktop\jason\accounts\models.py" in warm_gallery_images
177. num_created, failed_to_create = gallery_img_warmer.warm()
File "C:\Users\jason\AppData\Local\Programs\Python\Python36\lib\site-packages\versatileimagefield\image_warmer.py" in warm
143. reduce(getattr, self.image_attr.split("."), instance)
Exception Type: AttributeError at /accounts/profile/websitesetup5/upload/
Exception Value: 'ImageGallery' object has no attribute 'image_small'
I feel like I've followed the instructions on this page, but I'm still getting this error.
http://django-versatileimagefield.readthedocs.io/en/latest/improving_performance.html#turning-off-on-demand-image-creation
I should note that I do NOT have django REST framework installed. My understanding is that it's not required for this post-save method of prewarming images.
Thanks so much!

It looks like the image_attr field should point to the model field image:
#receiver(models.signals.post_save, sender=ImageGallery)
def warm_gallery_images(sender, instance, **kwargs):
gallery_img_warmer = VersatileImageFieldWarmer(
instance_or_queryset=instance,
rendition_key_set='image_gallery',
image_attr='image'
)
num_created, failed_to_create = gallery_img_warmer.warm()

Related

runtime error. there is no current event loop in thread 'Thread-1'

i followed this tutorial https://python.gotrained.com/scraping-telegram-group-members-python-telethon/, and the code ran successfully on a single python script. so i decided to implement the same code into django views.py. form.py, html templates and urls.py are properly connected. whenever i runserver and enter details it raises runtime error saying "There is no current event loop in thread 'Thread-1'."
views.py
import asyncio
from django.shortcuts import render,redirect
from telethon.sync import TelegramClient
from .form import DetailForm,CodeForm
from .models import Detail,Code
# Create your views here.
def home(request):
form = DetailForm
context = {'form':form}
if request.method=='POST':
form = DetailForm(request.POST)
if form.is_valid():
form.save()
details = Detail.objects.all().last()
api_id = int(details.api_id)
api_hash = details.api_hash
phone_number = details.phone_number
loop = asyncio.new_event_loop()
client = TelegramClient(phone_number, api_id, api_hash, loop=loop)
client.connect()
if not client.is_user_authorized():
client.send_code_request(phone_number)
# global client, phone_number
return redirect('code')
return render(request, 'teleapp/home.html', context)
def codeview(request):
code=CodeForm
# codemodel = Code.objects.all().last()
if request.method == 'POST':
codes = CodeForm(request.POST)
if codes.is_valid():
codes.save()
#what next...
return redirect('scrapy')
context = {'code':code}
return render(request, 'teleapp/code.html', context)
def scrappy(request):
details = Detail.objects.all().last()
context = {'details':details}
return render(request, 'teleapp/scrappy.html', context)```
**error raised**
Internal Server Error: /
Traceback (most recent call last):
File "C:\Users\User\PycharmProjects\telegram\venv\lib\site-packages\django\c
ore\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\User\PycharmProjects\telegram\venv\lib\site-packages\django\c
ore\handlers\base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\User\PycharmProjects\telegram\venv\lib\site-packages\django\c
ore\handlers\base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\User\PycharmProjects\telegram\scrapy\teleapp\views.py", line
24, in home
client.connect()
File "C:\Users\User\PycharmProjects\telegram\venv\lib\site-packages\telethon
\sync.py", line 35, in syncified
loop = asyncio.get_event_loop()
File "C:\Users\User\AppData\Local\Programs\Python\Python36-32\lib\asyncio\ev
ents.py", line 694, in get_event_loop
return get_event_loop_policy().get_event_loop()
File "C:\Users\User\AppData\Local\Programs\Python\Python36-32\lib\asyncio\events.py", line 602, in get_event_loop
% threading.current_thread().name)
RuntimeError: There is no current event loop in thread 'Thread-2'.

PIL module ImportError on Apache2 Server

I am getting this traceback on my Apache2 server when trying to upload an image. I am using Python 3.5.2 and Pillow 5.0.0 and Django 2.1.1
I was able to upload images successfully when I am working on my localhost, but when I uploaded this code to the apache2 server, then it gives this traceback. Pillow is in the requiremnts.txt though. No where in the code do I explicitly try to import PIL, but it seems to be getting called.
I have tried installing Pillow and PIL. I have tried to uninstall PIL, uninstall Pillow, I have tried re-install both. Nothing seems to be working.
View.py
from django.http import JsonResponse, HttpResponse
from django.shortcuts import render
from django.core import serializers
from manager.models import Manager, Inspection
from . import InspectorService
from . import InspectionService
import json
from .forms import InspectionImageForm
... more unrelevant code here ...
def upload_image(request):
if request.method == 'POST':
form = InspectionImageForm(request.POST, request.FILES)
inspection_id = request.POST.get('inspection_id')
if form.is_valid():
m = Inspection.objects.get(id = inspection_id)
m.image = form.cleaned_data['image']
m.save()
return JsonResponse({'error': False, 'message': 'Uploaded Successfully', 'location': m.image.name})
else:
return JsonResponse({'error': True, 'errors': form.errors})
else:
return JsonResponse({'error': True, 'errors': 'Post error'})
forms.py
from django import forms
class InspectionImageForm(forms.Form):
image = forms.ImageField()
inspection_id = forms.IntegerField()
Traceback
File "/opt/myenv/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
34. response = get_response(request)
File "/opt/myenv/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
126. response = self.process_exception_by_middleware(e, request)
File "/opt/myenv/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
124. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/var/www/html/CMPUT401/computronix/manager/views.py" in upload_image
119. if form.is_valid():
File "/opt/myenv/lib/python3.5/site-packages/django/forms/forms.py" in is_valid
185. return self.is_bound and not self.errors
File "/opt/myenv/lib/python3.5/site-packages/django/forms/forms.py" in errors
180. self.full_clean()
File "/opt/myenv/lib/python3.5/site-packages/django/forms/forms.py" in full_clean
381. self._clean_fields()
File "/opt/myenv/lib/python3.5/site-packages/django/forms/forms.py" in _clean_fields
397. value = field.clean(value, initial)
File "/opt/myenv/lib/python3.5/site-packages/django/forms/fields.py" in clean
584. return super().clean(data)
File "/opt/myenv/lib/python3.5/site-packages/django/forms/fields.py" in clean
147. value = self.to_python(value)
File "/opt/myenv/lib/python3.5/site-packages/django/forms/fields.py" in to_python
613. from PIL import Image
Exception Type: ImportError at /manager/upload_image/
Exception Value: No module named 'PIL'

How to choose a Wagtail image across a ParentalManyToManyField?

Environment:
Wagtail 1.13
Django Version: 1.11.11
Python Version: 2.7.12
I'm following the Wagtail documentation to try to add an image showcase (not a category) to a page using ParentalManyToManyField:
class HomePage(Page):
showcase_title = models.CharField(max_length=100, blank="true", default="SHOWCASE")
showcase_images = ParentalManyToManyField('wagtailimages.Image')
content_panels = Page.content_panels + [
FieldPanel('showcase_title'),
InlinePanel('showcase_images', label="Showcase images", panels=[
ImageChooserPanel('showcase_images')
]),
]
Everything is fine if I comment out the showcase_images editing panel, but I get a KeyError as soon as uncomment the panel showcase_images. In addition to the above variation of editing showcase_images, I've also tried simply InlinePanel('showcase_images'), FieldPanel('showcase_images'), InlinePanel('showcase_images', label="Showcase images", panels=[ImageChooserPanel('image')]), and probably another variation or two. Can someone propose a solution?
Environment:
Request Method: GET
Request URL: http://dev.somedomain.com:8181/admin/pages/3/edit/
Django Version: 1.11.11
Python Version: 2.7.12
Installed Applications:
[u'my_app',
u'search',
u'wagtail.wagtailforms',
u'wagtail.wagtailredirects',
u'wagtail.wagtailembeds',
u'wagtail.wagtailsites',
u'wagtail.wagtailusers',
u'wagtail.wagtailsnippets',
u'wagtail.wagtaildocs',
u'wagtail.wagtailimages',
u'wagtail.wagtailsearch',
u'wagtail.wagtailadmin',
u'wagtail.wagtailcore',
u'wagtail.contrib.wagtailstyleguide',
u'modelcluster',
u'taggit',
u'wagtailfontawesome',
u'django.contrib.admin',
u'django.contrib.auth',
u'django.contrib.contenttypes',
u'django.contrib.sessions',
u'django.contrib.messages',
u'django.contrib.staticfiles']
Installed Middleware:
[u'django.contrib.sessions.middleware.SessionMiddleware',
u'django.middleware.common.CommonMiddleware',
u'django.middleware.csrf.CsrfViewMiddleware',
u'django.contrib.auth.middleware.AuthenticationMiddleware',
u'django.contrib.messages.middleware.MessageMiddleware',
u'django.middleware.clickjacking.XFrameOptionsMiddleware',
u'django.middleware.security.SecurityMiddleware',
u'wagtail.wagtailcore.middleware.SiteMiddleware',
u'wagtail.wagtailredirects.middleware.RedirectMiddleware']
Traceback:
File "/opt/virtualenvs/gmmiTMID/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner
41. response = get_response(request)
File "/opt/virtualenvs/gmmiTMID/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/opt/virtualenvs/gmmiTMID/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/opt/virtualenvs/gmmiTMID/lib/python2.7/site-packages/django/views/decorators/cache.py" in _cache_controlled
43. response = viewfunc(request, *args, **kw)
File "/opt/virtualenvs/gmmiTMID/lib/python2.7/site-packages/wagtail/wagtailadmin/urls/__init__.py" in wrapper
96. return view_func(request, *args, **kwargs)
File "/opt/virtualenvs/gmmiTMID/lib/python2.7/site-packages/wagtail/wagtailadmin/decorators.py" in decorated_view
31. return view_func(request, *args, **kwargs)
File "/opt/virtualenvs/gmmiTMID/lib/python2.7/site-packages/wagtail/wagtailadmin/views/pages.py" in edit
481. edit_handler = edit_handler_class(instance=page, form=form)
File "/opt/virtualenvs/gmmiTMID/lib/python2.7/site-packages/wagtail/wagtailadmin/edit_handlers.py" in __init__
269. self.children.append(child(instance=self.instance, form=self.form))
File "/opt/virtualenvs/gmmiTMID/lib/python2.7/site-packages/wagtail/wagtailadmin/edit_handlers.py" in __init__
269. self.children.append(child(instance=self.instance, form=self.form))
File "/opt/virtualenvs/gmmiTMID/lib/python2.7/site-packages/wagtail/wagtailadmin/edit_handlers.py" in __init__
693. self.formset = form.formsets[self.__class__.relation_name]
Exception Type: KeyError at /admin/pages/3/edit/
Exception Value: u'showcase_images'
InlinePanel doesn't work with ParentalManyToManyField relations, only ParentalKey relations. You'll need to set up an intermediate model to define the relation between pages and images, like BlogPageGalleryImage in the tutorial's 'images' section (but with the caption field omitted in this case, so it's just a direct association between pages and images).
(Alternatively, you can use a plain FieldPanel with ParentalManyToManyField, but this will just give you a set of checkboxes for all images in the system, rather than the the image chooser interface.)

Displaying PDFs in django; decoding error

I'm trying to pass a PDF into a Django app and am running into an issue with unicode/decoding the PDF. The PDFs are being stored in a mysql database, in a mediumblob field. I'd appreciate any help on this, as it seems the encoding is running into a problem with the metadata of the PDF, and I'm not sure where to go with this - I've checked out several questions that seem similar but can't find what I'm looking for. Do I need to decode/recode the PDFs somehow? Thanks!
Here is the error:
Request Method: POST
Request URL: http://0.0.0.0:8000/admin/pdf/abc/
Exception Type: DjangoUnicodeDecodeError
Exception Value: 'utf8' codec can't decode byte 0x89 in position 614: unexpected code byte
Exception Location: /usr/lib/python2.5/site-packages/django/utils/encoding.py in force_unicode, line 92
Python Executable: /usr/bin/python
My code is below:
class ABCAdmin(admin.ModelAdmin):
actions = ['print_selected_pdf']
def get_user(self):
return '%s'%(self.user.username)
def create_pdf(self, queryset):
response = HttpResponse(mimetype="applicaton/pdf")
response['Content-Disposition'] = 'attachment; filename=form.pdf'
p=canvas.Canvas(response)
# loop through the objects
for obj in queryset:
string1 = (obj.form)
# update the label_printed to true
obj.pdf_printed=True
obj.save()
p.save()
return response
def print_selected_pdf(self, request, queryset):
# prints the pdfs for those that are selected,
# regardless if the pdf_printed field is true or false
return self.create_pdf(queryset.order_by('user'))
print_selected_pdf.short_description = "Print selected PDF"
get_user.short_description='Printed By'
list_display=('form_no',get_user,'request_date','pdf_printed')
def queryset(self,request):
# get the user id
user = User.objects.get(username=request.user)
if request.user.is_superuser:
qs = self.model._default_manager.all()
else:
qs = self.model._default_manager.filter(user=user.id)
return qs
def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == "user" and not request.user.is_superuser:
# get the user id
user = User.objects.get(username=request.user)
kwargs["queryset"]=User.objects.filter(id=user.id)
return db_field.formfield(**kwargs)
return super(ABCAdmin,self).formfield_for_foreignkey(
db_field, request, **kwargs)
admin.site.register(ABC, ABCAdmin)
Edit: Full trackback:
Environment:
Request Method: POST
Request URL: http://0.0.0.0:8000/admin/pdf/abc/
Django Version: 1.1
Python Version: 2.5.2
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.admin',
'app.pdf']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware')
Traceback:
File "/usr/lib/python2.5/site-packages/django/core/handlers/base.py" in get_response
92. response = callback(request, *callback_args, **callback_kwargs)
File "/usr/lib/python2.5/site-packages/django/contrib/admin/options.py" in wrapper
226. return self.admin_site.admin_view(view)(*args, **kwargs)
File "/usr/lib/python2.5/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
44. response = view_func(request, *args, **kwargs)
File "/usr/lib/python2.5/site-packages/django/contrib/admin/sites.py" in inner
186. return view(request, *args, **kwargs)
File "/usr/lib/python2.5/site-packages/django/contrib/admin/options.py" in changelist_view
912. response = self.response_action(request,queryset=cl.get_query_set())
File "/usr/lib/python2.5/site-packages/django/contrib/admin/options.py" in response_action
694. response = func(self, request, queryset.filter(pk__in=selected))
File ".../pdf/admin.py" in print_selected_pdf
56. return self.create_pdf(queryset.order_by('user'))
File ".../pdf/admin.py" in create_pdf
48. obj.save()
File "/usr/lib/python2.5/site-packages/django/db/models/base.py" in save
410. self.save_base(force_insert=force_insert, force_update=force_update)
File "/usr/lib/python2.5/site-packages/django/db/models/base.py" in save_base
474. rows = manager.filter(pk=pk_val)._update(values)
File "/usr/lib/python2.5/site-packages/django/db/models/query.py" in _update
444. return query.execute_sql(None)
File "/usr/lib/python2.5/site-packages/django/db/models/sql/subqueries.py" in execute_sql
120. cursor = super(UpdateQuery, self).execute_sql(result_type)
File "/usr/lib/python2.5/site-packages/django/db/models/sql/query.py" in execute_sql
2369. cursor.execute(sql, params)
File "/usr/lib/python2.5/site-packages/django/db/backends/util.py" in execute
22. sql = self.db.ops.last_executed_query(self.cursor, sql, params)
File "/usr/lib/python2.5/site-packages/django/db/backends/__init__.py" in last_executed_query
213. u_params = tuple([to_unicode(val) for val in params])
File "/usr/lib/python2.5/site-packages/django/db/backends/__init__.py" in <lambda>
211. to_unicode = lambda s: force_unicode(s, strings_only=True)
File "/usr/lib/python2.5/site-packages/django/utils/encoding.py" in force_unicode
92. raise DjangoUnicodeDecodeError(s, *e.args)
Exception Type: DjangoUnicodeDecodeError at /admin/pdf/abc/
Exception Value: 'utf8' codec can't decode byte 0x89 in position 614: unexpected code byte. You passed in [redacted for length - here it displayed all of the metadata in the PDF]
The PDFs are being stored in a mysql database, in a mediumblob field.
You just lost the game. Use a FileField instead.
I had to use a queryset to avoid the decoding error. Filefield is not practical in my situation.

FormWizard and saving data

im trying to use django formWizard, all is fine, but im getting an error in the last step after submit
Traceback (most recent call last):
File "/home/vacantes/webapps/django/lib/python2.6/django/core/handlers/base.py", line 100, in get_response
response = callback(request, *callback_args, **callback_kwargs)
File "/home/vacantes/webapps/django/lib/python2.6/django/utils/decorators.py", line 21, in _wrapper
return decorator(bound_func)(*args, **kwargs)
File "/home/vacantes/webapps/django/lib/python2.6/django/utils/decorators.py", line 76, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/home/vacantes/webapps/django/lib/python2.6/django/utils/decorators.py", line 17, in bound_func
return func(self, *args2, **kwargs2)
File "/home/vacantes/webapps/django/lib/python2.6/django/contrib/formtools/wizard.py", line 101, in __call__
return self.done(request, final_form_list)
File "/home/vacantes/webapps/django/hay_vacantes/curriculums/forms.py", line 56, in done
website = data['website']
TypeError: __init__() got an unexpected keyword argument 'website'
i dont know where is the problem, in my Model i have a website field, but ... why the error?
model.py
class Generales(models.Model):
usuario = models.OneToOneField(User,unique=True)
.....
estado_civil = models.SmallIntegerField(max_length=1,choices=CIVIL)
website = models.URLField(verbose_name='Website', verify_exists=False,null=True,blank=True)
and in my forms:
forms.py
#others forms
.....
class ContactWizard(FormWizard):
def done(self, request, form_list):
data = {}
for form in form_list:
data.update(form.cleaned_data)
generales = Generales(
usuario = request.user.id,
documento_identidad = data['documento_identidad'],
tipo = data['tipo'],
tel_fijo = data['tel_fijo'],
celular = data['celular'],
foto = data['foto'],
sexo = data['sexo'],
direccion = data['direccion'],
codigo_postal = data['codigo_postal'],
pais = data['pais'],
ciudad = data['ciudad'],
fecha_nacimiento = data['fecha_nacimiento'],
estado_civil = data['estado_civil'],
website = data['website']
)
generales.save()
# others forms
.....
return HttpResponseRedirect('/panel/completo/')
EDIT
urls.py
from vacantes.curriculums.forms import Generales,Areas,Experiencia,Referencias,ContactWizard
urlpatterns = patterns('',
url(r'^completar/$', ContactWizard([Generales, Areas,Experiencia,Referencias])),
)
i dont know if im saving the data like formwizard need, but im trying.
any idea about the error?
thanks
As i have already used FormWizard in my app, the keys of submitted data are not 'website' or 'estado_civil',... but in the form "< form_number>-< field_name>". For example: '0-website' if the field 'website' is in the first form, '1-website' if it is in the second form and so on.
You can print out the whole submitted data dictionary to check that.