csv error while uploading file in django admin - django

I'm uploading a file via admin and I'm doing a small cleaning and saving the data in the Model.
So in utils I have a small function
def _map_file_to_model(row):
_into_product = {
"qr_id": "",
"ean": "",
"description": "",
"category": "",
"marketing_text": "",
"bullet": "",
"brand_image": "",
"image": ""
}
return {
_into_product[key]: value
for key, value in row.items()
}
this is a representation of the model. In the admin I'm doing this
#admin.register(Product)
class ProductAdmin(admin.ModelAdmin):
change_list_template = 'product/products_blob.html'
date_hierarchy = 'created_at'
def get_urls(self):
urls = super().get_urls()
qr_urls = [
path('import-file/', self.import_file, name='import-file'),
]
return qr_urls + urls
def import_file(self, request):
if request.method == "POST":
_file = request.FILES['text_file']
reader = csv.reader(_file)
self.message_user(request, "You're text file has been imported")
_read_file = [_map_file_to_model(row) for row in reader]
_product_model = _read_file
Product.objects.bulk_create([
Product(**data) for data in _product_model
])
form = QrImportForm()
payload = {"form": form}
return render(
request, 'product/text_form.html', payload
)
and the complete Traceback
Traceback:
File "/home/copser/Documents/Project/qr-backend/venv/lib/python3.7/site-packages/django/core/handlers/exception.py" in inner
34. response = get_response(request)
File "/home/copser/Documents/Project/qr-backend/venv/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response
115. response = self.process_exception_by_middleware(e, request)
File "/home/copser/Documents/Project/qr-backend/venv/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response
113. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python3.7/contextlib.py" in inner
74. return func(*args, **kwds)
File "/home/copser/Documents/Project/qr-backend/server/products/admin.py" in import_file
36. _read_file = [_map_file_to_model(row) for row in reader]
File "/home/copser/Documents/Project/qr-backend/server/products/admin.py" in <listcomp>
36. _read_file = [_map_file_to_model(row) for row in reader]
Exception Type: Error at /admin/products/product/import-file/
Exception Value: iterator should return strings, not bytes (did you open the file in text mode?)
So can someone please help me explain what is happening, I'm all ready uploading a file so no need to do whit open... but still.

Your request.FILES['text_file'] is an UploadedFile from django and not your file content. You need to call csv.reader(_file.read()) to read your uploaded file before parsing it.

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'.

Unable to search using elasticsearch in django with django-elasticsearch-dsl-drf (Set fielddata=true on [title.raw])

I have followed the quick start guide shown here, in order to experiment with elasticsearch searching and a sample Django app I am playing with.
Using elasticsearch 6.3.1 and latest django-elasticsearch-dsl-drf
The results is the following error.
RequestError at /search/movies/
RequestError(400, 'search_phase_execution_exception', 'Fielddata is disabled on text fields by default. Set fielddata=true on [title.raw] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead.')
I have added in the django project an extra app named search_indexes.
Here is the documents.py from this app.
# Name of the Elasticsearch index
INDEX = Index('search_movies')
# See Elasticsearch Indices API reference for available settings
INDEX.settings(
number_of_shards=1,
number_of_replicas=1
)
html_strip = analyzer(
'html_strip',
tokenizer="standard",
filter=["standard", "lowercase", "stop", "snowball"],
char_filter=["html_strip"]
)
#INDEX.doc_type
class MovieDocument(DocType):
"""Movie Elasticsearch document."""
id = fields.IntegerField(attr='id')
title = fields.StringField(
analyzer=html_strip,
fields={
'raw': fields.StringField(analyzer='keyword'),
}
)
summary = fields.StringField(
analyzer=html_strip,
fields={
'raw': fields.StringField(analyzer='keyword'),
}
)
Now, after running manage.py search_index --rebuild, i can visit the url http://localhost:9200/search_movies/_search?pretty where I can see that the index has been created properly and I can see the data as well.
Moving on to the next part this is my serializers.py file
from django_elasticsearch_dsl_drf.serializers import DocumentSerializer
from .documents import MovieDocument
class MovieDocumentSerializer(DocumentSerializer):
"""Serializer for the document."""
class Meta(object):
"""Meta options."""
# Specify the correspondent document class
document = MovieDocument
# List the serializer fields. Note, that the order of the fields
# is preserved in the ViewSet.
fields = (
'id',
'title',
'summary',
'people',
'genres',
)
and then my views.py
from django_elasticsearch_dsl_drf.constants import (
LOOKUP_FILTER_TERMS,
LOOKUP_FILTER_RANGE,
LOOKUP_FILTER_PREFIX,
LOOKUP_FILTER_WILDCARD,
LOOKUP_QUERY_IN,
LOOKUP_QUERY_GT,
LOOKUP_QUERY_GTE,
LOOKUP_QUERY_LT,
LOOKUP_QUERY_LTE,
LOOKUP_QUERY_EXCLUDE,
)
from django_elasticsearch_dsl_drf.filter_backends import (
FilteringFilterBackend,
IdsFilterBackend,
OrderingFilterBackend,
DefaultOrderingFilterBackend,
SearchFilterBackend,
)
from django_elasticsearch_dsl_drf.viewsets import BaseDocumentViewSet
from django_elasticsearch_dsl_drf.pagination import PageNumberPagination
from .documents import MovieDocument
from .serializers import MovieDocumentSerializer
class MovieDocumentView(BaseDocumentViewSet):
"""The MovieDocument view."""
document = MovieDocument
serializer_class = MovieDocumentSerializer
pagination_class = PageNumberPagination
lookup_field = 'id'
filter_backends = [
FilteringFilterBackend,
IdsFilterBackend,
OrderingFilterBackend,
DefaultOrderingFilterBackend,
SearchFilterBackend,
]
# Define search fields
search_fields = (
'title',
'summary',
)
# Define filter fields
filter_fields = {
'id': {
'field': 'id',
# Note, that we limit the lookups of id field in this example,
# to `range`, `in`, `gt`, `gte`, `lt` and `lte` filters.
'lookups': [
LOOKUP_FILTER_RANGE,
LOOKUP_QUERY_IN,
LOOKUP_QUERY_GT,
LOOKUP_QUERY_GTE,
LOOKUP_QUERY_LT,
LOOKUP_QUERY_LTE,
],
},
'title': 'title.raw',
'genres': {
'field': 'genres',
# Note, that we limit the lookups of `genres` field
# to `terms, `prefix`, `wildcard`, `in` and
# `exclude` filters.
'lookups': [
LOOKUP_FILTER_TERMS,
LOOKUP_FILTER_PREFIX,
LOOKUP_FILTER_WILDCARD,
LOOKUP_QUERY_IN,
LOOKUP_QUERY_EXCLUDE,
],
},
'genres.raw': {
'field': 'genres.raw',
'lookups': [
LOOKUP_FILTER_TERMS,
LOOKUP_FILTER_PREFIX,
LOOKUP_FILTER_WILDCARD,
LOOKUP_QUERY_IN,
LOOKUP_QUERY_EXCLUDE,
],
},
}
# Define ordering fields
ordering_fields = {
'id': 'id',
'title': 'title.raw',
}
# Specify default ordering
ordering = ('id', 'title')
Lastly my urls.py is the following
from django.conf.urls import url, include
from rest_framework import routers
from .views import MovieDocumentView
router = routers.DefaultRouter()
router.register(r'movies', MovieDocumentView, base_name='moviedocument')
urlpatterns = [
url(r'^', include(router.urls)),
]
So, when i visit a url like http://localhost:8000/search/movies/ or http://localhost:8000/search/movies/?summary__contains=photography the error I mentioned above shows up.
Here is the stacktrace
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
/usr/local/lib/python3.7/dist-packages/django_elasticsearch_dsl_drf/filter_backends/search/historical.py:231: UserWarning: SearchFilterBackend is deprecated. Switch to `CompoundSearchFilterBackend`.
self.__class__.__name__
GET http://localhost:9200/search_movies/doc/_search [status:400 request:0.013s]
Internal Server Error: /search/movies/
Traceback (most recent call last):
File "/usr/local/lib/python3.7/dist-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/usr/local/lib/python3.7/dist-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python3.7/dist-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python3.7/dist-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/home/er/.local/lib/python3.7/site-packages/rest_framework/viewsets.py", line 103, in view
return self.dispatch(request, *args, **kwargs)
File "/home/er/.local/lib/python3.7/site-packages/rest_framework/views.py", line 483, in dispatch
response = self.handle_exception(exc)
File "/home/er/.local/lib/python3.7/site-packages/rest_framework/views.py", line 443, in handle_exception
self.raise_uncaught_exception(exc)
File "/home/er/.local/lib/python3.7/site-packages/rest_framework/views.py", line 480, in dispatch
response = handler(request, *args, **kwargs)
File "/home/er/.local/lib/python3.7/site-packages/rest_framework/mixins.py", line 48, in list
return Response(serializer.data)
File "/home/er/.local/lib/python3.7/site-packages/rest_framework/serializers.py", line 765, in data
ret = super(ListSerializer, self).data
File "/home/er/.local/lib/python3.7/site-packages/rest_framework/serializers.py", line 262, in data
self._data = self.to_representation(self.instance)
File "/home/er/.local/lib/python3.7/site-packages/rest_framework/serializers.py", line 683, in to_representation
self.child.to_representation(item) for item in iterable
File "/home/er/.local/lib/python3.7/site-packages/elasticsearch_dsl/search.py", line 329, in __iter__
return iter(self.execute())
File "/home/er/.local/lib/python3.7/site-packages/elasticsearch_dsl/search.py", line 706, in execute
**self._params
File "/home/er/.local/lib/python3.7/site-packages/elasticsearch/client/utils.py", line 84, in _wrapped
return func(*args, params=params, **kwargs)
File "/home/er/.local/lib/python3.7/site-packages/elasticsearch/client/__init__.py", line 844, in search
"GET", _make_path(index, doc_type, "_search"), params=params, body=body
File "/home/er/.local/lib/python3.7/site-packages/elasticsearch/transport.py", line 353, in perform_request
timeout=timeout,
File "/home/er/.local/lib/python3.7/site-packages/elasticsearch/connection/http_urllib3.py", line 236, in perform_request
self._raise_error(response.status, raw_data)
File "/home/er/.local/lib/python3.7/site-packages/elasticsearch/connection/base.py", line 162, in _raise_error
status_code, error_message, additional_info
elasticsearch.exceptions.RequestError: RequestError(400, 'search_phase_execution_exception', 'Fielddata is disabled on text fields by default. Set fielddata=true on [title.raw] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead.')
[07/Aug/2019 11:03:38] "GET /search/movies/?summary__contains=photography HTTP/1.1" 500 154541
What you're supposed to do is to use KeywordField instead of StringField with keyword analyzer:
title = fields.StringField(
analyzer=html_strip,
fields={
'raw': fields.KeywordField(), <---- change this
}
)

Error with prewarming VersatileImageField

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()

In certain cases(when copying text from word) when creating an object I get a Key error

I have a blog site with a form for creating and updating posts. For certain content, when I copy/paste from a word document into the form for creating/editing I get a key error. It doesn't happen for text that is directly written into the form and it doesn't always happen for text that is copied from Microsoft word.
I am completely flummoxed about this. Any help would be much appreciated.
views.py:
def post_detail(request, slug=None):
instance = get_object_or_404(Post, slug=slug)
if instance.publish > timezone.now().date() or instance.draft:
if not request.user.is_staff or not request.user.is_superuser:
raise Http404
share_string = quote_plus(instance.content)
...
Error message:
Traceback:
File "/Users/cward/projects/swanson_speech_therapy2/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
149. response = self.process_exception_by_middleware(e, request)
File "/Users/cward/projects/swanson_speech_therapy2/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
147. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/cward/projects/swanson_speech_therapy2/blog/src/posts/views.py" in post_detail
49. share_string = quote_plus(instance.content)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py" in quote_plus
1308. s = quote(s, safe + ' ')
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py" in quote
1303. return ''.join(map(quoter, s))
Exception Type: KeyError at /blog/test-8-9-10-11/
Exception Value: u'\u2019'

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.