def getData(self, station = None, years=None, **kwargs): where = Broadcast.BroadcastEnd != None if station is not None: where = And(where, Broadcast.StationID == station.ID) if years is not None: now = TimeUtils.nowDate() where = And(where, Broadcast.BroadcastStart >= TimeUtils.toTimestamp(datetime(year=now.year-years, month=now.month, day=1))) data = self.store.find( ( Station, Func("YEAR", Func("FROM_UNIXTIME", Broadcast.BroadcastStart)), Func("MONTH", Func("FROM_UNIXTIME", Broadcast.BroadcastStart)), Func("SUM", Broadcast.BroadcastEnd - Broadcast.BroadcastStart) ), Broadcast.StationID == Station.ID, where ).group_by(Station, Func("YEAR", Func("FROM_UNIXTIME", Broadcast.BroadcastStart)), Func("MONTH", Func("FROM_UNIXTIME", Broadcast.BroadcastStart))) data = list(self._splitData(data)) if len(data) == 0: raise NoDataArgError(kwargs, station=station, years=years) monthList = list(self._getMonthList(data)) data = list(self._mapData(data, monthList)) data.sort(key=lambda x: unicode(x[0])) dateList = [datetime(year, month+1, day=1, hour=0, minute=0) for year, month in monthList] xticks = list(self._filterTicksExt(dateList, monthList)) tickCoords = [float(i)+0.5 for i in self._filterTicksExt(range(len(monthList)), monthList)] coords = [i for i in xrange(len(monthList))] return (data, coords, xticks, tickCoords)
def handleException(self, trans, exceptionType, exception, tb): plainTextMessage = u"""On request: {0} {1} the following exception occured at {2}: """.format(trans.get_request_method(), trans.get_path(), TimeUtils.nowDate().isoformat()) plainTextMessage += self.generatePlainTextMessage( trans, exceptionType, exception, tb) print(plainTextMessage.encode("utf-8")) if "mail-to" in errors: try: mailConfig = errors["mail-to"] subject = mailConfig["subject"].format(exceptionType.__name__, unicode(exception)) to = mailConfig["to"] sender = mailConfig["sender"] smtp = mailConfig["smtp"] mail = MIMEText(plainTextMessage.encode("utf-8"), _charset="utf-8") mail["Subject"] = subject mail["To"] = ",".join(to) mail["From"] = sender mail["Date"] = self.model.formatHTTPTimestamp(TimeUtils.now()) host = smtp["host"] port = int(smtp.get("port", 25)) user = smtp.get("user", None) password = smtp.get("password", None) secure = smtp.get("secure", None) if not secure in ["starttls", "ssl"]: raise ValueError( "Invalid value for secure: {0}".format(secure)) if secure == "ssl": conn = smtplib.SMTP_SSL(host, port) else: conn = smtplib.SMTP(host, port) if secure == "starttls": conn.starttls() if user is not None and password is not None: conn.login(user, password) conn.sendmail(mail["From"], mail["To"], mail.as_string()) conn.quit() except Exception as e: print("Could not send exception mail: {0}".format(e)) trans.rollback() trans.set_response_code(500) self.out = trans.get_response_stream() trans.set_content_type(ContentType("text/html", "utf-8")) s = u""" <html> <head> <title>Priyom.org internal API error</title> <link rel="stylesheet" type="text/css" href="{0}"/> </head> <body> <h1>Internal API error</h1>""".format( application.get("urlroot", u"") + u"/css/error.css") if self.show: s += u""" <h2>Error information</h2> <dl class="exc-info"> <dt>Exception class:</dt> <dd>{1}</dd> <dt>Message:</dt> <dd>{2}</dd> </dl> <h2>Stacktrace</h2> <p>(most recent call last)</p> <ul>{0}</ul>""".format( u"\n".join(( u"""<li><div class="tb-item-head">File "<span class="tb-file">{0}</span>", line <span class="tb-lineno">{1:d}</span>, in <span class="tb-func">{2}</span></div><div class="tb-item-code">{3}</div>""" .format( escape(os.path.relpath(filename, application["root"])), lineno, escape(funcname), escape(text)) for (filename, lineno, funcname, text) in traceback.extract_tb(tb))), escape(unicode(exceptionType)), escape(unicode(exception)).replace("\n", "<br/>")) else: s += u""" <p>An internal error has occured. Please report this to <a href="mailto:{0}">{1}</a></p>""".format( admin["mail"], admin["name"]) s += u""" </body> </html>""" print >> self.out, s.encode("utf-8")
def handleException(self, trans, exceptionType, exception, tb): plainTextMessage = u"""On request: {0} {1} the following exception occured at {2}: """.format(trans.get_request_method(), trans.get_path(), TimeUtils.nowDate().isoformat()) plainTextMessage += self.generatePlainTextMessage(trans, exceptionType, exception, tb) print(plainTextMessage.encode("utf-8")) if "mail-to" in errors: try: mailConfig = errors["mail-to"] subject = mailConfig["subject"].format(exceptionType.__name__, unicode(exception)) to = mailConfig["to"] sender = mailConfig["sender"] smtp = mailConfig["smtp"] mail = MIMEText(plainTextMessage.encode("utf-8"), _charset="utf-8") mail["Subject"] = subject mail["To"] = ",".join(to) mail["From"] = sender mail["Date"] = self.model.formatHTTPTimestamp(TimeUtils.now()) host = smtp["host"] port = int(smtp.get("port", 25)) user = smtp.get("user", None) password = smtp.get("password", None) secure = smtp.get("secure", None) if not secure in ["starttls", "ssl"]: raise ValueError("Invalid value for secure: {0}".format(secure)) if secure == "ssl": conn = smtplib.SMTP_SSL(host, port) else: conn = smtplib.SMTP(host, port) if secure == "starttls": conn.starttls() if user is not None and password is not None: conn.login(user, password) conn.sendmail(mail["From"], mail["To"], mail.as_string()) conn.quit() except Exception as e : print("Could not send exception mail: {0}".format(e)) trans.rollback() trans.set_response_code(500) self.out = trans.get_response_stream() trans.set_content_type(ContentType("text/html", "utf-8")) s = u""" <html> <head> <title>Priyom.org internal API error</title> <link rel="stylesheet" type="text/css" href="{0}"/> </head> <body> <h1>Internal API error</h1>""".format(application.get("urlroot", u"") + u"/css/error.css") if self.show: s += u""" <h2>Error information</h2> <dl class="exc-info"> <dt>Exception class:</dt> <dd>{1}</dd> <dt>Message:</dt> <dd>{2}</dd> </dl> <h2>Stacktrace</h2> <p>(most recent call last)</p> <ul>{0}</ul>""".format( u"\n".join((u"""<li><div class="tb-item-head">File "<span class="tb-file">{0}</span>", line <span class="tb-lineno">{1:d}</span>, in <span class="tb-func">{2}</span></div><div class="tb-item-code">{3}</div>""".format(escape(os.path.relpath(filename, application["root"])), lineno, escape(funcname), escape(text)) for (filename, lineno, funcname, text) in traceback.extract_tb(tb))), escape(unicode(exceptionType)), escape(unicode(exception)).replace("\n", "<br/>") ) else: s += u""" <p>An internal error has occured. Please report this to <a href="mailto:{0}">{1}</a></p>""".format(admin["mail"], admin["name"]) s += u""" </body> </html>""" print >>self.out, s.encode("utf-8")