def filter_queryset(self, request, queryset, view): project_id = None if hasattr(view, "filter_fields") and "project" in view.filter_fields: project_id = request.QUERY_PARAMS.get("project", None) qs = queryset if request.user.is_authenticated() and request.user.is_superuser: qs = qs elif request.user.is_authenticated(): memberships_qs = Membership.objects.filter(user=request.user) if project_id: memberships_qs = memberships_qs.filter(project_id=project_id) # Force users_role table inclusion memberships_qs = memberships_qs.exclude( role__slug="not valid slug") where_sql = [ "users_role.permissions @> ARRAY['{}']".format(self.permission) ] memberships_qs = memberships_qs.extra(where=where_sql) projects_list = [ membership.project_id for membership in memberships_qs ] if len(projects_list) == 0: qs = qs.filter(Q(project__owner=request.user)) elif len(projects_list) == 1: qs = qs.filter( Q(project__owner=request.user) | Q(project=projects_list[0])) else: qs = qs.filter( Q(project__owner=request.user) | Q(project__in=projects_list)) extra_where = ExtraWhere([ "projects_project.public_permissions @> ARRAY['{}']".format( self.permission) ], []) qs.query.where.add(extra_where, OR) else: qs = qs.exclude(project__owner=-1) extra_where = ExtraWhere([ "projects_project.anon_permissions @> ARRAY['{}']".format( self.permission) ], []) qs.query.where.add(extra_where, AND) return super().filter_queryset(request, qs.distinct(), view)
def extra_join_cond(where_class, alias, related_alias): if (alias, related_alias) == ('[sys].[columns]', '[sys].[database_permissions]'): where = '[sys].[columns].[column_id] = ' \ '[sys].[database_permissions].[minor_id]' children = [ExtraWhere([where], ())] return where_class(children) return None
def where(self, *args): clone = self._clone() statement = AND(*args) _sql, _params = statement.as_sql(self.quote_name, clone) if hasattr(_sql, 'to_str'): _sql = _sql.to_str() clone.query.where.add(ExtraWhere([_sql], _params), "AND") return clone
def get_context_data(self, **kwargs): q = self.request.GET.get('q') results = [] if q: results = pg_search(q) if getattr(settings, 'UMAP_USE_UNACCENT', False): # Add unaccent support results.query.where.add(ExtraWhere(("ts @@ plainto_tsquery('simple', unaccent(%s))", ), [q, ]), OR) results = results.order_by('-rank', '-start_publish') results = self.paginate(results) results.object_list = [Map.objects.get(pk=i.obj_pk) for i in results] kwargs.update({ 'maps': results, 'q': q }) return kwargs
def get_active_extra_restriction(where_class, alias, related_alias): where = '{alias}.active = True'.format(alias=alias) children = [ExtraWhere([where], ())] return where_class(children)
def add_where_if_tt(query, alias): tt_ts = get_tt_ts() if tt_ts: sql = '%s.%s <= %%s AND %s.%s > %%s' % (alias, VF, alias, VU) params = (tt_ts, tt_ts) query.where.add(ExtraWhere((sql, ), params), AND)