def getUserTimezone(self): """Return user timezone defined in 'timezone' property of user class. If no such property exists return 0 """ userid = self.getuid() timezone = None try: tz = self.user.get(userid, 'timezone') date.get_timezone(tz) timezone = tz except KeyError: pass # If there is no class 'user' or current user doesn't have timezone # property or that property is not set assume he/she lives in # the timezone set in the tracker config. if timezone is None: timezone = self.config['TIMEZONE'] return timezone
def __init__(self, config): self.config = config # set to indicate to roundup not to actually _send_ email # this var must contain a file to write the mail to self.debug = os.environ.get("SENDMAILDEBUG", "") or config["MAIL_DEBUG"] # set timezone so that things like formatdate(localtime=True) # use the configured timezone # apparently tzset doesn't exist in python under Windows, my bad. # my pathetic attempts at googling a Windows-solution failed # so if you're on Windows your mail won't use your configured # timezone. if hasattr(time, "tzset"): os.environ["TZ"] = get_timezone(self.config.TIMEZONE).tzname(None) time.tzset()
def __init__(self, config): self.config = config # set to indicate to roundup not to actually _send_ email # this var must contain a file to write the mail to self.debug = os.environ.get('SENDMAILDEBUG', '') \ or config["MAIL_DEBUG"] # set timezone so that things like formatdate(localtime=True) # use the configured timezone # apparently tzset doesn't exist in python under Windows, my bad. # my pathetic attempts at googling a Windows-solution failed # so if you're on Windows your mail won't use your configured # timezone. if hasattr(time, 'tzset'): os.environ['TZ'] = get_timezone(self.config.TIMEZONE).tzname(None) time.tzset()
def handle(self, outfile=None): ''' Export the specified search query as special CSV format. ''' ''' Export the specified search query as CSV. ''' self._setup_request(True) self._setup() h = self.client.additional_headers h['Content-Type'] = 'text/csv' # some browsers will honor the filename here... h['Content-Disposition'] = 'inline; filename=%s' % self.filename self.client.header() if self.client.env['REQUEST_METHOD'] == 'HEAD': # all done, return a dummy string return 'dummy' sensorspec = {} for k, v in self.filterspec.iteritems(): if k.startswith('sensor.'): sensorspec[k[7:]] = v if k == 'sensor': sensorspec['id'] = v sensor_sort = \ [ 'device.device_group' , 'device.order' , 'device.name' , 'device.adr' , 'order' , 'name' , 'adr' ] last_dg = last_d = None lines = [[''], [''], ['Adr.'], ['date/time'], ['']] sids = [] for s in self.db.sensor.filter_iter \ (None, sensorspec, group = [('+', k) for k in sensor_sort]) : s = self.db.sensor.getnode(s) d = self.db.device.getnode(s.device) if d.device_group: dg = self.db.device_group.getnode(d.device_group) else: dg = None if dg and dg.id != last_dg: lines[0].append(charsetconv(dg.name)) last_dg = dg.id else: lines[0].append('') if d.id != last_d: lines[1].append(charsetconv(d.name)) lines[2].append(charsetconv(d.adr)) last_d = d.id else: lines[1].append('') lines[2].append('') lines[3].append(charsetconv(s.name)) lines[4].append(charsetconv(s.unit)) sids.append(s.id) index_by_sid = {} for n, sid in enumerate(sids): index_by_sid[sid] = n + 1 io = outfile if io is None: io = self.client.request.wfile writer = self.csv_writer \ ( io , dialect = 'excel' , delimiter = self.delimiter , quoting = self.quoting , quotechar = self.quotechar ) for l in lines: self.client._socket_op(writer.writerow, l) sort = [] for dir, key in self.group + self.sort: if key == 'date': sort.append((dir, key)) break else: sort.append(('+', 'date')) for k in sensor_sort: sort.append(('+', 'sensor.' + k)) repr_date = Repr_Date(self.klass) repr_number = Repr_Number(self.klass) # and search last_date = None line = None tz = self.klass.getprops()['date'].offset(self.db) # optimized sql version for postgres (maybe would work for # mysql, too, sqlite won't work, it doesn't return date as a # datetime object if self.db.__module__.endswith('back_postgresql'): TZ = get_timezone(tz) classname = self.klass.classname proptree, sql, args = self.klass._filter_sql \ (self.matches, self.filterspec, sort, retr=1) classes = {} for p in proptree: if 'retrieve' in p.need_for: cn = p.parent.classname if cn != classname: continue ptid = p.parent.id key = (cn, ptid) if key not in classes: classes[key] = ptdict = {} classes[key][p.name] = p assert (len(classes) == 1) self.db.sql(sql, args) while True: row = self.db.cursor.fetchone() if not row: break nodeid = str(row[ptdict['id'].sql_idx]) sens = str(row[ptdict['sensor'].sql_idx]) dt = row[ptdict['date'].sql_idx] val = row[ptdict['val'].sql_idx] if dt != last_date: if line: self.client._socket_op(writer.writerow, line) last_date = dt line = [''] * (len(sids) + 2) dt = datetime(tzinfo=UTC, *dt.timetuple()[:6]) line [0] = dt.astimezone (TZ).strftime \ ('%02d.%02m.%04Y %H:%M:%S') line[index_by_sid[sens]] = locale.format("%2.2f", val) else: for itemid in self.klass.filter_iter \ (self.matches, self.filterspec, sort) : item = self.klass.getnode(itemid) if item.date != last_date: if line: self.client._socket_op(writer.writerow, line) last_date = item.date line = [''] * (len(sids) + 2) d = item.date.local(tz) line [0] = '%02d.%02d.%04d %02d:%02d:%02d' \ % (d.day, d.month, d.year, d.hour, d.minute, d.second) line [index_by_sid [item.sensor]] = locale.format \ ("%2.2f", item.val) if line: self.client._socket_op(writer.writerow, line) return True_Value()