def save(self): fname = env.request.parameters.get("filter_name") if not fname: raise error.PrewikkaUserError(N_("Could not save filter"), N_("No name for this filter was provided")) criteria = dict(zip( env.request.parameters.getlist("filter_types"), (json.loads(c) for c in env.request.parameters.getlist("filter_criteria")) )) fltr = self._db.get_filter(env.request.user, fname) if fltr: if env.request.parameters.get("filter_id") != fname: raise error.PrewikkaUserError(N_("Could not save filter"), N_("The filter name is already used by another filter")) # Do not erase filter components if the dataprovider failed to load new_criteria = fltr.criteria new_criteria.update(criteria) criteria = new_criteria criteria = dict((k, v) for k, v in criteria.items() if v is not None) description = env.request.parameters.get("filter_description", "") self._db.upsert_filter(env.request.user, Filter(fname, description, criteria)) return response.PrewikkaDirectResponse({"type": "ajax-reload"})
def get_filter(self, user, name): rows = self.query("SELECT description, value FROM Prewikka_Filter " "WHERE userid = %s AND name = %s", user.id, name) if not rows: return None description, value = rows[0] return Filter(name, description, json.loads(value))
def get_filters(self, user, ftype=None): l = self.query("SELECT name, description, value FROM Prewikka_Filter " "WHERE userid = %s", user.id) l = next(hookmanager.trigger("HOOK_FILTER_LISTING", l), l) for name, description, value in l: criteria = json.loads(value) if not ftype or ftype in criteria: yield Filter(name, description, criteria)
def get_filter(self, user, name=None, id_=None): query = "SELECT id, name, category, description, value FROM Prewikka_Filter WHERE userid = %(user)s" if name: query += " AND name = %(name)s" if id_: query += " AND id = %(id)s" rows = self.query(query, user=user.id, name=name, id=id_) if not rows: return None id_, name, category, description, value = rows[0] return Filter(id_, name, category, description, json.loads(value))
def send_stream(self, data, event=None, evid=None, retry=None, sync=False): """ Used to print message in stdout. """ if len(data) == 0: return sys.stdout.write('env.web.request.send_stream():\n') try: for key, value in json.loads(data).iteritems(): sys.stdout.write(' %s: %s\n' % (key, value)) except ValueError: sys.stdout.write(' %s\n' % data)
def save(self, name=None): new_name = env.request.parameters.get("filter_name") category = env.request.parameters.get("filter_category") description = env.request.parameters.get("filter_description") if not new_name: raise error.PrewikkaUserError( N_("Could not save filter"), N_("No name for this filter was provided")) elif new_name.startswith("/"): raise error.PrewikkaUserError( N_("Could not save filter"), N_("The filter name cannot start with a slash")) criteria = dict( zip(env.request.parameters.getlist("filter_types"), (json.loads(c) for c in env.request.parameters.getlist("filter_criteria")))) filter_ = self._db.get_filter(env.request.user, name) if name else None filter_id = filter_.id_ if filter_ else None # Ensure the filter name is not already used by this user if new_name != name and self._db.get_filter(env.request.user, new_name): raise error.PrewikkaUserError( N_("Could not save filter"), N_("The filter name is already used by another filter")) # Do not erase filter components if the dataprovider failed to load if filter_: new_criteria = filter_.criteria new_criteria.update(criteria) criteria = new_criteria criteria = dict((k, v) for k, v in criteria.items() if v is not None) self._db.upsert_filter( env.request.user, Filter(filter_id, new_name, category, description, criteria)) return response.PrewikkaResponse({ "type": "reload", "target": "#main_menu_ng", "options": { "filter": new_name } })
def get_property_fail(self, user, key, view=None, default=__sentinel): config = {} view = view or "" rows = self.query( "SELECT view, name, value FROM Prewikka_User_Configuration WHERE userid = %s%s%s" % (self.escape(user.id), self._chk("view", view), self._chk("name", key))) for vname, name, val in rows: viewd = config.setdefault(vname or None, {}) viewd[name] = json.loads(val) if view is self.__ALL_PROPERTIES: return config view = config.get(view or None, {}) return view.get( key, default) if default is not self.__sentinel else view[key]
def __init__(self, param, id_=None, raw=True, set_id=True): dict.__init__(self, json.loads(param)) self._ignored = ["id", "realheight", "realwidth"] if set_id and id_ is not None: self["id"] = id_ if not all(hookmanager.trigger("HOOK_WIDGET_INIT", self)): return if "query" not in self: self["query"] = self._normalize_query() if not raw: limit = self["query"][0].get("limit", env.request.parameters["limit"]) self["title"] = _(self["title"]).replace( "{limit}", text_type(limit) if limit > 0 else "") # we don't use .format() to avoid potential errors
def test_loads(): """ Test prewikka.utils.json.loads(). """ assert json.loads('["foo", {"bar": ["baz", null, 1.0, 2]}]') == ['foo', {'bar': ['baz', None, 1.0, 2]}] assert json.loads('"\\"foo\\bar"') == '"foo\x08ar'
def _configuration(self): rows = env.db.query("SELECT config FROM Prewikka_User_Configuration WHERE userid = %s", self.id) if rows: return json.loads(rows[0][0]) else: return {}