def getAdminData(self):
    """
    Deliver admin content of module alarmobjects (ajax)

    :return: rendered template as string or json dict
    """
    if request.args.get('action') == 'uploadfile':  # add file for alarmobject
        ufile = request.files['uploadfile']
        filetype = request.form.get('name')
        alarmobject = AlarmObject.getAlarmObjects(id=request.form.get('alarmobject_id'))
        filename = '%s%s' % (str(random.random())[2:], os.path.splitext(ufile.filename)[1])

        if not os.path.exists('%salarmobjects/%s/' % (current_app.config.get('PATH_DATA'), alarmobject.id)):  # create base directory
            os.makedirs('%salarmobjects/%s/' % (current_app.config.get('PATH_DATA'), alarmobject.id))

        ufile.save('%salarmobjects/%s/%s' % (current_app.config.get('PATH_DATA'), alarmobject.id, filename))
        alarmobject.files.set(AlarmObjectFile(alarmobject.id, filename, filetype))
        db.session.commit()
        return render_template('admin.alarmobjects.file.html', alarmobject=alarmobject)

    elif request.args.get('action') == 'deletefile':  # delete file with id
        objectfile = AlarmObjectFile.getFile(id=request.args.get('id'))
        oid = objectfile.object_id
        if os.path.exists('%salarmobjects/%s/%s' % (current_app.config.get('PATH_DATA'), oid, objectfile.filename)):
            os.remove('%salarmobjects/%s/%s' % (current_app.config.get('PATH_DATA'), oid, objectfile.filename))
        db.session.delete(objectfile)
        db.session.commit()
        alarmobject = AlarmObject.getAlarmObjects(id=oid)
        return render_template('admin.alarmobjects.file.html', alarmobject=alarmobject)

    return ""
Example #2
0
def getAdminData(self):
    """
    Deliver admin content of module alarmobjects (ajax)

    :return: rendered template as string or json dict
    """
    if request.args.get('action') == 'uploadfile':  # add file for alarmobject
        ufile = request.files['uploadfile']
        filetype = request.form.get('name')
        alarmobject = AlarmObject.getAlarmObjects(
            id=request.form.get('alarmobject_id'))
        filename = '%s%s' % (str(
            random.random())[2:], os.path.splitext(ufile.filename)[1])

        if not os.path.exists('%salarmobjects/%s/' %
                              (current_app.config.get('PATH_DATA'),
                               alarmobject.id)):  # create base directory
            os.makedirs('%salarmobjects/%s/' %
                        (current_app.config.get('PATH_DATA'), alarmobject.id))

        ufile.save(
            '%salarmobjects/%s/%s' %
            (current_app.config.get('PATH_DATA'), alarmobject.id, filename))
        alarmobject.files.set(
            AlarmObjectFile(alarmobject.id, filename, filetype))
        db.session.commit()
        return render_template('admin.alarmobjects.file.html',
                               alarmobject=alarmobject)

    elif request.args.get('action') == 'deletefile':  # delete file with id
        objectfile = AlarmObjectFile.getFile(id=request.args.get('id'))
        oid = objectfile.object_id
        if os.path.exists(
                '%salarmobjects/%s/%s' %
            (current_app.config.get('PATH_DATA'), oid, objectfile.filename)):
            os.remove('%salarmobjects/%s/%s' %
                      (current_app.config.get('PATH_DATA'), oid,
                       objectfile.filename))
        db.session.delete(objectfile)
        db.session.commit()
        alarmobject = AlarmObject.getAlarmObjects(id=oid)
        return render_template('admin.alarmobjects.file.html',
                               alarmobject=alarmobject)

    return ""
 def objectfile_static(filename):
     id, name = filename.split('-')
     alarmobjectfile = AlarmObjectFile.getFile(id=id, filename=name)
     return send_from_directory(u'{}alarmobjects/{}/'.format(current_app.config.get('PATH_DATA'), id), alarmobjectfile.filename)