Skip to content

Instantly share code, notes, and snippets.

@mwohlgemuth
Created September 8, 2014 15:19
Show Gist options
  • Save mwohlgemuth/b1c9450a325381258dcb to your computer and use it in GitHub Desktop.
Save mwohlgemuth/b1c9450a325381258dcb to your computer and use it in GitHub Desktop.
sal printers plugin
from yapsy.IPlugin import IPlugin
from yapsy.PluginManager import PluginManager
from django.template import loader, Context
from django.db.models import Count, Avg
from server.models import *
from django.shortcuts import get_object_or_404
import server.utils as utils
from django.conf import settings
from datetime import datetime, timedelta
from django.db.models import Q
class UnauthorisedPrinters(IPlugin):
def show_widget(self, page, machines=None, theid=None):
try:
authorised_printers = settings.AUTHORISED_PRINTERS
except:
authorised_printers = []
if page == 'front':
t = loader.get_template('imp/unauthorisedprinters/templates/front.html')
if not machines:
machines = Machine.objects.all()
if page == 'bu_dashboard':
t = loader.get_template('imp/unauthorisedprinters/templates/id.html')
if not machines:
machines = utils.getBUmachines(theid)
if page == 'group_dashboard':
t = loader.get_template('imp/unauthorisedprinters/templates/id.html')
if not machines:
machine_group = get_object_or_404(MachineGroup, pk=theid)
machines = Machine.objects.filter(machine_group=machine_group)
if machines:
unwanted = []
facts = Fact.objects.filter(fact_name='printers').prefetch_related('machine')
for fact in facts:
printer_in_facts = list(set(fact) & set(authorised_printers))
if len(fact) - len(printer_in_facts) == 0:
unwanted.append(fact.machine)
# remove machines that don't have any fact data
machines = machines.exclude(id__in=[o.id for o in unwanted])
machines = machines.filter(fact__fact_name='printers')
count = machines.count()
else:
count = 0
if count == 0:
size = 0
else:
size = 3
if count == 1:
label = "Non-default Printer"
else:
label = "Non-default Printers"
c = Context({
'title': 'Printers',
'count': count,
'plugin': 'UnauthorisedPrinters',
'theid': theid,
'page': page,
'label': label
})
return t.render(c), size
def filter_machines(self, machines, data):
if data == 'printers':
authorised_printers = settings.AUTHORISED_PRINTERS
facts = Fact.objects.filter(fact_name='printers')
printer_in_facts = list(set(facts) & set(authorised_printers))
if len(facts) - len(printer_in_facts) == 0:
machines = machines.exclude(id=fact.machine.id)
title = 'Machines with Printers'
machines = machines.filter(fact__fact_name='printers')
else:
machines = None
title = None
return machines, title
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment