from yapsy.IPlugin import IPlugin
from yapsy.PluginManager import PluginManager
from django.template import loader, Context
from django.db.models import Count
from server.models import *
from django.shortcuts import get_object_or_404
import server.utils as utils

class OperatingSystem(IPlugin):
    def plugin_type(self):
        return 'builtin'

    def widget_width(self):
        return 4

    def get_description(self):
        return 'List of operating system versions'

    def widget_content(self, page, machines=None, theid=None):
        # The data is data is pulled from the database and passed to a template.

        # There are three possible views we're going to be rendering to - front, bu_dashbaord and group_dashboard. If page is set to bu_dashboard, or group_dashboard, you will be passed a business_unit or machine_group id to use (mainly for linking to the right search).
        if page == 'front':
            t = loader.get_template('operatingsystem/templates/os_front.html')

        if page == 'bu_dashboard':
            t = loader.get_template('operatingsystem/templates/os_id.html')

        if page == 'group_dashboard':
            t = loader.get_template('operatingsystem/templates/os_id.html')

        try:
            os_info = machines.values('operating_system').annotate(count=Count('operating_system')).order_by('operating_system')
        except:
            os_info = []

        c = Context({
            'title': 'Operating Systems',
            'data': os_info,
            'theid': theid,
            'page': page
        })
        return t.render(c)

    def filter_machines(self, machines, data):
        # You will be passed a QuerySet of machines, you then need to perform some filtering based on the 'data' part of the url from the show_widget output. Just return your filtered list of machines and the page title.

        machines = machines.filter(operating_system__exact=data)

        return machines, 'Machines running '+data

@@ -1,16 +1,60 @@
+from distutils.version import LooseVersion%0A%0A
 from yapsy.IPlug
@@ -648,16 +648,26 @@
 sed to a
+%0A        #
  templat
@@ -743,16 +743,26 @@
 ing to -
+%0A        #
  front, 
@@ -812,16 +812,26 @@
 s set to
+%0A        #
  bu_dash
@@ -877,16 +877,26 @@
 passed a
+%0A        #
  busines
@@ -951,16 +951,26 @@
 nking to
+%0A        #
  the rig
@@ -1396,16 +1396,33 @@
 nnotate(
+%0A                
 count=Co
@@ -1455,34 +1455,177 @@
 rder_by(
-'operating_system'
+%0A                    'operating_system')%0A            os_info = sorted(%0A                os_info,%0A                key=lambda x: LooseVersion(x%5B%22operating_system%22%5D)
 )%0A      
@@ -1961,16 +1961,26 @@
  need to
+%0A        #
  perform
@@ -2031,24 +2031,34 @@
 the url from
+%0A        #
  the show_wi
@@ -2099,24 +2099,34 @@
 ered list of
+%0A        #
  machines an

Update operating system plugin to sort correctly by version