Exemple #1
0
    def hasDefinition(self, department):
        """
        Get definition for current alarmkey of given department

        :param department: id of department
        :return: :py:class:`emonitor.modules.alarmkeys.alarmkey.Alarmkey` or *None*
        """
        return AlarmkeyCars.getAlarmkeyCars(kid=self.id or 9999, dept=department) is None
    def hasDefinition(self, department):
        """
        Get definition for current alarmkey of given department

        :param department: id of department
        :return: :py:class:`emonitor.modules.alarmkeys.alarmkey.Alarmkey` or *None*
        """
        return AlarmkeyCars.getAlarmkeyCars(kid=self.id or 9999, dept=department) is None
Exemple #3
0
    def _getCars(self, cartype, department):
        """
        Prototype method for car or material lists

        :param cartype: 1|2|3: cars1, cars2, material as integer
        :param department: id of department as integer
        :return: list of cars, material
        """
        alarmcars = AlarmkeyCars.getAlarmkeyCars(kid=self.id or 9999, dept=department)

        if not alarmcars:
            # try default
            alarmcars = AlarmkeyCars.getAlarmkeyCars(kid=9999, dept=department)

        if alarmcars:
            if cartype == 1:
                return alarmcars.cars1
            elif cartype == 2:
                return alarmcars.cars2
            elif cartype == 3:
                return alarmcars.materials
        else:
            return []
    def _getCars(self, cartype, department):
        """
        Prototype method for car or material lists

        :param cartype: 1|2|3: cars1, cars2, material as integer
        :param department: id of department as integer
        :return: list of cars, material
        """
        alarmcars = AlarmkeyCars.getAlarmkeyCars(kid=self.id or 9999, dept=department)

        if not alarmcars:
            # try default
            alarmcars = AlarmkeyCars.getAlarmkeyCars(kid=9999, dept=department)

        if alarmcars:
            if cartype == 1:
                return alarmcars.cars1
            elif cartype == 2:
                return alarmcars.cars2
            elif cartype == 3:
                return alarmcars.materials
        else:
            return []
Exemple #5
0
    def setCars(self, department, **kwargs):
        """
        Set carlist of department

        :param department: id of department as integer
        :param kwargs:
            - *cars1*: list of :py:class:`emonitor.modules.cars.car.Car` objects for cars1
            - *cars2*: list of :py:class:`emonitor.modules.cars.car.Car` objects for cars2
            - *material*: list of :py:class:`emonitor.modules.cars.car.Car` objects for material
        """
        alarmcars = AlarmkeyCars.getAlarmkeyCars(kid=self.id, dept=department)
        if not alarmcars:
            alarmcars = AlarmkeyCars(self.id, department, '', '', '')
            db.session.add(alarmcars)
        if "cars1" in kwargs.keys():
            alarmcars._cars1 = kwargs['cars1']
        if "cars2" in kwargs.keys():
            alarmcars._cars2 = kwargs['cars2']
        if "material" in kwargs.keys():
            alarmcars._material = kwargs['material']
    def setCars(self, department, **kwargs):
        """
        Set carlist of department

        :param department: id of department as integer
        :param kwargs:
            - *cars1*: list of :py:class:`emonitor.modules.cars.car.Car` objects for cars1
            - *cars2*: list of :py:class:`emonitor.modules.cars.car.Car` objects for cars2
            - *material*: list of :py:class:`emonitor.modules.cars.car.Car` objects for material
        """
        alarmcars = AlarmkeyCars.getAlarmkeyCars(kid=self.id, dept=department)
        if not alarmcars:
            alarmcars = AlarmkeyCars(self.id, department, "", "", "")
            db.session.add(alarmcars)
        if "cars1" in kwargs.keys():
            alarmcars._cars1 = kwargs["cars1"]
        if "cars2" in kwargs.keys():
            alarmcars._cars2 = kwargs["cars2"]
        if "material" in kwargs.keys():
            alarmcars._material = kwargs["material"]
Exemple #7
0
def getAdminContent(self, **params):
    """
    Deliver admin content of module alarmkeys

    :param params: use given parameters of request
    :return: rendered template as string
    """
    module = request.view_args['module'].split('/')

    if len(module) < 2:
        module.append(u'1')

    if len(module) > 2 and module[2] == 'upload':  # upload alarmkeys
        params.update({'': int(module[1]), 'department': module[1]})
        return render_template('admin.alarmkeys.upload.html', **params)

    else:
        depid = 1
        try:
            if int(module[1]):
                depid = int(module[1])
        except:
            pass

        if request.method == 'POST':
            if request.form.get('action') == 'adddefault':  # add default aao
                params.update({
                    'alarmkey': Alarmkey('', '', '', ''),
                    'depid': depid,
                    'departments': Department.getDepartments(),
                    'cars': Car.getCars(),
                    'type': 0
                })
                return render_template('admin.alarmkeys_actions.html',
                                       **params)

            elif request.form.get(
                    'action') == 'savedefault':  # save default aao
                alarmkeycar = AlarmkeyCars.getAlarmkeyCars(
                    kid=9999, dept=request.form.get(
                        'deptid'))  # 9999 = default department
                if not alarmkeycar:  # add
                    alarmkeycar = AlarmkeyCars(9999,
                                               request.form.get('deptid'), '',
                                               '', '')
                    db.session.add(alarmkeycar)

                alarmkeycar.cars1 = request.form.get('cars1')
                alarmkeycar.cars2 = request.form.get('cars2')
                alarmkeycar.materials = request.form.get('material')
                if alarmkeycar.kid != 9999 and request.form.get(
                        'cars1') == request.form.get(
                            'cars2') == request.form.get(
                                'material') == '':  # remove
                    db.session.delete(alarmkeycar)
                db.session.commit()

            elif request.form.get(
                    'action') == 'editdefault':  # edit default aao
                params.update({
                    'alarmkey': Alarmkey('', '', '', ''),
                    'depid': depid,
                    'departments': Department.getDepartments(),
                    'cars': Car.getCars(),
                    'type': -1
                })
                return render_template('admin.alarmkeys_actions.html',
                                       **params)

            elif request.form.get('action') == 'addkey':  # add key
                params.update({
                    'alarmkey': Alarmkey('', '', '', ''),
                    'depid': depid,
                    'departments': Department.getDepartments(),
                    'cars': Car.getCars(),
                    'type': -2
                })
                return render_template('admin.alarmkeys_actions.html',
                                       **params)

            elif request.form.get('action') == 'savekey':  # save key
                if request.form.get('keyid') == 'None':  # add new
                    alarmkey = Alarmkey('', '', '', '')
                    db.session.add(alarmkey)
                    db.session.commit()

                else:  # update existing
                    alarmkey = Alarmkey.getAlarmkeys(request.form.get('keyid'))

                alarmkey.category = request.form.get('category')
                alarmkey.key = request.form.get('key')
                alarmkey.key_internal = request.form.get('keyinternal')
                alarmkey.remark = request.form.get('remark')
                alarmkey.setCars(int(request.form.get('deptid')),
                                 cars1=request.form.get('cars1'),
                                 cars2=request.form.get('cars2'),
                                 material=request.form.get('material'))
                db.session.commit()

            elif request.form.get('action').startswith(
                    'deletecars_'):  # delete car definition
                _op, _kid, _dept = request.form.get('action').split('_')
                keycar = AlarmkeyCars.getAlarmkeyCars(kid=_kid, dept=_dept)
                if keycar:
                    db.session.delete(keycar)
                    db.session.commit()

                # delete key if no definied cars
                if len(AlarmkeyCars.getAlarmkeyCars(kid=_kid)) == 0:
                    key = Alarmkey.getAlarmkeys(id=_kid)
                    db.session.delete(key)
                db.session.commit()

            elif request.form.get('action').startswith(
                    'editcars_'):  # edit key with cars
                _op, _kid, _dept = request.form.get('action').split('_')
                params.update({
                    'alarmkey': Alarmkey.getAlarmkeys(id=_kid),
                    'depid': _dept,
                    'departments': Department.getDepartments(),
                    'cars': Car.getCars(),
                    'type': 1
                })
                return render_template('admin.alarmkeys_actions.html',
                                       **params)

        alarmkeys_count = []
        ak = Alarmkey
        counted_keys = db.get(ak.category.label('category'),
                              func.count(ak.key).label('key'),
                              ak.id.label('id')).group_by(ak.category)
        _sum = 0
        for r in counted_keys.all():
            alarmkeys_count.append([r.category, r.key, r.id])
            _sum += int(r.key)

        params.update({
            'alarmkeys_count': alarmkeys_count,
            'depid': depid,
            'defaultcars': Alarmkey.getDefault(depid),
            'sum': _sum
        })
        return render_template('admin.alarmkeys.html', **params)
Exemple #8
0
def getAdminContent(self, **params):
    """
    Deliver admin content of module alarmkeys

    :param params: use given parameters of request
    :return: rendered template as string
    """
    module = request.view_args['module'].split('/')

    if len(module) < 2:
        module.append(u'1')

    if len(module) > 2 and module[2] == 'upload':  # upload alarmkeys
        params.update({'': int(module[1]), 'department': module[1]})
        return render_template('admin.alarmkeys.upload.html', **params)

    else:
        depid = 1
        try:
            if module[1].isnumeric():
                depid = int(module[1])
        except:
            pass

        if request.method == 'POST':
            if depid == 0:
                if request.form.get('action',
                                    '') == 'createset':  # add alarmkeyset
                    params.update({
                        'alarmkeyset':
                        AlarmkeySet('', '', datetime.date.today(),
                                    datetime.date.today(), ''),
                        'setbases':
                        AlarmkeySet.getBases(),
                        'orphankeys':
                        Alarmkey.getOrphanKeys()
                    })
                    return render_template('admin.alarmkeys.set_actions.html',
                                           **params)

                elif request.form.get('action', '').startswith('deleteset_'):
                    db.session.delete(
                        AlarmkeySet.getAlarmkeySets(
                            request.form.get('action', '').split('_')[-1]))
                    db.session.commit()

                elif request.form.get(
                        'action',
                        '').startswith('detailset_'):  # edit alarmkeyset
                    params.update({
                        'alarmkeyset':
                        AlarmkeySet.getAlarmkeySets(
                            request.form.get('action').split('_')[-1]),
                        'setbases':
                        AlarmkeySet.getBases(),
                        'orphankeys':
                        Alarmkey.getOrphanKeys()
                    })
                    return render_template('admin.alarmkeys.set_actions.html',
                                           **params)

                elif request.form.get(
                        'action', '') == 'savealarmkeyset':  # save alarmkeyset
                    if request.form.get('set_id', '') == 'None':  # add
                        akset = AlarmkeySet('', '', datetime.date.today(),
                                            datetime.date.today(), '')
                        db.session.add(akset)
                    else:
                        akset = AlarmkeySet.getAlarmkeySets(
                            request.form.get('set_id'))
                    akset.name = request.form.get('set_name')
                    if request.form.get('set_startdate', '') != "":
                        akset.startdate = datetime.datetime.strptime(
                            request.form.get('set_startdate'), '%d.%m.%Y')
                    else:
                        akset.startdate = None
                    if request.form.get('set_enddate', '') != "":
                        akset.enddate = datetime.datetime.strptime(
                            request.form.get('set_enddate'), '%d.%m.%Y')
                    else:
                        akset.enddate = None
                    akset.remark = request.form.get('set_remark')
                    akset.base = request.form.get('set_base')
                    db.session.commit()
                    if 'set_assignorphan' in request.form.keys(
                    ):  # add all orphan keys to current set
                        for key in Alarmkey.getOrphanKeys():
                            key._keyset = int(akset.id)
                        db.session.commit()

                elif request.form.get('action', '').startswith('deletebase_'):
                    if os.path.exists("{}alarmkeysetbase/{}".format(
                            current_app.config.get('PATH_DATA'), "_".join(
                                request.form.get('action',
                                                 '').split('_')[1:]))):
                        os.remove("{}alarmkeysetbase/{}".format(
                            current_app.config.get('PATH_DATA'), "_".join(
                                request.form.get('action',
                                                 '').split('_')[1:])))

                params.update({
                    'aksets': AlarmkeySet.getAlarmkeySets(),
                    'akbases': AlarmkeySet.getBases()
                })
                return render_template('admin.alarmkeys.set_list.html',
                                       **params)

            if request.form.get('action') == 'adddefault':  # add default aao
                params.update({
                    'alarmkey': Alarmkey('', '', '', ''),
                    'depid': depid,
                    'departments': Department.getDepartments(),
                    'cars': Car.getCars(),
                    'type': 0
                })
                return render_template('admin.alarmkeys_actions.html',
                                       **params)

            elif request.form.get(
                    'action') == 'savedefault':  # save default aao
                alarmkeycar = AlarmkeyCars.getAlarmkeyCars(
                    kid=9999, dept=request.form.get(
                        'deptid'))  # 9999 = default department
                if not alarmkeycar:  # add
                    alarmkeycar = AlarmkeyCars(9999,
                                               request.form.get('deptid'), '',
                                               '', '')
                    db.session.add(alarmkeycar)

                alarmkeycar.cars1 = request.form.get('cars1')
                alarmkeycar.cars2 = request.form.get('cars2')
                alarmkeycar.materials = request.form.get('material')
                if alarmkeycar.kid != 9999 and request.form.get(
                        'cars1') == request.form.get(
                            'cars2') == request.form.get(
                                'material') == '':  # remove
                    db.session.delete(alarmkeycar)
                db.session.commit()

            elif request.form.get(
                    'action') == 'editdefault':  # edit default aao
                params.update({
                    'alarmkey': Alarmkey('', '', '', ''),
                    'depid': depid,
                    'departments': Department.getDepartments(),
                    'aksets': AlarmkeySet.getAlarmkeySets(),
                    'cars': Car.getCars(),
                    'type': -1
                })
                return render_template('admin.alarmkeys_actions.html',
                                       **params)

            elif request.form.get('action') == 'addkey':  # add key
                params.update({
                    'alarmkey': Alarmkey('', '', '', ''),
                    'depid': depid,
                    'departments': Department.getDepartments(),
                    'aksets': AlarmkeySet.getAlarmkeySets(),
                    'cars': Car.getCars(),
                    'type': -2
                })
                return render_template('admin.alarmkeys_actions.html',
                                       **params)

            elif request.form.get('action') == 'savekey':  # save key
                if request.form.get('keyid') == 'None':  # add new
                    alarmkey = Alarmkey('', '', '', '')
                    db.session.add(alarmkey)
                    db.session.commit()

                else:  # update existing
                    alarmkey = Alarmkey.getAlarmkeys(request.form.get('keyid'))

                alarmkey.category = request.form.get('category')
                alarmkey.key = request.form.get('key')
                alarmkey.key_internal = request.form.get('keyinternal')
                alarmkey.remark = request.form.get('remark')
                if request.form.get('keyset', '') == '':
                    alarmkey._keyset = None
                else:
                    alarmkey._keyset = request.form.get('keyset')
                alarmkey.setCars(int(request.form.get('deptid')),
                                 cars1=request.form.get('cars1'),
                                 cars2=request.form.get('cars2'),
                                 material=request.form.get('material'))

                alarmkey.keysetitem = None
                if request.form.get('keyset', '') != "":
                    keyset = AlarmkeySet.getAlarmkeySets(
                        request.form.get('keyset'))
                    if keyset:
                        item = filter(
                            lambda x: x.get('stichwort') == alarmkey.category
                            and x.get('schlagwort') == alarmkey.key,
                            keyset.alarmkeys)
                        if len(item) > 0:
                            alarmkey.keysetitem = item[0].get('nr')
                db.session.commit()  # readd

            elif request.form.get('action').startswith(
                    'deletecars_'):  # delete car definition
                _op, _kid, _dept = request.form.get('action').split('_')
                keycar = AlarmkeyCars.getAlarmkeyCars(kid=_kid, dept=_dept)
                if keycar:
                    db.session.delete(keycar)
                    db.session.commit()

                # delete key if no definied cars
                if len(AlarmkeyCars.getAlarmkeyCars(kid=_kid)) == 0:
                    key = Alarmkey.getAlarmkeys(id=_kid)
                    db.session.delete(key)
                db.session.commit()

            elif request.form.get('action').startswith(
                    'editcars_'):  # edit key with cars
                _op, _kid, _dept = request.form.get('action').split('_')
                params.update({
                    'alarmkey': Alarmkey.getAlarmkeys(id=_kid),
                    'depid': _dept,
                    'departments': Department.getDepartments(),
                    'aksets': AlarmkeySet.getAlarmkeySets(),
                    'cars': Car.getCars(),
                    'type': 1
                })
                return render_template('admin.alarmkeys_actions.html',
                                       **params)

        if depid == 0:  # edit options
            params.update({
                'aksets': AlarmkeySet.getAlarmkeySets(),
                'departments': Department.getDepartments(),
                'akbases': AlarmkeySet.getBases()
            })
            return render_template('admin.alarmkeys.set_list.html', **params)

        alarmkeys_count = []
        ak = Alarmkey
        counted_keys = db.get(ak.category.label('category'),
                              func.count(ak.key).label('key'),
                              ak.id.label('id')).group_by(ak.category, ak.id)
        _sum = 0
        for r in counted_keys.all():
            alarmkeys_count.append([r.category, r.key, r.id])
            _sum += int(r.key)

        params.update({
            'depid': depid,
            'defaultcars': Alarmkey.getDefault(depid),
            'aksets': AlarmkeySet.getAlarmkeySets()
        })
        return render_template('admin.alarmkeys.html', **params)
def getAdminContent(self, **params):
    """
    Deliver admin content of module alarmkeys

    :param params: use given parameters of request
    :return: rendered template as string
    """
    module = request.view_args['module'].split('/')

    if len(module) < 2:
        module.append(u'1')
    
    if len(module) > 2 and module[2] == 'upload':  # upload alarmkeys
        params.update({'': int(module[1]), 'department': module[1]})
        return render_template('admin.alarmkeys.upload.html', **params)
    
    else:
        depid = 1
        try:
            if int(module[1]):
                depid = int(module[1])
        except:
            pass
            
        if request.method == 'POST':
            if request.form.get('action') == 'adddefault':  # add default aao
                params.update({'alarmkey': Alarmkey('', '', '', ''), 'depid': depid, 'departments': Department.getDepartments(), 'cars': Car.getCars(), 'type': 0})
                return render_template('admin.alarmkeys_actions.html', **params)

            elif request.form.get('action') == 'savedefault':  # save default aao
                alarmkeycar = AlarmkeyCars.getAlarmkeyCars(kid=9999, dept=request.form.get('deptid'))  # 9999 = default department
                if not alarmkeycar:  # add
                    alarmkeycar = AlarmkeyCars(9999, request.form.get('deptid'), '', '', '')
                    db.session.add(alarmkeycar)

                alarmkeycar.cars1 = request.form.get('cars1')
                alarmkeycar.cars2 = request.form.get('cars2')
                alarmkeycar.materials = request.form.get('material')
                if alarmkeycar.kid != 9999 and request.form.get('cars1') == request.form.get('cars2') == request.form.get('material') == '':  # remove
                    db.session.delete(alarmkeycar)
                db.session.commit()
                
            elif request.form.get('action') == 'editdefault':  # edit default aao
                params.update({'alarmkey': Alarmkey('', '', '', ''), 'depid': depid, 'departments': Department.getDepartments(), 'cars': Car.getCars(), 'type': -1})
                return render_template('admin.alarmkeys_actions.html', **params)

            elif request.form.get('action') == 'addkey':  # add key
                params.update({'alarmkey': Alarmkey('', '', '', ''), 'depid': depid, 'departments': Department.getDepartments(), 'cars': Car.getCars(), 'type': -2})
                return render_template('admin.alarmkeys_actions.html', **params)

            elif request.form.get('action') == 'savekey':  # save key
                if request.form.get('keyid') == 'None':  # add new
                    alarmkey = Alarmkey('', '', '', '')
                    db.session.add(alarmkey)
                    db.session.commit()

                else:  # update existing
                    alarmkey = Alarmkey.getAlarmkeys(request.form.get('keyid'))

                alarmkey.category = request.form.get('category')
                alarmkey.key = request.form.get('key')
                alarmkey.key_internal = request.form.get('keyinternal')
                alarmkey.remark = request.form.get('remark')
                alarmkey.setCars(int(request.form.get('deptid')), cars1=request.form.get('cars1'), cars2=request.form.get('cars2'), material=request.form.get('material'))
                db.session.commit()
                
            elif request.form.get('action').startswith('deletecars_'):  # delete car definition
                _op, _kid, _dept = request.form.get('action').split('_')
                keycar = AlarmkeyCars.getAlarmkeyCars(kid=_kid, dept=_dept)
                if keycar:
                    db.session.delete(keycar)
                    db.session.commit()

                # delete key if no definied cars
                if len(AlarmkeyCars.getAlarmkeyCars(kid=_kid)) == 0:
                    key = Alarmkey.getAlarmkeys(id=_kid)
                    db.session.delete(key)
                db.session.commit()
                
            elif request.form.get('action').startswith('editcars_'):  # edit key with cars
                _op, _kid, _dept = request.form.get('action').split('_')
                params.update({'alarmkey': Alarmkey.getAlarmkeys(id=_kid), 'depid': _dept, 'departments': Department.getDepartments(), 'cars': Car.getCars(), 'type': 1})
                return render_template('admin.alarmkeys_actions.html', **params)

        alarmkeys_count = []
        ak = Alarmkey
        counted_keys = db.get(ak.category.label('category'), func.count(ak.key).label('key'), ak.id.label('id')).group_by(ak.category)
        _sum = 0
        for r in counted_keys.all():
            alarmkeys_count.append([r.category, r.key, r.id])
            _sum += int(r.key)

        params.update({'alarmkeys_count': alarmkeys_count, 'depid': depid, 'defaultcars': Alarmkey.getDefault(depid), 'sum': _sum})
        return render_template('admin.alarmkeys.html', **params)
Exemple #10
0
def getAdminContent(self, **params):
    """
    Deliver admin content of module alarmkeys

    :param params: use given parameters of request
    :return: rendered template as string
    """
    module = request.view_args['module'].split('/')

    if len(module) < 2:
        module.append(u'1')
    
    if len(module) > 2 and module[2] == 'upload':  # upload alarmkeys
        params.update({'': int(module[1]), 'department': module[1]})
        return render_template('admin.alarmkeys.upload.html', **params)
    
    else:
        depid = 1
        try:
            if module[1].isnumeric():
                depid = int(module[1])
        except:
            pass

        if request.method == 'POST':
            if depid == 0:
                if request.form.get('action', '') == 'createset':  # add alarmkeyset
                    params.update({'alarmkeyset': AlarmkeySet('', '', datetime.date.today(), datetime.date.today(), ''), 'setbases': AlarmkeySet.getBases(), 'orphankeys': Alarmkey.getOrphanKeys()})
                    return render_template('admin.alarmkeys.set_actions.html', **params)

                elif request.form.get('action', '').startswith('deleteset_'):
                    db.session.delete(AlarmkeySet.getAlarmkeySets(request.form.get('action', '').split('_')[-1]))
                    db.session.commit()

                elif request.form.get('action', '').startswith('detailset_'):  # edit alarmkeyset
                    params.update({'alarmkeyset': AlarmkeySet.getAlarmkeySets(request.form.get('action').split('_')[-1]), 'setbases': AlarmkeySet.getBases(), 'orphankeys': Alarmkey.getOrphanKeys()})
                    return render_template('admin.alarmkeys.set_actions.html', **params)

                elif request.form.get('action', '') == 'savealarmkeyset':  # save alarmkeyset
                    if request.form.get('set_id', '') == 'None':  # add
                        akset = AlarmkeySet('', '', datetime.date.today(), datetime.date.today(), '')
                        db.session.add(akset)
                    else:
                        akset = AlarmkeySet.getAlarmkeySets(request.form.get('set_id'))
                    akset.name = request.form.get('set_name')
                    if request.form.get('set_startdate', '') != "":
                        akset.startdate = datetime.datetime.strptime(request.form.get('set_startdate'), '%d.%m.%Y')
                    else:
                        akset.startdate = None
                    if request.form.get('set_enddate', '') != "":
                        akset.enddate = datetime.datetime.strptime(request.form.get('set_enddate'), '%d.%m.%Y')
                    else:
                        akset.enddate = None
                    akset.remark = request.form.get('set_remark')
                    akset.base = request.form.get('set_base')
                    db.session.commit()
                    if 'set_assignorphan' in request.form.keys():  # add all orphan keys to current set
                        for key in Alarmkey.getOrphanKeys():
                            key._keyset = int(akset.id)
                        db.session.commit()

                elif request.form.get('action', '').startswith('deletebase_'):
                    if os.path.exists("{}alarmkeysetbase/{}".format(current_app.config.get('PATH_DATA'), "_".join(request.form.get('action', '').split('_')[1:]))):
                        os.remove("{}alarmkeysetbase/{}".format(current_app.config.get('PATH_DATA'), "_".join(request.form.get('action', '').split('_')[1:])))

                params.update({'aksets': AlarmkeySet.getAlarmkeySets(), 'akbases': AlarmkeySet.getBases()})
                return render_template('admin.alarmkeys.set_list.html', **params)

            if request.form.get('action') == 'adddefault':  # add default aao
                params.update({'alarmkey': Alarmkey('', '', '', ''), 'depid': depid, 'departments': Department.getDepartments(), 'cars': Car.getCars(), 'type': 0})
                return render_template('admin.alarmkeys_actions.html', **params)

            elif request.form.get('action') == 'savedefault':  # save default aao
                alarmkeycar = AlarmkeyCars.getAlarmkeyCars(kid=9999, dept=request.form.get('deptid'))  # 9999 = default department
                if not alarmkeycar:  # add
                    alarmkeycar = AlarmkeyCars(9999, request.form.get('deptid'), '', '', '')
                    db.session.add(alarmkeycar)

                alarmkeycar.cars1 = request.form.get('cars1')
                alarmkeycar.cars2 = request.form.get('cars2')
                alarmkeycar.materials = request.form.get('material')
                if alarmkeycar.kid != 9999 and request.form.get('cars1') == request.form.get('cars2') == request.form.get('material') == '':  # remove
                    db.session.delete(alarmkeycar)
                db.session.commit()
                
            elif request.form.get('action') == 'editdefault':  # edit default aao
                params.update({'alarmkey': Alarmkey('', '', '', ''), 'depid': depid, 'departments': Department.getDepartments(), 'aksets': AlarmkeySet.getAlarmkeySets(), 'cars': Car.getCars(), 'type': -1})
                return render_template('admin.alarmkeys_actions.html', **params)

            elif request.form.get('action') == 'addkey':  # add key
                params.update({'alarmkey': Alarmkey('', '', '', ''), 'depid': depid, 'departments': Department.getDepartments(), 'aksets': AlarmkeySet.getAlarmkeySets(), 'cars': Car.getCars(), 'type': -2})
                return render_template('admin.alarmkeys_actions.html', **params)

            elif request.form.get('action') == 'savekey':  # save key
                if request.form.get('keyid') == 'None':  # add new
                    alarmkey = Alarmkey('', '', '', '')
                    db.session.add(alarmkey)
                    db.session.commit()

                else:  # update existing
                    alarmkey = Alarmkey.getAlarmkeys(request.form.get('keyid'))

                alarmkey.category = request.form.get('category')
                alarmkey.key = request.form.get('key')
                alarmkey.key_internal = request.form.get('keyinternal')
                alarmkey.remark = request.form.get('remark')
                if request.form.get('keyset', '') == '':
                    alarmkey._keyset = None
                else:
                    alarmkey._keyset = request.form.get('keyset')
                alarmkey.setCars(int(request.form.get('deptid')), cars1=request.form.get('cars1'), cars2=request.form.get('cars2'), material=request.form.get('material'))

                alarmkey.keysetitem = None
                if request.form.get('keyset', '') != "":
                    keyset = AlarmkeySet.getAlarmkeySets(request.form.get('keyset'))
                    if keyset:
                        item = filter(lambda x: x.get('stichwort') == alarmkey.category and x.get('schlagwort') == alarmkey.key, keyset.alarmkeys)
                        if len(item) > 0:
                            alarmkey.keysetitem = item[0].get('nr')
                db.session.commit()  # readd

            elif request.form.get('action').startswith('deletecars_'):  # delete car definition
                _op, _kid, _dept = request.form.get('action').split('_')
                keycar = AlarmkeyCars.getAlarmkeyCars(kid=_kid, dept=_dept)
                if keycar:
                    db.session.delete(keycar)
                    db.session.commit()

                # delete key if no definied cars
                if len(AlarmkeyCars.getAlarmkeyCars(kid=_kid)) == 0:
                    key = Alarmkey.getAlarmkeys(id=_kid)
                    db.session.delete(key)
                db.session.commit()
                
            elif request.form.get('action').startswith('editcars_'):  # edit key with cars
                _op, _kid, _dept = request.form.get('action').split('_')
                params.update({'alarmkey': Alarmkey.getAlarmkeys(id=_kid), 'depid': _dept, 'departments': Department.getDepartments(), 'aksets': AlarmkeySet.getAlarmkeySets(), 'cars': Car.getCars(), 'type': 1})
                return render_template('admin.alarmkeys_actions.html', **params)

        if depid == 0:  # edit options
            params.update({'aksets': AlarmkeySet.getAlarmkeySets(), 'departments': Department.getDepartments(), 'akbases': AlarmkeySet.getBases()})
            return render_template('admin.alarmkeys.set_list.html', **params)

        params.update({'depid': depid, 'defaultcars': Alarmkey.getDefault(depid), 'aksets': AlarmkeySet.getAlarmkeySets()})
        return render_template('admin.alarmkeys.html', **params)