I have a celery task which creates and sends a monthy report for every user through SMTP. But after the task is received, sometime later, I get the error message from celery saying,
Traceback (most recent call last):
File "/home/mahi/Desktop/Project_new/backend/flask/lib/python3.10/site-packages/celery/app/trace.py", line 451, in trace_task
R = retval = fun(*args, **kwargs)
File "/home/mahi/Desktop/Project_new/backend/flask/lib/python3.10/site-packages/celery/app/trace.py", line 734, in __protected_call__
return self.run(*args, **kwargs)
File "/home/mahi/Desktop/Quantified Self V2/backend/application.py", line 383, in daily_alert
with smtplib.SMTP("smtp.mail.yahoo.com") as connection:
File "/usr/lib/python3.10/smtplib.py", line 255, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python3.10/smtplib.py", line 341, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib/python3.10/smtplib.py", line 312, in _get_socket
return socket.create_connection((host, port), timeout,
File "/usr/lib/python3.10/socket.py", line 845, in create_connection
raise err
File "/usr/lib/python3.10/socket.py", line 833, in create_connection
sock.connect(sa)
TimeoutError: [Errno 110] Connection timed out
This is the code of my celery task and the setup:
application.config['CELERY_BROKER_URL'] = 'redis://localhost:6379/1'
celery = Celery(application.name , broker=application.config['CELERY_BROKER_URL'])
celery.conf.update(application.config)
#celery.task()
def monthly_report():
with application.app_context():
file_loader = PackageLoader("application", "templates")
env = Environment(loader=file_loader)
# Extracting user
users = user.query.all()
# Creating charts for the trackers
for ak in users:
uid = ak.uid
udata = ak.query.filter_by(uid=uid).first()
trackers = tracker.query.filter_by(u_id=uid).all()
data={}
l=[]
for i in trackers:
logs = logtable.query.filter_by(t_id=i.tracker_id).all()
if(i.tracker_type == 'Numerical' ):
sum,cnt = 0,0
for j in logs:
sum+=int(j.value)
cnt+=1
res = sum/cnt
else:
d,highest,res = {},0,''
for j in logs:
if (str(j.value) in d.keys()):
d[j.value]+=1
else:
d[j.value]=1
for j,k in d.items():
if(k>highest):
highest = k
res = j
data['tracker_name'] = i.tracker_name
data['res'] = res
l.append(data)
# Creating Monthly Report in Html
rendered = env.get_template("report.html").render(udata=udata,trackerdata=l)
filename = "report.html"
with open(f"c{filename}", "w") as f:
f.write(rendered)
msg = MIMEMultipart()
msg["From"] = 'quantified.self.v2#gmail.com'
msg["To"] = ak.mail
msg["Subject"] = "Monthly Report"
body = MIMEText("Inside Body, Testing", "plain")
msg.attach(body)
with open(f"{filename}", "r") as f:
attachment = MIMEApplication(f.read(), Name=basename(filename))
attachment["Content-Disposition"] = 'attachment; filename="{}"'.format(basename(filename))
msg.attach(attachment)
with smtplib.SMTP("smtp.mail.yahoo.com") as connection:
connection.starttls()
connection.login(user='quantified.self.v2#gmail.com', password='mahee#154')
connection.send_message(
msg=msg,
from_addr='quantified.self.v2#gmail.com',
to_addrs=[ak.mail],
)
return "Monthly Report Send"
#celery.on_after_finalize.connect
def setup_periodic_tasks(sender, **kwargs):
sender.add_periodic_task(crontab(hour=10, minute=0), daily_alert.s(), name='Daily Alert')
sender.add_periodic_task(crontab(hour=11, minute=0, day_of_month=1), monthly_report.s(), name="Monthly Reports")
Here user is my sqlite table that contains user data, tracker is the table that contains tracker data, logtable is the table that contains logs for each tracker. So can someone throw light on what's the problem here?
Related
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
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']))])
My scraping code works perfectly in my local computer(Windows 8) but fails in Digital Ocean droplet(Ubuntu 16.04)..Always fails on the second loop giving Bad Status Line or URLError..Have already spent a few hours googling this problem ...
Setup:
PhantomJS 2.1.1
Selenium 2.53.6
Python 2.7
class Elitebet:
t1 = time.time()
driver = webdriver.PhantomJS()
def controller(self):
self.driver.get("http://www.elitebetkenya.com/coupon.php?d")
element = WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.ID, "page")))
soup = BeautifulSoup(self.driver.page_source.encode('utf-8'),"html.parser")
page_number = self.number_of_pages(soup)
self.eliteparser(soup)
for i in range(0, page_number - 10):
page_click = self.driver.find_element_by_xpath("//input[#value='Next']")
page_click.click()
element = WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.ID, "page")))
time.sleep(randint(1,2))
soup = BeautifulSoup(self.driver.page_source.encode('utf-8'),"html.parser")
self.eliteparser(soup)
t2 = time.time() - self.t1
print t2
def number_of_pages(self, x):
numbers = x.find("div", {"class" : "pgLnx"}).contents[2]
return int(re.findall(r'\d+', numbers)[0])
def eliteparser(self,x):
tbody = x.find("tbody")
# league level
for i in tbody.findAll("tr", {"class": "league"}):
league = i.get_text()
handicap01_1, handicap01_draw, handicap01_2, handicap10_1, \
handicap10_2, handicap10_draw, overfh15, underfh15, under25,\
over25 = None,None, None, None, None, None, None, None, None, None
# fixture level
for each in i.find_next_siblings("tr"):
if "league" in each.get("class", []):
break
if "fixture" in each.get("class", []):
home = each.find("span", {"class" :"home uc"}).get_text(strip=True)
away = each.find("span", {"class":"away uc"}).get_text(strip=True)
fixture_time = each.br.get_text().strip()
# print "{} vs {}".format(home,away)
for foo in each.find_next_siblings("tr"):
if "fixture" in foo.get("class", []):
break
tds = foo.findAll("td")
if tds[0].get_text().strip() == "Win-Draw-Win":
home_odds = tds[3].get_text()
draw_odds = tds[4].get_text()
away_odds = tds[5].get_text()
elif tds[0].text == "Handicap (0:1)":
handicap01_1 = tds[3].get_text()
handicap01_draw = tds[4].get_text()
handicap01_2 = tds[5].get_text()
elif tds[0].text == "Double Chance":
oneordraw = tds[3].get_text()
oneortwo = tds[4].get_text()
drawortwo = tds[5].get_text()
elif tds[0].text == "Asian (Draw No Bet)":
asian1 = tds[3].get_text()
asian2 = tds[4].get_text()
elif tds[0].text == "Goal Under/Over (2.5)":
under25 = tds[3].get_text()
over25 = tds[4].get_text()
elif tds[0].text == "1st Half Goal Under/Over (1.5)":
underfh15 = tds[3].get_text()
overfh15 = tds[4].get_text()
elif tds[0].text == "Goal - No Goal":
goal = tds[3].get_text()
no_goal = tds[4].get_text()
elif tds[0].text == "Odd - Even Goal":
odd = tds[3].get_text()
even = tds[4].get_text()
elif tds[0].text == "Handicap (1:0)":
handicap10_1 = tds[3].get_text()
handicap10_draw = tds[4].get_text()
handicap10_2 = tds[5].get_text()
print league
print "{} vs {}".format(home,away)
elite = Elitebet()
elite.controller()
Error message is as follows:
File "elitebet.py", line 147, in <module>
elite.controller()
File "elitebet.py", line 45, in controller
page_click.click()
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webelement.py", line 72, in click
self._execute(Command.CLICK_ELEMENT)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webelement.py", line 461, in _execute
return self._parent.execute(command, params)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 234, in execute
response = self.command_executor.execute(driver_command, params)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/remote_connection.py", line 401, in execute
return self._request(command_info[0], url, body=data)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/remote_connection.py", line 471, in _request
resp = opener.open(request, timeout=self._timeout)
File "/usr/lib/python2.7/urllib2.py", line 429, in open
response = self._open(req, data)
File "/usr/lib/python2.7/urllib2.py", line 447, in _open
'_open', req)
File "/usr/lib/python2.7/urllib2.py", line 407, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 1228, in http_open
return self.do_open(httplib.HTTPConnection, req)
File "/usr/lib/python2.7/urllib2.py", line 1198, in do_open
raise URLError(err)
urllib2.URLError: <urlopen error [Errno 111] Connection refused>
That may be caused by SSL error, i suggest you to use these command-line options:
--ignore-ssl-errors=true --ssl-protocol=any --debug=true
You need to use onResourceError callback, to find out, what's going wrong.
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)
When makes group in view.py, i want make to many users for group.users [refer from_json(in model.py)]
However once user is doesn't matter but, lots of user is that problem
'AttributeError: 'NoneType' object has no attribute '_sa_instance_state''
model.py
user_group_relationship = db.Table('user_group_relationship',
db.Column('user_id', db.Integer,
db.ForeignKey('users.id'), nullable=False),
db.Column('group_id', db.Integer,
db.ForeignKey('groups.id'), nullable=False),
db.PrimaryKeyConstraint('user_id',`enter code here`'group_id'))
class User(db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(64), unique=True, index=True)
realname = db.Column(db.Text)
password_hash = db.Column(db.String(128))
confirmed = db.Column(db.Boolean)
picture = db.Column(db.Text)
groups = db.relationship('Group',
secondary=user_group_relationship,
backref=db.backref('user', lazy='dynamic'),
lazy='dynamic')
news = db.relationship('News', backref='author', lazy='dynamic')
comments = db.relationship('Comment', backref='author', lazy='dynamic')
class Group(db.Model):
__tablename__ = 'groups'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(64), unique=True, index=True)
description = db.Column(db.Text)
created_at = db.Column(db.DateTime, index=True, default=datetime.utcnow)
users = db.relationship('User',
secondary=user_group_relationship,
passive_deletes=True,
backref=db.backref('group', lazy='dynamic'),
lazy='dynamic')
news = db.relationship('News', backref='house', lazy='dynamic')
def from_json(json_group)
name = json_group.get('name')
if name is None or name == '':
raise ValidationError('group does not have a name')
description = json_group.get('description')
group = Group(name, description)
users = json_group.get('users')
if type(users) == list and users is not None and len(users) >= 1:
group.users = [ User.query.filter_by(id=user_id).first() for user_id in users]\
if User.query.filter_by(id=user_id).count() != 0 ]
return group
Here is my view model. When create group, Group has refer to from_json from model
#api.route('/groups', methods=['POST'])
#auth.login_required
#cross_origin(expose_headers='Location')
def post_group():
group = Group.from_json(request.json)
if Group.query.filter_by(name=group.name).count() != 0:
return forbidden('Already name is exist')
db.session.add(group)
db.session.commit()
resp = make_response()
resp.headers['Location'] = url_for('api.get_group', group_id=group.id)
return resp
Here's Debug Log
<!--
Traceback (most recent call last):
File "/home/ju1115kr/venv/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/home/ju1115kr/venv/lib/python2.7/site-packages/flask_cors/extension.py", line 188, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/home/ju1115kr/venv/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/ju1115kr/venv/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/home/ju1115kr/venv/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/ju1115kr/venv/lib/python2.7/site-packages/flask_cors/extension.py", line 188, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/home/ju1115kr/venv/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/ju1115kr/venv/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/home/ju1115kr/venv/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/ju1115kr/venv/lib/python2.7/site-packages/flask_httpauth.py", line 62, in decorated
return f(*args, **kwargs)
File "/home/ju1115kr/venv/lib/python2.7/site-packages/flask_cors/decorator.py", line 127, in wrapped_function
resp = make_response(f(*args, **kwargs))
File "/home/ju1115kr/afoccert/app/api_1_0/groups.py", line 60, in post_group
group = Group.from_json(request.json)
File "/home/ju1115kr/afoccert/app/models.py", line 181, in from_json
group.users = [ User.query.filter_by(id=user_id).first() for user_id in users]
File "/home/ju1115kr/venv/lib/python2.7/site-packages/sqlalchemy/orm/attributes.py", line 224, in __set__
instance_dict(instance), value, None)
File "/home/ju1115kr/venv/lib/python2.7/site-packages/sqlalchemy/orm/dynamic.py", line 139, in set
self._set_iterable(state, dict_, value)
File "/home/ju1115kr/venv/lib/python2.7/site-packages/sqlalchemy/orm/dynamic.py", line 161, in _set_iterable
collection_history=collection_history)
File "/home/ju1115kr/venv/lib/python2.7/site-packages/sqlalchemy/orm/dynamic.py", line 99, in fire_append_event
value = fn(state, value, initiator or self._append_token)
File "/home/ju1115kr/venv/lib/python2.7/site-packages/sqlalchemy/orm/attributes.py", line 1190, in emit_backref_from_collection_append_event
passive=PASSIVE_NO_FETCH)
File "/home/ju1115kr/venv/lib/python2.7/site-packages/sqlalchemy/orm/dynamic.py", line 202, in append
self.fire_append_event(state, dict_, value, initiator)
File "/home/ju1115kr/venv/lib/python2.7/site-packages/sqlalchemy/orm/dynamic.py", line 99, in fire_append_event
value = fn(state, value, initiator or self._append_token)
File "/home/ju1115kr/venv/lib/python2.7/site-packages/sqlalchemy/orm/unitofwork.py", line 46, in append
sess._save_or_update_state(item_state)
File "/home/ju1115kr/venv/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 1606, in _save_or_update_state
halt_on=self._contains_state):
File "/home/ju1115kr/venv/lib/python2.7/site-packages/sqlalchemy/orm/mapper.py", line 2610, in cascade_iterator
visited_states, halt_on))
File "/home/ju1115kr/venv/lib/python2.7/site-packages/sqlalchemy/orm/relationships.py", line 1528, in cascade_iterator
get_all_pending(state, dict_)
File "/home/ju1115kr/venv/lib/python2.7/site-packages/sqlalchemy/orm/dynamic.py", line 185, in get_all_pending
c.all_items
AttributeError: 'NoneType' object has no attribute '_sa_instance_state'
Solved by myself.
When
users = json_group.get('users')
if type(users) == list and users is not None and len(users) >= 1:
group.users = [ User.query.filter_by(id=user_id).first() for user_id in users]\
if User.query.filter_by(id=user_id).count() != 0 ]
users's type is unicode.
so,
>>> users=['ju1115kr']
>>> type(users)
>>> unicode
>>> for i in users:
>>> print i
[
'
j
u
1
1
1
5
k
r
'
]
so i fixed it
user_list = [ str(user) for user in users.strip('[]').split(',') ]
if type(user_list) == list and user_list is not None and len(user_list) >= 1:
group.users = [ User.query.filter_by(id=user_id).first() for user_id in user_list]\
if User.query.filter_by(id=user_id).count() != 0 ]