summaryrefslogtreecommitdiff
path: root/inventory/fabmap/xmlrpc.py
blob: 97285eb2d227a978a49c3af80444cf54b4c92cef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
from fabmap.models import *

# TODO: Authentication!!!
# TODO: Better error handling

def GetSiteList():
        """Returns a list of sites."""

        sites = Site.objects.all()              

        results = []

        for site in sites:
                results.append({ 'id': site.id, 'lat': site.lat, 'lon': site.lon, 'name': site.name, 'locname': site.locname, 'website': site.website})

        return results

def GetSiteDetails(siteid):
        site = Site.objects.get(id=siteid)
        if site.id:
                vals = {"site": site}
                equipcnt = site.equipment_set.count()
                if equipcnt > 0:
                        equip = []
                        for equipment in site.equipment_set.iterator():
                                caps = []
                                for capability in equipment.type.capabilities.iterator():
                                        caps.append(capability.name)
                                equip.append({"type": equipment.type.name, "maker": equipment.type.maker, "capabilities": caps, "notes": equipment.notes})
                        vals['equipment'] = equip
                
                return vals
        else:
                return {}

def GetEquipmentTypes():
        equiptypes = EquipmentType.objects.all()
        
        results = []
        for e in equiptypes.iterator:
                caps = []
                for capability in e.capabilities.iterator():
                        caps.append(capability.name)
                results.append({'name': e.name, 'maker': e.maker, 'capabilities': caps})
        
        return results
        
def GetCapabilities():
        caps = EquipmentCapability.objects.all()
        ret = []
        for a in caps.iterator():
                ret.append({'id': a.id, 'name': a.name})
        return ret
        
def AddEquipmentType(name, maker, capabilities):
        # Register an equipment type
        et = EquipmentType(name=name, maker=maker)
        et.save()
        for a in capabilities:
                if type(a) is int:
                        et.capabilities.add(a)
        et.save()
        return {'saved': True}
        
def AddEquipment(siteid, equipmenttype, notes):
        # Register equipment
        eq = Equipment(type=equipmenttype, site=siteid, notes=notes)
        eq.save()
        return {'saved': True}

def AddSite(name, locname, lat, lon, website, access):
        # Add a site.
        manager = 1
        # FIXME: Here we should make manager be the same as the authed user.
        site = Site(name=name, locname=locname, lat=lat, lon=lon, website=website, manager=manager, access=access)
        site.save()
        return {'site': site, 'saved': True}
        
def SetSite(siteid, name, locname, lat, lon, website, access):
        site = Site(siteid=siteid)
        if name:                site.name = name
        if locname:     site.locname = locname
        if latitude:    site.latitude = lat
        if longitude:   site.longitude = lon
        if website:     site.website = website
        if access:              site.access = access
        site.save()
        
def AddCapability(capability):
        # Add a capability class
        cap = EquipmentCapability(name=capability)
        cap.save()
        return {'saved': True, 'id': cap.id}
        
def FindEquipmentByCapability(capid):
        # Return a list of equipment, and each equipment type contains a list of sites where it exists.
        
        return {}