commit b0e7f310601c01258c9a4ad27e6fe59c2b7a1742
Author: Christos Stavrakakis <cstavr@grnet.gr>
Date:   Thu Mar 14 11:44:56 2013 +0200

    Add ListCommand to be used for all *-list commands
    
    Create a generic ListCommand to be user for all *-list management
    commands. ListCommand handles must tasks that are common to all
    management commands, like retrieving objects from db, filtering
    results and formating output to a pretty table, json or csv.
    
    Also, implement server-list, network-list, backend-list and flavor-list
    as subclasses of this command.

diff --git a/snf-cyclades-app/synnefo/api/management/commands/flavor-list.py b/snf-cyclades-app/synnefo/api/management/commands/flavor-list.py
index 510dec6..3970afa 100644
--- a/snf-cyclades-app/synnefo/api/management/commands/flavor-list.py
+++ b/snf-cyclades-app/synnefo/api/management/commands/flavor-list.py
@@ -1,4 +1,4 @@
-# Copyright 2012 GRNET S.A. All rights reserved.
+# Copyright 2012-2013 GRNET S.A. All rights reserved.
 #
 # Redistribution and use in source and binary forms, with or
 # without modification, are permitted provided that the following
@@ -31,67 +31,28 @@
 # interpreted as representing official policies, either expressed
 # or implied, of GRNET S.A.
 
-from optparse import make_option
+from synnefo.webproject.management.commands import ListCommand
+from synnefo.db.models import Flavor, VirtualMachine
 
-from django.core.management.base import BaseCommand, CommandError
-from synnefo.management.common import (format_bool, filter_results,
-                                       pprint_table)
 
-from synnefo.db.models import Flavor
+class Command(ListCommand):
+    help = "List available server flavors"
 
-FIELDS = Flavor._meta.get_all_field_names()
+    object_class = Flavor
+    deleted_field = "deleted"
 
+    def get_vms(flavor):
+        return VirtualMachine.objects.filter(flavor=flavor, deleted=False)\
+                                     .count()
 
-class Command(BaseCommand):
-    help = "List flavors"
+    FIELDS = {
+        "id": ("id", "Flavor's unique ID"),
+        "name": ("name", "Flavor's unique name"),
+        "cpu": ("cpu", "Number of CPUs"),
+        "ram": ("ram", "Size(MB) of RAM"),
+        "disk": ("disk", "Size(GB) of disk"),
+        "template": ("disk_template", "Disk template"),
+        "vms": (get_vms, "Number of active servers using this flavor")
+    }
 
-    option_list = BaseCommand.option_list + (
-        make_option(
-            '-c',
-            action='store_true',
-            dest='csv',
-            default=False,
-            help="Use pipes to separate values"),
-        make_option(
-            '--deleted',
-            action='store_true',
-            dest='deleted',
-            default=False,
-            help="Include deleted flavors"),
-        make_option(
-            '--filter-by',
-            dest='filter_by',
-            help="Filter results. Comma seperated list of key=val pairs"
-                 " that displayed entries must satisfy. e.g."
-                 " --filter-by \"cpu=1,ram!=1024\"."
-                 "Available keys are: %s" % ", ".join(FIELDS))
-    )
-
-    def handle(self, *args, **options):
-        if args:
-            raise CommandError("Command doesn't accept any arguments")
-
-        if options['deleted']:
-            flavors = Flavor.objects.all()
-        else:
-            flavors = Flavor.objects.filter(deleted=False)
-
-        filter_by = options['filter_by']
-        if filter_by:
-            flavors = filter_results(flavors, filter_by)
-
-        headers = ('id', 'name', 'cpus', 'ram', 'disk', 'template', 'deleted')
-        table = []
-        for flavor in flavors.order_by('id'):
-            id = str(flavor.id)
-            cpu = str(flavor.cpu)
-            ram = str(flavor.ram)
-            disk = str(flavor.disk)
-            deleted = format_bool(flavor.deleted)
-            fields = (id, flavor.name, cpu, ram, disk, flavor.disk_template,
-                      deleted)
-
-            table.append(fields)
-
-        separator = " | " if options['csv'] else None
-        pprint_table(self.stdout, table, headers, separator)
+    fields = ["id", "name", "cpu", "ram", "disk", "template", "vms"]
