示例#1
0
 def save(self):
     for key, value in self.data.iteritems():
         if isinstance(value, list):
             WidgetInstanceSensor.saveArrayKey(instance_id=self.instance.id,
                                               key=key,
                                               sensors=value)
         else:
             WidgetInstanceSensor.saveKey(instance_id=self.instance.id,
                                          key=key,
                                          sensor_id=value)
示例#2
0
    def post(self):
        action = self.get_argument('action', None)
        id = self.get_argument('id', None)
        if action=='widget':
            instance = WidgetInstance.get(id);
            forms = WidgetInstanceForms(instance=instance, handler=self)
            if forms.validate():
                forms.save();
                d = WidgetInstanceOption.getInstanceDict(instance_id=id)
                jsonoptions = {'instance_id':id, 'options':d}
                d = WidgetInstanceSensor.getInstanceDict(instance_id=id)
                jsonsensors = {'instance_id':id, 'sensors':d}
                d = WidgetInstanceCommand.getInstanceDict(instance_id=id)
                jsoncommands = {'instance_id':id, 'commands':d}
                for socket in socket_connections:
                    socket.sendMessage(['widgetinstance-options', jsonoptions]);
                    socket.sendMessage(['widgetinstance-sensors', jsonsensors]);
                    socket.sendMessage(['widgetinstance-commands', jsoncommands]);
                self.write("{success:true}")
            else:
                self.render('widgetConfiguration.html', instance=instance, forms=forms)
        elif action=='section':
            Section.update(id, self.get_argument('sectionName'), self.get_argument('sectionDescription', None))
            for p, v in self.request.arguments.iteritems():
                if p.startswith( 'params' ):
                    SectionParam.saveKey(section_id=id, key=p[6:], value=v[0])

            json = to_json(Section.get(id))
            json['params'] = dict ((p.key, p.value) for p in SectionParam.getSection(id))
            for socket in socket_connections:
                socket.sendMessage(['section-details', json])
            self.write("{success:true}")
示例#3
0
    def post(self):
        action = self.get_argument('action', None)
        id = self.get_argument('id', None)
        if action=='widget':
            instance = WidgetInstance.get(id);
            forms = WidgetInstanceForms(instance=instance, handler=self)
            if forms.validate():
                forms.save();
                if (instance.widget.default_style):
                    d = WidgetInstance.getFullOptionsDict(id=id)
                else:
                    d = WidgetInstance.getOptionsDict(id=id)
                jsonoptions = {'instance_id':id, 'options':d}
                d = WidgetInstanceSensor.getInstanceDict(instance_id=id)
                jsonsensors = {'instance_id':id, 'sensors':d}
                d = WidgetInstanceCommand.getInstanceDict(instance_id=id)
                jsoncommands = {'instance_id':id, 'commands':d}
                d = WidgetInstanceDevice.getInstanceDict(instance_id=id)
                jsondevices = {'instance_id':id, 'devices':d}
                for socket in socket_connections:
                    socket.sendMessage(['widgetinstance-options', jsonoptions]);
                    socket.sendMessage(['widgetinstance-sensors', jsonsensors]);
                    socket.sendMessage(['widgetinstance-commands', jsoncommands]);
                    socket.sendMessage(['widgetinstance-devices', jsondevices]);
                self.write("{success:true}")
            else:
                self.render('widgetConfiguration.html', instance=instance, forms=forms)
        elif action=='section':
            Section.update(id, self.get_argument('sectionName'), self.get_argument('sectionDescription', None))
            section = Section.get(id)
            themeSectionStyle = Theme.getParamsDict(section.theme.id, ["section"])

            widgetForm = WidgetStyleForm(handler=self, prefix='params')

            for p, v in self.request.arguments.iteritems():
                if p.startswith( 'params' ):
                    if v[0] and not (p[0] == 'params-SectionBackgroundImage' and v[0] == themeSectionStyle['SectionBackgroundImage']):
                        SectionParam.saveKey(section_id=id, key=p[7:], value=v[0])
                    else:
                        SectionParam.delete(section_id=id, key=p[7:])

            # Send section updated params
            json = to_json(Section.get(id))
            json['params'] = Section.getParamsDict(id)
            WSHandler.sendAllMessage(['section-params', json])

            self.write("{success:true}")
        elif action=='addsection':
            s = Section.add(id, self.get_argument('sectionName'), self.get_argument('sectionDescription'))
            for p, v in self.request.arguments.iteritems():
                if p.startswith( 'params' ):
                    if v[0]:
                        SectionParam.saveKey(section_id=s.id, key=p[7:], value=v[0])
                        print s.id, p[7:], v[0]

            json = to_json(s)
            WSHandler.sendAllMessage(['section-added', json])
            self.write("{success:true}")
示例#4
0
    def post(self):
        action = self.get_argument('action', None)
        id = self.get_argument('id', None)
        if action=='widget':
            instance = WidgetInstance.get(id);
            forms = WidgetInstanceForms(instance=instance, handler=self)
            if forms.validate():
                forms.save();
                if (instance.widget.default_style):
                    d = WidgetInstance.getFullOptionsDict(id=id)
                else:
                    d = WidgetInstance.getOptionsDict(id=id)
                jsonoptions = {'instance_id':id, 'options':d}
                d = WidgetInstanceSensor.getInstanceDict(instance_id=id)
                jsonsensors = {'instance_id':id, 'sensors':d}
                d = WidgetInstanceCommand.getInstanceDict(instance_id=id)
                jsoncommands = {'instance_id':id, 'commands':d}
                d = WidgetInstanceDevice.getInstanceDict(instance_id=id)
                jsondevices = {'instance_id':id, 'devices':d}
                for socket in socket_connections:
                    socket.sendMessage(['widgetinstance-options', jsonoptions]);
                    socket.sendMessage(['widgetinstance-sensors', jsonsensors]);
                    socket.sendMessage(['widgetinstance-commands', jsoncommands]);
                    socket.sendMessage(['widgetinstance-devices', jsondevices]);
                self.write("{success:true}")
            else:
                self.render('widgetConfiguration.html', instance=instance, forms=forms)
        elif action=='section':
            Section.update(id, self.get_argument('sectionName'), self.get_argument('sectionDescription', None))
            section = Section.get(id)
            themeSectionStyle = Theme.getParamsDict(section.theme.id, ["section"])

            widgetForm = WidgetStyleForm(handler=self, prefix='params')

            for p, v in self.request.arguments.iteritems():
                if p.startswith( 'params' ):
                    if v[0] and not (p[0] == 'params-SectionBackgroundImage' and v[0] == themeSectionStyle['SectionBackgroundImage']):
                        SectionParam.saveKey(section_id=id, key=p[7:], value=v[0])
                    else:
                        SectionParam.delete(section_id=id, key=p[7:])

            # Send section updated params
            json = to_json(Section.get(id))
            json['params'] = Section.getParamsDict(id)
            WSHandler.sendAllMessage(['section-params', json])

            self.write("{success:true}")
        elif action=='addsection':
            s = Section.add(id, self.get_argument('sectionName'), self.get_argument('sectionDescription'))
            for p, v in self.request.arguments.iteritems():
                if p.startswith( 'params' ):
                    if v[0]:
                        SectionParam.saveKey(section_id=s.id, key=p[7:], value=v[0])
                        print s.id, p[7:], v[0]

            json = to_json(s)
            WSHandler.sendAllMessage(['section-added', json])
            self.write("{success:true}")
示例#5
0
文件: forms.py 项目: ka2er/domoweb
    def __init__(self, instance, handler=None):
        self.instance = instance
        class OptionsForm(WidgetOptionsForm):
            pass
        class SensorsForm(WidgetSensorsForm):
            pass
        class CommandsForm(WidgetCommandsForm):
            pass
        class DevicesForm(WidgetDevicesForm):
            pass

        widgetoptions = WidgetOption.getWidget(instance.widget_id)
        if widgetoptions:
            self.has_options = True
        widgetsensors = WidgetSensor.getWidget(instance.widget_id) 
        if widgetsensors:
            self.has_sensors = True
        widgetcommands = WidgetCommand.getWidget(instance.widget_id)
        if widgetcommands:
            self.has_commands = True
        widgetdevices = WidgetDevice.getWidget(instance.widget_id)
        if widgetdevices:
            self.has_devices = True

        if not handler:
            options = WidgetInstanceOption.getInstance(instance.id)
            dataOptions = dict([(r.key, r.value) for r in options])
            sensors = WidgetInstanceSensor.getInstance(instance.id)
            dataSensors = dict([(r.key, r.sensor_id) for r in sensors])
            commands = WidgetInstanceCommand.getInstance(instance.id)
            dataCommands = dict([(r.key, r.command_id) for r in commands])
            devices = WidgetInstanceDevice.getInstance(instance.id)
            dataDevices = dict([(r.key, r.device_id) for r in devices])
        else:
            dataOptions = None
            dataSensors = None
            dataCommands = None
            dataDevices = None

        for option in widgetoptions:
            OptionsForm.addField(option=option)
        for option in widgetsensors:
            SensorsForm.addField(option=option)
        for option in widgetcommands:
            CommandsForm.addField(option=option)
        for option in widgetdevices:
            DevicesForm.addField(option=option)

        self.optionsform = OptionsForm(instance=instance, handler=handler, data=dataOptions, prefix='optionparam_')
        self.sensorsform = SensorsForm(instance=instance, handler=handler, data=dataSensors, prefix='sensorparam_')
        self.commandsform = CommandsForm(instance=instance, handler=handler, data=dataCommands, prefix='commandparam_')
        self.devicesform = DevicesForm(instance=instance, handler=handler, data=dataDevices, prefix='deviceparam_')
        self.generalform = WidgetGeneralForm(handler=handler, data=dataOptions, prefix='generalparam_')
        self.styleform = WidgetStyleForm(handler=handler, data=dataOptions, prefix='styleparam_')
示例#6
0
 def WSWidgetInstanceGetsensors(self, data):
     d = WidgetInstanceSensor.getInstanceDict(instance_id=data['instance_id'])
     json = {'instance_id':data['instance_id'], 'sensors':d}
     return ['widgetinstance-sensors', json];
示例#7
0
文件: forms.py 项目: ka2er/domoweb
 def save(self):
     for key, value in self.data.iteritems():
         if isinstance(value, list):
             value = ', '.join(value)
         WidgetInstanceSensor.saveKey(instance_id=self.instance.id, key=key, sensor_id=value)
示例#8
0
文件: views.py 项目: capof/domoweb
def page_elements(request, id):
    """
    Method called when a ui page widgets is accessed
    @param request : HTTP request
    @return an HttpResponse object
    """

    page = Page.objects.get(id=id)
    page_title = "%s %s" % (page.name, _("Widgets"))

    iconsets = PageIcon.objects.values('iconset_id', 'iconset_name').distinct()

    if request.method == 'POST': # If the form has been submitted...
        instances = getDictArray(request.POST, 'instance')
        keys = []
        for key in instances.keys():
            try:
                keys.append(int(key)) # Convert to int and remove new instances
            except ValueError:
                pass
        widgetinstances = WidgetInstance.objects.filter(page_id=id).exclude(id__in=keys).delete()
        for instanceid, instance in instances.items():
            if 'widgetid' in instance:
                w = WidgetInstance(order=0, page=page, widget_id=instance['widgetid'])
                w.save()
                if (instance['featuretype'].startswith('sensor')):
                    s = Sensor.objects.get(id=instance['featureid'])
                    ws = WidgetInstanceSensor(instance=w, key="primary", sensor=s)
                    ws.save()
                else:
                    c = Command.objects.get(id=instance['featureid'])
                    wc = WidgetInstanceCommand(instance=w, key="primary", command=c)
                    wc.save()
                    s = Sensor.objects.get(id=instance['sensor'])
                    ws = WidgetInstanceSensor(instance=w, key="event", sensor=s)
                    ws.save()
            else:
                if 'sensor' in instance: # A command with a sensor
                    w = WidgetInstance.objects.get(id=instanceid)
                    s = Sensor.objects.get(id=instance['sensor'])
                    try:
                        ws = WidgetInstanceSensor.objects.get(instance=w, key="event")
                        ws.sensor = s
                        ws.save()
                    except WidgetInstanceSensor.DoesNotExist:
                        ws = WidgetInstanceSensor(instance=w, key="event", sensor=s)
                        ws.save()
        return redirect('page_view', id=id) # Redirect after POST

    devices = Device.objects.all()
    widgets = Widget.objects.all()
    widgetinstances = WidgetInstance.get_page_list(id)
    
    return go_to_page(
        request, 'elements.html',
        page_title,
        page=page,
        iconsets=iconsets,
        devices=devices,
        widgets=widgets,
        widgetinstances=widgetinstances,
    )
示例#9
0
 def WSWidgetInstanceGetsensors(self, data):
     d = WidgetInstanceSensor.getInstanceDict(
         instance_id=data['instance_id'])
     json = {'instance_id': data['instance_id'], 'sensors': d}
     return ['widgetinstance-sensors', json]
示例#10
0
def page_elements(request, id):
    """
    Method called when a ui page widgets is accessed
    @param request : HTTP request
    @return an HttpResponse object
    """

    page = Page.objects.get(id=id)
    page_title = "%s %s" % (page.name, _("Widgets"))

    iconsets = PageIcon.objects.values('iconset_id', 'iconset_name').distinct()

    if request.method == 'POST':  # If the form has been submitted...
        instances = getDictArray(request.POST, 'instance')
        keys = []
        for key in instances.keys():
            try:
                keys.append(
                    int(key))  # Convert to int and remove new instances
            except ValueError:
                pass
        widgetinstances = WidgetInstance.objects.filter(page_id=id).exclude(
            id__in=keys).delete()
        for instanceid, instance in instances.items():
            if 'widgetid' in instance:
                w = WidgetInstance(order=0,
                                   page=page,
                                   widget_id=instance['widgetid'])
                w.save()
                if (instance['featuretype'].startswith('sensor')):
                    s = Sensor.objects.get(id=instance['featureid'])
                    ws = WidgetInstanceSensor(instance=w,
                                              key="primary",
                                              sensor=s)
                    ws.save()
                else:
                    c = Command.objects.get(id=instance['featureid'])
                    wc = WidgetInstanceCommand(instance=w,
                                               key="primary",
                                               command=c)
                    wc.save()
                    s = Sensor.objects.get(id=instance['sensor'])
                    ws = WidgetInstanceSensor(instance=w,
                                              key="event",
                                              sensor=s)
                    ws.save()
            else:
                if 'sensor' in instance:  # A command with a sensor
                    w = WidgetInstance.objects.get(id=instanceid)
                    s = Sensor.objects.get(id=instance['sensor'])
                    try:
                        ws = WidgetInstanceSensor.objects.get(instance=w,
                                                              key="event")
                        ws.sensor = s
                        ws.save()
                    except WidgetInstanceSensor.DoesNotExist:
                        ws = WidgetInstanceSensor(instance=w,
                                                  key="event",
                                                  sensor=s)
                        ws.save()
        return redirect('page_view', id=id)  # Redirect after POST

    devices = Device.objects.all()
    widgets = Widget.objects.all()
    widgetinstances = WidgetInstance.get_page_list(id)

    return go_to_page(
        request,
        'elements.html',
        page_title,
        page=page,
        iconsets=iconsets,
        devices=devices,
        widgets=widgets,
        widgetinstances=widgetinstances,
    )
示例#11
0
文件: forms.py 项目: Nico0084/domoweb
    def __init__(self, instance, handler=None):
        self.instance = instance
        class OptionsForm(WidgetOptionsForm):
            pass
        class SensorsForm(WidgetSensorsForm):
            pass
        class CommandsForm(WidgetCommandsForm):
            pass
        class DevicesForm(WidgetDevicesForm):
            pass

        widgetoptions = WidgetOption.getWidget(instance.widget_id)
        if widgetoptions:
            self.has_options = True
        widgetsensors = WidgetSensor.getWidget(instance.widget_id) 
        if widgetsensors:
            self.has_sensors = True
        widgetcommands = WidgetCommand.getWidget(instance.widget_id)
        if widgetcommands:
            self.has_commands = True
        widgetdevices = WidgetDevice.getWidget(instance.widget_id)
        if widgetdevices:
            self.has_devices = True

        if not handler:
            options = WidgetInstanceOption.getInstance(instance.id)
            dataOptions = dict([(r.key, r.value) for r in options])
            sensors = WidgetInstanceSensor.getInstance(instance.id)
            dataSensors = dict([(r.key, r.sensor_id) for r in sensors])
            # Rebuild array values
            reg = re.compile(r"(.*)-(\d+)")
            tmpdict = {}
            for k, v in dataSensors.iteritems():
                res = reg.match(k)
                if res:
                    if (res.group(1) not in tmpdict):
                        tmpdict[res.group(1)] = []
                    tmpdict[res.group(1)].append(v)
            dataSensors = dict(dataSensors.items() + tmpdict.items())

            commands = WidgetInstanceCommand.getInstance(instance.id)
            dataCommands = dict([(r.key, r.command_id) for r in commands])
            devices = WidgetInstanceDevice.getInstance(instance.id)
            dataDevices = dict([(r.key, r.device_id) for r in devices])
        else:
            dataOptions = None
            dataSensors = None
            dataCommands = None
            dataDevices = None

        for option in widgetoptions:
            OptionsForm.addField(option=option)
        for option in widgetsensors:
            SensorsForm.addField(option=option)
        for option in widgetcommands:
            CommandsForm.addField(option=option)
        for option in widgetdevices:
            DevicesForm.addField(option=option)

        self.optionsform = OptionsForm(instance=instance, handler=handler, data=dataOptions, prefix='optionparam_')
        self.sensorsform = SensorsForm(instance=instance, handler=handler, data=dataSensors, prefix='sensorparam_')
        self.commandsform = CommandsForm(instance=instance, handler=handler, data=dataCommands, prefix='commandparam_')
        self.devicesform = DevicesForm(instance=instance, handler=handler, data=dataDevices, prefix='deviceparam_')
        self.generalform = WidgetGeneralForm(handler=handler, data=dataOptions, prefix='generalparam_')
        self.styleform = WidgetStyleForm(handler=handler, data=dataOptions, prefix='styleparam_')
示例#12
0
文件: forms.py 项目: Nico0084/domoweb
 def save(self):
     for key, value in self.data.iteritems():
         if isinstance(value, list):
             WidgetInstanceSensor.saveArrayKey(instance_id=self.instance.id, key=key, sensors=value)
         else:
             WidgetInstanceSensor.saveKey(instance_id=self.instance.id, key=key, sensor_id=value)
示例#13
0
    def __init__(self, instance, handler=None):
        self.instance = instance

        class OptionsForm(WidgetOptionsForm):
            pass

        class SensorsForm(WidgetSensorsForm):
            pass

        class CommandsForm(WidgetCommandsForm):
            pass

        class DevicesForm(WidgetDevicesForm):
            pass

        widgetoptions = WidgetOption.getWidget(instance.widget_id)
        if widgetoptions:
            self.has_options = True
        widgetsensors = WidgetSensor.getWidget(instance.widget_id)
        if widgetsensors:
            self.has_sensors = True
        widgetcommands = WidgetCommand.getWidget(instance.widget_id)
        if widgetcommands:
            self.has_commands = True
        widgetdevices = WidgetDevice.getWidget(instance.widget_id)
        if widgetdevices:
            self.has_devices = True

        if not handler:
            options = WidgetInstanceOption.getInstance(instance.id)
            dataOptions = dict([(r.key, r.value) for r in options])
            sensors = WidgetInstanceSensor.getInstance(instance.id)
            dataSensors = dict([(r.key, r.sensor_id) for r in sensors])
            # Rebuild array values
            reg = re.compile(r"(.*)-(\d+)")
            tmpdict = {}
            for k, v in dataSensors.iteritems():
                res = reg.match(k)
                if res:
                    if (res.group(1) not in tmpdict):
                        tmpdict[res.group(1)] = []
                    tmpdict[res.group(1)].append(v)
            dataSensors = dict(dataSensors.items() + tmpdict.items())

            commands = WidgetInstanceCommand.getInstance(instance.id)
            dataCommands = dict([(r.key, r.command_id) for r in commands])
            devices = WidgetInstanceDevice.getInstance(instance.id)
            dataDevices = dict([(r.key, r.device_id) for r in devices])
        else:
            dataOptions = None
            dataSensors = None
            dataCommands = None
            dataDevices = None

        for option in widgetoptions:
            OptionsForm.addField(option=option)
        for option in widgetsensors:
            SensorsForm.addField(option=option)
        for option in widgetcommands:
            CommandsForm.addField(option=option)
        for option in widgetdevices:
            DevicesForm.addField(option=option)

        self.optionsform = OptionsForm(instance=instance,
                                       handler=handler,
                                       data=dataOptions,
                                       prefix='optionparam_')
        self.sensorsform = SensorsForm(instance=instance,
                                       handler=handler,
                                       data=dataSensors,
                                       prefix='sensorparam_')
        self.commandsform = CommandsForm(instance=instance,
                                         handler=handler,
                                         data=dataCommands,
                                         prefix='commandparam_')
        self.devicesform = DevicesForm(instance=instance,
                                       handler=handler,
                                       data=dataDevices,
                                       prefix='deviceparam_')
        self.generalform = WidgetGeneralForm(handler=handler,
                                             data=dataOptions,
                                             prefix='generalparam_')
        self.styleform = WidgetStyleForm(handler=handler,
                                         data=dataOptions,
                                         prefix='styleparam_')