# CCS CS Lab Laptops Views # # Douglas Thrift # # $Id$ from ccscslab.laptops.forms import PersonLaptopFormSet, MACLookupForm from ccscslab.laptops.jsons import OnlineJSONEncoder, SyncJSONEncoder, sync_object_hook from ccscslab.laptops.models import Entry, Laptop, OUI, Sync from ccscslab.main.utils import is_wireless from django.contrib.auth.decorators import login_required from django.db.models import Q from django.db.models.query import QuerySet from django.shortcuts import render_to_response from django.http import HttpResponse from django.template import RequestContext from dt_django_http_login import http_login_required, http_permission_required import pytz import re try: import simplejson as json except ImportError: import json @login_required def laptops(request): person = request.user.person_set.get() disabled = True if request.method == 'POST': laptop_formset = PersonLaptopFormSet(request.POST, instance = person) if laptop_formset.is_valid(): for laptop in laptop_formset.save(False): try: Entry.objects.create(person = person, mac_address = person.laptop_set.exclude(mac_address = laptop.mac_address).get(id = laptop.id).mac_address, deleted = True)() except Laptop.DoesNotExist: pass Entry.objects.create(person = person, mac_address = laptop.mac_address, name = laptop.fancy_name)() for laptop in laptop_formset.deleted_objects: Entry.objects.create(person = person, mac_address = laptop.mac_address, deleted = True)() laptop_formset = PersonLaptopFormSet(instance = person) else: disabled = False else: laptop_formset = PersonLaptopFormSet(instance = person) return render_to_response('laptops/laptops', {'disabled': disabled, 'laptop_formset': laptop_formset, 'laptops': person.laptop_set.all(), 'person': person}, context_instance = RequestContext(request)) def mac_lookup(request): mac_address = None oui_iab = None if request.REQUEST.items(): mac_lookup = MACLookupForm(request.REQUEST) if mac_lookup.is_valid(): mac_address = mac_lookup.cleaned_data['mac_address'] oui_iab = OUI.objects.lookup(mac_address) else: mac_lookup = MACLookupForm() return render_to_response('laptops/mac-lookup', {'mac_address': mac_address, 'mac_lookup': mac_lookup, 'oui_iab': oui_iab}, context_instance = RequestContext(request)) @http_login_required(login_realm = 'Laptops Online') def online(request): return HttpResponse(json.dumps(Laptop.objects.filter(online = True), cls = OnlineJSONEncoder), content_type = 'application/json') @http_permission_required('laptops.change_laptop', login_realm = 'Laptops Sync') def sync(request): entries = None if request.method == 'POST' and 's' in request.POST: for entry in json.loads(request.POST.get('e', '[]'), object_hook = sync_object_hook): entries = Entry.objects.filter(person = entry.person, mac_address = entry.mac_address).order_by('-when')[:1] if entries: call = entry.when > pytz.UTC.localize(entries[0].when) else: call = True entry.save() if call: entry() sync = Sync.objects.get_or_create(name = request.POST['s'])[0] entries = Entry.objects.exclude(sync = sync) if sync.last_uuid: entries = entries.filter(when__gt = Entry.objects.get(uuid = sync.last_uuid).when) elif request.method == 'GET' and 's' in request.GET: entries = Entry.objects.all() sync = Sync.objects.get_or_create(name = request.GET['s'])[0] sync.last_uuid = None sync.save() if entries: data = {'e': entries} sync.last_uuid = list(entries)[-1].uuid sync.save() else: data = {} return HttpResponse(json.dumps(data, cls = SyncJSONEncoder), content_type = 'application/json') _mac_address = re.compile(r'^[0-9A-Fa-f]{2}([-:])[0-9A-Fa-f]{2}(?:\1[0-9A-Fa-f]{2}){4}$') @http_permission_required('laptops.change_laptop', login_realm = 'Laptops Update') def update(request): if request.method == 'POST': post = dict(request.POST.lists()) laptops = dict(map(lambda mac_address, ip_address, name: (mac_address, (ip_address, name)), post.get('m', ()), post.get('i', ()), post.get('n', ()))) for mac_address in laptops.keys(): if _mac_address.match(mac_address) is None: del laptops[mac_address] for laptop in Laptop.objects.filter(mac_address__in = laptops.keys()): laptop.online = True laptop.ip_address, laptop.name = laptops[laptop.mac_address] if laptop.name == '*': laptop.name = '' laptop.save() Laptop.objects.exclude(mac_address__in = laptops.keys()).update(online = False, ip_address = None, name = '') return HttpResponse(content_type = 'text/plain')