def handle(self, *args, **options): if len(args) > 0: raise CommandError("List commands do not accept any argument") assert (self.object_class), "object_class variable must be declared" if options["list_fields"]: self.display_fields() return if options["list_filters"]: self.display_filters() return # --output option if options["fields"]: fields = options["fields"] fields = fields.split(",") self.validate_fields(fields) self.fields = options["fields"].split(",") # --filter-by option if options["filter_by"]: filters, excludes = utils.parse_filters(options["filter_by"]) else: filters, excludes = ({}, {}) self.filters.update(filters) self.excludes.update(excludes) # --user option user = options.get("user") if user: if "@" in user: ucache = UserCache(self.astakos_url, self.astakos_token) user = ucache.get_uuid(user) self.filters[self.user_uuid_field] = user # --deleted option if self.deleted_field: deleted = options.get("deleted") if deleted: self.filters[self.deleted_field] = True else: self.filters[self.deleted_field] = False # Special handling of arguments self.handle_args(self, *args, **options) objects = self.object_class.objects try: objects = objects.filter(**self.filters) objects = objects.exclude(**self.excludes) except FieldError as e: raise CommandError(e) except Exception as e: raise CommandError("Can not filter results: %s" % e) order_key = self.order_by if self.order_by is not None else 'pk' objects = objects.order_by(order_key) # --display-mails option display_mails = options.get("display_mails") if display_mails: if 'user_mail' in self.object_class._meta.get_all_field_names(): raise RuntimeError("%s has already a 'user_mail' attribute") self.fields.append("user.email") self.FIELDS["user.email"] =\ ("user_email", "The email of the owner.") uuids = [getattr(obj, self.user_uuid_field) for obj in objects] ucache = UserCache(self.astakos_url, self.astakos_token) ucache.fetch_names(list(set(uuids))) for obj in objects: uuid = getattr(obj, self.user_uuid_field) obj.user_email = ucache.get_name(uuid) # Special handling of DB results objects = list(objects) self.handle_db_objects(objects, **options) headers = self.fields columns = [self.FIELDS[key][0] for key in headers] table = [] for obj in objects: row = [] for attr in columns: if callable(attr): row.append(attr(obj)) else: item = obj attrs = attr.split(".") for attr in attrs: item = getattr(item, attr) row.append(item) table.append(row) # Special handle of output self.handle_output(table, headers) # Print output output_format = options["output_format"] if output_format != "json" and not options["headers"]: headers = None utils.pprint_table(self.stdout, table, headers, output_format)
def test_parse_many(self): res = utils.parse_filters("x=2,x>=3,y!=4,z<3") filters = {"x": "2", "x__gte": "3", "z__lt": "3"} excludes = {"y": "4"} self.assertEqual(res, (filters, excludes))
def test_parse_empty(self): res = utils.parse_filters("") self.assertEqual(res, ({}, {}))
def test_parse_one(self): res = utils.parse_filters("x=2") self.assertEqual(res, ({"x": "2"}, {})) res = utils.parse_filters("x!=2") self.assertEqual(res, ({}, {"x": "2"}))
def handle(self, *args, **options): if len(args) > 0: raise CommandError("List commands do not accept any argument") assert(self.object_class), "object_class variable must be declared" if options["list_fields"]: self.display_fields() return if options["list_filters"]: self.display_filters() return # --output option if options["fields"]: fields = options["fields"] fields = fields.split(",") self.validate_fields(fields) self.fields = options["fields"].split(",") # --filter-by option if options["filter_by"]: filters, excludes = utils.parse_filters(options["filter_by"]) else: filters, excludes = ({}, {}) self.filters.update(filters) self.excludes.update(excludes) # --user option user = options.get("user") if user: if "@" in user: ucache = UserCache(self.astakos_url, self.astakos_token) user = ucache.get_uuid(user) self.filters[self.user_uuid_field] = user # --deleted option if self.deleted_field: deleted = options.get("deleted") if deleted: self.filters[self.deleted_field] = True else: self.filters[self.deleted_field] = False # Special handling of arguments self.handle_args(self, *args, **options) objects = self.object_class.objects try: objects = objects.filter(**self.filters) objects = objects.exclude(**self.excludes) except FieldError as e: raise CommandError(e) except Exception as e: raise CommandError("Can not filter results: %s" % e) order_key = self.order_by if self.order_by is not None else 'pk' objects = objects.order_by(order_key) # --display-mails option display_mails = options.get("display_mails") if display_mails: if 'user_mail' in self.object_class._meta.get_all_field_names(): raise RuntimeError("%s has already a 'user_mail' attribute") self.fields.append("user.email") self.FIELDS["user.email"] =\ ("user_email", "The email of the owner.") uuids = [getattr(obj, self.user_uuid_field) for obj in objects] ucache = UserCache(self.astakos_url, self.astakos_token) ucache.fetch_names(list(set(uuids))) for obj in objects: uuid = getattr(obj, self.user_uuid_field) obj.user_email = ucache.get_name(uuid) # Special handling of DB results objects = list(objects) self.handle_db_objects(objects, **options) headers = self.fields columns = [self.FIELDS[key][0] for key in headers] table = [] for obj in objects: row = [] for attr in columns: if callable(attr): row.append(attr(obj)) else: item = obj attrs = attr.split(".") for attr in attrs: item = getattr(item, attr) row.append(item) table.append(row) # Special handle of output self.handle_output(table, headers) # Print output output_format = options["output_format"] if output_format != "json" and not options["headers"]: headers = None utils.pprint_table(self.stdout, table, headers, output_format)