def sanitizeParam(param_key, val): """ return a sanitized parameter value """ if val is not None: constraint = cal_params.get(param_key) if constraint is not None: if constraint == 'date': try: return caldate.parseDateString(val) except (TypeError, ValueError): return None elif constraint == 'int': try: return int(val) except ValueError: return None elif constraint == 'int_list': return strToIntList(val) elif type(constraint) == set: s = val.strip().lower() if s in constraint: return s return None
def eventsByDateRange(self, start, end, org_list, **kwa): """ Returns a sequence of Events with dates between start and end for organizations in org_list. Also, optionally, selects by several criteria from kwa. """ if org_list: oid_test = "AND e.oid in (%s)" % ','.join( [str(int(i)) for i in org_list] ) else: oid_test = "" public = kwa.get('public', 'b') if public != 'b': public_test = "AND e.public = %s" % self._sql_quote(public) else: public_test = "" free = kwa.get('free', 'b') if free != 'b': free_test = "AND e.free = %s" % self._sql_quote(free) else: free_test = "" common = kwa.get('common', 'b') if common != 'b': common_test = "AND e.community = %s" % self._sql_quote(common) else: common_test = "" udf1 = kwa.get('udf1') if udf1 is None: udf1_test = "" else: udf1_test = "AND e.udf1 = %s" % self._sql_quote(udf1) udf2 = kwa.get('udf2') if udf2 is None: udf2_test = "" else: udf2_test = "AND e.udf2 = %s" % self._sql_quote(udf2) gcid = kwa.get('gcid') if gcid is None: gcid_test = "" gcid_from = "" else: gcid_test = "AND EvCats.eid = ev.eid AND EvCats.gcid = %d" % gcid gcid_from = ", EvCats" query = """ SELECT DISTINCT e.eid, e.title, e.description, e.startTime, e.endTime, e.location, e.eventUrl, o.acronym, o.name as orgname, o.url, DATE_FORMAT(ev.sdate, "%%Y-%%m-%%d") as start, DATE_FORMAT(ev.edate, "%%Y-%%m-%%d") as end, TIME_FORMAT(e.startTime, "%%l:%%i %%p") as begins, TIME_FORMAT(e.endTime, "%%l:%%i %%p") as ends, ev.recurs FROM EvDates ev, Events e, Orgs o %s WHERE (ev.sdate <= "%s") AND (ev.edate >= "%s") AND ev.eid = e.eid AND o.oid = e.oid %s %s %s %s %s %s %s ORDER BY ev.sdate, e.startTime, e.title """ % ( gcid_from, cleanDate(end), cleanDate(start), oid_test, public_test, free_test, common_test, udf1_test, udf2_test, gcid_test, ) dicts = Results(self.reader.query(query)).dictionaries() for adict in dicts: for s in ('start', 'end'): adict[s] = caldate.parseDateString(adict[s]) for s in ('begins', 'ends'): adict[s] = adict[s].replace(' ', '').lower() decodeStrings(adict) return dicts