Django download S3 file and include in zip - django

I've this piece of code to add files to a zip in Django. Works fine when media is local but when using an s3 bucket I get the following error:
FileNotFoundError at /en/admin/certification/application/
[Errno 2] No such file or directory: 'https://bucket-name.s3.amazonaws.com/media/application_files/name_of_the_pdf.pdf'
Here's the code:
filename = settings.MEDIA_ROOT + q.user.username + '-' + q.certification.standard.name + '-' + q.certification.name
zf = zipfile.ZipFile(byte, "w")
zipped_files = []
zip_filename = filename + '.zip'
csv_filename = filename + '.csv'
with open(csv_filename, 'w') as csvfile:
filewriter = csv.writer(csvfile, delimiter=',', quoting=csv.QUOTE_MINIMAL)
for answer in q.answer_set.all():
if answer.question.question_type in ['file', 'files']:
if answer.f :
if 'RDS_HOSTNAME' in os.environ:
res = answer.f.url
else: res = settings.BASE_DIR + answer.f.url
zf.write(res)
elif answer.question.question_type == 'text' : res = answer.text
else : res = answer.checkbox.name
filewriter.writerow([answer.question.question, res])
zf.write(csv_filename)
zf.close()
resp = HttpResponse(
byte.getvalue(), content_type="application/x-zip-compressed")
resp['Content-Disposition'] = 'attachment; filename=%s' % zip_filename
And the traceback:
Traceback:
File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
34. response = get_response(request)
File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
126. response = self.process_exception_by_middleware(e, request)
File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
124. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/contrib/admin/options.py" in wrapper
604. return self.admin_site.admin_view(view)(*args, **kwargs)
File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapped_view
142. response = view_func(request, *args, **kwargs)
File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
44. response = view_func(request, *args, **kwargs)
File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/contrib/admin/sites.py" in inner
223. return view(request, *args, **kwargs)
File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapper
45. return bound_method(*args, **kwargs)
File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapped_view
142. response = view_func(request, *args, **kwargs)
File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/contrib/admin/options.py" in changelist_view
1701. response = self.response_action(request, queryset=cl.get_queryset(request))
File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/contrib/admin/options.py" in response_action
1400. response = func(self, request, queryset)
File "/opt/python/current/app/certification/admin_actions.py" in download_application
31. zf.write(res)
File "/usr/lib64/python3.6/zipfile.py" in write
1617. zinfo = ZipInfo.from_file(filename, arcname)
File "/usr/lib64/python3.6/zipfile.py" in from_file
507. st = os.stat(filename)
Exception Type: FileNotFoundError at /en/admin/certification/application/
Exception Value: [Errno 2] No such file or directory: 'https://bucket-name.s3.amazonaws.com/media/application_files/name_of_the_pdf.pdf'
I have changed the bucket and file names to examples.
What have I got wrong?

This is how I resolved it:
if answer.f :
if 'RDS_HOSTNAME' in os.environ:
res = answer.f.url
response = requests.get(answer.f.url)
zf.writestr(answer.f.url, response.content)
else:
res = settings.BASE_DIR + answer.f.url
zf.write(res)

Related

Django - __str__ returned non-string (type tuple)

I am trying to open the template sem.html and year.html from my project, but it is showing an error unnecessarily. Earlier it will open the template smoothly but when I m trying with the foreign key it is giving an error that str returned a non-string value. I am joining my models.py file code where I created two models named sem and year and in forms.py I have created two forms semforms and yearforms with two fields.
class subjects(models.Model):
subject_code = models.CharField(max_length=20,null=True)
subject_name = models.CharField(max_length=100,null=True)
subject_abv = models.CharField(max_length=10,null=True)
semester = models.IntegerField(null=True)
theory_load = models.IntegerField(null=True)
max_numb_students = models.CharField(max_length=65)
faculty = models.ManyToManyField(facultyload)
def __str__(self):
return self.subject_code,self.subject_name
class semester(models.Model):
sem_num = models.CharField(max_length=10,null=True)
sem_courses = models.ManyToManyField(subjects,related_name='sem_courses')
#property
def get_courses(self):
return self.sem_courses
def __str__(self):
return self.sem_num
class Year(models.Model):
year_name = models.CharField(max_length=50)
courses = models.ManyToManyField(subjects,related_name='courses')
#property
def get_courses(self):
return self.courses
def __str__(self):
return self.year_name
forms.py
class semforms(forms.ModelForm):
class Meta:
model = semester
fields = ['sem_num','sem_courses']
class yearforms(forms.ModelForm):
class Meta:
model = Year
fields = ['year_name','courses']
It is giving this error
TypeError at /sem/
__str__ returned non-string (type tuple)
Request Method: GET
Request URL: http://127.0.0.1:8000/sem/
Django Version: 3.1.4
Exception Type: TypeError
Exception Value:
__str__ returned non-string (type tuple)
Anyone can solve this, please help.
views.py - (same for year function, instead of sem I have taken year)
#login_required(login_url='login')
#allowed_users(allowed_roles=['admin','customer'])
def sem(request):
if request.method == 'POST':
form = semforms(request.POST)
if form.is_valid():
try:
form.save()
return redirect('/sem')
except:
pass
else:
form = semforms()
sem = semester.objects.all()
total_sem = sem.count()
context = {'sem':sem,'total_sem':total_sem,'form':form}
return render(request,"sem.html",context)
#allowed_users(allowed_roles=['admin'])
def edit_sem(request, id):
sem = semester.objects.get(id=id)
return render(request,'editsem.html',{'sem':sem})
#allowed_users(allowed_roles=['admin'])
def update_sem(request, id):
sem = semester.objects.get(id=id)
form = semforms(request.POST,instance=sem)
if form.is_valid():
form.save()
messages.success(request,"Record updated successfully......")
return render(request,"editsem.html",{'sem':sem})
#allowed_users(allowed_roles=['admin'])
def delete_sem(request, id):
load = semester.objects.get(id=id)
load.delete()
return render(request,'sem.html',{'load':load})
Also, this is my full Traceback error.
Internal Server Error: /sem/
Traceback (most recent call last):
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py", line 179, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\contrib\auth\decorators.py", line 21, in _wrapped_view
return view_func(request, *args, **kwargs)
File "C:\Users\Virti Parekh\projects\final\total_load\decorators.py", line 22, in wrapper_func
return view_func(request, *args, **kwargs)
File "C:\Users\Virti Parekh\projects\final\total_load\views.py", line 462, in sem
return render(request,"sem.html",context)
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\shortcuts.py", line 19, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\loader.py", line 62, in render_to_string
return template.render(context, request)
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\backends\django.py", line 61, in render
return self.template.render(context)
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 170, in render
return self._render(context)
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 162, in _render
return self.nodelist.render(context)
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 938, in render
bit = node.render_annotated(context)
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 905, in render_annotated
return self.render(context)
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\loader_tags.py", line 150, in render
return compiled_parent._render(context)
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 162, in _render
return self.nodelist.render(context)
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 938, in render
bit = node.render_annotated(context)
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 905, in render_annotated
return self.render(context)
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\loader_tags.py", line 62, in render
result = block.nodelist.render(context)
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 938, in render
bit = node.render_annotated(context)
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 905, in render_annotated
return self.render(context)
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\defaulttags.py", line 312, in render
return nodelist.render(context)
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 938, in render
bit = node.render_annotated(context)
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 905, in render_annotated
return self.render(context)
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 988, in render
output = self.filter_expression.resolve(context)
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 671, in resolve
obj = self.var.resolve(context)
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 796, in resolve
value = self._resolve_lookup(context)
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 858, in _resolve_lookup
current = current()
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\forms\forms.py", line 297, in as_p
errors_on_separate_row=True,
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\forms\forms.py", line 236, in _html_output
'field_name': bf.html_name,
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\utils\html.py", line 376, in <lambda>
klass.__str__ = lambda self: mark_safe(klass_str(self))
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\forms\boundfield.py", line 34, in __str__
return self.as_widget()
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\forms\boundfield.py", line 97, in as_widget
renderer=self.form.renderer,
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\forms\widgets.py", line 241, in render
context = self.get_context(name, value, attrs)
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\forms\widgets.py", line 678, in get_context
context = super().get_context(name, value, attrs)
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\forms\widgets.py", line 639, in get_context
context['widget']['optgroups'] = self.optgroups(name, context['widget']['value'], attrs)
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\forms\widgets.py", line 587, in optgroups
for index, (option_value, option_label) in enumerate(self.choices):
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\forms\models.py", line 1157, in __iter__
yield self.choice(obj)
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\forms\models.py", line 1171, in choice
self.field.label_from_instance(obj),
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\forms\models.py", line 1240, in label_from_instance
return str(obj)
TypeError: __str__ returned non-string (type tuple)
Change your subjects class __str__ method to this:
class subjects(models.Model):
def __str__(self):
return f"{self.subject_code},{self.subject_name}"

Boto3 SES throws TypeError: sequence item 0: expected str instance, tuple found

I am trying to integrate amazon simple email service to my app. My app is a python3 flask app.
I am using boto3. I am currently getting an error when I try to send a raw email.
I have created a separate route where I call this class and obtain send the "SendEmail" method.
I have a sample data json that would have to be passed on in SendEmail in the comments
I have provided the log of the error below as well
class SES():
def __init__(self, **kwargs):
"""Create template emails that can be used in the code.
The template emails will be stored in the brokerportal
will then get used by the aws ses boto3 client
When sending template emails:
1. Check if the template is part of the listed templates
2. Call the necessary template to build the message
3. Send it to the parties using send email """
self.region = 'ap-south-1'
self.aws_access_key_id = current_app.config['ses_access_key_id'],
self.aws_secret_access_key = current_app.config['ses_secret_access_key']
self.ses = boto3.client('ses', region_name = self.region,
aws_access_key_id = self.aws_access_key_id,
aws_secret_access_key = self.aws_secret_access_key)
def SendEmail(self, data, **kwargs):
"""takes the data and transforms the data into the appropriate frmat
before sending the email
data should have the following:
data = {
'toList':['s#domain.com'],
'ccList':['b#domain.com'],
'bccList':['p#domain.com'],
'TemplateData':{vars},
'Attachment':True,
'fileKey':'<S3path>',
'bucketName': <Amazon s3 bucket name>
'Subject': 'This is a subject',
'TemplateName':'TemplateName'
}"""
CHARSET = "utf-8"
Destination = {
'ToAddresses':data['toList'],
'CcAddresses':data['ccList'],
'BccAddresses':data['bccList']
}
# ReplyToAddresses = [current_app.config['MAIL_USERNAME']],
ReplyToAddresses = ["a#domain.com"],
msg = MIMEMultipart('mixed')
msg['Subject'] = data['Subject']
msg['From'] = "a#domain.com"
if Destination['ToAddresses']:
msg['To'] = ', '.join(str(v) for v in Destination['ToAddresses'])
if Destination['CcAddresses']:
msg['Cc'] = ', '.join(str(v) for v in Destination['CcAddresses'])
if Destination['BccAddresses']:
msg['Bcc'] = ', '.join(str(v) for v in Destination['BccAddresses'])
msg_body = MIMEMultipart('alternative')
#Obtain the template details
# v = current_app.config['db'].view('_design/Email/_view/templates')
v = current_app.config['db'].view('_design/Email/_view/templates')
for row in v[data['TemplateName']]:
Tmplte = Jinja2Template(row.value['HtmlPart'])
HtmlPart = Tmplte.render(data = data['TemplateData'])
htmlpart = MIMEText(HtmlPart)
msg_body.attach(htmlpart)
msg.attach(msg_body)
#Attachment
if data['Attachment']:
filehelper = FileHelper(bucketName=data['bucketName'])
att = MIMEApplication(filehelper.load(data['fileKey']))
att.add_header('Content-Disposition','attachment',filename=fileKey)
msg.attach(att)
else:
pass
try:
current_app.logger.debug(msg)
print(msg)
response = self.ses.send_raw_email(
RawMessage = {
'Data': msg.as_string()
})
return msg
except ClientError as e:
current_app.logger.debug(e.response['Error']['Message'])
return ""
Error:
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1997, in __call__
return self.wsgi_app(environ, start_response) File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1985, in wsgi_app
response = self.handle_exception(e) File "/usr/local/lib/python3.6/dist-packages/flask_cors/extension.py", line 161, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs))) File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb) File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 33, in reraise
raise value File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request() File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e) File "/usr/local/lib/python3.6/dist-packages/flask_cors/extension.py", line 161, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs))) File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb) File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 33, in reraise
raise value File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request() File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args) File "/home/sunil/perilwise/brokerportalbe/main.py", line 522, in sesmailcheck
response = ses.SendEmail(data = data) File "/home/sunil/perilwise/brokerportalbe/repository/sesRepository.py", line 117, in SendEmail
'Data': msg.as_string() File "/usr/local/lib/python3.6/dist-packages/botocore/client.py", line 312, in _api_call
return self._make_api_call(operation_name, kwargs) File "/usr/local/lib/python3.6/dist-packages/botocore/client.py", line 592, in _make_api_call
operation_model, request_dict) File "/usr/local/lib/python3.6/dist-packages/botocore/endpoint.py", line 141, in make_request
return self._send_request(request_dict, operation_model) File "/usr/local/lib/python3.6/dist-packages/botocore/endpoint.py", line 166, in _send_request
request = self.create_request(request_dict, operation_model) File "/usr/local/lib/python3.6/dist-packages/botocore/endpoint.py", line 150, in create_request
operation_name=operation_model.name) File "/usr/local/lib/python3.6/dist-packages/botocore/hooks.py", line 227, in emit
return self._emit(event_name, kwargs) File "/usr/local/lib/python3.6/dist-packages/botocore/hooks.py", line 210, in _emit
response = handler(**kwargs) File "/usr/local/lib/python3.6/dist-packages/botocore/signers.py", line 90, in handler
return self.sign(operation_name, request) File "/usr/local/lib/python3.6/dist-packages/botocore/signers.py", line 154, in sign
auth.add_auth(request) File "/usr/local/lib/python3.6/dist-packages/botocore/auth.py", line 366, in add_auth
self._inject_signature_to_request(request, signature) File "/usr/local/lib/python3.6/dist-packages/botocore/auth.py", line 369, in _inject_signature_to_request
l = ['AWS4-HMAC-SHA256 Credential=%s' % self.scope(request)] File "/usr/local/lib/python3.6/dist-packages/botocore/auth.py", line 319, in scope
return '/'.join(scope) TypeError: sequence item 0: expected str instance, tuple found
I solved this by making the SES() like a module that can be called from main.py (i've given the code below):
class SES(object):
def __init__(self, app = None):
"""Create template emails that can be used in the code.
The template emails will be stored in the brokerportal
will then get used by the aws ses boto3 client
When sending template emails:
1. Check if the template is part of the listed templates
2. Call the necessary template to build the message
3. Send it to the parties using send email """
if app is not None:
self.init_app(app)
def init_app(self, app):
self.region = app.config.get('ap-south-1')
self.aws_access_key_id = app.config.get('ses_access_key_id')
self.aws_secret_access_key = app.config.get('ses_secret_access_key')
self.ses = self._connect()
def _connect():
clt = boto3.client('ses', region_name = self.region,
aws_access_key_id = self.aws_access_key_id,
aws_secret_access_key = self.aws_secret_access_key)
return clt
def SendEmail(self, data, **kwargs):
"""takes the data and transforms the data into the appropriate frmat
before sending the email
data should have the following:
data = {
'toList':['s#domain.com'],
'ccList':['b#domain.com'],
'bccList':['p#domain.com'],
'TemplateData':{vars},
'Attachment':True,
'fileKey':'<S3path>',
'bucketName': <Amazon s3 bucket name>
'Subject': 'This is a subject',
'TemplateName':'TemplateName'
}"""
CHARSET = "utf-8"
Destination = {
'ToAddresses':data['toList'],
'CcAddresses':data['ccList'],
'BccAddresses':data['bccList']
}
# ReplyToAddresses = [current_app.config['MAIL_USERNAME']],
ReplyToAddresses = ["a#domain.com"],
msg = MIMEMultipart('mixed')
msg['Subject'] = data['Subject']
msg['From'] = "a#domain.com"
if Destination['ToAddresses']:
msg['To'] = ', '.join(str(v) for v in Destination['ToAddresses'])
if Destination['CcAddresses']:
msg['Cc'] = ', '.join(str(v) for v in Destination['CcAddresses'])
if Destination['BccAddresses']:
msg['Bcc'] = ', '.join(str(v) for v in Destination['BccAddresses'])
msg_body = MIMEMultipart('alternative')
#Obtain the template details
# v = current_app.config['db'].view('_design/Email/_view/templates')
v = current_app.config['db'].view('_design/Email/_view/templates')
for row in v[data['TemplateName']]:
Tmplte = Jinja2Template(row.value['HtmlPart'])
HtmlPart = Tmplte.render(data = data['TemplateData'])
htmlpart = MIMEText(HtmlPart)
msg_body.attach(htmlpart)
msg.attach(msg_body)
#Attachment
if data['Attachment']:
filehelper = FileHelper(bucketName=data['bucketName'])
att = MIMEApplication(filehelper.load(data['fileKey']))
att.add_header('Content-Disposition','attachment',filename=fileKey)
msg.attach(att)
else:
pass
try:
current_app.logger.debug(msg)
print(msg)
response = self.ses.send_raw_email(
RawMessage = {
'Data': msg.as_string()
})
return msg
except ClientError as e:
current_app.logger.debug(e.response['Error']['Message'])
return ""
I then called ses = SES(app) in the main.py route and called ses like a function
This solved my problem

how to resolve error KeyError: 'employee_id'

i'm using odoo 8 and i have develop a custom module for biometric device and it shows error
userHasUserDevice = biometric_user_obj.search([('employee_id', '=', int(values['employee_id'])),
KeyError: 'employee_id'
i tried to fix the problem but it always display the same problem. Any idea for help please ?
Traceback
Traceback (most recent call last):
File "D:\odoo_8.0.20170129\odoo-8.0-20170129\openerp\http.py", line 544, in _handle_exception
return super(JsonRequest, self)._handle_exception(exception)
File "D:\odoo_8.0.20170129\odoo-8.0-20170129\openerp\http.py", line 581, in dispatch
result = self._call_function(**self.params)
File "D:\odoo_8.0.20170129\odoo-8.0-20170129\openerp\http.py", line 317, in _call_function
return checked_call(self.db, *args, **kwargs)
File "D:\odoo_8.0.20170129\odoo-8.0-20170129\openerp\service\model.py", line 118, in wrapper
return f(dbname, *args, **kwargs)
File "D:\odoo_8.0.20170129\odoo-8.0-20170129\openerp\http.py", line 314, in checked_call
return self.endpoint(*a, **kw)
File "D:\odoo_8.0.20170129\odoo-8.0-20170129\openerp\http.py", line 810, in __call__
return self.method(*args, **kw)
File "D:\odoo_8.0.20170129\odoo-8.0-20170129\openerp\http.py", line 410, in response_wrap
response = f(*args, **kw)
File "D:\odoo_8.0.20170129\odoo-8.0-20170129\openerp\addons\web\controllers\main.py", line 948, in call_button
action = self._call_kw(model, method, args, {})
File "D:\odoo_8.0.20170129\odoo-8.0-20170129\openerp\addons\web\controllers\main.py", line 936, in _call_kw
return getattr(request.registry.get(model), method)(request.cr, request.uid, *args, **kwargs)
File "D:\odoo_8.0.20170129\odoo-8.0-20170129\openerp\api.py", line 268, in wrapper
return old_api(self, *args, **kwargs)
File "D:\odoo_8.0.20170129\odoo-8.0-20170129\openerp\addons\hr_biometric_machine\models\biometric_data_wizard.py", line 22, in import_attendance
biometric_attendance.crate_attendance_in_openep()
File "D:\odoo_8.0.20170129\odoo-8.0-20170129\openerp\api.py", line 266, in wrapper
return new_api(self, *args, **kwargs)
File "D:\odoo_8.0.20170129\odoo-8.0-20170129\openerp\addons\hr_biometric_machine\models\biometric_data_wizard.py", line 36, in crate_attendance_in_openep
biometric_machine, biometric_data_obj, biometric_user_obj,)
File "D:\odoo_8.0.20170129\odoo-8.0-20170129\openerp\addons\hr_biometric_machine\models\biometric_data.py", line 145, in import_data_classmethod
attendances = biometric_machine.getattendance()
File "C:\Python2.7.13\lib\site-packages\mock.py", line 1201, in patched
return func(*args, **keywargs)
File "D:\odoo_8.0.20170129\odoo-8.0-20170129\openerp\addons\hr_biometric_machine\models\biometric_machine.py", line 136, in getattendance
self.create_user()
File "D:\odoo_8.0.20170129\odoo-8.0-20170129\openerp\api.py", line 266, in wrapper
return new_api(self, *args, **kwargs)
File "D:\odoo_8.0.20170129\odoo-8.0-20170129\openerp\addons\hr_biometric_machine\models\biometric_machine.py", line 119, in create_user
'biometric_device': self.id, }
File "D:\odoo_8.0.20170129\odoo-8.0-20170129\openerp\api.py", line 266, in wrapper
return new_api(self, *args, **kwargs)
File "D:\odoo_8.0.20170129\odoo-8.0-20170129\openerp\addons\hr_biometric_machine\models\biometric_user.py", line 33, in create
userHasUserDevice = biometric_user_obj.search([('employee_id', '=', int(values['employee_id'])),
KeyError: 'employee_id'
biometric_user.py
class BiometricUser(models.Model):
_name = 'biometric.user'
biometric_id = fields.Integer('Id in biometric device')
name = fields.Char('Name in biometric device')
employee_id = fields.Many2one('hr.employee', 'Related employee')
biometric_device = fields.Many2one(
'biometric.machine', 'Biometric device', )
#api.model
def create(self, values):
userExist = None
biometric_machine_obj = self.env['biometric.machine']
biometric_user_obj = self.env['biometric.user']
userExist = biometric_user_obj.search([('biometric_id', '=', int(values['biometric_id'])),
('biometric_device', '=', int(values['biometric_device']))])
userHasUserDevice = biometric_user_obj.search([('employee_id', '=', int(values['employee_id'])),
('biometric_device', '=', int(values['biometric_device']))])
print(len(userExist))
if len(userExist) != 0:
raise Warning(_('User exist in ZK machine'))
else:
if len(userHasUserDevice) != 0:
raise Warning(_('Every employee doesn\'t have more than one user in a specific biometric device !'))
else:
openerp_machine = biometric_machine_obj.search([('id', '=', values['biometric_device']), ], )
print ("openerp_machine:", openerp_machine['ip_address'])
print ("biometric_device:", values['biometric_device'])
conn = None
zk = ZK(str(openerp_machine['ip_address']), port=int(openerp_machine['port']), timeout=5)
try:
print 'Connecting to device ...'
conn = zk.connect()
print 'Disabling device ...'
conn.disable_device()
conn.set_user(uid=int(str(values['biometric_id'])), name=str(values['name']),
privilege=const.USER_DEFAULT, password='', group_id='',
user_id=str(values['biometric_id']))
print 'Enabling device ...'
conn.enable_device()
res_id = super(BiometricUser, self).create(values)
except Exception, e:
print "Process terminate : {}".format(e)
raise Warning(_('Something bad happened. ' + str(e)))
finally:
if conn:
conn.disconnect()
return res_id
Please check values of create() method, employee_id is not available and you are trying to get employee_id from values.
You should check that employee_id is available in values or not as like following :
if 'employee_id' in values :
userHasUserDevice = biometric_user_obj.search([('employee_id', '=', int(values['employee_id'])),
('biometric_device', '=', int(values['biometric_device']))])

Django request.FILES deletes after use?

Please help me understand :
With that code, the file is stored and everything goes well :
uploaded_file = request.FILES['file']
file_name = str(uploaded_file.name)
print ("filename = ", file_name)
#project_name = extract_project_name(uploaded_file)
#print ("project name = ", project_name)
found_entries = Project.objects.filter(name=file_name)
if(found_entries.count() == 0):
project = Project()
project.create(
file=uploaded_file,
)
But if I uncomment, I have an error to re extract data when creating the object in the database, because it is None ...
Any Idea ?
Traceback :
Traceback (most recent call last):
File "<some path>/.local/lib/python3.5/site-packages/django/core/handlers/base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "<some path>/.local/lib/python3.5/site-packages/django/core/handlers/base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "<path here>/views.py", line 101, in upload_project_file
file=uploaded_file,
File "<path here>/project.py", line 33, in create
project.extract_project_infos()
File "<path here>/project.py", line 40, in extract_project_infos
Project_Data = json.loads(data)
File "/usr/lib/python3.5/json/__init__.py", line 319, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.5/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.5/json/decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Odoo from old apie to new api and error TypeError: unhashable type: 'list'

I was trying to use functions from old api to new api:
#api.multi
def search(self, *args, **kwargs):
offset = kwargs.get("offset", 0)
limit = kwargs.get("limit", None)
order = kwargs.get("order", 'complete_name')
context = kwargs.get("context", None)
count = kwargs.get("count", False)
for index, expr in enumerate(args):
if expr[1] in ['like', 'ilike', 'not ilike', 'not like', '=like']:
args[index][2] = re.sub(r"\s+", '%', expr[2])
return super(stock_location, self).search(*args, **kwargs)
and
def name_search(self, cr, uid, name='', args=None, operator='ilike', context=None, limit=80):
args.append(['complete_name', operator, name])
ids = self.search(cr, uid, args, limit=limit, context=context)
return self.name_get(cr, uid, ids)
I got error.
Odoo Server Error
Traceback (most recent call last):
File "/opt/odoo/openerp/http.py", line 643, in _handle_exception
return super(JsonRequest, self)._handle_exception(exception)
File "/opt/odoo/openerp/http.py", line 680, in dispatch
result = self._call_function(**self.params)
File "/opt/odoo/openerp/http.py", line 316, in _call_function
return checked_call(self.db, *args, **kwargs)
File "/opt/odoo/openerp/service/model.py", line 118, in wrapper
return f(dbname, *args, **kwargs)
File "/opt/odoo/openerp/http.py", line 309, in checked_call
result = self.endpoint(*a, **kw)
File "/opt/odoo/openerp/http.py", line 959, in __call__
return self.method(*args, **kw)
File "/opt/odoo/openerp/http.py", line 509, in response_wrap
response = f(*args, **kw)
File "/opt/odoo/addons/web/controllers/main.py", line 892, in call_kw
return self._call_kw(model, method, args, kwargs)
File "/opt/odoo/addons/web/controllers/main.py", line 884, in _call_kw
return getattr(request.registry.get(model), method)(request.cr, request.uid, *args, **kwargs)
File "/opt/odoo/openerp/api.py", line 250, in wrapper
return old_api(self, *args, **kwargs)
File "/opt/odoo/openerp/api.py", line 381, in old_api
result = method(recs, *args, **kwargs)
File "/opt/odoo/openerp/models.py", line 6053, in onchange
record._onchange_eval(name, field_onchange[name], result)
File "/opt/odoo/openerp/models.py", line 5910, in _onchange_eval
method_res = method(self)
File "/home/del/addons/mik_stock/wizard/stock_operation.py", line 193, in onchange_picking_type
if self.env['stock.location'].search_count(domain_from) == 1:
File "/opt/odoo/openerp/api.py", line 248, in wrapper
return new_api(self, *args, **kwargs)
File "/opt/odoo/openerp/api.py", line 490, in new_api
result = method(self._model, cr, uid, *args, **old_kwargs)
File "/opt/odoo/openerp/models.py", line 1634, in search_count
res = self.search(cr, user, args, context=context, count=True)
File "/opt/odoo/openerp/api.py", line 250, in wrapper
return old_api(self, *args, **kwargs)
File "/opt/odoo/openerp/api.py", line 380, in old_api
recs = self.browse(cr, uid, ids, context)
File "/opt/odoo/openerp/api.py", line 250, in wrapper
return old_api(self, *args, **kwargs)
File "/opt/odoo/openerp/models.py", line 5361, in browse
return self._browse(Environment(cr, uid, context or {}), ids)
File "/opt/odoo/openerp/models.py", line 5354, in _browse
env.prefetch[cls._name].update(ids)
TypeError: unhashable type: 'list'
When I use only search function with decorator #api.multi everything seems ok. So maybe this error because of name_search function? Do I have decorate this function and cr, uid, ids change to self.env?
You are not passing your domain (search conditions) properly.
Your args should contain List of tuples,
like
[('phone','=','1122445566'),('id','!=',self.id)]
In short,
[(field, operator, value)]